#!/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
#
# Shell script to generate the real Makefile.
#
# 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

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


SYSTEM=""

if [ -x /usr/bin/uname ];	then SYSTEM=`/usr/bin/uname`; fi
if [ -x /bin/uname ];		then SYSTEM=`/bin/uname`; 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 "'"`./next_uname -a` ; `cc -v 2>&1 | tail -1`\"" >version.h
else
  echo '#define SYSTEM "'"`uname -a 2>/dev/null` ; `gcc -v 2>&1 | tail -1`\"" >version.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 `uname -a | awk '{print $3}'` in
		  4*) SYSTEM=SOLARIS1;;
		  5*) SYSTEM=SOLARIS2;;
		esac
esac

exec >Makefile
echo "
$header
SHELL=/bin/sh
STRIP=strip
SYSTEM=$SYSTEM
"

case "$SYSTEM" in
  CONVEXOS)	echo 'CC=cc -O -D$(SYSTEM)';;
  SOLARIS1)	echo 'CC=gcc -O2 -D$(SYSTEM)';;
  IRIX*)	echo 'CC=gcc -Wall -DIRIX';;
  NEXT)		echo 'CC=cc  -Wall -posix -O -D$(SYSTEM)';;
  LINUX)	echo 'CC=gcc -Wall -ansi -O2 -D$(SYSTEM)';;
  ULTRIX)	echo 'CC=gcc -Wall -O2 -G0 -D$(SYSTEM)';;
  *)		echo 'CC=gcc -Wall -O2 -D$(SYSTEM)';;
esac

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

echo '
all:	sendfile sendmsg receive sendfiled

sendfile: sendfile.o message.o utf7.o pstring.o string.o net.o io.o destination.o
	$(LINK) sendfile.o message.o utf7.o pstring.o string.o net.o io.o \
	        destination.o -o sendfile
	$(STRIP) sendfile
	
sendmsg: sendmsg.o message.o utf7.o pstring.o string.o net.o io.o destination.o
	$(LINK) sendmsg.o message.o utf7.o pstring.o string.o net.o io.o \
	        destination.o -o sendmsg
	$(STRIP) sendmsg

receive: receive.o message.o utf7.o pstring.o string.o spool.o reply.o io.o
	$(LINK) receive.o message.o utf7.o pstring.o string.o spool.o \
	        reply.o io.o -o receive
	$(STRIP) receive

sendfiled: sendfiled.o message.o utf7.o pstring.o peername.o string.o \
	   reply.o io.o spool.o
	$(LINK) sendfiled.o message.o utf7.o pstring.o peername.o string.o \
	        reply.o io.o spool.o -o sendfiled
	$(STRIP) sendfiled


install: all .
	@sh install $(SYSTEM)
	
clean:	
	rm -f core *.o *~ a.out sendfile sendfiled sendmsg receive Makefile \
              version.h sendfile.tar.gz
	sh genmake clean

make:
	sh genmake

wc:
	@wc -cl 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] config.h \
		bsd.h genmake install check_sendfile sf_cleanup nosendfile \
		sendfile.cf
	@echo
	@wc -cl LIESMICH* README* HISTORY sendmsg.1 sendfile.1 receive.1 \
	        bi.txt doku.txt

dis: clean
	@mkdir sendfile
	@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] config.h bsd.h \
	    sendmsg.1 sendfile.1 receive.1 Makefile genmake install \
	    check_sendfile next_uname nosendfile sendfile.cf sf_cleanup \
	    HISTORY LIESMICH* README* COPYING ../doku.ps sendfile/
	tar cvf - sendfile | gzip > sendfile.tar.gz
	@rm -rf sendfile
	@genmake

floppy: clean
	(cd ..; tar cvzf /dev/fd0 develop)
	genmake

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

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

receive.o: receive.c config.h utf7.h spool.h string.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
	$(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
	$(CC) -c destination.c

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

reply.o: reply.c reply.h config.h version.h
	$(CC) -c reply.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
'
