#! /bin/sh

# usage: inst FILE DIRECTORY

# Installs FILE in DIRECTORY, printing appropriate messages.
# For use in binary distribution Makefiles.

path=$1
dir=$2
file=`basename $path`

# Make sure $path exists
if [ ! -r $path ] ; then
  echo "Error: File $path is missing and hence cannot be installed"
  exit 1
fi

# Make sure $dir is a directory that we have write permission in
if [ ! -d $dir ] ; then
  echo "Error: $dir does not exist or is not a directory; cannot install $path"
  exit 1
fi
if [ ! -w $dir ] ; then
  echo "Error: Cannot write to directory $dir; cannot install $path"
  exit 1
fi


# See if $dir/$file already exists, and if so, try to remove it,
# unless it's the same as $path
if [ -r $dir/$file ] ; then
  if [     `ls -i $dir/$file | awk '{print $1}'`  \
	!= `ls -i $path | awk '{print $1}'` ] ; then
    /bin/rm -f "$dir/$file" >&- 2>&-
    if [ -f "$dir/$file" ]; then
      echo Error: cannot remove previously existing $dir/$file
      exit 1
    else
      echo Removed previously existing $dir/$file
    fi
  else
    echo $file is already in $dir
    exit 0
  fi
fi

# Now attempt to copy $path into $dir
if cp $path $dir ; then
  echo Installed $path in $dir
else
  echo Error: could not install $path in $dir
  exit 1
fi
