#!/usr/bin/env python

# Twisted, the Framework of Your Internet
# Copyright (C) 2001 Matthew W. Lefkowitz
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""tap2deb"""
import sys, os, string, pwd, shutil

### Twisted Preamble
# This makes sure that users don't have to set up their environment
# specially in order to run these programs from bin/.
import sys,os,string

if string.find(os.path.abspath(sys.argv[0]),'Twisted') != -1:
    sys.path.append(os.path.dirname(
        os.path.dirname(os.path.abspath(sys.argv[0]))))
sys.path.append('.')
### end of preamble

from twisted.python import usage

class MyOptions(usage.Options):
    optFlags = [["unsigned", "u"]]
    optStrings = [["tapfile", "t", "twistd.tap"],
                  ["maintainer", "m", ""],
                  ["protocol", "p", ""],
                  ["description", "e", ""],
                  ["long_description", "l", ""],
                  ["version", "v", "1.0"],
                  ["debfile", "d", None]]
try:
    config = MyOptions()
    config.parseOptions()
except usage.error, ue:
    sys.exit("%s: %s" % (sys.argv[0], ue))

def save_to_file(file, text):
    open(file, 'w').write(text)

tap_file = config.tapfile
protocol = config.protocol or os.path.splitext(tap_file)[0]
deb_file = config.debfile or 'twisted-'+protocol
version = config.version
maintainer = config.maintainer
description = config.description or ('A TCP server for %(protocol)s' % vars())
long_description = config.long_description or 'Automatically created by tap2deb'
date = string.strip(os.popen('822-date').read())
directory = deb_file + '-' + version

if os.path.exists(os.path.join('.build', directory)):
    os.system('rm -rf %s' % os.path.join('.build', directory))
os.makedirs(os.path.join('.build', directory, 'debian'))

shutil.copy(tap_file, os.path.join('.build', directory))

save_to_file(os.path.join('.build', directory, 'debian', 'README.Debian'), 
'''\
This package is auto-generated by tap2deb
''')

save_to_file(os.path.join('.build', directory, 'debian', 'conffiles'), 
'''\
/etc/init.d/%(deb_file)s
/etc/default/%(deb_file)s
/etc/%(tap_file)s
''' % vars())

save_to_file(os.path.join('.build', directory, 'debian', 'default'), 
'''\
pidfile=/var/run/%(deb_file)s.pid \
rundir=/var/lib/%(deb_file)s/ \
file=/etc/%(tap_file)s \
logfile=/var/log/%(deb_file)s.log
''' % vars())

save_to_file(os.path.join('.build', directory, 'debian', 'init.d'),
'''\
#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

pidfile=/var/run/%(deb_file)s.pid \
rundir=/var/lib/%(deb_file)s/ \
file=/etc/%(tap_file)s \
logfile=/var/log/%(deb_file)s.log

[ -r /etc/default/%(deb_file)s ] && . /etc/default/%(deb_file)s

test -x /usr/bin/twistd || exit 0
test -r $file || exit 0


case "$1" in
    start)
	echo -n "Starting %(deb_file)s: twistd"
	start-stop-daemon --start --quiet --exec /usr/bin/twistd -- \
	                  --pidfile=$pidfile \
	                  --rundir=$rundir \
	                  --file=$file \
	                  --logfile=$logfile \
	                  --quiet
	echo "."	
    ;;

    stop)
	echo -n "Stopping %(deb_file)s: twistd"
	start-stop-daemon --stop --quiet  \
	    --pidfile $pidfile
	echo "."	
    ;;

    restart)
	$0 stop
	$0 start
    ;;
    
    force-reload)
        $0 restart
    ;;

    *)
	echo "Usage: /etc/init.d/%(deb_file)s {start|stop|restart|force-reload}" >&2
	exit 1
    ;;
esac

exit 0
''' % vars())

os.chmod(os.path.join('.build', directory, 'debian', 'init.d'), 0755)

save_to_file(os.path.join('.build', directory, 'debian', 'postinst'),
'''\
#!/bin/sh
if [ "$1" = "configure" ]; then
	if [ -d /usr/doc -a ! -e /usr/doc/%(deb_file)s -a -d /usr/share/doc/%(deb_file)s ]; then
		ln -sf ../share/doc/%(deb_file)s /usr/doc/%(deb_file)s
	fi
fi

update-rc.d %(deb_file)s defaults >/dev/null
sh /etc/init.d/%(deb_file)s start
''' % vars())

save_to_file(os.path.join('.build', directory, 'debian', 'prerm'),
'''\
#!/bin/sh
if [ \( "$1" = "upgrade" -o "$1" = "remove" \) -a -L /usr/doc/%(deb_file)s ]; then
	rm -f /usr/doc/%(deb_file)s
fi
sh /etc/init.d/%(deb_file)s stop
''' % vars())

save_to_file(os.path.join('.build', directory, 'debian', 'postrm'),
'''\
#!/bin/sh
if [ "$1" = purge ]; then
	update-rc.d %(deb_file)s remove >/dev/null
fi
''' % vars())

save_to_file(os.path.join('.build', directory, 'debian', 'changelog'),
'''\
%(deb_file)s (%(version)s) unstable; urgency=low

  * Created by tap2deb

 -- %(maintainer)s  %(date)s

Local variables:
mode: debian-changelog
End:
''' % vars())

save_to_file(os.path.join('.build', directory, 'debian', 'control'),
'''\
Source: %(deb_file)s
Section: net
Priority: extra
Maintainer: %(maintainer)s
Build-Depends: debhelper
Standards-Version: 3.5.6

Package: %(deb_file)s
Architecture: all
Depends: twisted
Description: %(description)s
 %(long_description)s
''' % vars())

save_to_file(os.path.join('.build', directory, 'debian', 'copyright'),
'''\
This package was auto-debianized by %(maintainer)s on
%(date)s

It was auto-generated by tap2deb

Upstream Author(s): 
Moshe Zadka <moshez@twistedmatrix.com> -- tap2deb author

Copyright:

tap2deb is released under the GNU Lesser Generl Public License,
and this package contains bits of code from tap2deb, and hence is
a derived work of tap2deb, and is under the GNU Lesser General
Public License too
''' % vars())

save_to_file(os.path.join('.build', directory, 'debian', 'dirs'),
'''\
etc/init.d
etc/default
var/lib/%(deb_file)s
usr/share/doc/%(deb_file)s
''' % vars())

save_to_file(os.path.join('.build', directory, 'debian', 'rules'),
'''\
#!/usr/bin/make -f

export DH_COMPAT=1

build: build-stamp
build-stamp:
	dh_testdir
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp install-stamp
	dh_clean

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/tmp.
	cp %(tap_file)s debian/tmp/etc/
	cp debian/init.d debian/tmp/etc/init.d/%(deb_file)s
	cp debian/default debian/tmp/etc/default/%(deb_file)s
	cp debian/copyright debian/tmp/usr/share/doc/%(deb_file)s/
	cp debian/README.Debian debian/tmp/usr/share/doc/%(deb_file)s/
	touch install-stamp

binary-arch: build install

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_strip
	dh_compress
	dh_installchangelogs
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
''' % vars())

os.chmod(os.path.join('.build', directory, 'debian', 'rules'), 0755)

os.chdir('.build/%(directory)s' % vars())
os.system('dpkg-buildpackage -rfakeroot'+ ['', ' -uc -us'][config.unsigned])
