#!/bin/sh
#
# Welcome to the DTC install maker !
# Maintainer: Thomas GOIRAND <thomas [ at ] goirand.fr>
# please do not ship with you packages
# this is only a small coder tool...
# $Source: /var/lib/cvs/dtc/bin/makeDebianSource,v $
# $Date: 2006/11/13 08:05:35 $
# $Revision: 1.11 $


VERS=`cat version`
RELS=`cat release`
if [ ""$VERS == "" ]; then
VERS=0
fi
if [ ""$RELS == "" ]; then
RELS=0
fi
VERSION=$VERS"-"$RELS
UNIX_TYPE=debian

echo " --- Making DTC debian source package --- "

# purge the existing directories
# [no need to purge, the Makefile takes care of that more correctly]
# rm -rf debian-src
# Make the directories
mkdir -p debian-src/dtc-$VERS.orig
mkdir -p debian-src/dtc-$VERS

# Copy DTC's files into the two directories above
cp -rf ../doc ../admin ../client ../shared ../email debian-src/dtc-$VERS.orig
cp -rf ../doc ../admin ../client ../shared ../email debian-src/dtc-$VERS
rm -rf debian-src/dtc-$VERS.orig/doc/LICENSE debian-src/dtc-$VERS/doc/LICENSE

# now create an empty Makefile for dh_make
cat > debian-src/dtc-$VERS/Makefile << Make-File
default:
	@-echo "Building... not..."
clean:	
	if [ -n ""\$(DESTDIR) ]; then \
	(rm -rf \$(DESTDIR)/usr/share/dtc/doc \$(DESTDIR)/usr/share/dtc/admin \$(DESTDIR)/usr/share/dtc/client \$(DESTDIR)/usr/share/dtc/shared \$(DESTDIR)/usr/share/dtc/email) ;\
	fi
	@-echo "OK, clean :)"
install:
	@-echo "Copying to \$(DESTDIR)..."
	mkdir -p \$(DESTDIR)/usr/share/dtc/etc
	cp -rf doc admin client shared email \$(DESTDIR)/usr/share/dtc
	if ! [ -e \$(DESTDIR)/usr/share/dtc/admin/gfx ] ; \
	then \
		ln -s ../shared/gfx \$(DESTDIR)/usr/share/dtc/admin/gfx  ;\
	fi
	if ! [ -e \$(DESTDIR)/usr/share/dtc/email/gfx ]  ;\
	then \
		ln -s ../shared/gfx \$(DESTDIR)/usr/share/dtc/email/gfx  ;\
	fi
	if ! [ -e \$(DESTDIR)/usr/share/dtc/client/gfx ]  ;\
	then \
		ln -s ../shared/gfx \$(DESTDIR)/usr/share/dtc/client/gfx  ;\
	fi
	if ! [ -e \$(DESTDIR)/usr/share/dtc/admin/imgcache ]  ;\
	then \
		ln -s ../shared/imgcache \$(DESTDIR)/usr/share/dtc/admin/imgcache  ;\
	fi
	if ! [ -e \$(DESTDIR)/usr/share/dtc/client/imgcache ] ;\
	then \
		ln -s ../shared/imgcache \$(DESTDIR)/usr/share/dtc/client/imgcache ;\
	fi
	if ! [ -e \$(DESTDIR)/usr/share/dtc/email/imgcache ] ;\
	then \
		ln -s ../shared/imgcache \$(DESTDIR)/usr/share/dtc/email/imgcache ;\
	fi
	@#now for documentation
	@-mkdir -p \$(DESTDIR)/usr/share/doc/dtc/upstream
	@-cp -rf doc \$(DESTDIR)/usr/share/doc/dtc/upstream
	if ! [ -e \$(DESTDIR)/usr/share/dtc/doc ] ;\
	then \
		ln -s /usr/share/doc/dtc \$(DESTDIR)/usr/share/dtc/doc ;\
	fi
	@# create an empty directory for config storage
	@-mkdir -p \$(DESTDIR)/usr/share/dtc/etc/zones

	chown -R root:root \$(DESTDIR)/usr/share/dtc
	chown nobody:65534 \$(DESTDIR)/usr/share/dtc/client/imgcache \$(DESTDIR)/usr/share/dtc/admin/imgcache \$(DESTDIR)/usr/share/dtc/shared/imgcache \$(DESTDIR)/usr/share/dtc/email/imgcache
	chown nobody:65534 \$(DESTDIR)/usr/share/dtc/client/gfx \$(DESTDIR)/usr/share/dtc/admin/gfx \$(DESTDIR)/usr/share/dtc/shared/gfx \$(DESTDIR)/usr/share/dtc/email/gfx
	gzip -9 \$(DESTDIR)/usr/share/dtc/doc/changelog

Make-File
# copy it into the orig folder too, so it doesn't appear in our diff
cp debian-src/dtc-$VERS/Makefile debian-src/dtc-$VERS.orig

# create the empty debian directory
mkdir -p debian-src/dtc-$VERS/debian/

# update the changelog if we haven't done so already... 

echo "dtc ($VERSION) unstable; urgency=low
" > debian-src/dtc-$VERS/debian/changelog
echo "  * Fake changelog entry
" >> debian-src/dtc-$VERS/debian/changelog
NOWDATE=`date +'%a, %d %b %G %H:%m:%S %z'`
echo " -- Thomas GOIRAND <thomas@goirand.fr>  $NOWDATE
" >> debian-src/dtc-$VERS/debian/changelog

# now add the previous changelog we have checked in already
cat sources/debian/changelog >> debian-src/dtc-$VERS/debian/changelog

# push the DTC version into the source archive
echo "<?php
\$conf_dtc_version= \""$VERS"\"; \$conf_dtc_release= \""$RELS"\"; \$conf_unix_type= \""$UNIX_TYPE"\";
?>" > debian-src/dtc-$VERS/shared/dtc_version.php

# remove the default mysql configuration
rm -rf debian-src/dtc-$VERS/shared/mysql_config.php
mkdir -p debian-src/dtc-$VERS/shared/imgcache

# create the control file for the source package
echo "Source: dtc
Section: admin
Priority: optional
Maintainer: Thomas GOIRAND <thomas@goirand.fr>
Build-Depends: debhelper (>> 4.0.0), dh-make
Standards-Version: 3.6.1" > debian-src/dtc-$VERS/debian/control

# append the binary package portion
echo "" >> debian-src/dtc-$VERS/debian/control # add an empty line

# Store DTC's package name and version in the control file
echo "Package: dtc
Version: "$VERS >> debian-src/dtc-$VERS/debian/control
cat sources/debian/control >> debian-src/dtc-$VERS/debian/control

# now for the 'copyright' file (copy from docs)
cp ../doc/copyright debian-src/dtc-$VERS/debian/

# copy the default 'rules' file
cp sources/debian/rules debian-src/dtc-$VERS/debian/

# create a 'conffiles' file (don't mark them yet)
#echo "/usr/share/dtc/etc" > debian-src/dtc-$VERS/debian/conffiles

# create a 'docs' file

# hmm... TODO
# documentation steps
#mkdir -p debian-src/usr/share/doc/dtc
#cp -rf ../doc/* debian-src/usr/share/doc/dtc
#rm debian-src/usr/share/doc/dtc/LICENSE debian-src/usr/share/doc/dtc/INSTALL
#gzip -f -9 debian-src/usr/share/doc/dtc/changelog

# Copy the table dump in the admin directory
if ! [ -f sources/dtc_db.php ]
then
	curdir=`pwd`
	cd sources
	echo "Dumping your DTC SQL structures."
	echo -n "Please enter your mysql password: "
	read conf_dbpass
	php backup_db.php $conf_dbpass
	cd $curdir
fi
cp sources/dtc_db.php sources/restor_db.php debian-src/dtc-$VERS/admin
cp sources/dtc_db.php sources/restor_db.php debian-src/dtc-$VERS.orig/admin

# Make the config file directory and script using standard
# plus debian specific scripts
cp -f sources/debian/prerm sources/debian/config sources/debian/postinst sources/debian/templates debian-src/dtc-$VERS/debian
# somehow the config file gets lost
cp -f sources/debian/config debian-src/dtc-$VERS/debian/dtc.config

cat sources/setup_mysql_db.sh >>debian-src/dtc-$VERS/debian/postinst
cat sources/create_chroot.sh >>debian-src/dtc-$VERS/debian/postinst
cat sources/configure_deamons.sh >>debian-src/dtc-$VERS/debian/postinst
cat sources/uninstall_deamons.sh >>debian-src/dtc-$VERS/debian/prerm



find ./debian-src -type d | xargs chmod 755
find debian-src/ -iname 'CVS' -exec rm -rf {} \; &>/dev/null
find debian-src/ -iname '*~' -exec rm -rf {} \; &>/dev/null

pushd debian-src/dtc-$VERS/
#fakeroot dpkg-buildpackage
dpkg-buildpackage -rfakeroot -us -uc
popd

#mv debian.deb "dtc_"$VERS"_all.deb"

# echo " -- Succesfully made dtc_"$VERS"_all.deb ---"
echo " -- Succesfully made debian-src/dtc_"$VERSION"_all.deb ---"

# curdir=`pwd`
# cd /home/ftp/debian
# dpkg-scanpackages dists/stable/main/binary-i386 /dev/null | gzip -9 >dists/stable/main/binary-i386/Packages.gz
# cd $curdir
