#!/bin/sh
# Copyright (c) 1998 Universities Research Association, Inc.
#       All Rights Reserved
#
#
# This material resulted from work developed under a Government Contract and
# is subject to the following license:  The Government retains a paid-up,
# nonexclusive, irrevocable worldwide license to reproduce, prepare derivative
# works, perform publicly and display publicly by or for the Government,
# including the right to distribute to other Government contractors.  Neither
# the United States nor the United States Department of Energy, nor any of
# their employees, makes any warranty, express or implied, or assumes any
# legal liability or responsibility for the accuracy, completeness, or
# usefulness of any information, apparatus, product, or process disclosed, or
# represents that its use would not infringe privately owned rights.
#
# Current script for ups
#
# This file is created by the ups product configure script.
#
# The primary function of this script is to copy the files from 
# the bootstrap sub-directory of the ups product to the 
# ~products/etc 
# directory, if needed.
#

Name_Of_Ups=ups

Setups_Home=~products/etc

Startup_Home=/fnal/ups/db/.upsfiles

BootStrap_Dir=/fnal/ups/db/.upsfiles/configure/v4_3_Linux+2_

# Setups_Products_Init_File=upsdb_list

# Setups_Products_Script=setups_products_init.sh

if test -x /bin/echo
then
    ECHO=/bin/echo
elif test  -x /usr/bin/echo
then
    ECHO=/usr/bin/echo
else
    ECHO=echo
fi

# =====  DO NOT EDIT BELOW THIS LINE  ===   DO NOT REMOVE THIS LINE  ====

Ups_Dir=/fnal/ups/prd/ups/v4_3/Linux+2

if test x${Test_Prod_Dir} != x
then
    Ups_Dir=$Test_Prod_Dir
fi

error='0'
if test ! -f $BootStrap_Dir/setups.common
then
    echo '****' Configure script has failed or was not executed.
    echo '****' $Name_Of_Ups current script cannot execute.
    exit 1
fi

if test `echo $Setups_Home|cut -c1` != '/'
then
    TeMp=/tmp
    if test x${TMPDIR:-} != x
    then
        TeMp=$TMPDIR
    fi
    for sh in bash ksh ash sh
    do
	if test -x /usr/bin/${sh}
	then
	    Name_Of_Sh=/usr/bin/${sh}
	    break
	elif test -x /bin/${sh}
	then
	    Name_Of_Sh=/bin/${sh}
	    break
	fi
    done
    cat <<XxEoF >$TeMp/Ash_$$
#!${Name_Of_Sh}
eval dir=\$1
$ECHO \$dir
XxEoF
    chmod +x $TeMp/Ash_$$
    Setups_Home=`(unset ENV; $TeMp/Ash_$$ $Setups_Home )`
    rm -f $TeMp/Ash_$$ 
fi

if test ! -d $Setups_Home
then
    mkdir $setups_Home
fi

if test ! -d $Setups_Home
then
    echo '****' Unable to create $Setups_Home
    exit 1
fi
#
# If the ~products/etc
# is in AFS space, 
# force it to the read/write area.
#
# The following algorithm is known to be overly simplistic
# A better one is in the works
# However, this should work most of the time at Fermilab
#
cp /fnal/ups/prd/ups/v4_3/Linux+2/ups/configure $Setups_Home/.Write_Test
if test ! -f $Setups_Home/.Write_Test
then
    Setups_Home=`echo $Setups_Home|sed 's%/afs/fnal/products%/afs/.fnal.gov/products%'`
    Setups_Home=`echo $Setups_Home|sed 's%/afs/fnal.gov/products%/afs/.fnal.gov/products%'`
fi
cp /fnal/ups/prd/ups/v4_3/Linux+2/ups/configure $Setups_Home/.Write_Test
if test ! -f $Setups_Home/.Write_Test
then
    echo '****' Unable to write to $Setups_Home
    echo '****' $Name_Of_Ups current script failure
    exit 1
else
    rm -f $Setups_Dir/.Write_Test
    chmod +rx $Setups_Home
fi
#
# Mark ALL BootStrap files current.
#
rm -f /tmp/ups_cur_$$
for filex in $BootStrap_Dir/set* $BootStrap_Dir/ups*
do
#    ed -  $filex <$BootStrap_Dir/zzzzzzzzzz >/dev/null
    sed -e 's/Setups_Current=0/Setups_Current=1/' $filex >/tmp/ups_cur_$$
    cp /tmp/ups_cur_$$ $filex
    rm /tmp/ups_cur_$$
done
#
# Move all the files from the bootstrap directory to 
# ~products/etc
# Only move the files which have changed
#
    for filex in $BootStrap_Dir/set*
    do
        Diff_rc='0'
	file=`echo $filex|sed "s%$BootStrap_Dir/%%"`
        if test ! -f $Setups_Home/$file
        then
            Diff_rc='1'
        else
            diff -b $BootStrap_Dir/$file $Setups_Home/$file >/dev/null
            Diff_rc=$?
        fi
        if test $Diff_rc != '0'
        then
	    echo '**' Copying $file to $Setups_Home
	    if test -f $Setups_Home/$file
	    then
		cp  $Setups_Home/$file $Setups_Home/${file}.BAK
	    fi
            cp $BootStrap_Dir/$file $Setups_Home
	    if test $? != '0'
	    then
		error='1'
		echo '****' An error has prevented the copy of
		echo '****' $BootStrap_Dir/$file to 
		echo '****' $Setups_Home
	    fi
	    chmod +x $Setups_Home/$file
	else
	    echo '**' $file identical to $Setups_Home/$file - not copied.
        fi
    done

if test $error = '1'
then
    echo '**** Error(s) have caused the current script to fail'
    exit 1
fi
#
# Copy the startup and shutdown scripts to their final
# resting place. This is sensitive to the structure
# of the directory path.  Sub-directories may need to
# be to be created.
#
for Start_Or_Stop in startup shutdown
do
    if test -f $BootStrap_Dir/ups_${Start_Or_Stop}
    then
	if test `echo $Startup_Home|grep -c '.upsfiles'` != 0
	then
 	    if test ! -d $Startup_Home/$Start_Or_Stop
	    then
	        mkdir $Startup_Home/$Start_Or_Stop
	    fi
	    echo '**' Copying ups_${Start_Or_Stop} to $Startup_Home/$Start_Or_Stop
    	    cp $BootStrap_Dir/ups_${Start_Or_Stop} $Startup_Home/${Start_Or_Stop}/ups_${Start_Or_Stop}
            cp  $BootStrap_Dir/ups_${Start_Or_Stop} $Startup_Home/${Start_Or_Stop}/ups_${Start_Or_Stop}.sh       
            cp  $BootStrap_Dir/ups_${Start_Or_Stop} $Startup_Home/${Start_Or_Stop}/ups_${Start_Or_Stop}.csh
	    chmod +x $Startup_Home/${Start_Or_Stop}/ups_${Start_Or_Stop}
	    chmod +x $Startup_Home/${Start_Or_Stop}/ups_${Start_Or_Stop}.csh
	    chmod +x $Startup_Home/${Start_Or_Stop}/ups_${Start_Or_Stop}.sh
	else
	    cp  $BootStrap_Dir/ups_${Start_Or_Stop} $Startup_Home/ups_${Start_Or_Stop}
	    cp  $BootStrap_Dir/ups_${Start_Or_Stop} $Startup_Home/ups_${Start_Or_Stop}.sh 
	    cp  $BootStrap_Dir/ups_${Start_Or_Stop} $Startup_Home/ups_${Start_Or_Stop}.csh 
	    chmod +x $Startup_Home/ups_${Start_Or_Stop}
	    chmod +x $Startup_Home/ups_${Start_Or_Stop}.csh
	    chmod +x $Startup_Home/ups_${Start_Or_Stop}.sh
	fi     
   fi
done

echo '**' Current script completed successfully
    
