######################################################################
#
# ASTK installation script

# $Id: inst_astk.sh,v 1.20 2004/05/07 16:22:44 mcourtoi Exp $
# $Name: v7_3-8 $


######################################################################
#
# Previously defined :
#  inst_dir, dirhome, gzip, is_tktable
#  SHELL_INSTALL_ASTER, tktable_dir, tcl_dir, tcl_bin, wish_bin
#  vastk
#  astk_inst = 0 if installation with Code_Aster, -1 if alone.

#################################################################
# fonction d'install du client
function install_client
{
   echo " Installing ASTK client..."
   cd $astk_inst_dir
   if [ -e ASTK_CLIENT ]; then
      rm -rf ASTK_CLIENT.old
      mv -f ASTK_CLIENT ASTK_CLIENT.old
   fi
   if [ ! -f ASTK_CLIENT.tar ]; then
      echo "   ASTK client is not available in $astk_inst_dir/"
      echo "   Abort."
      exit 4
   fi
   tar xf ASTK_CLIENT.tar
   rm -f ASTK_CLIENT.tar
   cd ASTK_CLIENT
   for ff in bin/astk bin/asjob bin/bsf
   do
      sed -e 's#SHELL_EXECUTION#'$SHELL_INSTALL_ASTER'#' \
          -e 's#HOME_ASTK#'$astk_inst_dir'#' \
          -e 's#HOME_TKTABLE#'$tktable_dir'#' \
          -e 's#HOME_TCL_TK#'$tcl_dir'#g' \
          -e 's#TCLSH_BIN#'$tcl_bin'#g' \
          -e 's#WISH_BIN#'$wish_bin'#g' \
               $ff > $ff.out
      mv -f $ff.out $ff
      chmod +x $ff
   done
   cd ..
   # last configuration
   if [ -d ASTK_CLIENT.old/lib/ASTK/astkrc ]; then
      mv ASTK_CLIENT/lib/ASTK/astkrc ASTK_CLIENT/lib/ASTK/astkrc.new
      cp -r ASTK_CLIENT.old/lib/ASTK/astkrc ASTK_CLIENT/lib/ASTK/
      echo "   Keep last default users configuration, new 'astkrc' directory saved as $astk_inst_dir/ASTK_CLIENT/lib/ASTK/astkrc.new"
      echo "   Verify that syntax has not changed between 'astkrc' and 'astkrc.new'."
   else
      echo "   You must check configuration files in ASTK_CLIENT/lib/ASTK/astkrc"
   fi
}

#################################################################
# fonction d'install du serveur
function install_serveur
{
   echo " Installing ASTK server..."
   cd $astk_inst_dir
   if [ -e ASTK_SERV ]; then
      rm -rf ASTK_SERV.old
      mv -f ASTK_SERV ASTK_SERV.old
   fi
   if [ ! -f ASTK_SERV.tar ]; then
      echo "   ASTK server is not available in $astk_inst_dir/"
      echo "   Abort."
      exit 4
   fi
   tar xf ASTK_SERV.tar
   rm -f ASTK_SERV.tar
   cd ASTK_SERV
   for ff in bin/* lib/sub_script
   do
      sed -e 's#SHELL_EXECUTION#'$SHELL_INSTALL_ASTER'#' \
          -e 's#HOME_ASTK#'$astk_inst_dir'#' \
          -e 's#HOME_TKTABLE#'$tktable_dir'/lib#' \
          -e 's#HOME_TCL_TK#'$tcl_dir'#g' \
          -e 's#TCLSH_BIN#'$tcl_bin'#g' \
          -e 's#WISH_BIN#'$wish_bin'#g' \
               $ff > $ff.out
      mv -f $ff.out $ff
      chmod +x $ff
   done
   cd ..
   # last configuration
   if [ -d ASTK_SERV.old/conf ]; then
      mv ASTK_SERV/conf ASTK_SERV/conf.new
      cp -r ASTK_SERV.old/conf ASTK_SERV/
      echo "   Keep last server configuration, new 'conf' directory saved as $astk_inst_dir/ASTK_SERV/conf.new"
      echo "   Verify that syntax has not changed between 'conf' and 'conf.new'."
   else
      echo "   You must check configuration files in ASTK_SERV/conf"
   fi
}

#################################################################
# fonction d'install du serveur de la machine de référence
function install_serveur_ref
{
   echo " Installing ASTK server for reference machine..."
   cd $astk_inst_dir
   if [ -e ASTK_SERV_REF ]; then
      rm -rf ASTK_SERV_REF.old
      mv -f ASTK_SERV_REF ASTK_SERV_REF.old
   fi
   if [ ! -f ASTK_SERV_REF.tar ]; then
      echo "   ASTK_REF server is not available in $astk_inst_dir/"
      echo "   Abort."
      exit 4
   fi
   tar xf ASTK_SERV_REF.tar
   rm -f ASTK_SERV_REF.tar
   cd ASTK_SERV_REF
   for ff in bin/* lib/*
   do
      rep=`dirname $ff`
      mv $ff $astk_inst_dir/ASTK_SERV/$rep
   done
   cd ..
   chmod 755 ASTK_SERV/bin/* ASTK_SERV/lib/*sh ASTK_CLIENT/bin/*
   rm -rf ASTK_SERV_REF
}

#################################################################
# create_config_VVV fich idir tools_dir
#     create a config.VVV file for ASTK_SERV
# RETURN  : 0
function create_config_VVV
{
   if [ $# -ne 3 ]; then
      echo "Wrong arguments for create_config_VVV"
      exit 1
   fi
   fich=$1
   idir=$2
   tools_dir=$3
cat << EOF > $fich
# root directory of the version
rep_ref : $idir

# tools directory
rep_outils : $tools_dir

# nodebug ans debug binairies
execu : $idir/asteru.exe
execd : $idir/asterd.exe

# catalogues
rep_coco : $idir/commande
elco : $idir/elements

# path to run_aster
run_aster : $idir/tcl/run_aster
EOF
}

#################################################################
# modif_config fich ifdef machine vaster edit
#     modify the config file for ASTK_SERV
# RETURN  : 0
function modif_config
{
   if [ $# -ne 5 ]; then
      echo "Wrong arguments for modif_config"
      exit 1
   fi
   fich=$1
   ifdef=$2
   mach=$3
   vaster=$4
   edit=$5
   if [ ! -f $fich ]; then
      echo " File not found : $fich"
      return 1
   fi
   mv -f $fich $fich.old
   nl=`egrep -n 'vers *: *VVV' $fich.old | $awk -F':' '{print $1}'`
   sed -e "s/#$ifdef//" \
       -e "s#IFDEF#$ifdef#" \
       -e "s#NODE#$mach#" \
       -e "s#EDITOR#$edit#" \
      $fich.old > $fich.1
   head -$nl $fich.1 > $fich
   echo "vers : $vaster" >> $fich
   nl1=`echo | $awk -v nl=$nl '{print nl+1}'`
   tail +$nl1 $fich.1 >> $fich
   rm -f $fich.1
}

######################################################################
#
#  ASTK installation directory
#
CLEAR
echo " ASTK INSTALLATION DIRECTORY"
echo " "
iret=1
while [ $iret -ne 0 ];
do
   iret=0
   echo " "
   echo " Pathname of ASTK install directory (default $inst_dir/ASTK) : "
   read rtn
   if [ -z "$rtn" ]; then
      rtn="$inst_dir/ASTK"
   fi
   astk_inst_dir=$rtn
   jret=0
   if [ ! -d "$astk_inst_dir" ]; then
      mkdir -p $astk_inst_dir > /tmp/$$.instv71 2>&1
      iret=$?
      if [ $iret -ne 0 ]; then
   		echo " Fail to create directory $astk_inst_dir "
         echo " System message :"
         cat /tmp/$$.instv71
         echo " "
	      confirm "Change installation directory" "bidon" "y"
         jret=$?
	      if [ $jret -ne 0 ]; then
	         echo " Installation aborted !"
	      	exit 8
	      fi
      fi
      if [ -f /tmp/$$.instv71 ]; then
         rm -f /tmp/$$.instv71
      fi
   fi
   compdir "$astk_inst_dir"
   astk_inst_dir="$fulldir"
   if [ $jret -eq 0 ]; then
	   if [ ! -w $astk_inst_dir  ]; then
         iret=1
	   	echo " No write access to directory $astk_inst_dir."
	   	confirm "Change installation directory" "bidon" "y"
         jret=$?
	   	if [ $jret -ne 0 ]; then
	   	   echo " Installation aborted !"
	   		exit 8
	   	fi
	   fi
   fi
done

######################################################################
#
# Ready
#
CLEAR
echo "ASTK installation - Summary :"
echo " "
echo "  Destination            : $astk_inst_dir"
echo "  Tcl/Tk binaries        : $tcl_dir/bin"
if [ $astk_inst -eq 0 ]; then
   echo "  Tools                  : $tools_dir"
fi
echo "  GZIP                   : $gzip/gzip"
echo " "
confirm "Ready to install ASTK" "bidon" "y"
iret=$?
if [ $iret -ne 0 ]; then
   echo " Re-run ./install.sh to modify parameters..."
   exit 8
fi

######################################################################
#
# extract tar file
#
CLEAR
cd $astk_inst_dir
echo " "
echo " Extracting ASTK tar file, please wait..."
echo " "
$gzip/gzip -d < $dirhome/astk.tar.gz | tar -xf -

######################################################################
#
# GO !
CLEAR
echo "Starting ASTK installation."

# Install IHM
install_client

# Install server part
install_serveur

# Prepare tarball for future installations (normally of server part)
if [ $astk_inst -eq 0 ]; then
   cd $dirhome
   cp $astk_inst_dir/README_ASTK .
   tar cf astk_$vastk.tar README_ASTK install.sh \
      `ls UTILS/* | grep -v main_aster.sh` \
      astk.tar.gz
   rm -f README_ASTK
fi

######################################################################
#
# Configure ASTK tools and default server
#
if [ $astk_inst -ne 0 ]; then
   tools_dir="XXX"
   home_xterm="/usr/bin"
   edit_cmd="nedit"
fi
fout=$astk_inst_dir/ASTK_CLIENT/lib/ASTK/astkrc/outils
mv -f $fout $fout.old
sed -e "s#HOME_ASTK#$astk_inst_dir#" \
    -e "s#TOOLS_DIR#$tools_dir#" \
   $fout.old > $fout

fout=$astk_inst_dir/ASTK_CLIENT/lib/ASTK/astkrc/config_serveurs
mv -f $fout $fout.old
mach=`uname -n`
sed -e "s#HOME_ASTK#$astk_inst_dir#" \
    -e "s#HOME_XTERM#$home_xterm#" \
    -e "s#FULL_SERVER_NAME#$nom_complet#" \
    -e "s#SERVER_NAME#$mach#" \
    -e "s#EDITOR#$edit_cmd#" \
   $fout.old > $fout

fout=$astk_inst_dir/ASTK_CLIENT/lib/ASTK/astkrc/prefs
mv -f $fout $fout.old
sed -e "s#HOME_XTERM#$home_xterm#" \
    -e "s#VVV#$pseudo_vers#" \
    -e "s#EDITOR#$edit_cmd#" \
    -e "s#DOMAIN_NAME#$domain_name#" \
   $fout.old > $fout

######################################################################
#
# Add links to tools_dir
#
if [ $astk_inst -eq 0 ]; then
   CLEAR
   cd $tools_dir
   rm -f astk bsf
   ln -s "$astk_inst_dir/ASTK_CLIENT/bin/astk" .
   ln -s "$astk_inst_dir/ASTK_CLIENT/bin/bsf" .
   echo " 'astk' and 'bsf' symbolic links added to $tools_dir."
else
   echo " You must to configure ASTK_SERV/conf files to reflect"
   echo " the installation of your Code_Aster versions."
fi
echo " "

echo "ASTK $vastk installation completed !"
echo " "

######################################################################
#
# instructions
#
echo "To run ASTK, type :"
if [ $astk_inst -eq 0 ]; then
   echo "  $tools_dir/astk"
else
   echo "  $astk_inst_dir/ASTK_CLIENT/bin/astk"
fi
echo " "
