[net.sources] shar--yet another shell archiver

sjoerd@tjalk.UUCP (Sjoerd Mullender) (12/19/84)

Here is a shell archiver written in the Bourne shell.
It will archive the files and directories given to it as its arguments
(current directory default).  If it comes across a directory the
contents of the directory will be archived also.  The modes of the
files and directories will be restored.
Usage is:
	shar [ -o output ] [ -r ] [ files and directories ]
The options it recognizes are:
	-o output	The archive will come in the file output
			(standard output default).
	-r		Do not archive subdirectories.
Even if you don't want this program, it is worth looking at.
8<---------CUT HERE---------------------HERE TOO---------------------
: This is a shar archive.  Extract with sh, not csh.
: This archive ends with exit, so do not worry about trailing junk.
echo 'Extracting shar'
sed 's/^X//' > shar << '+ END-OF-FILE shar'
X: Usage message.
Xusage="Usage: $0 [ -o output ] [ -r ] [ file ] ..."
X: Output file -- standard output default.
Xoutput=''
X: Separator.
Xseparator='+ END-OF-FILE'
X: Default is: recursively archive subdirectories.
Xrecurse=yes
X: Procedure to set the mode of the file to the correct value.
Xchmod='
X	ls="`/bin/ls -ld $f`"
X	echo "chmod '\''`echo $ls | sed -e '\''s/.\(...\)\(...\)\(...\).*/u=\1,g=\2,o=\3/'\'' -e '\''s/-//g'\''`'\'' '\''$f'\''"
X'
X: Procedure to archive the file / directory
Xshar='
X	if test -d $f
X	then	: We have a directory here.
X		case $recurse in
X		yes)	: Check whether there is a file with the same name
X			:    if so, remove it.
X			echo "if test -f '\''$f'\''"
X			echo "then	rm '\''$f'\''"
X			echo "fi"
X			: If the directory does not yet exist, create it.
X			echo "if test -d '\''$f'\''"
X			echo "then	:"
X			echo "else	echo '\''Making     $f/'\''"
X			echo "	mkdir '\''$f'\''"
X			echo "fi"
X			: Change the mode of the directory to the correct value.
X			eval "$chmod"
X			: Now archive the contents of the directory.
X			for f in $f/*
X			do	: Recursive call.
X				eval "$shar"
X			done
X		esac
X	else	: We have a file.
X		echo "echo '\''Extracting $f'\''"
X		echo "sed '\''s/^X//'\'' > $f << '\''$separator $f'\''"
X		: Here the file is put into the archive.
X		sed '\''s/^/X/'\'' $f
X		: End with the separator.
X		echo "$separator $f"
X		: Change the mode of the file to the correct value.
X		eval "$chmod"
X		: Show the size of the file as sent.
X		echo "echo '\''	$ls        (as sent)'\''"
X		: Show the size of the file as received.
X		echo "echo -n '\''	'\''"
X		echo "/bin/ls -l $f"
X	fi
X'
Xfor arg
Xdo	case $arg in
X	-o)	: Output file name.
X		: Get rid of the -o.
X		shift
X		: Is there an argument left?
X		case $# in
X		0)	: No.  Exit with an error message.
X			echo "$usage" 1>&2
X			exit 1
X		esac
X		: Yes.
X		output="$1"
X		;;
X	-o*)	: Output file name.
X		output=`expr "$arg" : '-o\(.*\)'`
X		;;
X	-r)	: Do not archive subdirectories.
X		recurse=no
X		;;
X	-*)	: Unknown option.
X		echo "$usage" 1>&2
X		exit 1
X		;;
X	*)	: Break out of the for loop.
X		break
X	esac
X	shift
Xdone
X: If no arguments left.  Archive current directory.
Xcase $# in
X0)	set *
Xesac
X: Redirect output if necessary.
Xcase "X$output" in
XX)	;;
X*)	exec >"$output"
Xesac
Xecho ': This is a shar archive.  Extract with sh, not csh.'
Xecho ': This archive ends with exit, so do not worry about trailing junk.'
X: Here the work is being done.
Xfor f in $*
Xdo	eval "$shar"
Xdone
X: Let the archive end with an exit.
Xecho 'exit 0'
Xexit 0
+ END-OF-FILE shar
chmod 'u=rwx,g=rx,o=rx' 'shar'
echo '	-rwxr-xr-x 2 sjoerd   2473 Dec 19 19:45 shar        (as sent)'
echo -n '	'
/bin/ls -l shar
exit 0
-- 
			Sjoerd Mullender
			...!{decvax,philabs,seismo}!mcvax!vu44!sjoerd