#! /bin/sh

# This file belongs to the CEP package | Ten plik nale/zy do pakietu CEP
# This package is public domain        | Pakiet stanowi dobro powszechne
# For more info see `0CEP_LIC.ENG'     | Wi/ecej informacji w ,,0CEP_LIC.POL''
# ===========================================================================
# E: PS file compression program
#    This program compresses any PS file using Postscript Level 2 methods.
#
# P: Program do kompresji plik/ow PS.
#    S/lu/zy on do pakowania oraz kodowania dowolnych plik/ow
#    PostScriptowych za pomoc/a standardowych algorytm/ow
#    Postscript-u Level 2.
#

usage() {
cat <<EOF
E: USAGE:
      cop input_file output_file [options]
    the program recognizes the following options:
           8 -- use ASCII85 coding (default)
      b or B -- use binary coding
      h or H -- use HEX (hexadecimal) coding
      r or R -- use RLE (RunLength) compression (default)
      l or L -- use LZW compression
      f or F -- use Flate compression (non-standard!)
      n or N -- don't compress
    (names of input_file and output_file must differ)

P: WYWO/LANIE:
      cop plik_wej/sciowy plik_wyj/sciowy [opcje]
    program rozpoznaje nast/epuj/ace opcje:
            8 -- kodowanie ASCII85 (domy/slnie)
      b lub B -- kodowanie binarne
      h lub H -- kodowanie HEX -- szesnastkowe
      r lub R -- kompresja RLE -- RunLength (domy/slnie)
      l lub L -- kompresja LZW
      f lub F -- kompresja Flate (niestandardowa!)
      n lub N -- bez kompresji
    (nazwa pliku wyj/sciowego musi by/c r/o/zna od nazwy pliku wej/sciowego)
EOF
exit 0
}

# orig: if "%2"=="" goto USAGE
[ -z "$2" ] && usage;

# orig: if "%1"=="%2" goto USAGE
[ "$1" = "$2" ] && usage;

# orig: if not exist %1 goto USAGE
# Well, I test if the file is readable, not just exists -- KL
[ ! -r "$1" ] && usage;

# Few things... put tmp in /tmp directory and try hard to delete it at
# the end, even if the user hits Ctrl-C
TMP=${TMP:-/tmp}
TMPSX=$TMP/cop-$$.psx

# Kill temporary file if something goes wrong.
trap '/bin/rm -rf $TMPSX; exec' 0 2 3 5 7 10 13 15 

if mawk -f /usr/share/cep/cop.awk -vTMPSX=$TMPSX -vOUTF="$2" -vCVM="$3$4" $1
then
    if [ -r $TMPSX ]
    then
	/usr/bin/gs -q -dNODISPLAY $TMPSX
	rm $TMPSX
    fi
else
    echo "Something went wrong with awk"
fi

rm -rf $TMPSX

