#! /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: A decompression program of PS files compressed with COP
#
# P: Program do rozpakowywania plik/ow PS spakowanych za pomoc/a COP-a
#

usage() {
cat <<EOF
E: USAGE:
      uncop input_file output_file
    (decompression and decoding method is taken from input file;
     names of input_file and output_file must differ)

P: WYWO/LANIE:
      uncop plik_wej/sciowy plik_wyj/sciowy
    (metoda dekodowania jest ustalana na postawie pliku wej/sciowego;
     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/uncop-$$.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/uncop.awk -vTMPSX=$TMPSX -vOUTF="$2" "$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

