#!/bin/csh
#******************************************************************************
#                              ISTP LZP SOFTWARE
#                      Property of the U.S. Government
#                              NASA/GSFC/Code 500
#******************************************************************************
#
#    UNIT NAME:  Install_ISTP
#
#    PURPOSE:  To install, compress, decompress code for ISTP subsystems
#
#    INVOCATION METHOD:  Install_ISTP
#
#    EXTERNAL VARIABLES:
#    Source          Name            Type       Use  Description
#    --------------  --------------  ---------  ---  -----------
#    C-Shell         $status         string     I/O  success = 0, else failure
#
#    EXTERNAL UNIT/MACRO/PROCEDURE/FUNCTION REFERENCES:
#    Name            Purpose
#    --------------  ----------------
#    Create_ISTP.*   To create directory tree for ISTP subsystem *
#    Extract_ISTP.*  To extract ISTP subsystem * from PVCS archives
#
#    ABNORMAL TERMINATION CONDITIONS, ERROR MESSAGES:
#    $status <> 0 when creation/extraction/transfer fails
#
#    ASSUMPTIONS, CONSTRAINTS, RESTRICTIONS:
#    Requires standard tar, compress and uncompress utility programs
#
#    DEVELOPMENT HISTORY:
#    Author           Change-ID        Release  Date      Description of Change
#    ---------------  ---------------  -------  --------  ---------------------
#    R. Wiechert      ISTP             R1.0     07/14/97  Original code
#    R. Wiechert      ISTP             R1.0     07/29/97  Updated default settings
#    R. Wiechert      ISTP             R1.0     08/01/97  Make more user friendly
#    R. Wiechert      ISTP             R1.0     09/12/97  Remove ftp, keep tar
#    R. Wiechert      ISTP             R1.0     09/18/97  tar hidden files as well
#    R. Wiechert      ISTP             R1.0D    03/05/98  Obsolete trend subsystem
#
#    NOTES:
#    The user is responsible for transporting compressed code between
#    the archive host and the runtime host.  Both binary mode FTP and
#    physical tape media are suitable means for accomplishing this.
#
#******************************************************************************

set archive_host   = tools1
set archive_tree   = `pwd`
set runtime_tree   = /opt/local/cm/B1
set create_script  = Create_ISTP
set extract_script = Extract_ISTP

#******************************************************************************
echo " "
echo " "
echo " "
echo "============================================================"
echo " "
echo "                Installing  ISTP  Software"
echo " "
echo "============================================================"
set local_host = `hostname`
if ( "$local_host" == "$archive_host" ) then
   set default_tree = "$archive_tree"
else
   set default_tree = "$runtime_tree"
endif



set create = "Y"
while ( "$create" == "Y" )
   echo " "
   echo " "
   echo " "
   echo -n "Create directory tree (Y/N)? "
   set create = "$<"
   if ( "$create" == "Y" ) then

      echo "Enter full path of target tree ($default_tree): "
      set top = "$<"
      if ( "$top" == "" || "$top" == "*" ) set top = "$default_tree"

      echo -n "Enter subsystem (fs/ad/trend/recon/dps/all): "
      set subsys = "$<"
      if ( "$subsys" == "fs" || "$subsys" == "ad" || "$subsys" == "trend" \
           || "$subsys" == "recon" || "$subsys" == "dps" || "$subsys" == "all" ) then

         if ( -d "$top" ) then
            echo -n "Merge into existing tree (Y/N)? "
            set answer = "$<"
         else
            set answer = "Y"
            mkdir $top
         endif

         if ( "$answer" == "Y" ) then
            if ( "$subsys" == "all" ) then
               set errno = ""
               echo "Skipping fs..."
               echo "Creating ad..."
               $create_script.ad $top
               if ( "$status" != "0" ) set errno = "$errno ad=$status,"
               echo "Skipping trend..."
               echo "Creating recon..."
               $create_script.recon $top
               if ( "$status" != "0" ) set errno = "$errno recon=$status,"
               echo "Creating dps..."
               $create_script.dps $top
               if ( "$status" != "0" ) set errno = "$errno dps=$status,"
               if ( "$errno" == "" ) set errno = "0"
            else
               echo "Creating $subsys..."
               $create_script.$subsys $top
               set errno = "$status"
            endif
            if ( "$errno" == "0" && -d "$top" ) then
               echo " "
               echo "Creation complete"
               set default_tree = "$top"
            else
               echo "ERROR: creation failed: $errno"
               set default_tree = "$top"
            endif

         else
            echo "ERROR: creation aborted"
         endif

      else
         echo "ERROR: invalid subsystem"
      endif

   endif
end



set extract = "Y"
while ( "$extract" == "Y" )
   echo " "
   echo " "
   echo " "
   echo -n "Extract code from archives (Y/N)? "
   set extract = "$<"
   if ( "$extract" == "Y" ) then

      echo "Enter full path of target tree ($default_tree): "
      set top = "$<"
      if ( "$top" == "" || "$top" == "*" ) set top = "$default_tree"

      echo -n "Enter subsystem (fs/ad/trend/recon/dps/all): "
      set subsys = "$<"
      if ( "$subsys" == "fs" || "$subsys" == "ad" || "$subsys" == "trend" \
           || "$subsys" == "recon" || "$subsys" == "dps" || "$subsys" == "all" ) then

         if ( -d "$top" ) then

            echo -n "Extract by promotion or version label (P/V)? "
            set method = "$<"
            if ( "$method" == "P" || "$method" == "V") then
               if ( "$method" == "V" ) then
                  echo -n "Enter version label: "
                  set vlabel = "$<"
                  if ( "$vlabel" == "" || "$vlabel" == "*" ) then
                     echo "ERROR: invalid version label"
                     set default_tree = "$top"
                     continue
                  else
                     set getoption = "-v$vlabel"
                  endif
               endif
               if ( "$method" == "P" ) then
                  echo -n "Enter promotion group (DEV/IT/ST/AT/PRODUCTION): "
                  set plabel = "$<"
                  if ( "$plabel" != "DEV" && "$plabel" != "IT" && "$plabel" != "ST" && \
                       "$plabel" != "AT" && "$plabel" != "PRODUCTION" ) then
                     echo "ERROR: invalid promotion group"
                     set default_tree = "$top"
                     continue
                  else
                     set getoption = "-g$plabel"
                  endif
               endif
            else
               echo "ERROR: invalid extraction method"
               set default_tree = "$top"
               continue
            endif

            if ( "$subsys" == "all" ) then
               set errno = ""
               echo "Skipping fs..."
               echo "Extracting ad..."
               $extract_script.ad $top $getoption
               if ( "$status" != "0" ) set errno = "$errno ad=$status,"
               echo "Skipping trend..."
               echo "Extracting recon..."
               $extract_script.recon $top $getoption
               if ( "$status" != "0" ) set errno = "$errno recon=$status,"
               echo "Extracting dps..."
               $extract_script.dps $top $getoption
               if ( "$status" != "0" ) set errno = "$errno dps=$status,"
               if ( "$errno" == "" ) set errno = "0"
            else
               echo "Extracting $subsys..."
               $extract_script.$subsys $top $getoption
               set errno = "$status"
            endif
            if ( "$errno" == "0" && -d "$top" ) then
               echo " "
               echo "Extraction complete"
               set default_tree = "$top"
            else
               echo "ERROR: extraction failed: $errno"
               set default_tree = "$top"
            endif

         else
            echo "ERROR: directory does not exist"
         endif

      else
         echo "ERROR: invalid subsystem"
      endif

   endif
end



set compress = "Y"
while ( "$compress" == "Y" )
   echo " "
   echo " "
   echo " "
   echo -n "Compress source code (Y/N)? "
   set compress = "$<"
   if ( "$compress" == "Y" ) then
 
      echo "Enter full path of source tree ($default_tree): "
      set top = "$<"
      if ( "$top" == "" || "$top" == "*" ) set top = "$default_tree"
 
      echo -n "Enter subsystem (fs/ad/trend/recon/dps/all): "
      set subsys = "$<"
      if ( "$subsys" == "fs" || "$subsys" == "ad" || "$subsys" == "trend" \
           || "$subsys" == "recon" || "$subsys" == "dps" || "$subsys" == "all" ) then
 
         if ( "$subsys" == "all" ) then
            set source = "$top"
            set tarlist = `ls -A1 $source | grep -v "\.tar"`
         else
            set source = "$top/$subsys"
            set tarlist = "$subsys"
         endif

         if ( -d "$source" ) then

            echo " "
            set original_directory = `pwd`
            cd $top
            echo "Compressing files..."
            tar -cf $top/$subsys.tar $tarlist
            if ( "$status" == "0" ) then
               compress $top/$subsys.tar
               if ( "$status" == "0" ) then
                  echo "Compression complete"
                  echo "File is $top/$subsys.tar.Z"
                  set default_tree = "$top"
               else
                  echo "ERROR: compression failed: compress"
                  set default_tree = "$top"
               endif
            else
               echo "ERROR: compression failed: tar"
               set default_tree = "$top"
            endif
            cd $original_directory

         else
            echo "ERROR: directory does not exist"
         endif

      else
         echo "ERROR: invalid subsystem"
      endif

   endif
end



set decompress = "Y"
while ( "$decompress" == "Y" )
   echo " "
   echo " "
   echo " "
   echo -n "Decompress source code (Y/N)? "
   set decompress = "$<"
   if ( "$decompress" == "Y" ) then
 
      echo "Enter full path of source tree ($default_tree): "
      set top = "$<"
      if ( "$top" == "" || "$top" == "*" ) set top = "$default_tree"
 
      echo -n "Enter subsystem (fs/ad/trend/recon/dps/all): "
      set subsys = "$<"
      if ( "$subsys" == "fs" || "$subsys" == "ad" || "$subsys" == "trend" \
           || "$subsys" == "recon" || "$subsys" == "dps" || "$subsys" == "all" ) then
 
         if ( "$subsys" == "all" ) then
            set tarlist = " "
         else
            set tarlist = "$subsys"
         endif

         if ( -d "$top" ) then
            if ( -f "$top/$subsys.tar.Z" ) then

               echo " "
               set original_directory = `pwd`
               cd $top
               echo "Decompressing files..."
               uncompress $top/$subsys.tar.Z
               if ( "$status" == "0" ) then
                  tar -xpf $top/$subsys.tar $tarlist
                  if ( "$status" == "0" ) then
                     echo "Decompression complete"
                     set default_tree = "$top"
                  else
                     echo "ERROR: decompression failed: tar"
                     set default_tree = "$top"
                  endif
               else
                  echo "ERROR: decompression failed: uncompress"
                  set default_tree = "$top"
               endif
               cd $original_directory
 
            else
               echo "ERROR: $subsys.tar.Z does not exist"
            endif

         else
            echo "ERROR: directory does not exist"
         endif
 
      else
         echo "ERROR: invalid subsystem"
      endif
 
   endif
end



exit 0
