#!/bin/sh

########################################################################
#                 copyright 2001, Amoco Production Company             #
#                             All Rights Reserved                      #
#                     an affiliate of BP America Inc.                  #
########################################################################

# set -x
#

machine=`uname -n`

umask 2

if ( test $# = 0 || test "${FreeUSP}" = "" )
then
	echo " "
	echo "execopy:   FreeUSP bin/script update facility:"
	echo " "
	echo "Usage:"
	echo "        execopy [-noscript] [program names]"
	echo " "
	echo "Please set the env var FreeUSP to the proper location"
	exit 1
fi

if test $1 = "n32:=1"
then
N32="TRUE"
shift
fi

if test $1 = "-noscript"
then
NOSCRIPT="TRUE"
shift
fi

cdir=`pwd`

if test "${TARCH}" = "" 
then
  TARCH=`${DEPTROOT}/bin/tarch`
fi

# TARCH=`${DEPTROOT}/bin/tarch`

if test "${N32}" = "TRUE"
then
 TARCH=`echo ${TARCH}/n32 | sed 's/IRIX64/IRIX/'`
fi

PATH=${FreeUSP}/dist/bin:${DEPTROOT}/bin:${PATH}

. usp-osarch.sh

cwd=${DEPTROOT}/usp/bin/${TARCH}
cd ${cwd}

for i in $*
do
  explbin=${DEPTROOT}/usp/bin/${TARCH}/$i
#
# this is a new block to copy the binary to the appropriate leg of the
# new /tstapps structure
#
if test -d ${DEPTROOT}/bin
 then
	
	if test ! -x ${explbin}
	then
	  echo no binary found in ${explbin} 
	  exit 1
	fi
	if test "${NOSCRIPT}" != "TRUE"
	then
	 if test ! -f ${FreeUSP}/dist/bin/$i
	 then
	  echo Do you want to create a script for $i in ${FreeUSP}/dist/bin [ny]?
	  read ans
	  if test "$ans" = "y"
	  then
#	   echo Creating script in ${FreeUSP}/dist/bin for program $i
	   echo '#!/bin/sh' > ${FreeUSP}/dist/bin/$i
	   echo '#' >> ${FreeUSP}/dist/bin/$i
	   echo '# This script is a part of FreeUSP - the UNIX Seismic Processing system.' >> ${FreeUSP}/dist/bin/$i
	   echo '#' >> ${FreeUSP}/dist/bin/$i
	   echo '' >> ${FreeUSP}/dist/bin/$i
	   echo 'DIST_VERSION=`usparg -version "prod" $*`' >> ${FreeUSP}/dist/bin/$i
	   echo '' >> ${FreeUSP}/dist/bin/$i
	   echo '. usp-set-path' >> ${FreeUSP}/dist/bin/$i
	   echo '' >> ${FreeUSP}/dist/bin/$i
	   echo 'exec ${USP_MONITOR_CMD} ${APP} ${1:+"$@"}' >> ${FreeUSP}/dist/bin/$i
	   chmod 775 ${FreeUSP}/dist/bin/$i
#	   if test "${OSARCH_OS}" != "Linux"
#	   then
# 		echo ${TSTAPPS}/bin/$i >> ${filelist}
#		echo bin/$i >> ${filelist}
#	   fi
	  fi 
	 fi
	fi

	IRIX32_CHK=`echo ${TARCH} | sed 's/^\(......\).*/\1/'`
	if test "${IRIX32_CHK}" = "IRIX32" 
	then
	  OSARCH_OS="IRIX"
	fi

	if test "${N32}" = "TRUE" 
	then
	OSARCH_OS="IRIX"
	fi

	RelativeBinDir=${OSARCH_OS}/${OSARCH_MAJOR_OS_REV}.${OSARCH_MINOR_OS_REV}/usp/prod/${OSARCH_HARDWARE}
        TstappsBinDir=${FreeUSP}/dist/${RelativeBinDir}

	test ! -d ${TstappsBinDir} && mkdir -p ${TstappsBinDir}

	if test -x ${TstappsBinDir}/$i
	  then
	    test ! -d ${TstappsBinDir}/unlinked && \
		mkdir -p ${TstappsBinDir}/unlinked
	    mv ${TstappsBinDir}/$i ${TstappsBinDir}/unlinked/$i
	    rm ${TstappsBinDir}/unlinked/$i
	  fi

	echo ${machine}: Copying ${explbin} to ${TstappsBinDir}/$i
	cp  ${explbin} ${TstappsBinDir}/$i
fi

if test -x $i
 then
	echo ${machine}: Removing ${cwd}/$i
	rm $i
fi
done

cd $cdir
