#!/bin/sh
#
# File:         genmake
#
# Author:       Ulli Horlacher (framstag@rus.uni-stuttgart.de)
#
# History:      18 Sep 95   Framstag    initial version
#               25 Nov 95   Framstag	included makefile.template
#               12 Dec 95   Framstag    added Solaris-1 support
#               29 Feb 96   Framstag    new version labeling
#               28 Mar 96   Framstag    extended search for support programs
#
# Shell script to generate the real Makefile.
#
# Copyright  1995 Ulli Horlacher
# This file is covered by the GNU General Public License
#


# dirty hack for broken Ultrix test command
test -x genmake 2>/dev/null || exec ksh genmake

AWK=awk
export AWK
#eval `type nawk 2>&1 | awk '!/not/{print "AWK=nawk"}'`
#eval `type gawk 2>&1 | awk '!/not/{print "AWK=gawk"}'`

rm -f Makefile

header="\
# Makefile automaticly generated by genmake.
# DO NOT TOUCH THIS FILE!
# Instead edit config.h

"

if [ "$1" = clean ]; then
  cat <<EOD>Makefile
$header
first:
	@echo "Edit config.h if you wish and then type: make all"
all: make
	make
clean: make
	make clean
make:
	sh genmake
EOD
  exit
fi

#for prg in tar gzip recode ; do
# echo " `type $prg 2>/dev/null`" | $AWK '
#  { prg=toupper(prg)
#    gsub(/\)/,"",$0)
#    print "#ifndef "prg
#    print "#define "prg"\t\""substr($0,index($0,"/"))"\""
#    print "#endif"
#  }' prg=$prg
#done >support.h

SYSTEM=""

if [ -x /usr/bin/uname ];	then SYSTEM=`/usr/bin/uname -a`; fi
if [ -x /bin/uname ];		then SYSTEM=`/bin/uname -a`; fi
if [ -d /usr/apollo/bin ];	then SYSTEM=`ver sys5.3 /bin/uname`; fi
if [ -d /usr/bin/ncrm ];	then SYSTEM=NCR; fi
if [ -f /usr/bin/swconfig ];	then SYSTEM=SCO; fi

if [ -d /NextApps ]; then
# hostinfo | grep I386 >/dev/null
# if [ $? = 0 ]; then
#   SYSTEM=NEXT386
# else
#   SYSTEM=NEXT
# fi
  SYSTEM=NEXT
  echo '#define SYSTEM "'`sh next_uname -a`'"' >system.h
else
  echo '#define SYSTEM "'$SYSTEM'"' >system.h
  #echo '#define SYSTEM "'"`uname -a 2>/dev/null` with `cc -v 2>&1 | tail -1`\"" >system.h
fi


SYSTEM=`echo $SYSTEM | tr '[a-z]' '[A-Z]'`

case "$SYSTEM" in
  *BSD*)	SYSTEM=BSD;;
  *A/UX*)	SYSTEM=AUX;;
  *OSF/1*)	SYSTEM=OSF1;;
  *HP-UX*)	SYSTEM=HPUX;;
  *SMP_DC.SO*)	SYSTEM=PYRAMID;;
  *SUNOS*)	case `echo $SYSTEM | $AWK '{print $3}'` in
		  4*) SYSTEM=SOLARIS1;;
		  5*) SYSTEM=SOLARIS2;;
		esac;;
  *)		SYSTEM=`echo $SYSTEM | $AWK '{print $1}'`;;
esac

exec >Makefile

cat <<EOD
$header
SHELL=/bin/sh
STRIP=strip
SYSTEM=$SYSTEM
VERSION=`$AWK -F\" '/define VERSION/{print $2}' config.h`

EOD

case "$SYSTEM" in
  SOLARIS1)	echo 'CC=gcc -O2 -D$(SYSTEM)';;
  CONVEXOS)	echo 'CC=cc -O -D$(SYSTEM)';;
  IRIX64)	echo 'CC=cc -ansi -DIRIX64';;
  IRIX*)	echo 'CC=cc -O -ansi -DIRIX';;
  ULTRIX)	echo 'CC=gcc -Wall -O2 -G0 -D$(SYSTEM)';;
  NEXT)		echo 'CC=cc  -Wall -posix -O -D$(SYSTEM)';;
# HPUX)		echo 'CC=cc  -Aa -DHPUX -D_HPUX_SOURCE -O';;
  LINUX)	echo 'CC=gcc -Wall -ansi -pedantic -O2 -D$(SYSTEM)';;
  *)		echo 'CC=gcc -Wall -O2 -D$(SYSTEM)';;
esac

case "$SYSTEM" in
  SOLARIS2)	echo "LINK=gcc -lsocket -lnsl";;
  CONVEXOS)	echo 'LINK=cc';;
  IRIX*)	echo "LINK=cc";;
  NEXT)		echo "LINK=cc";;
  *)		echo "LINK=gcc";;
esac
echo "#LIBS=-l44bsd"

echo '
OBJ1= sendfile.o \
      message.o utf7.o pstring.o string.o io.o net.o destination.o spool.o
OBJ2= sendmsg.o \
      message.o utf7.o pstring.o string.o io.o net.o destination.o
OBJ3= receive.o \
      message.o utf7.o pstring.o string.o io.o spool.o getdate.o
OBJ4= sendfiled.o \
      message.o utf7.o pstring.o string.o io.o net.o spool.o peername.o reply.o
OBJ5= utf7encode.o \
      message.o utf7.o pstring.o


all:	sendfile sendmsg receive sendfiled utf7encode

sendfile: $(OBJ1)
	$(LINK) $(OBJ1) -o sendfile $(LIBS)
	$(STRIP) sendfile

sendmsg: $(OBJ2)
	$(LINK) $(OBJ2) -o sendmsg $(LIBS)
	$(STRIP) sendmsg

receive: $(OBJ3)
	$(LINK) $(OBJ3) -o receive $(LIBS)
	$(STRIP) receive

sendfiled: $(OBJ4)
	$(LINK) $(OBJ4) -o sendfiled $(LIBS)
	$(STRIP) sendfiled

utf7encode: $(OBJ5)
	$(LINK) $(OBJ5) -o utf7encode $(LIBS)
	$(STRIP) utf7encode
	@rm -f utf7decode
	ln -s utf7encode utf7decode

install: all .
	@sh install $(SYSTEM)

clean:
	rm -rf core *.o *~ a.out sendfile sendfiled sendmsg receive \
	       utf7??code Makefile system.h sendfile-$(VERSION)*
	sh genmake clean
	chmod +r *

make:
	sh genmake

wc:
	@wc -cl sendfile.c sendfiled.c sendmsg.c receive.c utf7encode.c \
	        string.[ch] utf7.[ch] pstring.[ch] message.[ch] peername.[ch] \
		io.[ch] net.[ch] destination.[ch] spool.[ch] reply.[ch] \
		getdate.c config.h bsd.h genmake install check_sendfile \
		sf_cleanup nosendfile sendfile.cf
	@echo
	@wc -cl LIESMICH LIESMICH.auch README README.too HISTORY \
	        sendmsg.1 sendfile.1 receive.1 \
		../doku/saft.html ../doku/doku.txt ../doku/doc.txt

trimeol:
	perl -pi -e "s/ *\$//" *.c *.h genmake
	perl -pi -e "s/\t*\$//" *.c *.h genmake

dis: clean
	@mkdir sendfile-$(VERSION)
	@cp sendfile.c sendfiled.c sendmsg.c receive.c string.[ch] utf7.[ch] \
	    pstring.[ch] message.[ch] peername.[ch] io.[ch] net.[ch] \
	    destination.[ch] spool.[ch] reply.[ch] getdate.c config.h bsd.h \
	    utf7encode.c sendmsg.1 sendfile.1 receive.1 Makefile genmake \
	    install check_sendfile next_uname nosendfile sendfile.cf \
	    sf_cleanup HISTORY LIESMICH* README* COPY* \
	    ../doku/doku.ps ../doku/doku.txt ../doku/doc.txt \
	    sendfile-$(VERSION)/
	tar cvf - sendfile-$(VERSION) | gzip > sendfile-$(VERSION).tar.gz
	@rm -rf sendfile-$(VERSION)
	@genmake
	@ls -l sendfile-$(VERSION).tar.gz

floppy: clean
	cd ..; tar cvzf /dev/fd0 develop; tar czf /tmp/sf.tgz develop
	@echo "insert a new disk and press enter"; read dummy
	mformat a:; mcopy /tmp/sf.tgz a:; rm -f /tmp/sf.tgz
	genmake

sendfile.o: sendfile.c config.h utf7.h string.h io.h
	$(CC) -c sendfile.c

sendmsg.o: sendmsg.c config.h utf7.h string.h
	$(CC) -c sendmsg.c

receive.o: receive.c config.h utf7.h spool.h string.h io.h
	$(CC) -c receive.c

sendfiled.o: sendfiled.c reply.h bsd.h config.h utf7.h spool.h string.h
	$(CC) -c sendfiled.c

spoolid.o: spoolid.c config.h
	$(CC) -c spoolid.c

message.o: message.c message.h config.h
	$(CC) -c message.c

utf7.o: utf7.c utf7.h config.h string.h
	$(CC) -c utf7.c

base64.o: base64.c base64.h config.h
	$(CC) -c base64.c

pstring.o: pstring.c pstring.h config.h
	$(CC) -c pstring.c

peername.o: peername.c peername.h bsd.h string.h config.h
	$(CC) -c peername.c

net.o: net.c net.h io.h bsd.h config.h
	$(CC) -c net.c

io.o: io.c io.h config.h
	$(CC) -c io.c

string.o: string.c string.h config.h
	$(CC) -c string.c

destination.o: destination.c destination.h config.h string.h
	$(CC) -c destination.c

spool.o: spool.c spool.h config.h string.h
	$(CC) -c spool.c

reply.o: reply.c reply.h config.h system.h net.h
	$(CC) -c reply.c

getdate.o: getdate.c config.h
	$(CC) -c getdate.c

utf7encode.o: utf7encode.c utf7.h
	$(CC) -c utf7encode.c


spoolid: spoolid.o message.o
	$(LINK) spoolid.o message.o -o spoolid

utf7: utf7.o pstring.o
	$(LINK) utf7.o pstring.o -o utf7

base64: base64.o pstring.o
	$(LINK) base64.o pstring.o -o base64
'
