###########################################################
#                                                        
#  Script for installing RESTRAX                         
#  J. Saroun, 28/1/2002                                   
#                                                         
#  Call "Install <dir> [ILL]" to install files to <dir>   
# <dir> must already exist and you need all permissions   
#                                                         
# ILL is optional parameter, which creates another start 
# script with additional menu for selection from ILL TAS instruments
###########################################################
DIRECTORIES="lib bin bin/exci include src src/exci doc doc/srcdoc"

DOCFILES="doc/images doc/images_eq doc/styles doc/srcdoc/srcexci doc/srcdoc/EXCI_OVERVIEW.html doc/srcdoc/restraxsrc.css `ls doc/*.*  doc/?00*` "

SOURCES="demo setup exci config include/exci.inc include/const.inc include/reclat.inc `ls src/exci/*.f` \
  README README_exci motd COPYING COPYING.LIB INSTALL_bin  README_windows.html makefile_exci.amk \
  $DOCFILES "
EXECS="configure.EXCI makeexci.bat"
BINARIES="bin/$EXEC `ls lib/$ELIB*.$SHEXT`"

TD=$1
# test syntax and accessibility
if [ "x$TD" != "x" ] ; then 
	if [ ! -d $TD ] ; then		
		echo "Target directory $TD not found."
		echo "Installation cancelled"
		exit
	fi
	if [ ! -w $TD ] ; then		
		echo "You don't have writing permission to $TD."
		echo "Installation cancelled"
		exit
	fi
else
	echo 
	echo "*** Script for installing $PGMNAME $VERSION *** "
	echo "Syntax: Install <target directory> "
	echo "<target directory> must already exist "
	exit
fi  

# create directories
for NN in $DIRECTORIES; 
do
	if [ ! -d $TD/$NN ] ; then
		mkdir $TD/$NN;
		chmod 755 $TD/$NN;
	fi	
done

# install resource files
for NN in $SOURCES; 
do
	if [ -d $NN ]; then
		tar -cf - $NN | tar -C$TD -xf -
	elif [ -f $NN ]; then
		cp -fp $NN $TD/$NN;
		chmod 644 $TD/$NN;
	fi
done

# install executable scripts
for NN in $EXECS; do
  if [ -f $NN ]; then
      cp -fp $NN $TD;
      chmod 755 $TD/$NN;
  fi
done

# install binaries
for NN in $BINARIES; do
  if [ -f $NN ]; then
      cp -fp $NN $TD/$NN;
  fi
done
echo $PGMNAME $VERSION installed in $TD.

# create scripts: start, restrax_run
# for ILL, pass the directory to local PGPLOT installation
os=`uname -s`
if [ "x$2" != "xILL" ] ; then 
	if [  "x$os" = "xLinux" ] ; then
		./Make_run "$TD" "$EXEC" "$TD/../pgplot_linux"
	else
		./Make_run "$TD" "$EXEC" "$TD/../pgplot"
	fi
else
	./Make_run "$TD" "$EXEC" "auto"
fi
./Make_start "$TD" "$PGMNAME" "$VERSION"  "$BDATE" "$2"

# process template files: substitute for variables
find $TD -name "*.in" -exec rm -f {} \;
FILES=`find . -name "*.in"`
for F in $FILES; do
	FF=`echo $F~~ | sed "s|.in~~||"`
       sed  "s|\@INSTDIR\@|$TD|" $F | sed  "s|\@SHEXT\@|$SHEXT|" > $TD/$FF
done


