1151497Sru#! /bin/sh
2151497Sru#
3151497Sru# grap2graph -- compile graph description descriptions to bitmap images
4151497Sru#
5151497Sru# by Eric S. Raymond <esr@thyrsus.com>, May 2003
6151497Sru#
7151497Sru# In Unixland, the magic is in knowing what to string together...
8151497Sru#
9151497Sru# Take grap description on stdin, emit cropped bitmap on stdout.
10151497Sru# The pic markup should *not* be wrapped in .G1/.G2, this script will do that.
11151497Sru# A -U option on the command line enables gpic/groff "unsafe" mode.
12151497Sru# A -format FOO option changes the image output format to any format
13151497Sru# supported by convert(1).  All other options are passed to convert(1).
14151497Sru# The default format is PNG.
15151497Sru#
16151497Sru
17151497Sru# Requires the groff suite and the ImageMagick tools.  Both are open source.
18151497Sru# This code is released to the public domain.
19151497Sru#
20151497Sru# Here are the assumptions behind the option processing:
21151497Sru#
22151497Sru# 1. None of the options of grap(1) are relevant.
23151497Sru#
24151497Sru# 2. Only the -U option of groff(1) is relevant.
25151497Sru#
26151497Sru# 3. Many options of convert(1) are potentially relevant, (especially 
27151497Sru# -density, -interlace, -transparency, -border, and -comment).
28151497Sru#
29151497Sru# Thus, we pass -U to groff(1), and everything else to convert(1).
30151497Sru#
31151497Sru# $Id: grap2graph.sh,v 1.4 2005/05/18 07:03:06 wl Exp $
32151497Sru#
33151497Srugroff_opts=""
34151497Sruconvert_opts=""
35151497Sruformat="png"
36151497Sru
37151497Sruwhile [ "$1" ]
38151497Srudo
39151497Sru    case $1 in
40151497Sru    -unsafe)
41151497Sru	groff_opts="-U";;
42151497Sru    -format)
43151497Sru	format=$2
44151497Sru	shift;;
45151497Sru    -v | --version)
46151497Sru	echo "GNU grap2graph (groff) version @VERSION@"
47151497Sru	exit 0;;
48151497Sru    --help)
49151497Sru	echo "usage: grap2graph [ option ...] < in > out"
50151497Sru	exit 0;;
51151497Sru    *)
52151497Sru	convert_opts="$convert_opts $1";;
53151497Sru    esac
54151497Sru    shift
55151497Srudone
56151497Sru
57151497Sru# create temporary directory
58151497Srutmp=
59151497Srufor d in "$GROFF_TMPDIR" "$TMPDIR" "$TMP" "$TEMP" /tmp; do
60151497Sru    test -z "$d" && continue
61151497Sru
62151497Sru    tmp=`(umask 077 && mktemp -d -q "$d/grap2graph-XXXXXX") 2> /dev/null` \
63151497Sru    && test -n "$tmp" && test -d "$tmp" \
64151497Sru    && break
65151497Sru
66151497Sru    tmp=$d/grap2graph$$-$RANDOM
67151497Sru    (umask 077 && mkdir $tmp) 2> /dev/null && break
68151497Srudone;
69151497Sruif test -z "$tmp"; then
70151497Sru    echo "$0: cannot create temporary directory" >&2
71151497Sru    { (exit 1); exit 1; }
72151497Srufi
73151497Sru
74151497Srutrap 'exit_status=$?; rm -rf $tmp && exit $exit_status' 0 2 15 
75151497Sru
76151497Sru# Here goes:
77151497Sru# 1. Add .G1/.G2.
78151497Sru# 2. Process through grap(1) to emit pic markup.
79151497Sru# 3. Process through groff(1) with pic preprocessing to emit Postscript.
80151497Sru# 4. Use convert(1) to crop the Postscript and turn it into a bitmap.
81151497Sru(echo ".G1"; cat; echo ".G2") | grap | groff -p $groff_opts -Tps -P-pletter | \
82151497Sru    convert -trim -crop 0x0 $convert_opts - $tmp/grap2graph.$format \
83151497Sru    && cat $tmp/grap2graph.$format
84151497Sru
85151497Sru# End
86