#! /bin/sh 
# @(#) smart-install.sh ShScript env 1.5 94/09/13 14:03:30 (/u/icdc/rdt/tools/mgmake/SCCS/s.smart-install.sh)
#
# Nom du Fichier : 	smart-install.sh
# Titre : 		|Titre|
# Auteur:		coetmeur		
# Date de creation :	01 Sep 94
#
# Description :
#	Document de reference : |doc|
#	Objet : |objet|
#
#
# 
# historique :
# |date|	coetmeur	|objet|
#
mycmdname=smart-install

usage() {
echo "usage: $mycmdname [<options>] [<file1> <file2>| <files...> <directory>/" 
echo "	<file1>		: source file"
echo "	<file2>		: destination file"
echo "	<files...>	: possibly empty list of source files"
echo "	<directory>/	: destination directory with '/' appended"
echo "trace options:  "
echo "	-trace		: activate trace"
echo "	-traceonly	: activate trace only, disactivate actions"
echo "	-echo		: echo actions"
echo "	-quiet		: reduce comment messages"
echo "	-usage		: show this usage"
echo "user options:  "
echo "	-compare	: don't copy if files are equals"
echo "	-used		: return error code 1 if nothing is done"
echo "	-preserve	: preserve date and flags in copy"
echo "	-apply <command>: apply a command on each file"
echo "	-owner <value>	: change owner"
echo "	-group <value>	: change group"
echo "	-mode <value>	: change protection mode"
echo "	-rename <sed-subst-patterns> : change name of destination "
echo "		in destination directory. "
echo "	<sed-s-pattern>	: pattern and replace parts "
echo "		of sed(1) 's' command. can add subdirectory."
echo "examples: "
echo " $mycmdname *.h -compare ../include/"
echo " $mycmdname  ../include/"
echo " $mycmdname -preserve -group kmem -mode 2555  a.out  ../bin/"
echo " $mycmdname -mode 444 -apply 'ranlib -t' *.a ../lib/"
echo " $mycmdname -rename '#^\\(.*\\)\\.\\([1-9l-p]\\)\\([a-z]*\\)\\.man\$#man\\2/\\1.\\2\\3#' \\"
echo "	*.man ../doc/man/"
}

badparameters() {
echo "$mycmdname: $*" >&2
usage
exit 2
}

traceandshow() {
echo "	$@"
"$@"
}

doit=""
traceit=":"
commentit="echo"
dest=""
sources=""
compareopt=""
modeopt=""
groupopt=""
owneropt=""
destdir=""
destisdir=""
builddirlist=""
preserveopt=""
applyopt=""
donestatus=0


while [ "$1" ] ; do
 case "$1" in
  -mode)	
	if [ "$modeopt" -a "$modeopt" != "$2" ] ; 
	then  badparameters "Ambiguous $1 options"; fi
	modeopt="$2"
	shift
	;;
  -owner)	
	if [ "$owneropt" -a "$owneropt" != "$2" ] ; 
	then  badparameters "Ambiguous $1 options"; fi
	owneropt="$2"
	shift
	;;
  -group)	
	if [ "$groupopt" -a "$groupopt" != "$2" ] ; 
	then  badparameters "Ambiguous $1 options"; fi
	groupopt="$2"
	shift
	;;
  -apply)	
	if [ "$applyopt" -a "$applyopt" != "$2" ] ; 
	then  badparameters "Ambiguous $1 options"; fi
	applyopt="$2"
	shift
	;;
  -rename)
	if [ "$renameopt" -a "$renameopt" != "$2" ] ; 
	then  badparameters "Ambiguous $1 options"; fi
	renameopt="$2"
	shift
	;;
  -traceonly)	
	doit="echo"
	traceit="echo"
	;;
  -trace)	
	doit="traceandshow"
	traceit="echo"
	;;
  -echo)	
	doit="traceandshow"
	traceit=":"
	;;
  -quiet)	
	commentit=":"
	doit=""
	traceit=":"
	;;
  -preserve)	
	preserveopt="true"
	;;
  -used)
	donestatus=1
	;;
  -compare)	
	compareopt="true"
	;;
  -usage)
	usage
	exit 0
	;;
  -*) 
	badparameters Unknown flag "$1"
	;;
  */) 	sources="$sources $dest"
	destdir=`dirname "$1."`
	dest=""
	destisdir="1$destisdir"
	;;
  *) 	
	sources="$sources $dest"
	dest="$1"
	;;
 esac
 shift
done
set - $sources
if [   0$destisdir -gt 1 \
	-o "$destisdir" -a "$dest"  \
	-o ! "$destisdir" -a "$renameopt" \
	-o ! "$destisdir" -a  $# -ne 1  ] ; then
	 badparameters Bad parameters 
fi
if [ "$dest" ] ; then destdir=`dirname $dest` ; fi
if [ "$preserveopt" ] ; then cpcmd="cp -p" ; else cpcmd="cp" ; fi


$traceit "	0=<$0>"
$traceit "	mycmdname=<$mycmdname>"
$traceit "	modeopt=<$modeopt>"
$traceit "	owneropt=<$owneropt>"
$traceit "	groupopt=<$groupopt>"
$traceit "	compareopt=<$compareopt>"
$traceit "	preserveopt=<$preserveopt>"
$traceit "	applyopt=<$applyopt>"
$traceit "	cpcmd=<$cpcmd>"
$traceit "	sources=<$sources>"
$traceit "	dest=<$dest>"
$traceit "	destisdir=<$destisdir>"
$traceit "	destdir=<$destdir>"


while [ "$1" ] ; do
 sourcefile="$1"
 shift 
 sourcename=`basename "$sourcefile"`
 $traceit "	sourcefile=<$sourcefile>"
 $traceit "	sourcename=<$sourcename>"
 if [ ! -r $sourcefile ] ; then
   echo "cannot read '$sourcefile'"  >&2
   exit 3
 fi
 if [ "$destisdir" ] ; then
  if [ "$renameopt" ] ; then
    if destsubname=`echo $sourcename | sed -e "s$renameopt"` &&
       [ "$destsubname" ] ; 
    then 
     dest="$destdir/$destsubname"
     destsubdir=`dirname $dest`
     destname=`basename $dest`
     $traceit "	destsubname=<$destsubname>"
    else 
     badparameters "Cannot rename '$sourcename' with: sed -e 's$renameopt'"
    fi
  else
    destname="$sourcename"
    destsubdir="$destdir"
    dest="$destdir/$destname"
  fi
 else
  destname=`basename $dest`
  destsubdir=`dirname $dest`
 fi
 $traceit "	dest=<$dest>"
 $traceit "	destname=<$destname>"
 $traceit "	destsubdir=<$destsubdir>"
 builddirlist=""
 subdir="$destsubdir"
 while [ ! -d "$subdir" ] ; do
   builddirlist="$subdir $builddirlist"
   subdir=`dirname "$subdir"`
 done
 $traceit "	builddirlist=<$builddirlist>"
 [ ! "$builddirlist" ] || $doit mkdir $builddirlist || exit 3 ;
 
 if [ "$compareopt" -a -r "$dest" ] && cmp -s $sourcefile $dest  ; then
 #  $commentit "${mycmdname}: $sourcefile == $dest"
   continue;
 fi
 donestatus=0
 desttmp="$destsubdir/#$$#$destname"
 $traceit "	desttmp=<$desttmp>"
 $commentit "${mycmdname}: $sourcefile -> $dest"
 if $doit $cpcmd "$sourcefile" "$desttmp" &&
    trap "$doit rm -f $desttmp" 0 && 
    if [ "$applyopt" -a ! -w "$desttmp" ] ; 
       then $doit chmod u+w "$desttmp" 2>/dev/null || true ; fi && 
    if [ "$applyopt" ] ; then $doit $applyopt "$desttmp" ; fi && 
    if [ "$owneropt" ] ; then $doit chown $owneropt "$desttmp" ; fi &&
    if [ "$groupopt" ] ; then $doit chgrp $groupopt "$desttmp" ; fi &&
    if [ "$modeopt" ] ; then $doit chmod $modeopt "$desttmp" ; fi &&
    $doit mv -f  "$desttmp" "$dest" &&
    trap 0 ; then : ; 
 else 
    echo "problem installing $sourcefile into $dest" >&2
    exit 3; fi
done

exit $donestatus