#!/usr/bin/perl # # prEnvelope.pl: Print out sender and recipient information to an envelope # Need a PostScript printer # usage: prEnvelope.pl [-f sender-info-file] [-ps | -pdf] \ # [country] # or: # prEnvelope.pl senderInfo # # $Id: prEnvelope.pl,v 1.4 2006/04/19 22:57:40 cvsremote Exp $ # # Sender Information # $SenderInfoFile = "$ENV{HOME}/.prEnvelope.pl"; $SenderName = "Sender Name"; $SenderStreet = "Sender Street"; $SenderCity = "Sender City"; $SenderState = "Sender State"; $SenderZip = "Sender ZIP"; $SenderCountry = "Sender Country"; # Current Time # ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) # 0 1 2 3 4 5 6 7 8 # @CurrTime = localtime(time); # Flag whether to create a ps file (default: no) # $isPs = 0; $isPdf = 0; @OutPdf = ( '%%BoundingBox: 261 331 424 709', '711 245 TR', '711 NE 245 NE TR', '567 609 TR', '567 NE 609 NE TR', '519 609 TR', '519 NE 609 NE TR', '488 609 TR', '488 NE 609 NE TR', '456 609 TR', '456 NE 609 NE TR', '643 245 TR', '643 NE 245 NE TR' ); @OutPs = ( '%%BoundingBox: 250 369 424 763', '711 149 TR', '711 NE 149 NE TR', '559 542 TR', '559 NE 542 NE TR', '503 542 TR', '503 NE 542 NE TR', '468 542 TR', '468 NE 542 NE TR', '436 542 TR', '436 NE 542 NE TR', '643 149 TR', '643 NE 149 NE TR' ); ### ### main ### mkSenderFile() if ($#ARGV == 0); if ($#ARGV < 4) { &usage(); } getopts: while ($ARGV[0] =~ /^-/) { if ($ARGV[0] eq '-f') { shift; $SenderInfoFile = $ARGV[0] if (-f $ARGV[0]); shift; next getopts; } if ($ARGV[0] eq '-ps') { die "-ps and -pdf cannot coexist." if ($isPdf); $isPs = 1; shift; } if ($ARGV[0] eq '-pdf') { die "-ps and -pdf cannot coexist." if ($isPs); $isPdf = 1; shift; } } if (-f $SenderInfoFile) { open(FILE, "$SenderInfoFile") || die "Can't open $SenderInfoFile.\n"; while() { eval $_; } close(FILE); } printEnvelope($isPs, $isPdf, @ARGV); sub usage { my($msg) = "prEnvelope.pl [-f sender-info-file] [-ps | -pdf] "; $msg .= " [country]"; $msg .= "\n\nor:\n\n"; $msg .= "prEnvelope.pl senderInfo\n"; printf(STDERR "$msg\n"); exit(1); } sub printEnvelope { my($isPs, $isPdf) = @_; shift; shift; ($name, $street, $city, $state, $zip, $country) = @_; my($fh); my($date) = sprintf("%s %s %d %02d:%02d:%02d %d", wday($CurrTime[6]), mname($CurrTime[4]), $CurrTime[3], $CurrTime[2], $CurrTime[1], $CurrTime[0], $CurrTime[5] + 1900); my(@line); my($psFile) = "${name}.ps"; $psFile =~ s/ /-/g; @line = $isPdf ? @OutPdf : @OutPs; # printf("(%d) %s: %s %s, %s %s %s\n", # $isPs, $name, $street, $city, $state, $zip, $country); if ($isPs || $isPdf) { open($fh, "> $psFile") || die "can't open $psFile.\n"; } else { open($fh, "|lpr") || die "can't pipe to lpr\n"; } print $fh <<__EOF__; %!PS-Adobe-3.0 $line[0] %%Title: $psFile %%CreationDate: $date %%Creator: mkenvelope.pl written by Yoichi Hariguchi (dad\@aya.us) %%ProducedBy: (unknown) %%Orientation: Portrait %%Pages: 1 %%DocumentFonts: (atend) %%EndComments %%BeginProlog /prenvelopedict 51 dict def prenvelopedict begin /TGMAX { exch dup 3 1 roll exch dup 3 1 roll gt { pop } { exch pop } ifelse } def /TGMIN { exch dup 3 1 roll exch dup 3 1 roll lt { pop } { exch pop } ifelse } def /TGSW { stringwidth pop } def /bd { bind def } bind def /GS { gsave } bd /GR { grestore } bd /NP { newpath } bd /CP { closepath } bd /CHP { charpath } bd /CT { curveto } bd /L { lineto } bd /RL { rlineto } bd /M { moveto } bd /RM { rmoveto } bd /S { stroke } bd /F { fill } bd /TR { translate } bd /RO { rotate } bd /SC { scale } bd /MU { mul } bd /DI { div } bd /DU { dup } bd /NE { neg } bd /AD { add } bd /SU { sub } bd /PO { pop } bd /EX { exch } bd /CO { concat } bd /CL { clip } bd /EC { eoclip } bd /EF { eofill } bd /IM { image } bd /IMM { imagemask } bd /ARY { array } bd /SG { setgray } bd /RG { setrgbcolor } bd /SD { setdash } bd /W { setlinewidth } bd /SM { setmiterlimit } bd /SLC { setlinecap } bd /SLJ { setlinejoin } bd /SH { show } bd /FF { findfont } bd /MS { makefont setfont } bd /AR { arcto 4 {pop} repeat } bd /CURP { currentpoint } bd /FLAT { flattenpath strokepath clip newpath } bd /TGSM { prenvelopeorigctm setmatrix } def /TGRM { savematrix setmatrix } def end %%EndProlog %%Page: 1 1 prenvelopedict begin /prenvelopesavedpage save def 1 SM 1 W 0 SG 72 0 MU 72 11 MU TR 72 128 DI 100.000 MU 100 DI DU NE SC GS /prenvelopeorigctm matrix currentmatrix def % TEXT NP 0 SG GS $line[1] [0.000 1.000 -1.000 0.000 40 -95] CO $line[2] GR GS 1 W $line[1] [0.000 1.000 -1.000 0.000 40 -95] CO 0 18 M GS 0 SG /Times-Roman FF [20 0 0 -20 0 0] MS ($SenderName) SH GR 0 22 RM GS 0 SG /Times-Roman FF [20 0 0 -20 0 0] MS ($SenderStreet) SH GR 0 22 RM GS 0 SG /Times-Roman FF [20 0 0 -20 0 0] MS ($SenderCity, $SenderState $SenderZip) SH GR GR % TEXT NP 0 SG GS $line[3] [0.000 1.000 -1.000 0.000 40 -95] CO $line[4] GR GS 1 W $line[3] [0.000 1.000 -1.000 0.000 40 -95] CO 0 28 M GS 0 SG /Times-Roman FF [32 0 0 -32 0 0] MS ($name) SH GR GR % TEXT NP 0 SG GS $line[5] [0.000 1.000 -1.000 0.000 40 -95] CO $line[6] GR GS 1 W $line[5] [0.000 1.000 -1.000 0.000 40 -95] CO 0 22 M GS 0 SG /Times-Roman FF [24 0 0 -24 0 0] MS ($street) SH GR GR % TEXT NP 0 SG GS $line[7] [0.000 1.000 -1.000 0.000 40 -95] CO $line[8] GR GS 1 W $line[7] [0.000 1.000 -1.000 0.000 40 -95] CO 0 22 M GS 0 SG /Times-Roman FF [24 0 0 -24 0 0] MS ($city, $state $zip) SH GR GR __EOF__ if ($country) { print $fh <<__EOF__; % TEXT NP 0 SG GS $line[9] [0.000 1.000 -1.000 0.000 40 -95] CO $line[10] GR GS 1 W $line[9] [0.000 1.000 -1.000 0.000 40 -95] CO 0 22 M GS 0 SG /Times-Roman FF [24 0 0 -24 0 0] MS ($country) DU TGSW EX SH GS CURP M 0 2 RM NE 0 RL S GR GR GR % TEXT NP 0 SG GS $line[11] [0.000 1.000 -1.000 0.000 40 -95] CO $line[12] GR GS 1 W $line[11] [0.000 1.000 -1.000 0.000 40 -95] CO 0 18 M GS 0 SG /Times-Roman FF [20 0 0 -20 0 0] MS ($SenderCountry) DU TGSW EX SH GS CURP M 0 2 RM NE 0 RL S GR GR GR __EOF__ } print $fh <<__EOF__; GR prenvelopesavedpage restore end showpage %%Trailer %MatchingCreationDate: $date %%DocumentFonts: Times-Roman %%EOF __EOF__ close($fh); if ($isPdf) { if (system("ps2pdf $psFile $pdfFile")) { printf(STDERR "ps2pdf %s %s failed.\n", $psFile, $pdfFile); unlink($psFile); exit(1); } unlink($psFile); } exit(0); } sub mkSenderFile { my($fh, $i); my($input); my(@input); my(@msg); my($msg) = "Thie program creates \"$SenderInfoFile\", " . "a default sender information file.\n" . "Answer the following questions.\n\n" . ""; print $msg; if (-f $SenderInfoFile) { print STDERR "$SenderInfoFile already exists. Overwrite? (y/n): "; $input = ; exit(0) unless ($input =~ /^y/i); open($fh, "$SenderInfoFile") || die "can't open $SenderInfoFile"; while(<$fh>) { eval($_); } $input[0] = $SenderName; $input[1] = $SenderStreet; $input[2] = $SenderCity; $input[3] = $SenderState; $input[4] = $SenderZip; $input[5] = $SenderCountry; $msg[0] = sprintf("Your Name (default: %-20s): ", $input[0]); $msg[1] = sprintf("Street (default: %-20s): ", $input[1]); $msg[2] = sprintf("City (default: %-20s): ", $input[2]); $msg[3] = sprintf("State (default: %-20s): ", $input[3]); $msg[4] = sprintf("Zip (default: %-20s): ", $input[4]); $msg[5] = sprintf("Country (default: %-20s): ", $input[5]); } else { $input[5] = "USA"; $msg[0] = "Your Name: "; $msg[1] = "Street (e.g., \`1600 Pennsylvania Ave. NW\'): "; $msg[2] = "City (e.g., \`Washington\'): "; $msg[3] = "State (e.g., \`DC\'): "; $msg[4] = "Zip (e.g., \`20500\'): "; $msg[5] = "Country (default: $input[5]): "; } loop: while(1) { for ($i = 0; $i <= $#msg; ++$i) { print STDERR $msg[$i]; $input = ; chop($input); $input[$i] = $input unless ($input eq ''); } print STDERR "Default sender information is:\n\n" . " $input[0]\n" . " $input[1]\n" . " $input[2], $input[3] $input[4]\n" . " $input[5]\n\n" . "Is it correct? (y/n): "; $input = ; last loop unless ($input =~ /^n/i); $msg[0] = sprintf("Your Name (default: %-20s): ", $input[0]); $msg[1] = sprintf("Street (default: %-20s): ", $input[1]); $msg[2] = sprintf("City (default: %-20s): ", $input[2]); $msg[3] = sprintf("State (default: %-20s): ", $input[3]); $msg[4] = sprintf("Zip (default: %-20s): ", $input[4]); $msg[5] = sprintf("Country (default: %-20s): ", $input[5]); } open($fh, "> $SenderInfoFile") || die "can't open $SenderInfoFile."; print $fh <<__EOF__; # # Default Sender Information for prEnvelope.pl # \$SenderName = \'$input[0]\'; \$SenderStreet = \'$input[1]\'; \$SenderCity = \'$input[2]\'; \$SenderState = \'$input[3]\'; \$SenderZip = \'$input[4]\'; \$SenderCountry = \'$input[5]\'; __EOF__ close($fh); exit(0); } sub wday { return "Sun" if ($_[0] == 0); return "Mon" if ($_[0] == 1); return "Tue" if ($_[0] == 2); return "Wed" if ($_[0] == 3); return "Thu" if ($_[0] == 4); return "Fri" if ($_[0] == 5); return "Sat" if ($_[0] == 6); return ""; } sub mname { return "Jan" if ($_[0] == 0); return "Feb" if ($_[0] == 1); return "Mar" if ($_[0] == 2); return "Apr" if ($_[0] == 3); return "May" if ($_[0] == 4); return "Jun" if ($_[0] == 5); return "Jul" if ($_[0] == 6); return "Aug" if ($_[0] == 7); return "Sep" if ($_[0] == 8); return "Oct" if ($_[0] == 9); return "Nov" if ($_[0] == 10); return "Dec" if ($_[0] == 11); return ""; }