#!/bin/sh

PATH=/usr/ucb:/usr/bin:/bin

DEBUG=
#DEBUG=echo	# comment out when done debugging
VERBOSE=1
SHOW=1

if [ x$1 = x-q ]; then
	VERBOSE=
	SHOW=
	shift
fi

# script to duplicate directory $1 as $2 as far as ownership, group, and modes

if [ -f /etc/fstab ]; then
    if [ -f /etc/chown ]; then
	CHOWN=/etc/chown
    else
	CHOWN=/bin/chown
    fi
    if [ -f /usr/5bin/ls ]; then
	LS=/usr/5bin/ls
    else
	LS=/bin/ls
    fi
	LSFLAGS=ldg
    if [ -f /usr/ucb/whoami ]; then
	WHOAMI=/usr/ucb/whoami
    else
	WHOAMI=/usr/bin/whoami
    fi
else
	CHOWN=/usr/ucb/chown
	LS=/bin/ls
	LSFLAGS=ld
	WHOAMI=/usr/ucb/whoami
fi

decode() {
case $1 in
	rwx) O=7;S=0 ;;
	rws|rwt) O=7;S=1 ;;
	rw-) O=6;S=0 ;;
	rwS|rwT) O=6;S=1 ;;
	r-x) O=5;S=0 ;;
	r-s|r-t) O=5;S=1 ;;
	r--) O=4;S=0 ;;
	r-S|r-T) O=4;S=1 ;;
	-wx) O=3;S=0 ;;
	-ws|-wt) O=3;S=1 ;;
	-w-) O=2;S=0 ;;
	-wS|-wT) O=2;S=1 ;;
	--x) O=1;S=0 ;;
	--s|--t) O=1;S=1 ;;
	---) O=0;S=0 ;;
	--S|--T|--l) O=0;S=1 ;;
	*) echo what do I know about $1\? ; exit 1
esac
	}

if [ $# -ne 2 ]; then
	echo usage: $0 source-dir target-dir
	exit 1
fi

if [ ! -d $1 ]; then
	echo $1 does not exist
	exit 1
fi

#LS1=`/bin/ls -$LSFLAGS $1 | awk '{if (length($1) == 10) {print} else {print substr($0,1,10),substr($0,11)}}'`
LS1=`$LS -ldn $1`
PROT=`echo $LS1 | awk '{print substr($1,2,9)}'`
OWNER=`echo $PROT | awk '{print substr($1,1,3)}'`
GROUP=`echo $PROT | awk '{print substr($1,4,3)}'`
OTHER=`echo $PROT | awk '{print substr($1,7,3)}'`
#echo $PROT $OWNER $GROUP $OTHER

decode $OWNER
#echo $OWNER = $O,$S
UN=$O;US=$S

decode $GROUP
#echo $GROUP = $O,$S
GN=$O;GS=$S

decode $OTHER
#echo $OTHER = $O,$S
ON=$O;T=$S

# if target is a symbolic link, refuse to go on.

if [ -h $2 ]; then
	echo $2 is a symbolic link
	exit 1
fi

# if target exists and is not a directory, refuse.

if [ -f $2 ]; then
	echo $2 is not a directory
	exit 1
fi

# if target exists and is owned by other than source, refuse.

#   Could be a little smarter here, as 4.1.x ls sometimes runs
#   directory modes and size together, but we'll cross that bridge if
#   we come to it.
#			daw, 2/7/95

O1=`echo $LS1 | awk '{print $3}'`
if [ -d $2 ]; then
#	LS2=`/bin/ls -$LSFLAGS $2 | awk '{if (length($1) == 10) {print} else {print substr($0,1,10),substr($0,11)}}'`
	LS2=`$LS -ldn $2`
	O2=`echo $LS2 | awk '{print $3}'`
	if [ $O1 != $O2 ]; then
		echo $2 is not owned by $O1
		exit 1
	fi
else
	O2=`$WHOAMI`
fi

# ok, let's do it

# build the directory if needed

if [ ! -d $2 ]; then
	if [ $VERBOSE ]; then
		echo /bin/mkdir $2
	fi
	$DEBUG /bin/mkdir $2
	if [ $? -ne 0 ]; then
		echo error creating $2
		exit 1
	fi
fi

# duplicate the protection

if [ $VERBOSE ]; then
	echo chmod $UN$GN$ON $2
fi
$DEBUG chmod $UN$GN$ON $2

# duplicate the group - use numbers,
# as sometimes string is gt 8 chars long

G1=`$LS -ldn $1 | awk '{print $4}'`
if [ $O1 != $O2 ]; then
	if [ $VERBOSE ]; then
		echo $CHOWN $O1.$G1 $2
	fi
	$DEBUG $CHOWN $O1.$G1 $2
fi
if [ $VERBOSE ]; then
	echo /bin/chgrp $G1 $2
fi
$DEBUG /bin/chgrp $G1 $2

# and finally, copy the extra bits

if [ $US -eq 1 ]; then
	if [ $VERBOSE ]; then
		echo chmod u+s $2
	fi
	$DEBUG chmod u+s $2
fi
if [ $GS -eq 1 ]; then
	if [ $VERBOSE ]; then
		echo chmod g+s $2
	fi
	$DEBUG chmod g+s $2
fi

if [ $T -eq 1 ]; then
	if [ $VERBOSE ]; then
		echo chmod +t $2
	fi
	$DEBUG chmod +t $2
fi

if [ $SHOW ]; then
	$LS -ld $1 $2
fi
