#!/bin/sh

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

#
# set -x
#
machine=`uname -n`

if ( test $# -lt 3 || test "${FreeUSP}" = "" )
then
	echo " "
	echo "apps-execopy:   generic executable update facility:"
	echo " "
	echo "Usage:"
	echo "        apps-execopy product version [program names]"
	echo "        ie. apps-execopy usp prod editt davc"
	echo "            apps-execopy xaprtools test xcat xbtnpress"
	echo " "
	echo "This will move the program binaries from the code structure"
	echo "to the appropriate architecture binary sub-directory of the"
	echo "FreeUSP structure."
	echo "Please set the env var FreeUSP to the proper location"
	exit 1
fi

cdir=`pwd`

PROD=$1
shift
VERSION=$1
shift

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

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

for i in $*
do
if ( test -d ${DEPTROOT}/bin )
 then
	
	PATH=${FreeUSP}/dist/bin:${DEPTROOT}/bin:${PATH}

	. usp-osarch.sh

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

 	APPS_BIN=${FreeUSP}/dist/${OSARCH_OS}/${OSARCH_MAJOR_OS_REV}.${OSARCH_MINOR_OS_REV}/${PROD}/${VERSION}/${OSARCH_HARDWARE}
	test ! -d ${APPS_BIN} && mkdir -p ${APPS_BIN}
	test -x ${APPS_BIN}/${i} && (mv ${APPS_BIN}/${i} ${APPS_BIN}/${i}.old ; rm ${APPS_BIN}/${i}.old )
	echo ${machine}: Copying ${cwd}/${i} to ${APPS_BIN}/${i}
	cp  ${i} ${APPS_BIN}/${i}

	if ( test -x $i)
	 then
	   echo ${machine}: Removing ${cwd}/$i
	   rm $i
	fi
	if  test ! -f ${FreeUSP}/dist/bin/$i  
	then
	  echo Do you want to create a script for $i [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
	  fi
	fi
else
	echo ${machine}: The ${FreeUSP} structure is not mounted. No action performed.
fi
done

cd $cdir
