1#!/usr/bin/env bash
2# generate a man page or html man page from m4 source
3function usage()
4{
5    echo >&2 "usage: $0 -t html|man -p product -n name [-o outfile] [-w] source"
6    exit 1
7}
8
9unset windows
10
11while [ $# -gt 0 ]
12do	case $1 in
13	    -t)
14		shift
15		case $1 in
16		    html|man)
17			type=$1
18			shift
19			;;
20		    *)
21			usage
22		esac
23		;;
24	    -p)
25		shift
26		product=$1
27		shift
28		;;
29	    -n)
30		shift
31		name=$1
32		shift
33		;;
34	    -o)
35		shift
36		outfile=$1
37		shift
38		;;
39	    -w)
40		shift
41		platform=windows
42		;;
43	    -*)
44		usage
45		;;
46	    *)
47		if [ -n "$source" ]
48		then	usage
49		fi
50		source=$1
51		shift
52	esac
53done
54if [ -z "$type" -o -z "$product" -o -z "$name" -o -z "$source" ]
55then	usage
56fi
57
58. ./version.txt
59date=`date "+%d %B %Y"`
60# platform
61if [ x"$platform" != xwindows ]
62then case $product in
63	    w*)
64		platform=windows
65		;;
66	    *)
67		platform=unix
68	esac
69fi
70if [ $platform = unix ]
71then
72	c3270=c3270
73	s3270=s3270
74	pr3287=pr3287
75	x3270=x3270
76	b3270=b3270
77else
78	c3270=wc3270
79	s3270=ws3270
80	pr3287=wpr3287
81	x3270=wc3270
82	b3270=wb3270
83fi
84# mode
85case $product in
86    c3270|wc3270)
87	mode=console
88	;;
89    s3270|ws3270|b3270)
90	mode=script
91	;;
92    *)
93	mode=$product
94	;;
95esac
96# interactive
97case $product in
98    x3270|c3270|wc3270)
99	interactive=yes
100	;;
101    *)
102	interactive=no
103	;;
104esac
105
106# set up output file, and make sure it will be deleted
107if [ -n "$outfile" ]
108then	tf=/tmp/m4man$$
109    	rm -f $tf
110	trap "rm -f $tf" exit
111	trap "exit" INT QUIT HUP TERM
112fi
113
114/usr/bin/m4 -DXX_PRODUCT=$product -DXX_PAGENAME=$name \
115    -DXX_PLATFORM=$platform -DXX_MODE=$mode -DXX_INTERACTIVE=$interactive \
116    -DXX_C3270=$c3270 -DXX_S3270=$s3270 -DXX_PR3287=$pr3287 -DXX_X3270=$x3270 \
117    -DXX_B3270=$b3270 -DXX_DATE="$date" -DXX_VERSION_NUMBER=$version \
118    -DXX_CYEAR=$cyear $type.m4 $source |
119 if [ -n "$outfile" ]
120 then	cat >$tf
121 else	cat
122 fi
123rc=$?
124
125if [ -n "$outfile" -a $rc -eq 0 ]
126then	mv $tf $outfile
127    	rc=$?
128fi
129
130exit $rc
131