#!/bin/csh
#
# ys_install.control
#                               S.L.Freeland
#                               7-Jun-1993
#				3-Nov-1993 - revised to do ftp step and increm
#
#; Name:    ys_install.control
#;
#; Purpose: control/perform install or upgrade Yohkoh SW on UNIX systems
#;
#;History:      SLF -  3-Jun-1993        # original (ys_install)
#;              SLF -  7-Jun-1993        # add document, set defaults, help...
#;		SLF - 16-Jun-1993	 # reference new awk script
#;					 # unalias rm in untar script
#;		SLF -  3-Nov-1993	 # add auto-ftp funct,split env setup
#;					 # broke control/install functions
#;

if (! $?ys) then
   echo ys not defined - run use ys_install front end
   exit
endif

if (-e $ys/site/ys_install.config) source $ys/site/ys_install.config
setenv ys_install $ys/swmaint/script	#slf 3-nov chaged working directory

setenv yssw_incremok 0
if  (  (!($yssw_full)) && (-e $ys/gen) ) then	# try incremental update
    echo " " 
    echo yohkoh_install.control: Initiating Incremental Upgrade
    source $ys_install/ys_install.increm
    if ($yssw_incremok) then
       echo ys_install.control: Incremental Upgrade completed, Normal exit
       exit						#### unstructured exit
    else
       echo " " 
       echo ys_install.control: Incremental Upgrade failed.
       echo ys_install.control: Continuing with full SW upgrade
       echo " " 
    endif
endif

if (!($yssw_noftp)) then
    source $ys_install/ys_install.ftpget	# ftp required tar files
endif

# --- determine data base file location ------------
# if there is a local setup file in local site directory, use it
if (-e $ys/site/setup/setup_paths) source $ys/site/setup/setup_paths
# (this probably defines local ydb area)

if ( (!($?ydb)) && (-e /ydb) ) setenv ydb /ydb  # use link?

if (!($?ydb)) then        		# ydb still not defined
   set ys = $ys
   if (!(-e $ys:h/ydb)) then
      mkdir -p $ys:h/ydb
      echo Cannot find seperate data base location, using: $ys:h/ydb
   endif
   setenv ydb $ys:h/ydb
endif

# ------------------------------------------------

if (!(-e $ys/site)) setenv yssw_dosite 1      # force site if no site branch already

# ---- help function --------- 
if ($yssw_dohelp == "1") then                        
   if (-e $ys_install/ys_install) then
      head -50 $ys_install/ys_install | grep "#;" | more
   else
      echo "Can not find script: "$ys_install/ys_install
   endif
   exit					### unstructured exit 
endif
# -----------------------------

if (!(-e $ys_install)) mkdir -p $ys_install
cd $ys_install
#
# ------ Now for the files ------------------
# Generate a script file based on current contents of ...swmaint/tar
set inst = $ys_install/ystar_install.src	# custom installation script
rm -f $inst
set date=`date`
if (!($?user)) set user="???"

# Now build a script to uncompress, untar , and install sets
set header="#\!/bin/csh"
echo $header > $inst
echo "# Yohkoh SW Installation Script Generated: "$date" by User: " $user >> $inst
echo  # >> $inst
# protect agains local environment problems
set lscmd=`alias ls`
set rmcmd=`alias rm`
unalias ls
unalias rm
set comptars=`ls $ys/swmaint/tar/*tar*`
echo "setenv ydb $ydb" >> $inst
echo "setenv ys $ys" >> $inst
echo set rmcmd = '`'alias rm'`' >> $inst
echo "unalias rm" >> $inst

echo "Building tar file installation script..."

if ($#comptars == 0) then
   echo "No tar files found in: $ys/swmaint/tar , exiting"
   exit					### unstructured exit 
endif

# --------- tar file loop -----------------------
foreach tarfile ($comptars)
   set tarname=$tarfile:t
   set splitfile=`echo $tarname | awk -f $ys_install/splitfile.awk`
   set toplev=$splitfile[1]
   set branch=$splitfile[2]
   switch ($toplev)
      case ys:                          # its a software branch
#        special cases:
#           1. site on request or if none exists
#           2. ignore 'install' branch (ys_install.tar)
         if ( (($branch != "site") || ($yssw_dosite == "1")) && ($branch != "install")) then
#           decompress if compressed
            if ($tarname == $toplev"_"$branch".tar.Z") then
               echo "uncompress -f $tarfile" >>  $inst
	    endif
            echo "cd $ys" >> $inst
            echo "rm -rf "$branch >> $inst
            echo "mkdir "$branch  >> $inst
	    echo "chmod 777 "$branch >> $inst
            set tarfil = "$tarfile:h""/"$toplev"_"$branch".tar"
            echo 'echo "Installing SW branch: "'$branch >> $inst
            echo "tar -xf "$tarfil" "$branch >> $inst
#           if site created, update host/ys configuration
            if ($branch == "site") then
                set host=`hostname`
                echo "echo $host  $ys > $ys/site/setup/yspaths.config" >> $inst
#
#               build site/setup_paths file for newly defined site directory
############################################################################
#               Define all environmentals used in gen/setup_dirs
#               ydb = Yohkoh data base environmental
#               gdb = Ground Observatory data base environmental
#               adb = ASCA data base environmental
                echo "echo setenv ydb $ydb >> $ys/site/setup/setup_paths" >> $inst
                echo "echo setenv gdb $ydb >> $ys/site/setup/setup_paths" >> $inst
                echo "echo setenv adb $ydb >> $ys/site/setup/setup_paths" >> $inst
############################################################################
            endif
#           remove tar files
            if ($yssw_remove == "1") then
               echo "echo removing tarfile via: rm -f $tarfil" >> $inst
               echo "rm -f $tarfil " >> $inst
            endif
         endif
         breaksw
      default:                                  # its a data branch
         if ($yssw_dodata == "1") then
            if ($tarname == $toplev"_"$branch".tar.Z") then
               echo "uncompress -f $tarfile">> $inst
	    endif
            echo  cd ' $'$toplev >> $inst
            echo "rm -rf "$branch >> $inst
            echo "mkdir "$branch >> $inst
	    echo "chmod 777 "$branch >> $inst
            set tarfil = "$tarfile:h""/"$toplev"_"$branch".tar"
            echo 'echo "Installing DB branch: "'$branch >> $inst
            echo "tar -xf "$tarfil" "$branch >> $inst
            if ($yssw_remove == "1") then
               echo "echo removing tarfile via: rm -f $tarfil" >> $inst
               echo "rm -f $tarfil " >> $inst
            endif
         endif
   endsw
end
# -------------- end of tar file loop -------------------

echo if '("$'rmcmd'"' != "''"')' alias rm '"$'rmcmd'"' >> $inst

if ("$lscmd" != "") alias ls $lscmd
if ("$rmcmd" != "") alias rm $lscmd

if ($yssw_doexe == "1") then
  echo "Executing installation script: "
  echo "     "$inst
  source $inst
  echo "Installation complete"
else
  echo "Installation inhibited"
  echo "Installation Script is: "
  echo "     "$inst
  echo "You must source that script to complete installation..."
endif

exit
