#!/bin/csh
#
#                               S.L.Freeland
#				2-Nov-1993 
#
#; Name:    ys_install
#;
#; Purpose: auto-ftp, then install or upgrade from compressed tar files
#;
#; Calling Sequence:
#;   ys_install [/noftp] [/site] [/nodelete] [/nodbase] [/noexe] [/help]
#;
#; Optional Parameters (override defaults):
#;	/full	  - force full upgrade
#;	/noftp	  - dont ftp new files (use existing $ys/swmaint/tar)
#;      /site     - force installation of site branch
#;      /nodbase  - dont unpack data base files
#;      /nodelete - dont remove tar files
#;      /noexe    - inhibits execution of customized script
#;      /size	  - print installation disk size requirements and exit
#;      /ysrem	  - remove branches from ys PRIOR to ftp transfer
#;      /help     - print out this header and exit with no action
#;
#; Notes:
#;      site branch - available for local routines and local cusomization
#;                    default is to not clobber this if it exists
#;      dbase files - these include Yohkoh ephemeris, Yohkoh event logs
#;                    goes events/data.... etc  These are highly recommended
#;                    for full SW function unless disk space is extremely
#;                    limited at your site
#;
#;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 -  2-Nov-1993	 # auto-ftp & Mail 
#		SLF -  3-Nov-1993	 # broke function into boot / install
#					 # this file sets defaults and gets
#					 # recent installation package
#		SLF -  7-Nov-1993 	 # Released 
#		SLF -  5-Feb-1994	 # revised (/help)
#		SLF - 14-Jul-1994 	 # allow boot tape (/noftp)
#;		SLF -  5-Aug-1994 	 # add SIZE and YSREM switches
#;
# --- determine location of $ys
set curdir=`pwd`
setenv yssw_curdir $curdir
if (!($?ys)) then
   if ($curdir:t == "tar") set curdir=$curdir:h
   if ($curdir:t == "swmaint") then
      setenv ys $curdir:h
   else
      if( (!($?ys)) && (-e /ys) ) then
        setenv ys /ys
      else
         echo  Please define environmental variable ys and retry
         echo  or run this from your tar file location...
         exit           #### unstructured exit
      endif
   endif
endif

# --- setup defaults---
setenv yssw_noftp  0	        # default is to ftp files required
setenv yssw_dosite 0            # instll ys_site
setenv yssw_dodata 1            # install any existing non-ys (ydb,etc)
setenv yssw_remove 1            # remove tar files after installation
setenv yssw_doexe  1            # execute customized script
setenv yssw_dohelp 0	        # dont show help
setenv yssw_full   0	        # default is incremental if available
setenv yssw_size   0		# if set, print size diagnostics and exit
setenv yssw_ysrem  0		# if set, remove ys before ftp transfer
# ----------------------------------------------------------------------
# update defaults if site config file exists
if (-e $ys/site/setup/ys_install.config) source $ys/site/setup/ys_install.config
# ----------------------------------------------------------------------
# --- handle parameters ---
foreach argx ($argv)
   switch ($argx)
      case full:		# force full upgrade (def=incremental)
      case /full:
         setenv yssw_full 1
	 breaksw
      case site:                # force site branch expansion
      case /site:
         setenv yssw_dosite 1
         breaksw
      case dbase:                # force data base expansion (default)
      case /dbase:
         setenv yssw_dodata 1
         breaksw
      case nodbase:		 # inhibit data base expansion
      case /nodbase:
         setenv yssw_dodata 0
         breaksw
      case delete:              # force tar file removal
      case /delete:
      case remove:
      case /remove:
         setenv yssw_remove 1
         breaksw
      case /nodelete:           # inhibit tar file removal
      case nodelete:
      case noremove:
      case /noremove:
          setenv yssw_remove 0
          breaksw
      case noexe:               # inhibit exectution of script
      case /noexec:
      case /noexe:
         setenv yssw_doexe 0
         breaksw
      case help:
      case /help:
         setenv yssw_dohelp 1
         breaksw
      case noftp:
      case /noftp:
	 setenv yssw_noftp 1
         breaksw
      case ysrem:
      case /ysrem:
         setenv yssw_ysrem 1
         breaksw
      case size:
      case /size:
         setenv yssw_size 1
         breaksw
      default:                                  # dont know this one
          echo Unrecognized option: $argx
	  echo Exiting....
	  exit					### unstructured exit
          breaksw
   endsw
end
# ------------------------------------------------------------------

if ($yssw_dohelp == 1) then
   cat $ys/swmaint/tar/ys_install | grep "#;"
   exit
endif

# ----- get newest installation kit ------------
unset noclobber				# protect agains local environ
set workdir=$ys/swmaint/script
set host=`hostname`
mkdir -p $workdir

# slf 13-Jul-1994 - make boot tape (/noftp) compatible with network install

set instools=ys_install.tar

if ( (-e ys_install.tar.Z) && ($yssw_noftp == 1)) then
   cp -p ys_install.tar.Z $workdir			# probably boot tape
   cd $workdir   
else
   cd $workdir
   set ftpins = $workdir/getins.ftp
   echo Generating ftp transfer script
   ########## build ftp script ########
   echo open 133.74.8.100     >  $ftpins
   echo user ftp $host	     >> $ftpins
   echo binary		     >> $ftpins
   echo cd pub/swmaint/tar   >> $ftpins
   echo get $instools".Z"    >> $ftpins
   echo bye		     >> $ftpins
   ####################################
   echo Starting ftp transfer of installation package: $instools".Z"
   ftp -in < $ftpins 
endif

if (!(-e $instools".Z")) then
   if ($yssw_noftp == 1) then
      echo "Could not find installation package:"$instools.".Z"
      echo "Contact software@isass0.solar.isas.ac.jp for guidence..."
   else
      echo "Trouble transfering installation package, try again later..."
   endif
endif

uncompress -f $instools".Z"
tar -xf $instools
# transfer control to $workdir/ys_install.control
echo Transferring control to: $workdir/ys_install.control
source $workdir/ys_install.control
exit
