#! /bin/sh
#
# shar		--- make a shell archive
#
# Non-recursive shar script that handles directory hierarchies.
# This one does simple error checking using wc(1).
# Will only package plain files and directories.
# Doesn't check for funny characters.
# Modes are the default ones (i.e., not those of the originals).
#
# Can also be used to pack files with tar, compress and btoa
#
# Author: Oscar Nierstrasz @ cui, Jun 25 1988.
# Modified Nov 22/88 to add tar option

u='Usage: shar [<options>] <file> ...
	-l : list contents (default yes)
	-n : no contents listing
	-tc : tar and compress
	-tcb : tar, compress and btoa'


eof=eof
ls=y
mode=shar

for arg
do
	case $arg in
	-t* )	mode=${arg} ;;
	-l )	ls=y ;; # list contents
	-n )	ls=n ;; # fast -- don't list contents
	-* )	echo "$u" 1>&2 ; exit ;;
	* )	files="$files $arg" ;;
	esac
done

case $files in
"" )	echo "$u" 1>&2
	exit ;;
esac

case $mode in
-tc )	tz=`echo $files | sed 's/ /-/g'`-tar.Z
	tar cvf - $files | compress > $tz
	echo "created $tz" 1>&2
	exit ;;
esac

cat << eof
#! /bin/sh
# This is a shell archive.  The files and directories it contains
# may be unpacked by running it through /bin/sh.
eof

echo "# Packaged by" $USER at `hostname` on `date`

case $ls in
y )	echo "# Building table of contents ..." 1>&2
	echo "# Contents:"
	for f in $files
	do
		find $f \( -type f -o -type d \) \
			-exec ls -ld {} \; | sed 's/^/# /'
	done
	;;
esac

case $mode in
-tcb )	echo "( sed 's/^|//' | atob | uncompress | tar xvpf - ) << '${eof}'"
	tar cvf - $files | compress | btoa | sed 's/^/|/'
	echo "${eof}"
	echo exit
	exit ;;
esac

# Make directories
for f in $files
do
	find $f -type d -print | \
	awk '{ printf "echo mkdir %s\nmkdir %s\n", $0, $0 }'
done

# Make files
for f in $files
do
	find $f -type f -print | \
	while read fname
	do
		echo "if test -f $fname"
		echo "then echo 'shar: $fname -- file exists'"
		echo "else echo x - $fname"
		echo "sed 's/^|//' > $fname << '${eof}'"
		sed 's/^/|/' < $fname
		echo "${eof}"
		bytes=`wc -c < $fname | sed 's/^ *//'`
		echo 'if test `wc -c < '$fname'` -ne '$bytes
		echo "then echo 'shar: $fname damaged (should be $bytes bytes)'"
		echo fi
		echo fi
	done
done
echo exit
exit

