#!/bin/sh
predir=PRE-030301
case $1 in
read*|data|*exec)
    type=$1 ; shift ;;
precious-*)
    precious=true
    type=`/usr/bin/expr $1 : 'precious-\(.*\)' \| $1` ; shift ;;
execsuid)
    type=execsuid ; shift;;
*)
    type=exec ;;
esac
dir=$1
shift
# If file is erpcd we do not want to overwrite because
# there may be an active erpcd daemon running
for file
do
    case $file in
	erpcd)
	    if [ ! -f $dir/$file ]; then
#		# Destination does not exist, just copy 
		/bin/cp $file $dir
	    else
#		# Destination exists, first backup with a COPY
#		# (unless backup already exists)
		if [ ! -f $dir/$predir/$file ]; then
		    [ -d $dir/$predir ] || /bin/mkdir $dir/$predir
		    /bin/cp $dir/$file $dir/$predir/$file
		fi

#		# Install the new version of the file
#		# (but do not overwrite erpcd, it may be running)
		/bin/cp $file $dir/$file.new

# Print warning message
echo " "
echo " "
echo "WARNING: If you have called \"make install\" yourself, then in directory"
echo "       :     $dir"
echo "       : you will have to copy file \"$file.new\" to \"$file\"."
echo "       : Make sure the $file daemon is not running when that is done."
echo "       : If the installation script called \"make install\" then the"
echo "       : copy will be done for you."
echo " "
echo " "

#		# Change the name of file so the chmod commands below
#		# work on the correct file (do this after warning message)
		file=$file.new
	    fi
	    ;;
	*)
#	    # If destination exists then backup with a MOVE
#	    # (unless the backup already exists)
	    if [ -f $dir/$file ]; then
		if ${precious-false}; then
		    echo $dir/$file exists, skipping install...
		    continue
		fi
		if [ ! -f $dir/$predir/$file ]; then
		    [ -d $dir/$predir ] || /bin/mkdir $dir/$predir
		    /bin/mv -f $dir/$file $dir/$predir/$file
		fi
	    fi
#	    # Install the new file
	    /bin/cp $file $dir
	    ;;
    esac
    case $type in
    read*)
        /bin/chmod 444 $dir/$file
        ;;
    data)
        /bin/chmod 644 $dir/$file
        ;;
    sysexec)
        /bin/chmod 544 $dir/$file
        ;;
    exec)
        /bin/chmod 755 $dir/$file
        /usr/bin/strip $dir/$file
        ;;
    execsuid)
        /usr/bin/strip $dir/$file
        /bin/chmod 4111 $dir/$file
        ;;

    esac
    /bin/chown root $dir/$file
done
