#!/bin/csh -f
alias osfile_stretch_file echo /dev/null
source ~/.cshrc
unalias osfile_stretch_file
#
#--------------------------------------------------------------------
#
#       new_car_smell
#
#       C shell script to make used pipelines like new again.
#
#       Copyright: (c) 2000 The Association of Universities for
#                  Research in Astronomy, Inc. All rights reserved.
#                  see: http://www.stsci.edu/policy/Copyright.html
#
#--------------------------------------------------------------------
#                          REVISION HISTORY
#--------------------------------------------------------------------
#  MOD             PR
# LEVEL   DATE   NUMBER  User     Description
# ----- -------- ------  -------  ------------------------------------
#  001  04/20/00 41456   WMiller  Initial code
#  002  03/22/01 43520   WMiller  Upgrade for 3.2
#--------------------------------------------------------------------
#
#
onintr INTRPT
#
set opsys = `uname -s`
switch ( $opsys )
   case OSF1:
      set arch = axp_unix
      breaksw
   case SunOS:
      set arch = sparc_solaris
      breaksw
   case Linux:
      set arch = linux
      breaksw
   default:
      echo ""
      echo "WARNING: Operating system type $opsys unknown."
      echo ""
      set query = "Press return to continue "
      set yes = EXIT
      set no = $yes
      set default = $yes
      goto YESORNO
endsw
#
# Check current version
#
if ( $?OPUS_VERSION ) then
   if ( "$OPUS_VERSION" == "Pipeline Software Release SHARE 2.2 ********* 11 May 2000 *********" ) then
      set at_2_2
   endif
endif
#
# Intro
#
clear
echo    "   ------------------------------------------------------"
echo    "   - Welcome to the OPUS v3.2 Pipeline Upgrade Utility\! -"
echo    "   ------------------------------------------------------"
echo    ""
echo    "This utility will convert one or more OPUS v1.4 or v2.2 pipelines to"
echo    "OPUS v3.2. It assumes that the account you are runnning it in already"
echo    "is set-up for OPUS v1.4 or v2.2, and that one or more pipelines have"
echo    "been defined (the OPUS Sample Pipeline or your own pipeline) and are"
echo    "accessible through the environment variable OPUS_DEFINITIONS_DIR."
echo    ""
echo    "Earlier releases of OPUS must be upgraded to v1.4 before this utility"
echo    "is used. If you need to upgrade to v1.4, run opus_install in the v1.4"
echo    "release first. DO NOT PROCEED UNLESS YOU ARE RUNNING AT LEAST V1.4\!"
echo    ""
echo    "In addition, before using this utility you should upgrade your OPUS"
echo    "distribution to OPUS v3.2 by running opus_install in the v3.2 release."
echo    ""
echo    "Details of the changes introduced in v3.2 can be found in the"
echo    "README file and the updated FAQ included in the v3.2 release. Please"
echo    "refer to those documents for additional information and instructions."
echo    ""
set query = "Do you wish to continue (y or n)? [n] "
set yes = UPGRADE_TASKS
set no = EXIT
set default = $no
goto YESORNO
UPGRADE_TASKS:
clear
echo ""
echo    "Enter the complete path to the OPUS v3.2 directory"
echo -n "(eg. /usr/local/opus): "
set opus_tree = $<
if ( ! -d $opus_tree ) then
   set query = "No such directory $opus_tree. Specify another (y or n)? [n] "
   set yes = UPGRADE_TASKS
   set no = EXIT
   set default = $no
   goto YESORNO
endif
if ( ! -e $opus_tree/lib/$arch/liboapi.so || ! -e $opus_tree/install/cshrc_stub || ! -e $opus_tree/install/opus_login_template.csh ) then
   echo ""
   echo "ERROR: I could not find the v3.2 installation. You must install OPUS"
   echo "       v3.2 before using this utility."
   echo ""
   set query = "Press return to continue "
   set yes = EXIT
   set no = $yes
   set default = $yes
   goto YESORNO
endif
#
# Need OAPI shared library for upcoming steps, so put it in path LD_LIBRARY_PATH
#
if ( $?LD_LIBRARY_PATH ) then
   setenv LD_LIBRARY_PATH $opus_tree/lib/$arch/:$LD_LIBRARY_PATH
else
   setenv LD_LIBRARY_PATH $opus_tree/lib/$arch/
endif
set bindir = $opus_tree/bin/$arch
#
#
# Check for OPUS_DEFINITIONS_DIR & OPUS_HOME_DIR in the environment
#
if ( ! $?OPUS_DEFINITIONS_DIR || ! $?OPUS_HOME_DIR ) then
   echo ""
   echo "SORRY: The environment variable OPUS_DEFINITIONS_DIR or OPUS_HOME_DIR"
   echo "       is not defined in your environment. This script must be run"
   echo "       from an account configured to run your v1.4 or v2.2 pipelines."
   echo ""
   set query = "Press return to continue "
   set yes = EXIT
   set no = $yes
   set default = $yes
   goto YESORNO
endif
#
# Resolve any stretches in OPUS_DEFINITIONS_DIR
#
set temp = ($OPUS_DEFINITIONS_DIR)
set odefdir = ""
while ( $#temp )
   set resolv = `osfile_stretch_file $temp[1]`
   set odefdir = "$odefdir $resolv"
   shift temp
end
#
# Resolve OPUS_HOME_DIR
#
set ohomdir = `osfile_stretch_file OPUS_HOME_DIR:`
#
# Warnings
#
echo ""
echo "WARNING: Prior to any upgrade operation (like this one), it would be"
echo "         wise to backup all of your pipeline files (a list of affected"
echo "         directories is displayed in the next few screens) in the event"
echo "         they need to be restored to v1.4 or v2.2 format for whatever"
echo "         reason."
echo ""
echo "WARNING: There should be no processes (including the pmg and omg)"
echo "         running in your pipeline before continuing. Any leftover"
echo "         PSTATs will not be converted to the new format."
echo ""
set query = "Do you wish to continue (y or n)? [n] "
set yes = UPGRADE_ITEMS
set no = EXIT
set default = $no
goto YESORNO
UPGRADE_ITEMS:
clear
echo "The following items may be upgraded by this script:"
echo ""
echo "   1. External poller process resource files (v1.4 only)"
echo "      - The -t option of xpoll is changed to -r in the TASK line."
echo "      - The token ENV. is prefixed to keywords that should be set"
echo "        in the environment of the xpoll COMMAND process." 
echo ""
echo "   2. opus.env file"
echo "      - New entries are added to support the OAPI."
echo "      - PSTAT node size is changed to 20."
echo ""
echo "   3. OSFs"
echo "      - OSF size is changed to accommodate 64 character dataset names."
echo ""
echo "   4. opus_login.csh (v1.4 only)"
echo "      - Add entry for LD_LIBRARY_PATH and modify XUSERFILESEARCHPATH."
echo ""
echo "   5. .cshrc"
echo "      - Update the path to OPUS v3.2."
echo ""
set query = "Press return to continue "
set yes = UDIRS
set no = $yes
set default = $yes
goto YESORNO
UDIRS:
clear
echo "This utility uses the definition of OPUS_DEFINITIONS_DIR to locate"
echo "files to be updated. For example, process resource files will be"
echo "searched for in the path defined by OPUS_DEFINITIONS_DIR. If you need"
echo "to update pipelines not reachable by the current definition of"
echo "OPUS_DEFINITIONS_DIR, then exit this utility and redefine this variable"
echo "to include those pipelines."
echo ""
echo "Given the current definition of OPUS_DEFINITIONS_DIR, the directories"
echo "in which upgrades will be performed are:"
echo ""
foreach d ($odefdir)
   echo "    "$d
end
echo ""
set query = "Continue (y or n)? [y] "
set yes = ODIRS
set no = EXIT
set default = $yes
goto YESORNO
ODIRS:
#
# If at v2.2, skip OSF stuff
#
if ( $?at_2_2 ) then
   goto DOIT
endif
#
# Display OSF directories that will be upgraded
#
echo ""
echo "Any directories containing OSFs as located through the"
echo "OPUS_OBSERVATIONS_DIR entry in your path files will be upgraded."
echo ""
echo "The OSF directories appear to include:"
echo ""
foreach d ($odefdir)
   /bin/ls $d/*.path >& /dev/null   # test for path file-less directories
   if ($status) continue
   set path_files = `/bin/ls $d/*.path`
   foreach pfile ($path_files)
      set res = `$bindir/get_path_item $pfile OPUS_OBSERVATIONS_DIR`
      if ($status == 0) echo "   "$res
   end
end
echo ""
set query = "Do you wish to begin the upgrade process (y or n)? [n] "
set yes = DOIT
set no = EXIT
set default = $no
goto YESORNO
DOIT:
clear
#
# Upgrade .cshrc
#
echo "Saving original .cshrc as cshrc_041367OPUS..."
/bin/cp ~/.cshrc ~/cshrc_041367OPUS
grep 041367OPUS ~/.cshrc >& /dev/null
if ( $status ) then
   echo ""
   echo "WARNING: Could not locate previous OPUS modifications to your .cshrc."
   echo "         Your path variable cannot be updated for v3.2 nor can your"
   echo "         opus_login.csh. If you choose to continue, you must make"
   echo "         these modifications manually after this script finishes."
   echo "         Consult the v3.2 README file for instructions on how to make"
   echo "         these changes."
   echo ""
   set query = "Continue with other upgrades (y or n)? [n] "
   set yes = PUPS
   set no = EXIT
   set default = $no
   goto YESORNO
endif
echo "Modifying .cshrc..."
set ologin = `cat ~/.cshrc | grep "041367OPUS:7" | awk '{print $2}'`
set lin = `cat ~/.cshrc | grep -n "041367OPUS:6" | awk -F: '{print $1}'`
@ strt = $lin - 1
head -$strt ~/.cshrc >! ~/cshrc_tmp_041367OPUS
cat $opus_tree/install/cshrc_stub | grep "041367OPUS:6" >>! ~/cshrc_tmp_041367OPUS
@ lin ++
tail +$lin ~/.cshrc >>! ~/cshrc_tmp_041367OPUS
/bin/mv -f ~/cshrc_tmp_041367OPUS ~/.cshrc
#
# Create OPUS_HOME_DIR files
#
echo "Creating CORBA object map in OPUS_HOME_DIR..."
touch $ohomdir/opus_iors
chmod go-rwx $ohomdir/opus_iors
#
# Upgrade opus_login.csh for v1.4 users
#
if ( $?at_2_2 ) then
   goto PUPS
endif
if ( $ologin == "" || ! -e $ologin ) then
   echo ""
   echo "WARNING: Could not locate your opus_login.csh:"
   echo "         $ologin"
   echo "         This file cannot be updated for v3.2. If you choose to"
   echo "         continue, you must make modifications to this file manually"
   echo "         after this script finishes. Consult the v3.2 README file for"
   echo "         instructions on how to make these changes."
   echo ""
   set query = "Continue with other upgrades (y or n)? [n] "
   set yes = PUPS
   set no = EXIT
   set default = $no
   goto YESORNO
endif
echo ""
echo "Saving original opus_login.csh as opus_login.csh_041367OPUS..."
/bin/cp $ologin ${ologin}_041367OPUS
echo "Modifying $ologin..."
set ispt = `/bin/cat $ologin | grep -n "END OF USER-DEFINED VARIABLES" | /bin/awk -F: '{print $1}'`
if ($status != 0) then
   set ispt = 2
endif
@ ispt --
head -$ispt ${ologin}_041367OPUS >! $ologin
cat $opus_tree/install/opus_login_template.csh | awk '{if (NR >= 50 && NR <= 70) print $0}' >>! $ologin
@ ispt ++
@ ispt ++
tail +$ispt ${ologin}_041367OPUS | awk '{if (index($0,"XUSERFILESEARCHPATH") && index($0,"setenv")) print "#"$0; else print $0}' >>! $ologin
#
# Perform pipeline upgrades
#
PUPS:
if ( $?ocobs) then
   unset ocobs
endif
foreach d ( $odefdir )
   echo ""
   echo    "Performing upgrades for directory $d..."
#
# Upgrade process resource files using update_xternal_pollers
#
   if ( $?at_2_2 ) then
      goto OPUS_ENV
   endif
   echo -n "RESOURCE FILES: "
   /bin/ls $d/*.resource >& /dev/null
   if ($status == 0) then
      set res_files = `/bin/ls $d/*.resource`
      set no_work = 1
      foreach pfile ( $res_files )
         $bindir/update_xternal_poller $pfile > ${pfile}_new
         if ($status == 0) then
            echo -n "   $pfile:"
            /bin/mv -f ${pfile}_new ${pfile}
            if ($status) then
               echo " update FAILED."
               continue
            else
               echo " updated."
               set no_work = 0
            endif
         else
            /bin/rm -f ${pfile}_new
         endif
      end
      if ($no_work) echo " No files updated."
   else
      echo " No files to be updated."
   endif
#
# Rename OSFs to have 64 character dataset fields
#
   echo ""
   echo -n "OSFs: "
   /bin/ls $d/*.path >& /dev/null
   if ($status == 0) then
      echo ""
      set omask = '????????-????????????????????????.???????????????????????-???-???-????'
      set xtra  = "_________________________________________"
      set path_files = `/bin/ls $d/*.path`
      foreach pfile ($path_files)
         set res = `$bindir/get_path_item $pfile OPUS_OBSERVATIONS_DIR`
         if ($status == 0) then
            set color = `basename $pfile`
            echo -n "   ${color}:"
            /bin/ls $res/$omask >& /dev/null
            if ($status == 0) then
               set bad_mv = 0
               set osfs = `/bin/ls $res/$omask`
               foreach o ($osfs)
                  set new_o = $o:r.`echo $o:e | awk -F- '{print $1}'`$xtra
                  set new_o = ${new_o}`echo $o:e | awk -F- '{print FS$2FS$3FS$4}'`
                  /bin/mv -f $o $new_o
                  if ($status) set bad_mv = 1
               end
               if ($bad_mv) then
                  echo " One or more OSF updates FAILED."
               else
                  echo " OSFs updated."
               endif
            else
               echo " No OSFs to update."
            endif
         endif
      end
   else
      echo " No path files found."
   endif
#
# Append new opus.env content to existing file
#
   OPUS_ENV:
   echo ""
   echo -n "opus.env:"
   /bin/ls $d/opus.env >& /dev/null
   if ($status == 0) then
      grep BB_TYPE $d/opus.env >& /dev/null
      if ($status) then
          /bin/cat $opus_tree/install/opus_env_stub >> $d/opus.env
          if ($status == 0) then
             echo " Updated."
          else
             echo " Update FAILED."
          endif
      else
          if ( $?updated_opus_env) then
             unset updated_opus_env
          endif
          grep "current options are: FILE, CORBA" $d/opus.env >& /dev/null
          if ( $status && -w $d ) then
             /bin/cat $d/opus.env | sed "s/current options are: FILE/current options are: FILE, CORBA/g" > $d/tmp.041367OPUS
             /bin/mv -f $d/opus.env $d/opus.env_old_1
             /bin/mv -f $d/tmp.041367OPUS $d/opus.env
             echo -n " Updated BB_TYPE comment;"
             set updated_opus_env
          endif
          set node_size = `grep "^[ \t]*NODE\.SIZE" $d/opus.env | tr -d "[a-zA-Z .=\t]"`
          set proc_size = `grep "^[ \t]*PROCESS\.SIZE" $d/opus.env | tr -d "[a-zA-Z .=\t]"`
          if ( $node_size != "20" && -w $d ) then
             /bin/cat $d/opus.env | sed "/^[ \t]*NODE.SIZE/{\
s/^/\!/\
a\\
NODE.SIZE             = 20\
}" > $d/tmp.041367OPUS
             /bin/mv -f $d/opus.env $d/opus.env_old_2
             /bin/mv -f $d/tmp.041367OPUS $d/opus.env
             echo -n " Updated PSTAT node size;"
             set updated_opus_env
          endif
          if ( $proc_size != "9" && -w $d ) then
             /bin/cat $d/opus.env | sed "/^[ \t]*PROCESS.SIZE/{\
s/^/\!/\
a\\
PROCESS.SIZE          = 9\
}" > $d/tmp.041367OPUS
             /bin/mv -f $d/opus.env $d/opus.env_old_3
             /bin/mv -f $d/tmp.041367OPUS $d/opus.env
             echo -n " Updated PSTAT process size;"
             set updated_opus_env
          endif
          if ( $?updated_opus_env ) then
             echo " Done."
          else
             echo " File up-to-date."
          endif
      else
          echo " File up-to-date."
      endif
   else
      echo " Not found here."
   endif
#
# Add opus_corba_objs
#
   echo -n "opus_corba_objs:"
   if ( ! $?ocobs ) then
      /bin/cp $opus_tree/definitions/unix/opus_corba_objs_template $d/opus_corba_objs
      set ocobs
      echo  " Copied to $d."
   else
      echo  " Already in stretch."
   endif
end
echo ""
echo "Update complete."
echo ""
set query = "Press return to continue "
set yes = EXIT
set no = $yes
set default = $yes
goto YESORNO
#
# Interrupt signal
#
INTRPT:
echo ""
if ( $?installing ) then
   echo ""
   echo "WARNING: You have interrupted the upgrade procedure."
   echo "Your pipeline may not be fully upgraded."
endif
echo ""
set query = "Press return to continue "
set yes = EXIT
set no = $yes
set default = $yes
goto YESORNO
#
# Generic exit
#
EXIT:
exit 0
#
# Yes or no query to user
#
YESORNO:
echo    ""
echo -n "$query"
set response = $<
if ( $response == "y" || $response == "Y" ) goto $yes
if ( $response == "n" || $response == "N" ) goto $no
if ( $response == "" ) goto $default
goto YESORNO
