[net.sources] rnget - a quick way to unpack shell archives

tron@nsc.UUCP (Ron Karr) (08/14/86)

The following is a handy shell script for unpacking a shell archive.
It can be used from `rn' using the command:

	| rnget dir

to cause the shell archive contained in the current article to be
extracted (using a rather simple algorithm) and piped into /bin/sh
for processing.  Unlike the simpler:  w | (cd dir; sh)  command,
this works if there is text before the actual start of the archive.

  Ronald S. Karr			USENET: hplabs!nsc!tron
  National Semiconductor, Sunnyvale	ARPA:   decwrl!nsc!tron@ucbvax.ARPA
-----------------------Cut Here--------------------------------------------
#!/bin/sh
#
#  This is a shell archive.  To extract the contents, delete everything
#  above the #!/bin/sh line and process the rest through /bin/sh.
#  This archive contains the following file:
#
#	rnget
#
#  Created by tron@nsc.UUCP on August 13, 1986

sed -e 's/^X//' > rnget <<\-EOF-
X#!/bin/sh
X#
X# NAME
X#	rnget - get a shar archive from rn output.
X#
X# SYNOPSIS
X#	rnget [ -p pattern ] [ directory ]
X#
X# DESCRIPTION
X#	Rnget changes to the given directory (if given) and then
X#	reads its standard input and searches for the given pattern
X#	(default to "[#:]") at the beginning of a line and passes that
X#	and all subsequent lines to the bourne shell for processing.
X#
X#	The idea is that the input is a shar file contained inside
X#	of a news article, with a scan done before hand for the
X#	beginning of the shar.  For shars that don't begin with '#'
X#	or ':' -p can be used to specify something else.
X#
X#	Note:  If the directory does not exist, it is created.
X
Xprog="$0"
Xusage="usage: $prog [-p pattern] [directory]"
Xpattern="/^[:#]/"
Xwhile [ $# -gt 0 ]; do
X	if [ $1 = -p ]; then
X		shift
X		if [ $# -lt 1 ]; then
X			echo $usage
X			echo 1
X		fi
X		pattern="/^$1/"
X		shift
X	else		# got a directory
X		if [ -f $1 ]; then
X			echo "$prog: $1:  Not a directory!!"
X			exit 2
X		fi
X		if [ ! -d $1 ]; then
X			mkdir $1 > /dev/null 2>&1
X			if [ $? -ne 0 ]; then
X				echo "$prog: $1:  Could not create directory"
X				exit 2
X			fi
X		fi
X		cd $1
X		shift
X	fi
Xdone
Xawk "
XBEGIN	{
X	skip = 1; copy = 2;
X	state = skip;
X}
Xstate == skip && $pattern	{
X	state = copy;
X}
Xstate == copy	{
X	print \$0
X}" | /bin/sh
Xif [ $? -ne 0 ]; then
X	echo "$prog:  error in writing to shell"
Xfi
-EOF-

exit 0
-- 
  Ronald S. Karr			USENET: hplabs!nsc!tron
  National Semiconductor, Sunnyvale	ARPA:   decwrl!nsc!tron@ucbvax.ARPA