#!/bin/sh
#******************************************************************
#ident fcp2fig 0.5 Time-stamp: <97/05/20 11:50:39 bav>
#******************************************************************
#                           Gerd Bavendiek  bav@rw.sni.de  97-05-10
#
# Script used for generating a fax cover page from a xfig-file.
#
# Usage: fcp2fig <fcp-file>
#
# fcp stands for: "fax cover page". My fcp-file contains settings like
#
# FCP_R_NAME Mr. Foo Bar
# FCP_R_FAX_NO 001 600 12345
#
# and so on.
#
# The xfig-file contains a generic fax cover page with placeholders
# like FCP_R_NAME. You got the idea :-)
# Depending on the settings in the fcp-file this script generates an
# actual cover page xfig-file. Using fig2dev we get postscript, which
# in turn can be feed to e.g. PalMail.
#
# There are three specials:
#
# fcp2fig takes everything with FCP_ as a variablename.
#
# fcp2fig looks for something like
#
# FCP_COVER_FILE /home/user/fax/cover-business.fig
#
# Text after a single line containing FCP_MEMO_START and before
# another single line containing FCP_MEMO_STOP will be treated as a
# memo-text. 
#
# This is quite an ugly hack. It was born mainly between 4 am and 5 am
# due to sleeplessness ...
#
# There will be problems esp. with values containing "'" or ")" e.g.
#-------------------------------------------------------------------
# Some legal stuff ...
#
# Copyright (C) 1997 Gerd Bavendiek <bav@rw.sni.de>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#-------------------------------------------------------------------

if [ $# -lt 1 ]; then
  echo "Usage: fcp2fig <fcp-file>"
  exit 1
fi
FCP_FILE=$1
if [ ! -r $FCP_FILE ]; then
  echo "fcp2fig: No such fcp-file: $FCP_FILE"
  exit 1
fi
FCP_COVER_FILE=`awk '/FCP_COVER_FILE/ { print $2 }' $FCP_FILE`
if [ -z "$FCP_COVER_FILE" ]; then
  echo "fcp2fig: You must specify FCP_COVER_FILE !"
  exit
fi
if [ ! -r $FCP_COVER_FILE ]; then
  echo "fcp2fig: No such xfig-file: $FCP_COVER_FILE"
  exit 1
fi

# The name of the fax 
FAX_NAME=`basename $FCP_FILE .fcp`
FCP_AWK_FILE=/tmp/${FAX_NAME}.awk
FCP_FIG_FILE=/tmp/${FAX_NAME}.fig
FAX_PS_FILE=${FAX_NAME}.ps

# Step 1: Generate a awk-file, containing the substitute-commands
awk '
/FCP_/ { WERT=substr($0, length($1)+2)
         # Do not touch, if there has been given no value
         if ( WERT != "" ) printf "/%s/ { gsub(\"%s\",\"%s\"); print; next }\n", $1, $1, WERT
       }
/FCP_MEMO_START/, /FCP_MEMO_STOP/ {
         if ( $1 !~ /FCP_MEMO_ST/)
         { 	
            i_memo++;
            if ( i_memo <= 9 ) VARIABLE="FCP_MEMO__"i_memo
            else
               VARIABLE="FCP_MEMO_"i_memo
            if ( $0 != "" ) printf "/%s/ { gsub(\"%s\",\"%s\"); print; next }\n", VARIABLE, VARIABLE, $0
         }
       }
END { printf "{print}\n" }
' $FCP_FILE > $FCP_AWK_FILE

# Step 2: substitute and
# Step 3: delete not assigned variables
awk -f $FCP_AWK_FILE $FCP_COVER_FILE  | sed '/FCP_/d' > $FCP_FIG_FILE

# Step 4: Make postscript  
fig2dev -L ps -P -z A4 $FCP_FIG_FILE $FAX_PS_FILE

# No longer used
rm -f $FCP_AWK_FILE $FCP_FIG_FILE


