#!/bin/sh
# debianify v1.0.7  (c) 15.3.2019 by Andreas Ley  (u) 1.4.2020
# Adapt a source directory to build a debian package

# Releases currently in our repository, see /home/sys/ftp/campus/debian/dists
#releases="wheezy jessie stretch buster"

usage()
{
	echo "Usage: `basename $0` [-v] [-i|-s|-l] [-p section] [-u uploader -U uploader-mail] [-S source] [-H homepage] [-d synopsis] [-D description] [-e] [-f]" >&2
	echo "-v  verbose mode" >&2
	echo "-i  Package is independent from processor architecture (scripts etc.)" >&2
	echo "-s  Package is dependent from processor architecture (compiled binaries etc.)" >&2
	echo "-l  Package is a library dependent from processor architecture (compiled shared objects etc.)" >&2
	echo "-p  Specify section" >&2
	echo "-u  Specify uploader's fullname" >&2
	echo "-U  Specify uploader's email address" >&2
	echo "-S  Specify source" >&2
	echo "-H  Specify homepage" >&2
	echo "-d  Specify synopsis" >&2
	echo "-D  Specify description" >&2
	echo "-f  Force rerun" >&2
	echo "-e  Keep example files" >&2
	exit 1
}

trace=false
verbose=false
class=
section=
upname=
upmail=
source=
homepage=
synopsis=
description=
copyright=
license=
examples=false
force=false
while getopts hxvislp:u:U:S:H:c:L:ef OPTION; do
	case ${OPTION} in
		h)	sed '1d;s/^# *//;/^$/q' $0; usage;;
		x)	set -x; trace=true;;
		v)	verbose=true;;
		i)	class="i";;
		s)	class="s";;
		l)	class="l";;
		p)	section="${OPTARG}";;
		u)	upname="${OPTARG}";;
		U)	upmail="${OPTARG}";;
		S)	source="${OPTARG}";;
		H)	homepage="${OPTARG}";;
		d)	synopsis="${OPTARG}";;
		D)	description="${OPTARG}";;
		c)	copyright="${OPTARG}";;
		L)	license="${OPTARG}";;
		e)	examples=true;;
		f)	force=true;;
		?)	usage;;
	esac
done
_XPG=1 shift $((${OPTIND} - 1))
test $# -ne 0 && usage

if ! test -s $HOME/.devscripts; then
	(echo 'DEBUILD_DPKG_BUILDPACKAGE_OPTS="-I -I.old"'
	echo 'DEBUILD_LINTIAN_OPTS="-i -I"') >$HOME/.devscripts
	echo "A default $HOME/.devscripts has been created. Adapt to your preferences."
fi

if test -z "${class}"; then
	class="i"
	test -f Makefile && grep -q '^INSTALL_PROGRAM = ' Makefile && class="s"
	test -f Makefile && grep -q '^CONFIGS = ' Makefile && class="l"
fi
case "${class}" in
	i)	multi="foreign";;
	s)	multi="foreign"; unset="-u CPPFLAGS -u CFLAGS -u LDFLAGS";;
	l)	multi="same"; unset="-u CPPFLAGS -u CFLAGS -u LDFLAGS";;
esac

if test ! -d debian; then
	# I know of debmake, but this generates files more error-prone and difficult to edit (like description in debian/control or single files in debian/copyright)
	test -d debian || \
		dh_make -y -C="${class}" -n -c ${license:-gpl2}
	# Homepage
#	if test -n "${homepage}"; then
#		sed -i "s!^\\(Homepage:\\).*!\\1 ${homepage}!" debian/control
#	else
#		sed -i "s!^\\(Homepage:\\).*!\\1 `url|grep '^http:'`!;/^#/d" debian/control
#	fi
	sed -i "s!^\\(Homepage:\\).*!\\1 ${homepage:-$(url|grep '^http:')}!;/^#/d" debian/control
	# Synopsis
	if test -z "${CONFIG}"; then
		# If CONFIG is not set by rzsoft but debianify is run manually, use a heuristic
		CONFIG="../$(basename $PWD).rzsoft"
	fi
	if test -z "${synopsis}" -a -n "${CONFIG}" -a -s "${CONFIG}"; then
		synopsis="`sed -n 's!^Publish: \(..*\)!\1!p' \"${CONFIG}\"`"
		test -n "${synopsis}" || synopsis="`sed -n 's!^Description: \(..*\)!\1!p' \"${CONFIG}\"`"
	fi
	if test -n "${synopsis}"; then
		sed -i "s!^\\(Description:\\).*!\\1 ${synopsis}!;/^ /d" debian/control
	fi
	# Description
	if test -z "${description}" -a -n "${CONFIG}" -a -s "${CONFIG}"; then
		description=$(sed '1,/^Publish:/d;/^[^[:space:]]/,$d;s/^[[:space:]]*//' "${CONFIG}" | lbreak -i 1 | sed 's/$/\\/')
		test -n "${description}" || description=$(sed '1,/^Description:/d;/^[^[:space:]]/,$d;s/^[[:space:]]*//' "${CONFIG}" | lbreak -i 1 | sed 's/$/\\/')
	fi
	if test -n "${description}"; then
		sed -i "/^Description:.*/a\
\ ${description}" debian/control
		sed -i 's/\\*$//' debian/control
	fi
	#
	# Changelog
	#
	test -n "${releases}" && sed -i "s/unstable;/${releases};/" debian/changelog
	#
	# Copyright
	#
	# Source
	if test -n "${source}"; then
		sed -i "s!^\\(Source:\\).*!\\1 ${source}!" debian/copyright
	else
		sed -i '/^Upstream-Name:/d;/^Source:/d' debian/copyright
	fi
	# Content author
	if test -n "${copyright}"; then
		sed -i "s!^\\(Copyright:\\) <years>.*!\\1 ${copyright}!;/<years>/,+1d" debian/copyright
	else
		sed -i '/^Copyright: <years>/,/^Files: debian/d' debian/copyright
	fi
	# Remove descriptive hints at the end
	sed -i '/^#/d' debian/copyright
	test -n "`tail -1 debian/copyright`" || sed -i '$d' debian/copyright
	rm -f debian/README*
	rm -f debian/*-docs.docs
	if ${examples}; then
		echo "Check debian/*.ex files, delete or rename"
	else
		rm -f debian/*.ex
		rm -f debian/*.EX
	fi
fi

# These are initial debian/ adaptions, but can be re-run without interfering with already made changes
#
# Control
#
if test -n "${section}"; then
	sed -i "s/^\\(Section:\\).*/\\1 ${section}/" debian/control
else
	echo "For debian/control, choose a section from https://packages.debian.org/$(lsb_release -cs)/"
	echo
fi
# Why did we introduce this?!
#	grep -q 'dpkg-dev (>= 1.9.0)' debian/control || \
#		sed -i 's/^Build-Depends:.*/&, dpkg-dev (>= 1.9.0)/' debian/control
if test -n "${multi}"; then
	grep -q '^Multi-Arch:' debian/control || \
		sed -i "/^Description:.*/i\
Multi-Arch: ${multi}" debian/control
fi
# Uploader
if test -n "${upname}${upmail}"; then
	sed -i "/^Maintainer:.*/a\
Uploader: ${upname} <${upmail}>" debian/control
fi
#
# Rules
#
sed -i 's/^#\(export\)/\1/;/^# dh_make generated override targets/,$d' debian/rules
#
# Lintian
#
if ! test -s debian/source/lintian-overrides; then
	(echo "# This is the way our fileserver generates unix permissions :-("
	echo "package-file-is-executable"
	echo "# Until we have test suites for everything"
	echo "testsuite-autopkgtest-missing") >debian/source/lintian-overrides
fi
sed -n 's/^Package: *//p' debian/control | while read pkg; do
	if ! test -s debian/$pkg.lintian-overrides; then
		# Since our fileserver generates executable unix permissions, lintian _runs_ this overrides file :-(
		(echo "#!/bin/sed 1d"
		echo "# Until we have manpages for everything"
		echo "binary-without-manpage") >debian/$pkg.lintian-overrides
	fi
done
if ! test -s debian/changes/lintian-overrides; then
	mkdir -p debian/changes
	(echo "multiple-distributions-in-changes-file"
	echo "bad-distribution-in-changes-file") >debian/changes/lintian-overrides
fi

if test -s Makefile; then
	if ! grep -q '^PREFIX = \$(if \$(DEB_BUILD_ARCH)' Makefile; then
		sed -i 's!^\(PREFIX = \)\(.*\)$!\1$(if $(DEB_BUILD_ARCH),$(DESTDIR)/usr,\2)!' Makefile
	fi
	if ! grep -q '^CHANGES = ' Makefile; then
		sed -i 's!^PREFIX = !CHANGES = ../$(PACKAGE)_$(shell dpkg-architecture -qDEB_HOST_ARCH).changes\
\
&!' Makefile
	fi
	if ! grep -q '^PACKAGE = ' Makefile; then
		sed -i 's!^CHANGES = !PACKAGE = $(shell dpkg-parsechangelog -S Source)_$(shell dpkg-parsechangelog -S Version)\
&!' Makefile
	fi
	sed -i 's!	lint !	#lint !' Makefile
	if ! grep -q '^deb $(CHANGES):' Makefile; then
		(echo
		echo "deb \$(CHANGES): \$(MAKEFILE_LIST) debian/* debian/*/* \$(SOURCES)"
		echo "	cme check -strict dpkg"
		echo "	env${unset:+ ${unset}} LC_ALL=C.UTF-8 dpkg-buildpackage -I -I.old --check-command=lintian --check-option=-i --check-option=-I --check-option=--suppress-tags-from-file=debian/changes/lintian-overrides -tc"
		echo
		echo "dput: \$(CHANGES)"
		echo "	dput \$(CHANGES) && rm -f ../\$(PACKAGE)[_.]*") >>Makefile
	fi

	if ! egrep -q '^Copyright: [0-9]+-' debian/copyright; then
		project="`sed -n 's/^PROJECT = //p' Makefile`"
		year=`sed -n 's/.*([Cc]) \([0-9.,-]*\) by .*/\1/p' "${project}" "${project}.c" "${project}.tmpl" 2>/dev/null | sed 's/.*\.//;s/[,-].*//' | head -1`
		if test -n "${year}"; then
			sed -i "s/Copyright: /&${year}-/" debian/copyright
		fi
	fi
fi

# Upgrade old standards when it's time
#sed -i 's/^\(Build-Depends:.*debhelper (>= \)9)/\111)/;s/^\(Standards-Version: \)3\.9\.8$/\14.1.3/' debian/control
if ! grep -q 'ancient-standards-version' debian/source/lintian-overrides; then
	(echo "# lintian croaks this way and cme the other :-("
	echo "ancient-standards-version"
	echo "package-uses-deprecated-debhelper-compat-version") >>debian/source/lintian-overrides
fi

fgrep -r '@rz.uni-karlsruhe.de' .
fgrep -r 'Andreas.Ley@' .

exit 0
