[alt.sources] unshar 1.3 - SHAR extract script

tneff@bfmny0.UUCP (Tom Neff) (08/09/89)

This is what I use to grab stuff from the source newsgroups.
It automatically skips the garbage in front of most archives.
It will create a new storage directory for you if you want.
I have spruced up the messages a bit in the interests of user
friendliness.  The 'sync' at the end is optional.

--- cut dear --- cut dear --- cut dear --- cut dear --- cut dear  ---
:
# @(#)$Id: unshar,v 1.3 89/08/08 14:14:27 tneff Exp $
#
#   PUBLIC DOMAIN - HACK AWAY
#
#   Unpack a SHAR archive, with optional leading and trailing garbage
#
#   This script uses sed(1) and sh(1) to unpack a shell archive from
#   the standard input.  Results are written to the current directory
#   unless you supply a directory name as the first argument.  If the
#   directory does not exist yet, you can force it to be created with
#   the -c switch.
#
#   Usage:  unshar [[-c] <dirname>]
#
#   Since unshar is a filter, it's nice with rn or mailers.
#
#   Example: at the rn article prompt in a source group, type:
#
#	what next? [^Nnpq] |unshar -c /usr/src/simelvis
#
#   $Log:	unshar,v $
# Revision 1.3  89/08/08  14:14:27  tneff
# Added error checking.
# Improved user messages and comments.
# Added -p switch to mkdir.
# 
# Revision 1.2  89/08/07  19:09:31  tneff
# Added -c flag to force creation of destination directory.
#
# Revision 1.1  89/07/18  19:34:14  tneff
# Initial revision
#

# set -vx

Command=`basename $0`
Tmpsh=/tmp/US$$.sh	# change this if needed

abort() {
	echo "$Command: $*" >&2
	exit 1
}

usage() {
	echo "syntax: unshar [[-c] <dirname>]" >&2
	exit 1
}

if [ "$1" = "-c" ] && [ "$2" ]
then
	if [ -d "$2" ]
	then
		abort dir $2 already exists
	else
		if mkdir -p $2
		then :
		else
			abort mkdir error
			exit 2
		fi
	fi
	shift
fi

case $# in
0)	;;
1)	if [ -d $1 ]
	then
		cd $1 || abort cannot cd to $1
		set ""
	else
		usage
	fi
	;;
*)	usage
	;;
esac

sed '/^#/,$!d' $1 > $Tmpsh
if [ $? -gt 0 ]
then
	echo "$0: sed error!"
	exit 2
fi

if /bin/sh $Tmpsh
then
	echo "Extracted OK"
else
	abort extract error!
fi

rm -f $Tmpsh

#   This is optional, comment it out if you prefer
sync

exit 0
-- 
"We walked on the moon --	((	Tom Neff
	you be polite"		 )) 	tneff@bfmny0.UU.NET