:
# NAME:
#	install.sh - portable version of install(1)
#
# SYNOPSIS:
#	install [-cs] [-o owner] [-g group] [-m mode] file1 file2 ...
#	install -d [-o owner] [-g group] [-m mode] directory ...
#
# DESCRIPTION:
#	Compatible with BSD install(1).  Except that unless the '-c'
#	option is given we move an already installed target aside as
#	this is important on many systems.
#	
#
# OPTIONS:
#	-c	copy on top of target rather than move it aside first.
#		This is useful for targets that have multiple links
#
#	-s	strip target
#
#	-o "owner"
#		make target owned by "owner"
#
#	-g "group"
#		make target group owned by "group"
#
#	-m "mode"
#		set permissions to "mode"
#
#	-i "flags"
#		Ignore errors from steps indicated by "flags" (``s,o,g,m'').
#
# BUGS:
#	The '-i' option is to save your sanity when 'bsd.prog.mk'
#	insists on haveing a '-o' "owner" option which is doomed to
#	fail on many systems.
#	
# AUTHOR:
#	Simon J. Gerraty <sjg@quick.com.au>
#

# RCSid:
#	$Id: install-sh,v 1.12 1997/02/08 07:13:22 sjg Exp $
#
#	@(#) Copyright (c) 1993 Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that 
#	the above copyright notice and this notice are
#	left intact. 
#      
#	Please send copies of changes and bug-fixes to:
#	sjg@quick.com.au
#

set -- `getopt csdo:g:m:i: $*`

copy=no				# move dest away
owner=
group=
mode=
strip=
mk_dir=
ignore_err=

for i in $*
do
  case $i in
  --)	shift; break;;
  -c)	copy=yes; shift;;	# overwrite dest
  -o)	owner="chown $2 ";
    	shift 2;;
  -g)	group="chgrp $2 ";
    	shift 2;;
  -m)	mode="chmod $2 ";
    	shift 2;;
  -s)	strip=strip; shift;;
  -d)	mk_dir=yes; shift;;
  -i)	ignore_err="$ignore_err$2"; shift 2;;
  esac
done


mkdirs()
{
  case $1 in
  /*)	pp=/;;
  *)	pp=;;
  esac
  for p in `echo $1 | tr / " "`
  do
    case "$pp" in
    "")	pp=$p;;
    /)	pp=/$p;;
    *)	pp=$pp/$p;;
    esac
    [ -d $pp ] || mkdir $pp || exit 1
  done
}

err() {
        case "$ignore_err" in
	*$1*) ;;
	*) exit 1;;
	esac
}

setem() {
  [ "$strip" ] && { $strip $1 || err s; }
  [ "$group" ] && { $group $1 || err g; }
  [ "$owner" ] && { $owner $1 || err o; }
  [ "$mode" ] && { $mode $1 || err m; }
  return 0
}

# a bug in HP-UX's /bin/sh, means we need to re-set $*
# after any calls to add_path()
args="$*"

# all this just for chown!
add_path () { [ -d $1 ] && eval ${2:-PATH}="\$${2:-PATH}:$1"; }
add_path /etc
add_path /usr/etc
add_path /sbin
add_path /usr/sbin

# restore saved $*
set -- $args

# make directories if needed
# and ensure mode etc are as desired
if [ "$mk_dir" = yes ]; then
  for d in $*
  do
    [ ! -d $d ] && mkdirs $d
    setem $d
  done
  exit 0			# that's all we do
fi

# install files
if [ $# -gt 2 ]; then
  dest_dir=yes
elif [ $# -eq 1 ]; then
  echo "what should I do with $*?" >&2
  exit 1
fi

# get list of files
while [ $# -gt 1 ]
do
  files="$files $1"
  shift
done
# last one is dest
dest=$1
shift


if [ "$dest_dir" = yes -a  ! -d $dest ]; then
  echo "no directory $dest" >&2
  exit 1
fi

for f in $files
do
  b=`basename $f`
  if [ -d $dest ]; then
    t=$dest/$b
  else
    t=$dest
  fi
  [ "$copy" != yes -a -f $t ] && { mv -f $t $t.old || exit 1; }
  { cp $f $t && setem $t; } || exit 1
done
exit 0
