#!/bin/sh
# NewMk v1.2.4  (c) 18.5.1996 by Andreas Ley  (u) 17.3.2020
# Create template Makefile

usage()
{
	echo "Usage: `basename $0` [-c] [-l] [-m] [-s] [-p prefix] project [version]" >&2
	echo "-c  Add targets for C programs" >&2
	echo "-l  Add targets for program libraries" >&2
	echo "-m  Add targets for man pages" >&2
	echo "-s  Add targets for sysconfig" >&2
	echo "-p  Define prefix (defaults to /usr/segment)" >&2
	exit 1
}

set -- `getopt clmsp:h $*` || usage

prefix=/usr/segment
while :; do
	case $1 in
		-c)     cprog=yes; unset='-u CPPFLAGS -u CFLAGS -u LDFLAGS'; shift;;
		-l)     libs=yes; shift;;
		-m)     manpage=yes; shift;;
		-s)     sysconfig=yes; shift;;
		-p)     prefix="$2"; shift 2;;
		-h)     sed '1d;s/^# *//;/^$/q' $0; usage;;
		--)     shift; break;;
	esac
done

case $# in
	0)	project=`pwd`
		project=`basename ${project}`
		version=`echo ${project} | sed 's/.*-\([0-9].*\)/\1/'`
		project=`echo ${project} | sed 's/\(.*\)-[0-9].*/\1/'`;;
	1)	project=$1; version=1.0;;
	2)	project=$1; version=$2;;
	*)	usage;;
esac

if test -f Makefile; then
	echo "Makefile exists" >&2
	exit 1
fi

# https://www.gnu.org/software/make/manual/make.html

exec >Makefile
cat <<!
PROJECT = ${project}
VERSION = ${version}

PREFIX = \$(if \$(DEB_BUILD_ARCH),\$(DESTDIR)/usr,${prefix})

BINDIR = \$(PREFIX)/bin
!
test -n "${libs}" && echo "LIBDIR = \$(PREFIX)/lib/\$(PROJECT)"
test -n "${manpage}" && echo "MANDIR = \$(PREFIX)/man"
test -n "${manpage}" && echo "MANEXT = 1"
echo
test -n "${cprog}" && echo "SOURCES = ${project}.c"
test -n "${cprog}" && echo "OBJECTS = ${project}.o"
test -n "${libs}" && echo "CONFIGS = ${project}.conf"
echo "TARGETS = ${project}"
test -n "${manpage}" && echo "MANPAGE = ${project}.man"
cat <<!

#INSTALL = /usr/machine/bin/install -c -p
!
test -n "${cprog}" && echo "INSTALL_PROGRAM = \$(INSTALL) -s -m 755"
test -z "${cprog}" && echo "INSTALL_SCRIPT = \$(INSTALL) -m 755"
test -n "${libs}${manpage}" && echo "INSTALL_DATA = \$(INSTALL) -m 644"
test -n "${sysconfig}" && cat <<!

.SUFFIXES: .in

.in:
	sysconfig $< >\$@
!
cat <<!

.PHONY: all install${manpage+ install.man} clean dist deb${cprog+ check depend}

all: \$(TARGETS)

!
test -n "${cprog}" && cat <<!
${project}: \$(OBJECTS)
	\$(CC) \$(CFLAGS) \$(OBJECTS) \$(LDFLAGS) -o \$@

!
cat <<!
install: all
	test -d \$(BINDIR) || mkdir -p \$(BINDIR)
!
test -n "${libs}" && echo "	test -d \$(LIBDIR) || mkdir -p \$(LIBDIR)"
test -n "${cprog}" && echo "	\$(INSTALL_PROGRAM) \$(TARGETS) \$(BINDIR)"
test -z "${cprog}" && echo "	\$(INSTALL_SCRIPT) \$(TARGETS) \$(BINDIR)"
test -n "${libs}" && echo "	\$(INSTALL_DATA) \$(CONFIGS) \$(LIBDIR)"
echo
test -n "${manpage}" && cat <<!
install.man: \$(MANPAGE)
	test -d \$(MANDIR) || mkdir -p \$(MANDIR)
	\$(INSTALL_DATA) \$(MANPAGE) \$(MANDIR)/man\$(MANEXT)/${project}.\$(MANEXT)

!
echo "clean:"
test -n "${cprog}" && echo "	rm -f *.o a.out core \$(TARGETS)" || \
(test -n "${sysconfig}" && echo "	rm -f \$(TARGETS)")
cat <<!

dist: clean
	cd ..; \
	tar cf - \$(PROJECT)-\$(VERSION) | bzip2 >\$(PROJECT)-\$(VERSION).tar.bz2

deb:
	cme check -strict dpkg
	env${unset:+ ${unset}} LC_ALL=C.UTF-8 dpkg-buildpackage -I -I.old --check-command=lintian --check-option=-i --check-option=-I -tc

dput: deb
	dput ../\$(PROJECT)_\$(VERSION)_\$\$(dpkg-architecture -qDEB_HOST_ARCH).changes
!
test -n "${cprog}" && cat <<!

check:
	! which lint >/dev/null || \\
		lint -u \$(SOURCES)
	! which cppcheck >/dev/null || \\
		cppcheck -q --enable=all \$(SOURCES)
	! which gcc >/dev/null || \\
		gcc -pedantic -fsyntax-only -W -Wall -Wtraditional -Winline -Wshadow -Wsign-compare -Wmissing-prototypes -Wmissing-declarations -Wbad-function-cast -Wcast-align -Wpointer-arith -Waggregate-return -Wcast-qual -Wwrite-strings -Wundef \$(SOURCES)

depend:
	makedepend -- \$(CFLAGS) \$(COPTS) -- \$(SOURCES)

!

exit 0
