#! /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: Bitmap EPS file compression program
#    This program compresses and codes, using standard methods of
#    Postscript Level 2, bitmap EPS files (hexadecimal, not compressed).
#
# P: Program do kompresji EPS-/ow bitmapowych.
#    S/lu/zy on do pakowania oraz kodowania bitmapowych, kodowanych
#    szesnastkowo, niekompresowanych plik/ow EPS za pomoc/a
#    standardowych algorytm/ow Postscriptu Level 2.
#

usage() {
cat <<EOF
E: USAGE:
      cep input_file output_file [options]
    the program recognizes the following options:
           8 -- use ASCII85 coding (default)
      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:
      cep plik_wej/sciowy plik_wyj/sciowy [opcje]
    program rozpoznaje nast/epuj/ace opcje:
            8 -- kodowanie ASCII85 (domy/slnie)
      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/cep-$$.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/cep.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

