#!/bin/sh
# ez-magic 1.0.5 - Do not redistribute modified versions of this program.
# Usage: magic [filename]
# (c)1997 Toby Reed for PorkNET Internet Systems (http://www.porknet.ml.org/)

# Helper Programs
ALCHEMY="/usr/local/bin"		# Path to alchemy
DJPEG="/usr/local/bin"			# Path to djpeg
GS="/usr/local/bin"			# Path to ghostscript
NETPBM="/usr/local/netpbm"		# Path to netpbm
TODOS="/usr/local/bin"			# Path to todos

# Helper Options
GSOPTS="-q -dSAFER -dNOPAUSE -r300"	# Ghostscript options
GSDEV="cdj550"				# Ghostscript device

# Printing Options
DISABLE_PRCAT="n"			# Disable STDIN input
DISABLE_MSGS="n"			# Disable all messages
DISABLE_EMSGS="n"			# Disable error messages
DISABLE_PRMSG="n"			# Disable prcat message

# Local Config
LOCALPRINTER="/dev/lp1"			# Printer device
SPECIAL_PRINT_COMMAND=""		# Special print command

# Network Config
ENABLE_NETWORK="y"			# Print over SMB
SMBCLIENT="/usr/local/samba/bin"	# Path to smbclient
PRINTSERV="PSERV"			# SMB printer host
PRINTSHARE="HP"				# SMB printer share
NUSER="PRINTER"				# SMB user name
NPASSWORD="NOT_SUPPORTED_YET"		# SMB password
SMBOPTS=""				# Special SMB options

# Debugging
DEBUG_PRINT_COMMAND=""			# Print command to use

#
# Supported Formats: txt, ps, gif, jpg, bmp, png, pcx, tiff
#

##############################################################################
# No User Servicable Parts Below:					     #
##############################################################################

FILE="$1"

GSOPTS="$GSOPTS -sDEVICE=$GSDEV"

if [ "$ENABLE_NETWORK" = "y" ]; then
  PCMD="$SMBCLIENT/smbclient \\\\\\\\$PRINTSERV\\\\$PRINTSHARE -U $NUSER -N -P $SMBOPTS"
fi

if [ ! "$ENABLE_NETWORK" = "y" ]; then
  PCMD="cat > $LOCALPRINTER"
  if [ ! "$SPECIAL_PRINT_COMMAND" = "" ]; then
    PCMD="$SPECIAL_PRINT_COMMAND"
  fi
fi

if [ ! "$DEBUG_PRINT_COMMAND" = "" ]; then
  PCMD="$DEBUG_PRINT_COMMAND"
fi

function printps () {
  if [ ! "$DISABLE_MSGS" = "y" ]; then
    echo "Printing Postscript..."
  fi
  (
    echo "print -"
    cat $FILE | $GS/gs $GSOPTS -sOutputFile=- -
  ) | $PCMD
}

function printtxt () {
  if [ ! "$DISABLE_MSGS" = "y" ]; then
    echo "Printing Text..."
  fi
  (
    echo "print -"
    cat $FILE | $TODOS/todos
    echo -n ""
  ) | $PCMD
}

function printgif () {
  if [ ! "$DISABLE_MSGS" = "y" ]; then
    echo "Printing `head -c 6 $FILE`..."
  fi
  (
    echo "print -"
    cat $FILE | $NETPBM/giftopnm | $NETPBM/pnmtops | $GS/gs $GSOPTS -sOutputFile=- -
  ) | $PCMD
}

function printjpg () {
  if [ ! "$DISABLE_MSGS" = "y" ]; then
    echo "Printing JPEG/JFIF..."
  fi
  (
    echo "print -"
    $DJPEG/djpeg -pnm $FILE | $NETPBM/pnmtops | $GS/gs $GSOPTS -sOutputFile=- -
  ) | $PCMD
}

function printbmp () {
  if [ ! "$DISABLE_MSGS" = "y" ]; then
    echo "Printing Bitmap..."
  fi
  (
    echo "print -"
    cat $FILE | $NETPBM/bmptoppm | $NETPBM/ppmtogif | $NETPBM/giftopnm | $NETPBM/pnmtops | $GS/gs $GSOPTS -sOutputFile=- -
  ) | $PCMD
}

function printpng () {
  if [ ! "$DISABLE_MSGS" = "y" ]; then
    echo "Printing PNG..."
  fi
  (
    echo "print -"
    TFILE=/tmp/pspr_`echo $FILE | sed "s/\///g" | tail -c 15 | head -c 10`.pcx
    $ALCHEMY/alchemy -p $FILE $TFILE
    $NETPBM/pcxtoppm $TFILE | $NETPBM/pnmtops | $GS/gs $GSOPTS -sOutputFile=- -
    rm -f $TFILE
  ) | $PCMD
}

function printpcx () {
  if [ ! "$DISABLE_MSGS" = "y" ]; then
    echo "Printing PCX..."
  fi
  (
    echo "print -"
    cat $FILE | $NETPBM/pcxtoppm | $NETPBM/pnmtops | $GS/gs $GSOPTS -sOutputFile=- -
  ) | $PCMD
}

function printtif () {
  if [ ! "$DISABLE_MSGS" = "y" ]; then
    echo "Printing TIFF..."
  fi
  (
    echo "print -"
    $NETPBM/tifftopnm $FILE | $NETPBM/pnmtops | $GS/gs $GSOPTS -sOutputFile=- -
  ) | $PCMD
}

function printit () {

if [ "$FILE" = "" ]; then
  if [ ! "$DISABLE_EMSGS" = "y" ]; then
    echo "$0 : no file specified!"
  fi
  exit 1
fi
if [ ! -e $FILE ]; then
  if [ ! "$DISABLE_EMSGS" = "y" ]; then
    echo "$0 : file not found."
  fi
  exit 1
fi
if [ "`head -c 2 < $FILE`" = "%!" ]; then
  printps
  exit 0
fi
if [ "`head -c 3 < $FILE`" = "%!" ]; then
  printps
  exit 0
fi
if [ "`head -c 3 < $FILE`" = "GIF" ]; then
  printgif
  exit 0
fi
if [ "`head -c 10 < $FILE | tail -c 4`" = "JFIF" ]; then
  printjpg
  exit 0
fi
if [ "`head -c 2 < $FILE`" = "BM" ]; then
  printbmp
  exit 0
fi
if [ "`head -c 4 < $FILE | tail -c 3`" = "PNG" ]; then
  printpng
  exit 0
fi
if [ "`echo $FILE | tail -c 4`" = "pcx" -o "`echo $FILE | tail -c 4`" = "PCX" ]; then
  printpcx
  exit 0
fi
if [ "`head -c 3 < $FILE`" = "II*" ]; then
  printtif
  exit 0
fi
if [ "`head -c 2 < $FILE`" = "MM" ]; then
  printtif
  exit 0
fi

printtxt
}

function prcatfn () {
  if [ ! "$DISABLE_PRMSG" = "y" -o ! "$DISABLE_MSGS" = "y" ]; then
    echo "Reading from standard input (hit ^C to abort or ^D to finish)"
  fi
  PRCATTMP=/tmp/prcat-tmp.`date +%s`
  cat > $PRCATTMP
  FILE=$PRCATTMP
  printit
  rm -f $PRCATTMP >& /dev/null
}

function showsig () {
  cat << EOF
FORMAT:		SIGNATURE:			DEPENDANCIES:
txt		default				todos
ps		%! or ^D%! (bytes 1-2, 1-3)	ghostscript
gif		GIF (bytes 1-3)			ghostscript,netpbm
jpg		JFIF (bytes 7-10)		ghostscript,netpbm,djpeg
bmp		BM (bytes 1-2)			ghostscript,netpbm
png		PNG (bytes 2-4)			ghostscript,netpbm,alchemy
pcx		.pcx or .PCX extention		ghostscript,netpbm
tiff		II* or MM (bytes 1-3, 1-2)	ghostscript,netpbm
EOF
  exit 0
}

function showhelp () {
  cat << EOF
ez-magic 1.0.5 - Do not redistribute modified versions of this program.
Usage: magic [filename]
(c)1997 Toby Reed for PorkNET Internet Systems (http://www.porknet.ml.org/)

ez-magic is an automatic printer filter for Linux systems. Invoke it with
"-s" for a list of supported formats, and their signatures/dependancies.
Run it with the filename as the first argument, or pipe data into it.

ez-magic is free software. Once free, now free, always free.
EOF
  exit 0
}

if [ ! "`echo $0 | grep prcat`" = "" -a ! "$DISABLE_PRCAT" = "y" ]; then
  prcatfn
  exit 0
fi
if [ "$1" = "" -a ! "$DISABLE_PRCAT" = "y" ]; then
  prcatfn
  exit 0
fi
if [ "$1" = "-s" -o "$1" = "-sig" ]; then
  showsig
  exit 0
fi
if [ "$1" = "-h" -o "$1" = "--help" ]; then
  showhelp
  exit 0
fi
printit
exit 0
