#!/bin/sh
export PATH || exec /bin/sh $0 argv:q

#
# DESCRIPTION
# 
#
# bsdinstall - install a program, script, or datafile
#
# $XConsortium: install.sh,v 1.2 89/12/18 14:47:22 jim Exp $
#
# This script is compatible with the BSD install script, but was written
# from scratch.
#
# Modified by Peter Pilgrim Tue Sep 26 15:19:50 BST 1995
# 
# RCS HEADER ``bsdinstall.sh''
# 
# $Author: peterp $
# $Date: 1996/05/31 15:41:55 $
# $Source: /usr/users/peterp/Projects/utils/RCS/bsdinstall.sh,v $
# $Revision: 1.7 $    $State: Exp $    $Locker:  $
#

#
# HISTORY
# 

# ********************************************************************************
PrintUsage ()
# ********************************************************************************
{
     cat << EOF
usage: $myname
		[ --mode  (-m) <PERMISSIONS> ]
		[ --owner (-o) <USERID> ] 
		[ --group (-g) <GROUPID> ]
		[ --strip (-s) ] 
		[ --move (-b) ] [ --copy  (-c) ]
		[ --verbose (-v) ] [ --quiet (-q) ] 
		[ --help (-h) ] [ --usage (-u) ]	
		SRCFILE  DESTFILE

OPTIONS
    '--mode'	specifies destination files/directory permission.
    '-m'
    
    '--owner'	specifies ownership of the destination file/dir.
    '-o'

    '--group'	specifies the group ownership of the destination file/dir.
    '-g'

    '--strip'	specifies that the destination [executable] file should be
    '-s'	stripped of symbolic linking/debugging information.

    '--move'	specifies that the source file should be *moved* to the
    '-b'	destination (-b is shortform for 'Bewegen' [The Co-author 
		*PP* can speak fluent GERMAN]).

    '--copy'	specifies that the source file should be *copied* to the
    '-c'	destination.

    '--dryrun'  run as a test.
    '-dr'

    '--verbose'	produce verbose output
    '-v'

    '--quiet'	produce no verbose output. DEFAULT operation
    '-q'

    '--help'	produces this brief text.
    '--usage'

DESCRIPTION
    bsdinstall - a bourne shell script to install a program, script, or datafile.

    e.g. % bsdinstall --v --copy --mode 444  XTerm-super.ad  ~/app-defaults/XTerm-super

ENVIRONMENTALS
    'MVPROG'	specifies the move program. (Default is 'mv')
    'CPPROG'	specifies the copy program. (Default is 'cp -f')
    'CHMODPROG'	specifies the copy program. (Default is 'chmod')
    'CHOWNPROG'	specifies the copy program. (Default is 'chown')
    'CHGRPPROG'	specifies the copy program. (Default is 'chgrp')
    'STRIPPROG'	specifies the copy program. (Default is 'strip')

EOF

    echo '$Author: peterp $ $Date: 1996/05/31 15:41:55 $ $RCSfile: bsdinstall.sh,v $ $Revision: 1.7 $'
}

# ********************************************************************************
# MAIN  MAIN  MAIN  MAIN  MAIN  MAIN  MAIN  MAIN  MAIN  MAIN  MAIN  MAIN  MAIN  
# ********************************************************************************

myname=`basename $0`

#
# set DOITPROG to echo to test this script
#
PrefixCmd="${DOITPROG:-}"

#
# put in absolute paths if you don't have them in your path; or use env. vars.
#
MvProg="${MVPROG:-mv}"
CpProg="${CPPROG:- cp}"
ChmodProg="${CHMODPROG:-chmod}"
ChownProg="${CHOWNPROG:-chown}"
ChgrpProg="${CHGRPPROG:-chgrp}"
StripProg="${STRIPPROG:-strip}"
RmProg="${RMPROG:-rm}"
InstCmd="$CpProg"
ChmodCmd=""
ChownCmd=""
ChgrpCmd=""
StripCmd=""
RmCmd="$RmProg -f"
SrcFile=""
DstFile=""
VerboseOpt="+x"
VerboseFlag=0

# Get the defaults
: ${AWK:=awk}
: ${TMPDIR:=/tmp}

DryRunFlag=0

#
# Interpret command line options
#
while [ x"$1" != x ]; do
    case $1 in
	-move | --move | --mov | --b | -b )
	    InstCmd="$MvProg"
	    shift
	    continue;;

	-copy | --copy | --cop | --co | --c | -c) 
	    InstCmd="$CpProg"
	    shift
	    continue;;

	-mode | --mode | --mod | --m | -m ) 
	    ChmodCmd="$ChmodProg $2"
	    shift
	    shift
	    continue;;

	-owner | --owner | --owne | --own | --ow | --o | -o) 
	    ChownCmd="$ChownProg $2"
	    shift
	    shift
	    continue;;

	-group | --group | --grou | --gro | --gr | --g | -g)
	    ChgrpCmd="$ChgrpProg $2"
	    shift
	    shift
	    continue;;

	-strip | --strip | --stri | --str | --st | --s | -s) 
	    StripCmd="$StripProg"
	    shift
	    continue;;



	--dryrun | --dryru | --dryr | --dry | --dr | -dr | \
	-dryrun | -dryru | -dryr | -dry | -dr | -dr )
	    PrefixCmd="echo =>"
	    DryRunFlag=1
	    shift
	    continue;;

	-quiet | --quiet | --quie | --qui | --qu | --q | -q )
	    VerboseOpt="+x"; 
	    shift
	    continue;;

	-verbose | -verb | \
	--verbose | --verbos | --verbo | --verb | --ver | --ve | --v | -v )
	    VerboseOpt="-x"; 
	    shift
	    continue;;

	-help | --help | --hel | --he | --h | \
	-usage | --usage | --usag | --usa | --us | --u | -u )
	    PrintUsage
	    exit ;;

	*)  if [ x"$SrcFile" = x ]
	    then
		SrcFile=$1
	    else
		DstFile=$1
	    fi
	    shift
	    continue;;
    esac
done

#
# Check for both source & destination file paths
#
if [ x"$SrcFile" = x ]
then
    echo "$myname: no input file specified" 1>&2
    exit 1
fi

if [ x"$DstFile" = x ]
then
    echo "$myname: no destination specified" 1>&2
    exit 1
fi

#
# Check that the source file exists ! *PP*
#
if [ ! -f $SrcFile ]; then
    echo "$myname: input source file '$SrcFile' not found." 1>&2
    exit 1
fi


#
# if destination is a directory, append the input filename; if your system
# does not like double slashes in filenames, you may need to add some logic
#
if [ -d $DstFile ]
then
	DstFile="$DstFile"/`basename $SrcFile`
fi

#
# get rid of the old one and install the new one in place by mode
#
if [ -r $DstFile ]; then
    DstINode=`ls -i ${DstFile} | awk '{ print $1 }'`
    status=$?
    if [ $status -ne 0 ]; then
	echo "$myname: failed to get destination file I-node number (status:$status)"
	exit 1
    fi
    SrcINode=`ls -i ${SrcFile} | awk '{ print $1 }'`
    status=$?
    if [ $status -ne 0 ]; then
	echo "$myname: failed to get source file I-node number (status:$status)"
	exit 1
    fi
    if [ "$SrcINode" = "$DstINode" ]; then
	echo "$myname: *ERROR* files '${SrcFile}' & '${DstFile}'"
	echo "Sorry source & destinations files are identical, because they have"
	echo "the same I-node number:${DstINode} (Therefore not copied!)"
	exit 1
    fi
    (set ${VerboseOpt}; ${PrefixCmd} $RmCmd $DstFile )
fi
(set ${VerboseOpt}; ${PrefixCmd} ${InstCmd} $SrcFile $DstFile )
status=$?
if [ $status -ne 0 ]; then
    echo "$myname: copying failure (status:$status)" 1>&2
    exit 1
fi

if [ $DryRunFlag -ne 0 ]; then
    echo "$myname: (Dryrun) exit "
    exit 0;
fi

#
# and set any options; do chmod last to preserve setuid bits
#
if [ x"$ChownCmd" != x ]; then 
   (set ${VerboseOpt}; ${PrefixCmd} ${ChownCmd} $DstFile ); status=$? 
   if [ $status -ne 0 ]; then
      echo "$myname: could not change ownership (status:$status)" 1>&2
   fi
fi
if [ x"$ChgrpCmd" != x ]; then 
   (set ${VerboseOpt}; ${PrefixCmd} ${ChgrpCmd} $DstFile ); status=$? 
   if [ $status -ne 0 ]; then
      echo "$myname: could not change group ownership (status:$status)" 1>&2
   fi
fi
if [ x"$StripCmd" != x ]; then 
   (set ${VerboseOpt}; ${PrefixCmd} ${StripCmd} $DstFile ); status=$? 
   if [ $status -ne 0 ]; then
      echo "$myname: could not strip the command (status:$status)" 1>&2
   fi
fi
if [ x"$ChmodCmd" != x ]; then 
   (set $VerboseOpt; ${PrefixCmd} ${ChmodCmd} $DstFile ); status=$? 
   if [ $status -ne 0 ]; then
      echo "$myname: could not change file permission (status:$status)" 1>&2
   fi
fi

exit $status
#fini
