#!/bin/sh

# determine the installation directory of stic_std_variables and execute inline
if test -n "$STIC_MAIN_DIR"
then
  stic_init_dir=$STIC_MAIN_DIR
elif test -r $HOME/.stic_main_dir
then
  stic_init_dir=$(cat $HOME/.stic_main_dir)
fi
if test "$stic_init_dir" = ""
then
  stic_init_dir=$(dirname "$0")/
else
  stic_init_dir="$stic_init_dir"/scripts/
fi
if test -r "$stic_init_dir"stic_std_variables
then
  . "$stic_init_dir"stic_std_variables
elif test  -z "$STIC_NO_BASH" -a -n "$(type -p stic_std_variables)"
then
  . stic_std_variables
fi

#
# $1  source file
# $2  target file
#
if test $# -lt 2
then
  echo "usage: $0 source target"
  exit 1
fi

source="$1"
target="$2"

map_cmd=""

if test -f "$source"
then
  dummy=dummy
else
  echo $0 : file "$source" not found or no data file >&2
  exit 1
fi

if test -d "$target"
then
  target="$2"/$(basename "$source") 
fi
if test -d $(dirname "$target")
then
  test -z ""
else
  echo $0 : directory $(dirname "$target") not found  >&2
  exit 1
fi


if test -e "$target"
then
  echo "overwrite $target ?"
  read antw
  if test "$antw" = "Y" -o "$antw" = "y" -o "$antw" = "J" -o "$antw" = "j"
  then
    if test -e $(dirname "$target")/.imv_guarded_directory
    then
      "$stic_bin_dir"similar $map_cmd -del_file:"$target"
    fi
    if rm "$target"
    then
      echo >/dev/null
    else
      if test -e $(dirname "$target")/.imv_guarded_directory
      then
        "$stic_bin_dir"similar $map_cmd -append:"$target"
      fi
      exit 1
    fi
  else
    exit 0
  fi
fi  

if test -e $(dirname "$source")/.imv_guarded_directory
then
  "$stic_bin_dir"similar $map_cmd -del_file:"$source"
fi

if mv "$source" "$target"
then
  if test -e $(dirname $target)/.imv_guarded_directory
  then
    "$stic_bin_dir"similar $map_cmd -append:"$target"
  fi
else
  if test -e $(dirname $source)/.imv_guarded_directory
  then
    "$stic_bin_dir"similar $map_cmd -append:"$source"
  fi
fi

