#!/bin/ksh # # $Id: faxcover,v 1.9 2002/01/07 19:23:54 vogelke Exp $ # # NAME: # faxcover # # SYNOPSIS: # faxcover [-n pages] [-d date] [-e your-email-addr] # [-f fax-phone] [-o your-organization] [-P printer] # [-s sender-fullname] [-p voice-phone] recipient # faxcover [-hv] # # DESCRIPTION: # Sends PostScript fax coversheet to stdout. Inputs are taken # from either the command line or the file $HOME/.faxcover, if # it exists. Command-line args will overwrite any other options. # # OPTIONS: # recipient holds the person to whom the FAX is addressed; # the only required option. # # -n pages number of pages, not including coversheet. # -d date holds a cover-sheet date, like "tomorrow". # Used as an argument to the date command. # -P printer destination printer; default printer will be # used if one is not specified. # # -h, --help print this message # -v, --version print the version and exit # # The remaining options are self-explanatory. # # EXAMPLE: # faxcover -n 1 'John Doe' | lpr # Prints a sheet for "John Doe", 1 page plus cover. # # AUTHOR: # Based on Free Software Foundation configure scripts. # # Karl Vogel # Sumaria Systems, Inc. PATH=/bin:/usr/sbin:/usr/bin:/usr/local/bin export PATH umask 022 tag=`basename $0` tmp=/tmp/faxcover.tmp.$$ trap "rm -f $tmp; exit 1" ERR # ksh variants only # ======================== FUNCTIONS ============================= # # logmsg: prints a string to the system log. logmsg () { logger -t $tag "$*" } # die: prints an optional argument to stderr and exits. # warn: prints an optional argument to stderr. # A common use for "die" is with a test: # test -f /etc/passwd || die "no passwd file" # This works in subshells and loops, but may not exit with # a code other than 0. die () { echo "$tag: error: $*" 1>&2 exit 1 } warn () { echo "$tag: warning: $*" 1>&2 } # checkvar: reject invalid shell variable names. checkvar () { case "`echo $1 | sed 's/[-a-zA-Z0-9_]//g'`" in "") ;; *) die "$1: invalid feature name" ;; esac } # usage: prints an optional string plus part of the comment # header (if any) to stderr, and exits with code 1. usage () { lines=`egrep -n '^# (NAME|AUTHOR)' $0 | sed -e 's/:.*//'` ( case "$#" in 0) ;; *) echo "usage error: $*"; echo ;; esac case "$lines" in "") ;; *) set `echo $lines | sed -e 's/ /,/'` sed -n ${1}p $0 | sed -e 's/^#//g' | egrep -v AUTHOR: ;; esac ) 1>&2 exit 1 } # version: prints the current version to stdout. version () { lsedscr='s/RCSfile: // s/.Date: // s/,v . .Revision: / v/ s/\$//g' lrevno='$RCSfile: faxcover,v $ $Revision: 1.9 $' lrevdate='$Date: 2002/01/07 19:23:54 $' echo "$lrevno $lrevdate" | sed -e "$lsedscr" exit 0 } # defaults: prints default values email="$USER" date=`date` faxphone="937-255-7465" org="$ORGANIZATION" pcount=1 sender="$FULLNAME" voicephone="937-255-3688" printer="$PRINTER" test -f $HOME/.faxcover && . $HOME/.faxcover defaults () { cat << EOF CURRENT VALUES: date=$date email=$email faxphone=$faxphone org=$org pcount=$pcount plus coversheet sender=$sender voicephone=$voicephone printer=$printer EOF } # ======================== MAIN PROGRAM ========================== # Defaults: ac_prev= ac_invalid="invalid option; use --help to show usage" argv= # Initialize some variables set by options. prefix=/usr/local silent=no verbose=no enable_abc=yes for ac_option do # If the previous option needs an argument, assign it. case "$ac_prev" in "") ;; *) eval "$ac_prev=\$ac_option"; ac_prev=; continue ;; esac case "$ac_option" in -*=*) ac_optarg=`echo "$ac_option" | sed 's/[-_a-zA-Z0-9]*=//'` ;; *) ac_optarg= ;; esac # main switch case "$ac_option" in -h | -help | --help | --hel | --he) defaults; usage ;; -v | -version | --version | --versio |\ --versi | --vers) version ;; -d) case "$ac_optarg" in tomorrow) str="1 day" ;; yesterday) str="1 day ago" ;; *) str="$ac_optarg" ;; esac date=`date -d "$str" 2> /dev/null` ;; -e) ac_prev=email ;; -f) ac_prev=faxphone ;; -n) ac_prev=pcount ;; -o) ac_prev=org ;; -p) ac_prev=voicephone ;; -P) ac_prev=printer ;; -s) ac_prev=sender ;; -*) die "$ac_option: $ac_invalid" ;; *) argv="$argv $ac_option" ;; esac done case "$ac_prev" in "") ;; *) die "missing arg to --`echo $ac_prev | sed 's/_/-/g'`" ;; esac case "$argv" in "") usage "I need a recipient"; exit 1 ;; *) to="$argv" ;; esac # # Put the date in ARPA format. # case "$date" in "") date=`date` ;; esac set X $date date="$2, $4 $3 $7" # # Do replacements, write coversheet to printer. # # @DATE@ Today's date. # @EMAIL@ Email address of fax sender. # @FAXPHONE@ Where to fax a reply to. # @ORG@ Organization of fax sender. # @PCOUNT@ # of pages in the message, including coversheet. # @PSTR@ "page" or "pages", depending on PCOUNT. # @RECIPIENT@ Name of fax recipient. # @SENDER@ Name of fax sender. # @VOICEPHONE@ Regular (voice) phone number of fax sender. # template="/usr/local/lib/fax.cover" test -f $template || die "$template: not found" case "$pcount" in 1) pstr="page" ;; *) pstr="pages" ;; esac script=" s!@DATE@!$date!g s!@EMAIL@!$email!g s!@FAXPHONE@!$faxphone!g s!@ORG@!$org!g s!@PCOUNT@!$pcount!g s!@PSTR@!$pstr!g s!@RECIPIENT@!$to!g s!@SENDER@!$sender!g s!@VOICEPHONE@!$voicephone!g " sed -e "$script" < $template exit 0