#!/bin/sh
#
# anytopnm - attempt to convert an unknown type of image file to a P?M file.
#
# Copyright (C) 1991 by Jef Poskanzer.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation.  This software is provided "as is" without express or
# implied warranty.

if [ $# -gt 1 ] ; then
    echo "Usage: $0 infilename > outfilename.pnm" 1>&2
    echo "       $0 - < infilename > outfilename.pnm" 1>&2
    echo "  This will convert the input file into a PNM file" 1>&2
    echo "  It automatically determines the format of the input file" 1>&2
    echo "    with the 'file' command, and acts accordingly" 1>&2
    echo "  If the infilename is a '-', uses standard input" 1>&2
    exit 1
fi

tmpfiles=""

# Take out all spaces
# Find the filename extension for last-ditch efforts later
fileextension=`echo "$1" | awk '{gsub(" ","");gsub(".*\\\\.",".");print}'`

# Sanitize the filename by making our own temporary files as safely as
# possible.
file="/tmp/atn.stdin.$$"
rm -f "$file"
if [ $# -eq 0 -o "$1" = "-" ] ; then
	cat > "$file"
else
    if [ ! -e "$1" ] ; then
        echo "$0: $1: No such file" 1>&2
        exit 1
    fi
    
    if [ ! -f "$1" ] ; then
        echo "$0: $1: Not a file" 1>&2
        exit 1
    fi
    
    if [ ! -r "$1" ] ; then
        echo "$0: $1: Not a readable file" 1>&2
        exit 1
    fi
    
    if [ -z "$1" ] ; then
        echo "$0: $1: Empty file" 1>&2
        exit 1
    fi
    
    cat < "$1" > "$file"
fi

tmpfiles="$tmpfiles $file"



filetype=`file "$file" | cut -d: -f2-`

case "$filetype" in

    *PBM* | *PGM* | *PPM* )
    cat "$file"
    ;;

    *uuencoded* )
    newfile="/tmp/atn.decode.$$"
    rm -f "$newfile"
    (echo begin 600 $newfile; tail +2 < "$file") | uudecode
    tmpfiles="$tmpfiles $newfile"
    anytopnm "$newfile"
    ;;

    *bzip2*compressed*data* )
    bzip2 -dk < "$file" | anytopnm -
    ;;

    *bzip*compressed*data* )
    bzip -dk < "$file" | anytopnm -
    ;;

    *gzip*compressed*data* )
    gzip --decompress --to-stdout < "$file" | anytopnm -
    ;;

    *compress* )
    uncompress -c < "$file" | anytopnm -
    ;;

    *btoa* )
    atob < "$file" | anytopnm -
    ;;

    *Sun* | *rasterfile* )
    rasttopnm "$file"
    ;;

    *GIF* )
    giftopnm "$file"
    ;;

    *TIFF* )
    tifftopnm "$file"
    ;;

    *IFF*ILBM* )
    ilbmtoppm "$file"
    ;;

    *Lisp* )
    lispmtopgm "$file"
    ;;

    *PC*Paintbrush* )
    pcxtoppm "$file"
    ;;

    *Bennet* )
    ybmtopbm "$file"
    ;;

    *pixmap*image*text* )
    xpmtoppm < "$file"
    ;;

    # This has to come after all other 'text' files, or you may be
    # disappointed.
    *text* )
    pbmtext -builtin fixed < "$file"
    ;;

    *JPEG* | *JFIF* )
    jpegtopnm "$file"
    ;;

    *PNG* )
    pngtopnm "$file"
    ;;

    *MicroDesign* )
    mdatopbm -d -- "$file"
    ;;

    *PC*bitmap*data* )
    bmptoppm "$file"
    ;;
    
    * )
    # Can't figure out the file type from the magic number,
    # try the extension.
    case "$fileextension" in

        *.pbm | *.pbm.* | *.pgm | *.pgm.* | *.ppm | *.ppm.* )
        cat "$file"
        ;;
        *.x | *.x.* | *.xbm | *.xbm.* | *.x10bm | *.x10bm.* | \
            *.x11bm | *.x11bm.* | *.bitmap | *.bitmap.* )
        xbmtopbm "$file"
        ;;
        *.r | *.r.* | *.rast | *.rast.* )
        rasttopnm "$file"
        ;;
        *.mac | *.mac.* | *.macp | *.macp.* )
        macptopbm "$file"
        ;;
        *.g3 | *.g3.* | *.fax | *.fax.* )
        g3topbm "$file"
        ;;
        *.xwd | *.xwd.* | *.x10wd | *.x10wd.* | *.x11wd | *.x11wd.* )
        xwdtopnm "$file"
        ;;
        *.brush | *.brush.* )
        brushtopbm "$file"
        ;;
        *.img | *.img.* )
        gemtopbm "$file"
        ;;
        *.pcx | *.pcx.* )
        pcxtoppm "$file"
        ;;
        *.pic | *.pic.* | *.pict | *.pict.* | *.pict2 | *.pict2.* )
        picttoppm "$file"
        ;;
        *.tif | *.tif.* | *.tiff | *.tiff.* )
        tifftopnm "$file"
        ;;
        *.fs | *.fs.* | *.face | *.face.* )
        fstopgm "$file"
        ;;
        *.hips | *.hips.* )
        hipstopgm "$file"
        ;;
        *.fits | *.fits.* )
        fitstopnm "$file"
        ;;
        *.gif | *.gif.* )
        giftopnm "$file"
        ;;
        *.iff | *.iff.* | *.ilbm | *.ilbm.* )
        ilbmtoppm "$file"
        ;;
        *.lispm | *.lispm.* )
        lispmtopgm "$file"
        ;;
        *.mtv | *.mtv.* )
        mtvtoppm "$file"
        ;;
        *.qrt | *.qrt.* )
        qrttoppm "$file"
        ;;
        *.tga | *.tga.* | *.targa | *.targa.* )
        tgatoppm "$file"
        ;;
        *.xim | *.xim.* )
        ximtoppm "$file"
        ;;
        *.xpm | *.xpm.* | *.xpm2 | *.xpm2.* )
        xpmtoppm "$file"
        ;;
        *.pi1 | *.pi1.* )
        pi1toppm "$file"
        ;;
        *.pi3 | *.pi3.* )
        pi3topbm "$file"
        ;;
        *.spu | *.spu.* )
        sputoppm "$file"
        ;;
        *.spc | *.spc.* )
        spctoppm "$file"
        ;;
        *.ybm | *.ybm.* | *.face | *.face.* )
        ybmtopbm "$file"
        ;;
        *.JPEG | *.jpeg | *.jpg | *.JPG )
        jpegtopnm "$file"
        ;;
        *.png | *.PNG )
        pngtopnm "$file"
        ;;
        *.mda | *.mdp )
        mdatopbm -d -- "$file"
        ;;
        * )
        echo "$0: unknown file type: $filetype" 1>&2
        exit 1
        ;;

    esac
    ;;

esac


if [ "$tmpfiles" ] ; then
    rm -f $tmpfiles
fi
exit 0
