[net.sources] bundle - a super-shar script

diamond@unirot.UUCP (Robert Diamond) (01/05/87)

Here is a shell script for bundle -- It is a shar-like program that has
some neat options like autocompile, chmod-ing, and makefile execution.
The enclosed package contains the program and some documentation.
It is written by Dan Rogers, and he may be reached at:
  anywhere!rutgers!atux01!piscv!piscq!djr


--------------------------(cut here)-----------------------------------------
#/bin/sh
#This is a shar file.  To use:
#  1. Remove everything before the /bin/sh line
#  2. Execute with /bin/sh (not csh) to extract the files:
#         bundle
#         bundle.info
file="${0}"
echo extracting bundle 1>&2
cat >bundle << 'EnD of bundle'
#super bundle: group files into distribution package
#
# author Dan Rogers 
# with original idea from "The UNIX Programming Environment" 
# by Kernighan/Pike.
#
# options: 
#	-n (default)  Normal bundle operation (resets -c and -x)
#
#	-x files which follow this are chmod to execute up to
#	   the next option 
#
#	-c AUTOCOMPILE: files following this are compiled as
#	   "c" programs at the time of unbundle (does not handle curses)
#
#	-k causes removal of the bundled file on unpack, leaving
#	   only the original files.
#
#	-m specifies the name of a single makefile to execute
#	   after all files are unbundled.
#
# Usage: '$0 option file(s) option file(s) ...  > package'
# 	( all option flags followed by a space )
#
if [ $# -eq 0 ]
then
	cat "${0}" | sed '23q'
	exit 5
fi
terminator='EnD of '

echo '#/bin/sh'
echo '#This is a shar file.  To use:'
echo '#  1. Remove everything before the /bin/sh line'
echo '#  2. Execute with /bin/sh (not csh) to extract the files:'
for i
do
	case "${i}" in
		-*)	continue
			;;
		*)	echo '#         '`basename "${i}"`
			continue
			;;
	esac
done
#

echo 'file="${0}"'
kill=0
flag=0
mflag=0
mfile=""
string=""
for i
do
	case "${i}" in
		-m)	if [ "${mflag}" -eq 1 ]
			then
				echo "ERROR: Only one makefile Allowed." >&2
				exit 10
			else
				mflag=1
			fi
			continue
			;;
		-x)	flag=1
			continue
			;;
		-c)	if [ "${flag}" -eq 1 ]
			then
				if [ -z "${string}" ]
				then
					echo "# bad execute flag"
				else
					echo "chmod +x ${string}"
					string=""
				fi
			fi
			flag=2
			continue
			;;
		-n)	if [ "${flag}" -eq 1 ]
			then
				if [ -z "${string}" ]
				then
					echo "# bad execute flag"
				else
					echo "chmod +x ${string}"
					string=""
				fi
			fi
			flag=0
			continue
			;;
		-k)	kill=1
			continue
			;;
	esac
	k=`basename "${i}" 2>/dev/null`
	if [ "${flag}" -eq 1 ]
	then
		if [ -z "${string}" ]
		then
			string="${k}"
		else
			nstring="${string} ${k}"
			string="${nstring}"
		fi
	fi
	if [ "${mflag}" -eq 1 ]
	then
		mfile="${k}"
	fi
	echo "echo extracting ${k} 1>&2"
	echo "cat >${k} << '${terminator}${k}'"
	cat $i
	echo "${terminator}${k}"
#
#	check to see if compile flag is set
	if [ "${flag}" -eq 2 ]
	then
		if echo "${k}" | grep '.c' 1>/dev/null 2>/dev/null
		then
			k2=`echo "${k}" | cut -d. -f1` 
			if [ -z "${k2}" ]
			then
				echo "# bad compile condition in bundle"
			else
				echo "echo Compiling ${k2}"
				echo "cc -o${k2} ${k}"
			fi
		else
			echo "\n# **** not a c program, cannot compile"
		fi
	fi
done
if [ "${flag}" -eq 1 ]
then
	if [ -z "${string}" ]
	then
		echo "# bad execute flag"
	else
		echo "chmod +x ${string}"
	fi
fi
# check for a makefile
if [ "${mflag}" -eq 1 ]
then
	echo "echo executing make on ${mfile}"
	echo "make -f ${mfile}"
fi
# check the kill flag
if [ "${kill}" -eq 1 ]
then
	echo '/bin/rm "${file}"'
fi
EnD of bundle
echo extracting bundle.info 1>&2
cat >bundle.info << 'EnD of bundle.info'
Bundle: a shell program to allow convenient packaging
	of ascii files into a distribution package.  

FORMAT:  bundle [-x -c -k -n -m ] [list] [....] > package

Bundle may be activated in several modes.  These are:

-n  (default mode)  Normal ascii file, no action taken on
	unbundling.  File is to be extracted without any
	special action to be taken.

	Format: bundle file1 file 2    > package
		bundle -n file1 file2 ... > package

-x  (execute mode) The list of files that follow the -x option
	are made to be executable (chmod +x) on unbundling.

	Format: bundle file1 -x file2 ... > package

	Causes the file named file2 to be changed to executable
	when the package is unbundled.  File1 is left as a normal
	ascii file.

-c   ("C" mode)  The list of files following the -c option (up to
	the next option) are to be compiled as "C" programs on
	unbundling.  The resultant executable file is named the 
	root name of the .c file.  The file must be named in the
	form file.c

	Format: bundle file1 -x file2 -c file.c > package

	Causes the file named file 1 to be treated as a normal ascii
	file, file2 is changed to be an executable ascii file, and
	file.c is compiled automatically on unbundle as file.

-k  (Kill mode) no list follows the -k option.  -k may appear
	anywhere in the argument list.  Causes the package to be
	removed after unbundling.

-m  (Make mode)	A single makefile may be included in the list
	of arguments, preceded by the -m option.  Causes the
	makefile to be executed as the last step in the unbundle
	process.  This does not effect the status of other flags,
	so it is recommended that the -m option be first in the
	argument list, or following the -n option.

These options may be mixed in any order in the argument list.
Each option must be followed by at least one space character.  The
space character is the argument list delimeter.  Upon encountering
an option, the provious mode is canceled, and action procedes based
on the new option.  This does not apply to the -k option, since the -k
option specifies the kill option with no arguments.  So, a bundle
line may look like this:

bundle file1 -c file.c -x file2 -c file3.c -k -n file4 -m file.mk >package
The results of this command will be:  

file1 and file4 are normal ascii files, and are left non-executable
on unbundle.

file.c, file2.c and file3.c are compiled automatically into executable
files named file  and file3 respectively.

file2 is left executable on unbundle.

make is executed on file.mk as the last step of unbundling
The entire bundle package is removed after unbundling.

To unbundle a bundle package :

sh package

causes the bundle package file named package to be unbundled.  As you
can see, the bundle program creates shell programs which recreate the
original files upon unbundling.

HELP:  Onscreen help is availabel by simply typing:

bundle

Super-bundle (bundle) creates files that are in shar (shell archiver) 
format.
EnD of bundle.info