#!/bin/ksh
#
# $Revision: 1.5 $ $Date: 2012-07-26 17:11:47-04 $
# $Source: /home/vogelke/bin/RCS/cshar,v $
# $Host: sys7.com $
# $UUID: 91bf7b9b-3211-3fd3-b1a1-48e5ba0a72eb $
#
#<cshar: creates a very basic shell archive.

#  Usage message.
usage="Usage: $0 [ -o output ] [ -r ] [ file ] ..."

#  Output file -- standard output default.
output=''

#  Separator.
separator='+ END-OF-FILE'

#  Default is: recursively archive subdirectories.
recurse=yes

#  Procedure to set the mode of the file to the correct value.
chmod='
    ls="$(/bin/ls -ld $f)"
    echo "chmod '\''$(echo $ls | sed -e '\''s/.\(...\)\(...\)\(...\).*/u=\1,g=\2,o=\3/'\'' -e '\''s/-//g'\'')'\'' '\''$f'\''"
'

#  Procedure to archive the file / directory
shar='
    if test -d $f
    then    : We have a directory here.
        case $recurse in
        yes)    : Check whether there is a file with the same name
            :    if so, remove it.
            echo "if test -f '\''$f'\''"
            echo "then  rm '\''$f'\''"
            echo "fi"
            : If the directory does not yet exist, create it.
            echo "if test -d '\''$f'\''"
            echo "then  :"
            echo "else  echo '\''Making     $f/'\''"
            echo "  mkdir '\''$f'\''"
            echo "fi"
            : Change the mode of the directory to the correct value.
            eval "$chmod"
            : Now archive the contents of the directory.
            for f in $f/*
            do  : Recursive call.
                eval "$shar"
            done
        esac
    else    : We have a file.
        echo "echo '\''Extracting $f'\''"
        echo "sed '\''s/^X//'\'' > $f << '\''$separator $f'\''"
        : Here the file is put into the archive.
        sed '\''s/^/X/'\'' $f
        : End with the separator.
        echo "$separator $f"
        : Change the mode of the file to the correct value.
        eval "$chmod"
        : Show the size of the file as sent.
        echo "echo '\''$ls (as sent)'\''"
        : Show the size of the file as received.
        echo "/bin/ls -l $f"
    fi
'

for arg
do  case $arg in
    -o) : Output file name.
        : Get rid of the -o.
        shift
        : Is there an argument left?
        case $# in
        0)  : No.  Exit with an error message.
            echo "$usage" 1>&2
            exit 1
        esac
        : Yes.
        output="$1"
        ;;
    -o*)    : Output file name.
        output=$(expr "$arg" : '-o\(.*\)')
        ;;
    -r) : Do not archive subdirectories.
        recurse=no
        ;;
    -*) : Unknown option.
        echo "$usage" 1>&2
        exit 1
        ;;
    *)  : Break out of the for loop.
        break
    esac
    shift
done

#  If no arguments left.  Archive current directory.
case $# in
    0)  set *
esac

#  Redirect output if necessary.
case "X$output" in
    X)  ;;
    *)  exec >"$output"
esac

echo ': This is a shar archive created by cshar.  Extract with sh, not csh.'
echo ': This archive ends with exit, so do not worry about trailing junk.'

#  Here the work is being done.
for f in $*
    do  eval "$shar"
done

#  Let the archive end with an exit.
echo 'exit 0'
exit 0
