# shar - group files into distribution package.  Shar creates an archive
# which can be unpacked on an Amiga using bsh or on a **IX system using
# the Bourne shell.  To use the -c option, you must have wc and test
# somewhere in your path; to use the -p option, you must have sed in
# your path; the -o option requires that you have test in your path
# when unpacking only.  Note that this script will probably not work
# well with file names containing tabs or spaces; this can easily be
# fixed by quoting "$file" everywhere, but it remains problematical
# whether the receiving system will accept such file names.  Shar can
# also be used to unpack archives by specifying the -u option.  This
# is not fool-proof; some shell-archives have **IX or VMS file-system
# specific constructs which may not work on an Amiga.  Shell-archives
# containing AWK scripts will probably not work at all.  Shar is
# provided as is; no warranty is either expressed or implied as to its
# fitness for any purpose.  Use at your own risk, blah, blah, etc.
if #argv < 2
    label usage
    echo >&2 "usage: shar [-p prefix] [-d delim] [-cov] [>foo.sh] list of files"
    echo >&2 "\t-c add check for correct size to archive"
    echo >&2 "\t-c override overwrite check when unpacking"
    echo >&2 "\t-o check for existing file"
    echo >&2 "\t-v be verbose when unpacking archive"
    echo >&2 "    to unpack, type:"
    echo >&2 " % source foo.sh"
    echo >&2 "  or:
    echo >&2 " % shar -u foo.sh"
    echo >&2 "\tor just type:"
    echo >&2 " % foo"
    echo >&2 "\tto unpack on a **IX system:"
    echo >&2 " $ sh foo.sh"
    return 0
endif
local file j=1 delim="SHAR_EOF" prefix=0 count=0
local overwrite=0 verbose=0 len unpack=0

# process arguments
while j < #argv
    if left(argv[j], 1) != '-'
	break
    endif
    if substr(argv[j], 2, 1) == 'p' && j+1 < #argv
	prefix = argv[j+1], j+=2
	continue
    elseif substr(argv[j], 2, 1) == 'd' && j+1 < #argv
	delim = argv[j+1], j+=2
	continue
    elseif substr(argv[j], 2, 1) == 'c'
	count++
    elseif substr(argv[j], 2, 1) == 'o'
	overwrite++
    elseif substr(argv[j], 2, 1) == 'v'
	verbose++
    elseif substr(argv[j], 2, 1) == 'u'
	unpack++
	if j+1 < #argv
	    file=argv[j+1]
	else
	    file=0
	endif
    else
	goto usage
    endif
    if (len = strlen(argv[j])) > 2
	argv[j] = cat("-", right(argv[j], len - 2))
    else
	j++
    endif
endwhile
if unpack
    # unpack archives
    local flvl vrbs=?_verbose nclbr=?_noclobber str1=""
    onerr goto errxit
    onintr goto errxit
    alias export abort
    set flvl `failat` # obtain current fail level so we can restore it later
    if flvl[2] < 20
	failat 20
    endif
    # remember current setting of verbose, noclobber flags
    if count
	unset _noclobber
	str1="-c"
    endif
    if verbose
	set _verbose 1
    endif
    if file
	source $file $str1
    else
	bsh <&0
    endif
    label errxit
    onintr;onerr
    if vrbs==0; unset _verbose; endif
    if nclbr!=0; set _noclobber 1; endif
    failat $flvl[2]
    unalias export
    return
endif

# it is now known to be a packing operation
if j>=#argv
    goto usage
endif
echo "#\tThis is a shell archive."
echo "#\tRemove everything above and including the cut line."
echo "#\tThen run the rest of the file through bsh."
echo "#----cut here-----cut here-----cut here-----cut here----#"
echo "#!/bin/sh"
echo "# shar: Shell Archiver"
echo "#\tRun the following text through bsh to create:"
foreach file ( $argv[$j-*] )
    if -f "$file"		# Don't include non-files in the manifest.
	echo "#\t$file"
    endif
end
echo "# This archive created: `date`"
if $?NAME
    if $?ORGANIZATION
	echo "# By:\t$NAME  ($ORGANIZATION)"
    else
	echo "# By:\t$NAME"
    endif
endif
foreach file ( $argv[$j-*] )
    if -f "$file"
	if verbose
	    echo >&2 shar: packing $file
	endif
	if overwrite
	    echo "if test -f $file -a \"\$1\" != \"-c\""
	    echo "then"
	    echo "    echo shar: Will not over-write existing file $file"
	    echo "else"
	endif
	echo "echo shar: extracting $file"
	if prefix
	    echo "sed 's/^$prefix//' >$file << \\$delim"
	    sed s/\^/$prefix/ $file
	else
	    echo "cat >$file << \\$delim"
	    cat $file
	endif
	echo "$delim"
	if count
	    len=-s "$file"
	    echo "if test $len -ne \`wc -c $file\`; then"
	    echo "    echo shar: $file unpacked with wrong size!"
	    echo "fi"
	endif
	if overwrite
	    echo "# end of overwrite check"
	    echo "fi"
	endif
    endif
end
echo "#\tEnd of shell archive"
echo "exit 0"
