#!/bin/ksh # # $Id: mkmail,v 1.7 2001/05/18 21:03:28 vogelke Exp $ # # NAME: # mkmail # # SYNOPSIS: # mkmail [-hmv] [--help] [--version] [-s subject] [-t to] [file] # # DESCRIPTION: # Read one or more files, add a mail header, write to stdout. # Relies on GNU date. # # OPTIONS: # -h, --help print this message # -v, --version print the version and exit # -s specifies subject # -t specifies recipient # -m mail to myself # # EXAMPLE: # mkmail # # AUTHOR: # Based on Free Software Foundation configure scripts. # # Karl Vogel # Sumaria Systems, Inc. PATH=/usr/local/bin:/bin:/usr/bin:/usr/sbin:/usr/ucb export PATH tag=`basename $0` # ======================== 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: mkmail,v $ $Revision: 1.7 $' lrevdate='$Date: 2001/05/18 21:03:28 $' echo "$lrevno $lrevdate" | sed -e "$lsedscr" exit 0 } # fqdn: call hostname, domainname fqdn () { h=`hostname` case "$h" in *.*) echo $h ;; *) d=`domainname`; echo "$h.$d" ;; esac } # ======================== MAIN PROGRAM ========================== # Defaults: ac_prev= ac_invalid="invalid option; use --help to show usage" argv= # Initialize some variables set by options. s="Your subject here" t=nobody@nowhere.com 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) usage ;; -m) t=myself ;; -s) ac_prev=s ;; -t) ac_prev=t ;; -v | -version | --version | --versio |\ --versi | --vers) version ;; -*) 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 # # Real work starts here. Test for specific features. # sub="$s" to="$t" # # fdate = from-line date # adate = ARPA-style date # fhost = fully-qualified name of my host # case "$argv" in "") list="-" ;; *) list="$argv" ;; esac case "$MAILHOST" in "") fhost=`fqdn` ;; *) fhost="$MAILHOST" ;; esac case "$MAILUSER" in "") myname=`whoami` ;; *) myname="$MAILUSER" ;; esac fdate=`gdate "+%a %b %d %T %Y"` adate=`gdate --rfc-2822` case "$to" in myself) to="$myname@`fqdn`" ;; *) ;; esac # # Print results. # cat < Date: $adate `msgid -d $tag` From: $myname@$fhost To: $to Subject: $sub EndHeader cat $list echo exit 0