#!/bin/sh

#--------------------------------------------------
# main ()
#--------------------------------------------------

#--------------------------------------------------
#
#  First try to figure out where we are now.
#
#--------------------------------------------------

##set -v

here=`pwd`                #should be .../CMT/vxxx/mgr
here=`dirname ${here}`    #should be .../CMT/vxxx
here=`dirname ${here}`    #should be .../CMT
here=`basename ${here}`   #should be CMT

if test ! "${here}" = "CMT" ; then  # Are we outside of CMT ?
  echo " INSTALL should be run from within the mgr branch of the CMT package"
  echo " Please go there first and restart this script"
  echo "  > cd <...>/CMT/v.../mgr"
  echo "  > ./INSTALL"
  exit 1
fi

p=`pwd`
CMTROOT=`dirname $p`

version=`basename ${CMTROOT}`
p=`dirname ${CMTROOT}`
ppath=`dirname ${p}`

#--------------------------------------------------
#
#   On some sites, it is convenient to reformat the
# dir. name obtained by pwd (eg. for removing the /tmp_mnt prefixes)
#   Some of these replacements are available in 
# the ${CMTROOT}/mgr/cmt_mount_filter file.
#
#   You may add new substitutions in this file, according to your
# specific file system configuration.
#
#   Each entry takes the form :
#
#dir-name      replacement-name
#
#   (with space[s] as separator)
#
#--------------------------------------------------

if test -r cmt_mount_filter ; then
  e=`sed -e 's/[ ][ ]*/#/' -e 's/^[ ]*/s#/' -e 's/[ ]*$/#/' -e 's/^s##$//' cmt_mount_filter`
  ppath=`echo ${ppath} | sed -e "$e"`
fi

#--------------------------------------------------
#
#   Building the setup.csh script
#
#--------------------------------------------------

cat <<END >setup.csh
# echo "Setting CMT ${version} in ${ppath}"
    
setenv CMTROOT ${ppath}/CMT/${version}

END

cat ../src/setup.csh >>setup.csh

#--------------------------------------------------
#
#   Building the setup.sh script
#
#--------------------------------------------------

cat <<END >setup.sh
# echo "Setting CMT ${version} in ${ppath}"
    
CMTROOT=${ppath}/CMT/${version}; export CMTROOT

END

cat ../src/setup.sh >>setup.sh

#--------------------------------------------------
#
#   Building the cleanup.csh script
#
#--------------------------------------------------

cp ../src/cleanup.csh ./cleanup.csh

#--------------------------------------------------
#
#   Building the cleanup.sh script
#
#--------------------------------------------------

cp ../src/cleanup.sh ./cleanup.sh

#--------------------------------------------------
#
#  In case one needs to re-build cmt from the sources
#  it's likely that cmt.exe does not exist yet thus
#  we cannot expect that dependencies are re-built.
#
#  rather, we expect they have been downloaded from 
#  the distribution kit and thus we just have to 
#  touch them.
#
#--------------------------------------------------

touch ../src/*.make ../mgr/*.make ../mgr/cmt_path.make

exes=`ls ../*/cmt.exe | sed -e 's#\.\./#    #g' -e 's#/cmt.exe##g'`

echo "============================================"
echo "       CMT installation terminated.         "
echo "           --------------------             "
if test "${exes}" = "" ; then
  echo " cmt.exe is not built on this site"
  echo " you might need to build it as follows:"
  echo " > source setup.csh"
  echo " > [g]make"
else
  echo " cmt.exe is available on this site for:"
    echo "${exes}"
fi
echo "============================================"
