#! /bin/sh

process_template() {
	local res;
	local tmpl="`cat $1`"

	eval "res=\"$tmpl\""
	echo "$res"
}

export LC_TIME=C

for i in top_srcdir MINOR_VERSION PATCH_VERSION RELEASE_VERSION; do
	eval res="\$$i"
	if test -z "$res"; then
		echo "$i not specified"
		exit 1
	fi
done

rm -rf ${top_srcdir}/debian
mkdir -p ${top_srcdir}/debian/source
ln -s ../LICENSE ${top_srcdir}/debian/copyright

cd ${top_srcdir}/debian
process_template ${top_srcdir}/project/debian.control > control
process_template ${top_srcdir}/project/debian.rules > rules
process_template ${top_srcdir}/project/debian.changelog > changelog
chmod +x rules
echo "7" > compat
echo "3.0 (quilt)" > source/format

# main (apache, cgi, delegation)
cat > gridsite.install <<EOF
usr/lib/apache2/modules/mod_gridsite.so
usr/sbin/real-gridsite-admin.cgi
usr/sbin/gridsite-delegation.cgi
EOF
cat > gridsite.manpages <<EOF
doc/gridsite-delegation.8
doc/mod_gridsite.8
EOF
cat > gridsite.docs <<EOF
debian/tmp/usr/share/doc/gridsite-${MINOR_VERSION}/httpd-*.conf
doc/delegation-1.1.0.wsdl
src/mod_gridsite_example.c
EOF

# commands
cat > gridsite-commands.install <<EOF
usr/bin/htcp
usr/bin/htls
usr/bin/htll
usr/bin/htrm
usr/bin/htmkdir
usr/bin/htmv
usr/bin/htping
usr/bin/htfind
usr/bin/urlencode
usr/bin/findproxyfile
EOF
cat > gridsite-commands.manpages <<EOF
doc/htcp.1
doc/htrm.1
doc/htls.1
doc/htll.1
doc/htmkdir.1
doc/htmv.1
doc/htping.1
doc/htfind.1
doc/urlencode.1
doc/findproxyfile.1
EOF

# service-clients
cat > gridsite-service-clients.install <<EOF
usr/bin/htproxyput
usr/bin/htproxydestroy
usr/bin/htproxytime
usr/bin/htproxyunixtime
usr/bin/htproxyrenew
usr/bin/htproxyinfo
EOF
cat > gridsite-service-clients.manpages <<EOF
doc/htproxyput.1
doc/htproxydestroy.1
doc/htproxytime.1
doc/htproxyunixtime.1
doc/htproxyrenew.1
doc/htproxyinfo.1
EOF

# shared
cat > libgridsite2.docs <<EOF
CHANGES
INSTALL
README
VERSION
doc/*.html
doc/fuse.spec
doc/gsoap-devel.spec
doc/build-apache2.sh
EOF
cat > libgridsite2.install <<EOF
usr/lib/lib*.so.*
EOF

# devel
cat > libgridsite-dev.install <<EOF
usr/include/*
usr/lib/lib*.a
usr/lib/lib*.so
usr/lib/pkgconfig/*.pc
EOF

cd - >/dev/null
