#!/bin/csh -f
#
# Before running this script one should be user fxa.  This script should
# only be run using the host meant to serve as the alps PFP display
# machine.  It is expected that this came from the PFP tarball, which
# was placed in /data/fxa and untarred from there.
#
if ( "$1" == "aXbYc" ) then
    while (1)
       set one = "$<"
       if ( "$one" == "" ) exit
       echo "about to process '$one'"
       if ( -e "$one" ) then
            echo $one already exists in local nationalData/
           continue
       endif
       echo make link for $one to default nationalData/
       ln -s /data/fxa/nationalData/$one "$one"
    end
    exit
endif
#
# See if this is a developer test install.
#
set def_home = /awips/fxa
set def_user = fxa
while ( "$1" == "=$FXA_HOME" )
    if ( $USER == fxa ) break
    if ( ! ( -d $FXA_HOME/src ) ) break
    set def_home = $FXA_HOME
    set def_user = $USER
    shift
    break
end
if ( "$*" != "" ) then
    echo "Improper args: $*"
    echo "ABORTING\!\!\!"
    exit
endif
#
# Validate the user
#
if ( $USER != $def_user ) then
    echo "This script needs to be run with the fxa account"
    echo "ABORTING\!\!\!"
    exit
endif
#
# Make sure that this is not happening on a dx, px, or lx
#
hostname | grep -E '^dx|^px|^lx' >& /dev/null
if ( $status == 0 ) then
    echo "This script should not be run on an ingest or baseline machine"
    echo "ABORTING\!\!\!"
    exit
endif
#
# Verify that the user really wants to do this.
#
set hhh = `hostname`
echo "This script is about to install the PFP project version of the ALPS"
echo "display software on ${hhh}.  All existing AWIPS software on this"
echo "machine under the directory $FXA_HOME will be deleted."
echo "This installation assumes that the $def_user account is set up"
echo "as a normal AWIPS fxa account. Furthermore, this installation"
echo "assumes that a /scratch partition exists on ${hhh} that is"
echo "writable by the $def_user account and has 20-50GB of free space."
echo "Finally, it is assumed that this site is at least at release OB9.1,"
echo "and that ${hhh} is using Red Hat Enterprise 5."
echo
echo -n "If this is OK type  install  and hit return: "
if ( "$<" != "install" ) then
    echo "Aborting on user request\!\!\!"
    exit
endif
#
# Do a sanity check on some of our environment variables
#
if ( ! ( $?FXA_INGEST_SITE ) ) then
    echo there is no value for FXA_INGEST_SITE
    exit
endif
if ( ! ( $?FXA_LOCAL_SITE ) ) then
    echo there is no value for FXA_LOCAL_SITE
    exit
endif
if ( "$FXA_LOCAL_SITE" == "" ) then
    echo there is no value for FXA_LOCAL_SITE
    exit
endif
if ( "$FXA_LOCAL_SITE" != "$FXA_INGEST_SITE" ) then
    echo FXA_LOCAL_SITE and FXA_LOCAL_SITE should be identical
    exit
endif
if ( ! ( -d $FXA_HOME  ) || ( $FXA_HOME != $def_home ) ) then
    echo FXA_HOME does not refer to viable directory $FXA_HOME
    exit
endif
#
#  Use default linux versions of some commands
#
unalias rm
unalias cp
unalias ls
unalias mv
rehash
#
# Establish hard path to this script so we can call it recursively
#
set here = `pwd`
set me = `which $0`
if ( ! ( -x $me ) ) then
    echo "Could not determine path of this script for recursive call"
    echo "ABORTING\!\!\!"
    exit
endif
if ( -x $here/$me ) then
    set me = $here/$me
endif
#
set mydir = `dirname $me`
set tarFil = `find $mydir -name installPFPalps.tar.gz`
if ( ! ( -e $tarFil ) ) then
    echo "Could not locate the installation tar file installPFPalps.tar.gz"
    echo "ABORTING\!\!\!"
    exit
endif
#
# get rid of existing old saves if desired
#
cd $FXA_HOME
set oldSaves = `find . -maxdepth 1 -type d -name '*Tmp2*' | cut -c3-200`
while ( $#oldSaves > 0 )
    echo "It appears that the following directories:"
    echo $oldSaves
    echo "in $FXA_HOME/"
    echo "are left over work directories from a previous PFP install."
    echo
    echo -n "If it is OK to leave them, type  go  and hit return: "
    if ( "$<" == "go" ) then
        break
    endif
    foreach oned ( $oldSaves )
       chmod -R 775 $oned
       rm -rf $oned
       if ( $status != 0 ) then
           echo "Could not remove the directory $FXA_HOME/$oned"
           echo "You may be experiencing permission problems"
           echo "ABORTING\!\!\!"
           exit
       endif
    end
    break
end
#
#  If the ingest is up, note that and stop it
#
set ingYes = no
ps -e | grep Grib >& /dev/null
if ( $status == 0 ) then
    $FXA_HOME/bin/stopIngest.alps
    set ingYes = yes
endif
if ( -x purgeProcess ) mv purgeProcess $FXA_HOME/purgeProcess.tmp
#
#  Determine if this is a completely new install.
#
set preserve = no
if ( -d $FXA_HOME/PFP/customFiles ) then
    echo "It appears that the PFP package has already been installed once."
    while (1)
        echo -n "Do you want to preserve your customizations? "
        set ans = `echo "$<" | tr 'a-z' 'A-Z'`
        if ( ( "$ans" == "Y" ) || ( "$ans" == "YES" ) ) then
            set preserve = yes
            break
        else if ( ( "$ans" == "N" ) || ( "$ans" == "NO" ) ) then
            break
        endif
    end
endif
rm -rf $FXA_HOME/tempSiteKeep
if ( "$preserve" == "yes" ) then
    mkdir -p $FXA_HOME/tempSiteKeep
    chmod 775 $FXA_HOME/tempSiteKeep
    cd $FXA_HOME/data/localization
    set keepers = `ls | grep '^[A-Z][A-Z]*'`
    mv $keepers $FXA_HOME/tempSiteKeep
    mv realizations $FXA_HOME/tempSiteKeep
endif
#
#  Get rid of existing installation, unntar the new installation.
#
cd $FXA_HOME
set tmpstamp = `date -u '+%Y%m%d_%H%M'`
set tmpstamp = "Tmp$tmpstamp"
chmod -R 775 bin >& /dev/null
mv bin bin${tmpstamp} >& /dev/null
if ( -e bin ) then
    echo "Could not move directory $FXA_HOME/bin/"
    echo "out of the way for the install."
    echo "You may be experiencing permission problems"
    echo "ABORTING\!\!\!"
    exit
endif
chmod -R 775 data >& /dev/null
mv data data${tmpstamp} >& /dev/null
if ( -e data ) then
    echo "Could not move directory $FXA_HOME/data/"
    echo "out of the way for the install."
    echo "You may be experiencing permission problems"
    echo "ABORTING\!\!\!"
    exit
endif
chmod -R 775 lib >& /dev/null
mv lib lib${tmpstamp} >& /dev/null
if ( -e lib ) then
    echo "Could not move directory $FXA_HOME/lib/"
    echo "out of the way for the install."
    echo "You may be experiencing permission problems"
    echo "ABORTING\!\!\!"
    exit
endif
rm -rf bin${tmpstamp} data${tmpstamp} lib${tmpstamp} >& /dev/null
chmod -R 775 customStage >& /dev/null
mv customStage customStage${tmpstamp} >& /dev/null
if ( -e customStage ) then
    echo "Could not move directory $FXA_HOME/customStage/"
    echo "out of the way for the install."
    echo "You may be experiencing permission problems"
    echo "ABORTING\!\!\!"
    exit
endif
rm -rf customStage${tmpstamp} >& /dev/null
tar xzvf $tarFil
if ( "$preserve" == "yes" ) then
    cd $FXA_HOME/tempSiteKeep
    set keepers = `ls`
    foreach one ( $keepers )
       mkdir -p $FXA_HOME/data/localization/$one
       mv $one/* $FXA_HOME/data/localization/$one
    end
endif
rm -rf $FXA_HOME/tempSiteKeep
#
#  Put something on dx1 that will create a small data structure that
#  allows notification to work even if the grid indexing is different
#  between the alps machine and dx1.
#
cd $FXA_HOME/data/localization/scripts/
if ( $def_home == /awips/fxa ) then
    cp dsVBkeySpace.csh /data/fxa
    chmod +x /data/fxa/dsVBkeySpace.csh
    ssh dx1 /data/fxa/dsVBkeySpace.csh
endif
#
# Make a link from our local nationalData to any files in the default
# nationalData not also in the local
#
if ( ! ( -d /data/fxa/nationalData ) ) then
    echo "Default national data directory /data/fxa/nationalData not found"
    echo "ABORTING\!\!\!"
    exit
endif
#
cd $FXA_HOME/data/localization/nationalData
find /data/fxa/nationalData -maxdepth 1 ! -type d | \
   sed 's|^/data/fxa/nationalData/||g' | $me aXbYc
#
#  Make a separate local custom files, populate it with a few things
#  from the install.
#
mkdir -p $FXA_HOME/PFP/customFiles
setenv FXA_CUSTOM_FILES $FXA_HOME/PFP/customFiles
if ( "$preserve" == "yes" ) then
    cd $FXA_HOME/customStage
    set allFiles = `ls`
    foreach one ( $allFiles )
       if ( -e $FXA_HOME/PFP/customFiles/$one ) continue
       mv $one $FXA_HOME/PFP/customFiles
    end
else
    rm -rf $FXA_HOME/PFP/customFiles/*
    mv $FXA_HOME/customStage/* $FXA_HOME/PFP/customFiles
endif
#
#  Run our localization, including purge task
#
cd $FXA_HOME/data/localization/scripts
if ( -e handleNDMfiles.csh ) rm -f handleNDMfiles.csh
./mainScript.csh n $FXA_LOCAL_SITE $FXA_INGEST_SITE
./mainScript.csh -purge
#
#  A few extra things not directly taken care by the localization.
#
cd $FXA_HOME/
cp bin/readenv.* .
cp bin/fxa.cshrc .cshrc
cp bin/fxa.environs .environs
set myhost = `hostname | cut '-d.' -f1`
sed "s/^FXA_INGEST_SITE.*/FXA_INGEST_SITE   $FXA_INGEST_SITE/g" \
    bin/fxa.environs.localhost  | \
    sed "s|^FXA_CUSTOM_FILES.*|FXA_CUSTOM_FILES   $FXA_CUSTOM_FILES|g" \
    > .environs.$myhost
chmod 775 readenv* .env* .cshrc*
rm /tmp/environs.csh
#
#  Update the cron to keep the local purger up.
#
cd $FXA_HOME/bin
crontab -l | tee cttmp.tmp | grep startPurgeProcess >& /dev/null
if ( $status != 0 ) then
    cat crontab.alps >> cttmp.tmp
    crontab cttmp.tmp
endif
rm cttmp.tmp
#
#  Install template files for local ingest directories.
#
mkdir -p /scratch/local/data/fxa
setenv FXA_DATA /scratch/local/data/fxa
cd $FXA_HOME/data/localizationDataSets/$FXA_LOCAL_SITE
set templDat = `grep /scratch/local/data/fxa storageSourceTable.txt | \
                grep -v '| *i *|' | cut '-d|' -f1,4 | tr '|' ' '`
while ( $#templDat > 0 )
   set templ = $templDat[1] 
   shift templDat
   set onedir = $templDat[1] 
   shift templDat
   mkdir -p $onedir
   cp ${templ}.cdlTemplate $onedir/template
end
mkdir -p /scratch/local/data/fxa/Grid/SBN/Raw
mkdir -p /scratch/local/data/fxa/Grid/SBN/Bad
mkdir -p /scratch/local/data/fxa/Grid/SBN/rawGrib2
mkdir -p /scratch/local/data/fxa/Grid/SBN/badGrib2
#
#  Make comms router and grid router be local.
#
cd $FXA_HOME/data
set locip = `grep $myhost /etc/hosts | awk '{print $1}'`
sed "s/dx3f/$locip/g" ipc.config > ipc.config.tmp
mv ipc.config.tmp ipc.config
#
#  Put in place one instance of 'canned' data unless there is already
#  something in this directory.
#
if ( -d /scratch/local/data/fxa/Grid/Local/CONUS212/SREF ) then
    cd /scratch/local/data/fxa/Grid/Local/CONUS212/SREF
    set n = `find . -name '2*' -maxdepth 1 | wc -l`
    if ( $n == 0 ) then
        gzip -d -c $mydir/20090224_1500.gz > 20090224_1500
    endif
endif
#
#  Let user know if files were left behind from the clean up process.
#
echo "ALPS PFP installation is done"
set leftovers = `find $FXA_HOME -maxdepth 1 -type d -name "*${tmpstamp}"`
if ( "$leftovers" != "" ) then
    echo "Because of permissions problems, you may have to become root"
    echo "to delete the contents of the following directories left over"
    echo "from the installation process: "
    ls -ld $leftovers
endif
#
#  Restart ingest, bump purger, if needed.
#
cd $FXA_HOME/bin
if ( "$ingYes" == "yes" ) ./startIngest.alps
ps -e | grep purge
if ( $status == 0 ) then
    ./stopPurgeProcess
    ./startPurgeProcess
endif
rm -rf $FXA_HOME/purgeProcess.tmp
#

