#!/bin/sh
set -e

name=lprng
daname=debathena-lprng
daversionappend=debathena8
section=debathena

hack_package () {
    # Rename the package and make it not conflict with lpr or cupsys-bsd.
    perl -pe 's/^Package: lprng$/Package: debathena-lprng/' -i debian/control
    perl -ne '/^Provides: lpr$/ || print;' -i debian/control
    perl -pe '/^Conflicts/ && s/lpr, //' -i debian/control
    perl -pe '/^Conflicts/ && s/cupsys-bsd/lprng/' -i debian/control
    perl -pe '/^Replaces/ && s/lpr/lprng/' -i debian/control

    perl -n -i - debian/rules << 'EOS'

	if (/^build:/) {
	    # lprng has a gcc 4.2.3-related build failure on some dists.
	    # src/common/plp_snprintf.c uses a macro safestrlen() on
	    # character arrays in four places; the macro triggers GCC's
	    # -Waddress warning since it tests for the falsehood of an
	    # array address.  This is a workaround.
	    print "confflags += --disable-werror\n";
	}

	# Since we're renaming the main package, we have to adjust
	# where things get installed.  But make sure not to change
	# references to debian/lprng-doc, which we aren't renaming.
	s|debian/lprng|debian/debathena-lprng|;
	s|debian/debathena-lprng-doc|debian/lprng-doc|;

	print;

	# Rename the commands.
	if (/^(?:[^#]*MAKE.*install|\tdh_auto_install)/) {
	    my $usr = "debian/debathena-lprng/usr";
	    my $man1 = "$usr/share/man/man1";
	    my $man8 = "$usr/share/man/man8";
	    print "\trm $usr/bin/cancel\n";
	    print "\tln -s mit-lprm $usr/bin/cancel\n";
	    print "\trm $usr/bin/lp\n";
	    print "\tln -s mit-lpr $usr/bin/lp\n";
	    print "\tmv $usr/bin/cancel $usr/bin/mit-cancel\n";
	    print "\tmv $usr/bin/lp $usr/bin/mit-lp\n";
	    print "\tmv $usr/bin/lpq $usr/bin/mit-lpq\n";
	    print "\tmv $usr/bin/lprm $usr/bin/mit-lprm\n";
	    print "\tmv $usr/bin/lpr $usr/bin/mit-lpr\n";
	    print "\tmv $usr/bin/lpstat $usr/bin/mit-lpstat\n";
	    print "\tmv $usr/sbin/lpc $usr/sbin/mit-lpc\n";
	    print "\tmv $man1/cancel.1 $man1/mit-cancel.1\n";
	    print "\tmv $man1/lp.1 $man1/mit-lp.1\n";
	    print "\tmv $man1/lpq.1 $man1/mit-lpq.1\n";
	    print "\tmv $man1/lprm.1 $man1/mit-lprm.1\n";
	    print "\tmv $man1/lpr.1 $man1/mit-lpr.1\n";
	    print "\tmv $man1/lpstat.1 $man1/mit-lpstat.1\n";
	    print "\tmv $man8/lpc.8 $man8/mit-lpc.8\n";
	}
EOS

    grep -q 'mit-lpr' debian/rules

    # The postinst script sets or unsets the setuid bit of
    # lpr/lprm/lpq and thus needs to be adjusted for the command
    # renaming.
    perl -pe 's|/usr/bin/lp|/usr/bin/mit-lp|' -i debian/postinst

    # The renamed binary package no longer matches the source package
    # name, so we have to rename a bunch of debhelper files.
    for f in config cron.daily dirs examples init.d mime postinst postrm \
      preinst prerm templates; do
	[ -f debian/$f ] && mv debian/$f debian/debathena-lprng.$f
    done
    perl -pe 's/templates/debathena-lprng.templates/' -i debian/po/POTFILES.in

    # lprng's cron job generates mail to root each day if you're not
    # running a server.  Fix this problem by making the cron script
    # conditional on the existence of /etc/printcap, just as the
    # init.d script is.
    #
    # Except CUPS happily generates an incompatibile /etc/printcap, because
    # they hate you.  So instead, bail if cups generated /etc/printcap
    perl -ne 'print; /#.*main/ && print "# Debathena: Exit if CUPS is our print server\n(! [ -f /etc/printcap ] || grep -q \"automatically generated by cupsd\" /etc/printcap) && exit 0\n";' \
      -i debian/debathena-lprng.cron.daily

    append_description <<EOF
 .
 This package was rebuilt for the Debathena project to rename the
 package and commands so that it no longer conflicts with cupsys-bsd.
 Additionally, the daily cron job will not be run if CUPS generated 
 /etc/printcap.
EOF
    add_changelog 'Rename commands.'
    munge_sections
    set_debathena_maintainer

    perl -pe 'm{^Package: lprng-doc$} .. s{^Section: debathena}{Section: debathena-system}' -i debian/control
}

. ../common/debathenificator.sh
