#!/bin/sh
#----------------------------------------------------------------------------
#  Name:     xdprint
#  Project:  Xdiary
#
#  Description:
#    Example of a script file to print entries in XDiary. This file
#    can be modified to suit your needs.
#
#    The parameters passed to this script are as follows:
#
#      <entries>     - File containing the entries to print. This file
#                      should be removed afterwards.
#
#      <printerName> - Printer name.
#
#      <printerType> - Printer type.
#
#      <style>       - Style to use:
#                        EntryList
#                        WeekList
#                        CompWeekList
#                        DayList
#                        CompDayList
#                        MonthList
#
#      <output>      - Send output to:
#                        Printer     - printer
#                        File        - file
#                        PrinterFile - both printer and file
#
#      <paper size>  - The requested paper size:
#                        A3
#                        A4
#                        A5
#                        Letter
#                        Legal
#
#      <page/sheet> - Number of pages to print per sheet:
#                        1
#                        2
#
#    This script must use these parameters to pass the output through
#    the correct filters (for example to produce A5 output).
#
#  Filename:         xdprint.script
#
#  Authors:          Roger Larsson, Ulrika Bornetun
#  Creation date:    1991-04-28
#
#
#  (C) Copyright Ulrika Bornetun, Roger Larsson (1995)
#      All rights reserved
#
#  Permission to use, copy, modify, and distribute this software and its
#  documentation for any purpose and without fee is hereby granted,
#  provided that the above copyright notice appear in all copies. Ulrika
#  Bornetun and Roger Larsson make no representations about the usability
#  of this software for any purpose. It is provided "as is" without express
#  or implied warranty.
#----------------------------------------------------------------------------
#
# SCCSID = @(#) Module: xdprint.script, Version: 1.1, Date: 95/02/18 14:15:15

program=xdprint

CAT=/bin/cat
CD=cd
MV=/bin/mv
RM=/bin/rm


# We must have XDHOME.
if [ ! -n "$XDHOME" ]; then
  echo "$program: The environment variable XDHOME is not defined."
  exit 1
fi

# Temporary files to use.
outFile=$HOME/XDiary.out
prTmpFile=$HOME/.XDiary.print_tmp


# Get the parameters.
dbFile="$1"
shift
printerName="$1"
shift
printerType="$1"
shift
style="$1"
shift
outDest="$1"
shift
paperSize="$1"
shift
sheetsPerPage="$1"
shift

# Rename the base file.
if [ ! -r $dbFile ]; then
  echo "$program: The file $dbFile does not exist!"
  exit 1
fi

$MV $dbFile ${dbFile}_$$
dbFile=${dbFile}_$$

# How do we do printing?
if [ $outDest != "File" ]; then
  printCommand="lp -d$printerName"
fi

twoColumn="false"

case $printerType in
  ASCII|Ascii|ascii)
    printerType="ascii"
    style="Ascii"
    ;;

  PS|Ps|ps)
    printerType="ps"
    ;;

  *)
    echo "$program: Not a valid printer type."
    exit 1
    ;;
esac


# Format the data to print.
>$prTmpFile

case $printerType in
  "ascii")
    ( echo "XDiary Printout"                      
      echo "---------------"                      
      echo ""                                     
      echo "For user:   `whoami`"
      echo "Calendar:   $calName"
      echo "Dates:      $dateRange"
      echo ""
      echo "Print date: `date +'%a, %h %d 19%y %T'`"
      echo ""                                     
    ) >>$prTmpFile
    ;;

  "ps")
    ( echo "%!PS-Adobe-1.0"
      echo "%%DocumentFonts: Courier Helvetica"
      echo "%%+ Helvetica-Bold Helvetica-BoldOblique"
      echo "%%Title: XDiary_Print"
      echo "%%Creator: XDiary"
      echo "%%CreationDate: `date`"
      echo "%%For: `whoami`"
      echo "%%EndComments"
      echo ""
    ) >>$prTmpFile
    ;;
esac

# Filter the output.
xdprformat -paper $paperSize -prologue $XDHOME \
           -style $style -sheets $sheetsPerPage \
           $dbFile >>$prTmpFile

if [ $? -ne 0 ]; then
  $RM -f $dbFile $prTmpFile 2>/dev/null
  exit 1
fi

# Send the output to the printer.
case $outDest in
  Printer)
    $CAT $prTmpFile | $printCommand >/dev/null
    ;;
  File)
    $MV $prTmpFile $outFile
    ;;
  PrinterFile)
    $MV $prTmpFile $outFile
    $CAT $outFile | $printCommand >/dev/null
    ;;
esac

# Clean up.
$RM -f $dbFile $prTmpFile 2>/dev/null

exit 0
