#!/usr/bin/perl

# Copyright 1993, 1999 Frank Adelstein.  All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this
# software is hereby granted without fee, provided that
# the copyright notice and permission notice are not removed.
#
# --FNA 8/29/93

# a voyage into Perl and termcaps.  Let's see what we can do.
# Initial termcap stuff shamelessly stolen from Tom Fine.
#
# This is based on my 'ab' (addressbook) program.  It reads
# in a file in the ab format and then generates postscript
# output to standard output whihch is a hardcopy of the addressbook.
# My online addressbook is what is current, but I've been caught
# too many times without computer access.
#
# --FNA 11/04/99

$psfile = "/home/frank/bin/ab.ps";

#deal with the options
#
do 'getopts.pl' || die "can't find YAGRIP library, stopped";
&Getopts("f:bc") || die ("usage: $0 [-f addressfile] \n");
if (defined($opt_f)) {
  $abfile = $opt_f;
} else {
  $abfile = $ENV{'HOME'} . "/.addresses";
}

&readdata;
&writepostscript;
exit;

sub readdata {
  if (! -e $abfile) { 
    #file doesn't exist
    exit;
  }
  if (!open(ADDRESSES, $abfile)) {
    printf ("Error: can't open address file for read.");
    exit;
  }
  $addindex = 0;
  while (<ADDRESSES>) {
    chop;
    # drop anything in parentheses
    s/\(.*\)//;
    $name[$addindex] = $_;
    $number1[$addindex] = <ADDRESSES>;
    $number2[$addindex] = <ADDRESSES>;
    $address1[$addindex] = <ADDRESSES>;
    $address2[$addindex] = <ADDRESSES>;
    $address3[$addindex] = <ADDRESSES>;
    $birthday[$addindex] = <ADDRESSES>;
    $comment1[$addindex] = <ADDRESSES>;
    $comment2[$addindex] = <ADDRESSES>;
    $comment3[$addindex] = <ADDRESSES>;
    $email[$addindex] = <ADDRESSES>;
    chop $number1[$addindex];
    chop $number2[$addindex];
    chop $address1[$addindex];
    chop $address2[$addindex];
    chop $address3[$addindex];
    chop $birthday[$addindex];
    chop $comment1[$addindex];
    chop $comment2[$addindex];
    chop $comment3[$addindex];
    chop $email[$addindex];
    $addindex++;
  }
  close (ADDRESSES);
}

#
# Now this subroutine is a little more complex because it
# writes postscript in "signature" style, which is printing
# double-sided, two pages per side, so four pages per sheet.
# It also orders things so that it'll come out properly.
#

sub writepostscript {

  # we're actually starting on the backside of the sheet
  $sheetnum = 2;
  $page = 1;
  $box = 1;
  $startletter = "";

  # maximum number of pages (given 4 per sheet) equals:
  # number of pages (divide by entries per page and add one)
  # then number of sheets (divide by sheets per page and add one)
  # then back to number of pages (multiply by sheets per page)
  $maxpage = (int ( int(($#name-1) / 4) / 4) + 1) * 4;
  $maxup = int(($#name-1) / 4) + 1;

  # two counters, one will go down, the other up
  $cpdn = int($maxpage / 2) - 1;
  $cpup = $cpdn + 1;

print STDERR "max pages is $maxup, max pages for sheets is $maxpage\n"; 
print STDERR "starting at $cpdn and $cpup\n";

  # print out the postscript header file
  system ("cat $psfile");

#  for ($i = 0; $i < $addindex; $i++) {
  while ($cpdn >= 0 || $box != 1) {
    $i++;
    if ($box == 1) {
      # at the top of the page, figure out the index du jour
      if ($sheetnum % 2) {
        if ($page == 1) {
          # left justified sheet, left side
print STDERR "printing page $cpup\n";
          $i = 4 * $cpup++;
          if ($cpup > $maxup) {
            # not enough pages, leave it blank
            $i = -1;
            $box = 4;
          }
        } else {
          # left justified sheet, right side
print STDERR "printing page $cpdn\n";
          $i = 4 * $cpdn--;
        }
      } else { 
        if ($page == 1) {
          # right justified sheet, left side
print STDERR "printing page $cpdn\n";
          $i = 4 * $cpdn--;
        } else {
          # right justified sheet, right side
print STDERR "printing page $cpup\n";
          $i = 4 * $cpup++;
          if ($cpup > $maxup) {
            # not enough pages, leave it blank
            $i = -1;
            $box = 4;
          }
        }
      }
      $temp = $name[$i];
      # for indexing, drop anything after an ampersand (&)
      $temp =~ s/ \&.*$//g;
      $temp =~ m/\s(\w)\w+$/;
      $startletter = $1;
    }
    if ($i != -1) {
      print "$page $box 1 ($name[$i]) drawaddress \n";
      print "$page $box 1 ($number1[$i]) drawphone \n";
      print "$page $box 2 ($number2[$i]) drawphone \n"; 
      print "$page $box 2 ($address1[$i]) drawaddress \n";
      print "$page $box 3 ($address2[$i] $address3[$i]) drawaddress \n";
    }
    $box++;
    if ($box == 5) {
      $box = 1;
      $temp = $name[$i];
      # for indexing, drop anything after an ampersand (&)
      $temp =~ s/ \&.*$//g;
      # find the first letter of the last word
      $temp =~ m/\s(\w)\w+$/;
      if ($i != -1) {
        print "$page 1 ($startletter - $1) drawindex\n";
      }
      if ($page == 1) {
       $page = 2;
      } else {
        print "showpage\n";
        # handle the funky odd/even sheet thing
        # technically, the mod 2 operation is out of sync
        # but it's OK, because we increment $sheetnum above first
        if ($sheetnum % 2) {
          # just a little offset to keep it away from the very edge
          print "-30 20 translate\n";
          print "612 x sub 0 translate\n";
          print "612 0 translate\n";
          print "0.35 0.35 scale\n";
          print "-612 0 translate\n";
        } else {
          # just a little offset to keep it away from the very edge
          #print "21 22 translate\n";
          #print "40 18 translate\n";
          print "45 13 translate\n";
          print "0.35 0.35 scale\n";
        }
        print "drawpage\n";
        $page = 1;
        $sheetnum++;
      }
    }
  }
  print "showpage\n";
}

