#! /bin/ksh
#
# Name: unsneakout
#
# The unsneakout source routine based on KCS used by NFS only.
#
# Syntax:  unsneakout file(s)
#    where file is: one or more files to be checked in.


#
# If no RMTBRANCH is in the current directory, generate an error and stop.
#

if [ ! -f RMTBRANCH ] ; then
    print "Error: There must be an RMTBRANCH file in this directory."
    print "       Where RMTBRANCH contains -"
    print "           branch-name/kcs-dir-path/RCS/ 300|800|conv"
    exit 1
fi

#
# Make sure at least one file was specified.
#

if [ $# = 0 ] ; then
    print "Error: No file(s) where specified."
    exit 1
fi

#
# Capture file or files to be checked in.
#

FILELIST=$@
if [ "${FILELIST}" = "" ] ; then
   print "Error: No file(s) where specified."
   exit 1
fi


USER=`whoami`
nfs=${Nfs}/develop/${USER}

#
# See if a system build is in progress
#

if [ ! -w $Nfs/log/update ] ; then
    cat <<-EOF

	NOTE: Updates are disabled.  Your files have not been modified.
	Please try again later.
	EOF
    exit 1
fi

#
# Set defaults for the base directory path for the pseudoroot location
#

umask 022

PATH="$Nfs/bin:/bin:/usr/bin"
export PATH nfs

#
# Get base path for the file(s) from the RMTBRANCH file.  Base path is the
# KCS directory path to a file.
#

RMTFILE=$(cat < RMTBRANCH)

# Remove the 'RCS/ arch' from the RMTBRANCH file
BASEDIR=${RMTFILE%%RCS/*}

# Remove the branch name from the RMTBRANCH file. This now leave the base path.
BASEDIR=${BASEDIR#*/}

# print "final result of base path is '${BASEDIR}'."

#
# Loop for each file.
#

for FILENAME in ${FILELIST}
do
    CHKNAME=${FILENAME##*/}
    if [ "${FILENAME}" != "${CHKNAME}" ] ; then
        print "The filename is: ${FILENAME}"
        print "Error: You must be in the directory where the files are located."
        exit 1
    fi

    # make sure we are in the checkout directory and not in the user base
    # directory for the check in process.

    cd $nfs/checkout/${BASEDIR}

    if [ ! -f RMTBRANCH ] ; then
    cat > RMTBRANCH <<-EOF
        ${RMTFILE}
	EOF
    fi
    
    # print "Current Dir: $(pwd)"
    # print "rm ${FILENAME}"
    rm ${FILENAME}
    
    # print "Go to $nfs/${BASEDIR} for new ln"
    cd $nfs/${BASEDIR}

    # print "Current dir is now: $(pwd)"

    # Link the working file to the development working directory

    # print "ln -f $Nfs/${BASEDIR}/${FILENAME} ${FILENAME}"
    ln -f $Nfs/${BASEDIR}/${FILENAME} ${FILENAME}

    # Return to working directory
    cd ${CURDIR}
done
