#!/bin/sh -f

###set -x

#------------------------------------------------------------------------------
#
# 1  absolute source directory
# 2  file name
# 3  absolute installation directory
# 4  installation system command
#
#------------------------------------------------------------------------------

source_dir=$1
file_name=$2
install_dir=$3
install_command=$4

dest_file_path=${install_dir}/${file_name}
ref_file=${dest_file_path}.cmtref
full_source_name=${source_dir}/${file_name}

if test "${CMTINSTALLAREA}" = "" ; then
  echo "No installation directory specified"
  exit 0
fi

b=`echo ${CMTINSTALLAREA} | sed -e 's#/InstallArea##'`
b=`(cd ${b}; pwd)`
a=`pwd | grep -e "${b}"`
if test "${a}" = ""; then
  echo "Current directory outside of the installation area. Cannot proceed"
  exit 0
fi

if test "${install_dir}" = "" ; then
  exit 0
fi

if test ! -d ${install_dir}; then
  mkdir -p ${install_dir}
fi

if test ! -d ${install_dir}; then
  echo "Cannot install file ${file_name}, no installation directory specified"
  exit 0
fi

echo "Installing file ${file_name} into ${install_dir}"
if test -f ${dest_file_path}; then
  /bin/rm -f ${dest_file_path}
fi

if test -f ${ref_file}; then
  /bin/rm -f ${ref_file}
fi

${install_command} ${full_source_name} ${dest_file_path}
echo ${full_source_name} >${ref_file}
