#!/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
#
# 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` with `cc -v 2>&1 | tail -1`\"" >version.h
else
  echo '#define SYSTEM "'"`uname -a 2>/dev/null` with `cc -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

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

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

EOD

case "$SYSTEM" in
  CONVEXOS)	echo 'CC=cc -O -D$(SYSTEM)';;
  SOLARIS1)	echo 'CC=gcc -O2 -D$(SYSTEM)';;
  IRIX64)	echo 'CC=cc -DIRIX64';;
  IRIX*)	echo 'CC=gcc -DIRIX';;
  NEXT)		echo 'CC=cc  -Wall -posix -O -D$(SYSTEM)';;
#  LINUX)	echo 'CC=gcc -Wall -ansi -pedantic -O2 -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
  SOLARIS2)	echo "LINK=gcc -lsocket -lnsl";;
  CONVEXOS)	echo 'LINK=cc';;
  IRIX64)	echo "LINK=cc";;
  NEXT)		echo "LINK=cc";;
  *)		echo "LINK=gcc";;
esac

echo '
all:	sendfile sendmsg receive sendfiled

sendfile: $(OBJ1)
	$(LINK) $(OBJ1) -o sendfile
	$(STRIP) sendfile
	
sendmsg: $(OBJ2)
	$(LINK) $(OBJ2) -o sendmsg
	$(STRIP) sendmsg

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

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


install: all .
	@sh install $(SYSTEM)
	
clean:	
	rm -f core *.o *~ a.out sendfile sendfiled sendmsg receive Makefile \
              version.h sendfile-$(VERSION).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] getdate.c \
		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-$(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 \
	    sendmsg.1 sendfile.1 receive.1 Makefile genmake install \
	    check_sendfile next_uname nosendfile sendfile.cf sf_cleanup \
	    HISTORY LIESMICH* README* COPY* ../doku.ps doku.txt doc.txt \
	    sendfile-$(VERSION)/
	tar cvf - sendfile-$(VERSION) | gzip > sendfile-$(VERSION).tar.gz
	@rm -rf sendfile-$(VERSION)
	@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 string.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 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 version.h
	$(CC) -c reply.c

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