#!/bin/bash
#
# Welcome to the DTC install maker !
# This will produce the tarball package
# Maintainer: Thomas GOIRAND <thomas [ at ] goirand.fr>
# please do not ship with you packages
# this is only a small coder tool...
#

if [ -z $FAKED_MODE ]; then
	echo "Please run this with fakeroot, ie fakeroot ./makeRedhat"
	exit 1
fi

VERSION=`cat version`
RELEASE=`cat release`
UNIX_TYPE=redhat

echo " --- Making DTC Redhat Manager Pachage (RPM) package ---"

# This block should be common to most unix packages :
echo "===> Copying files"
rm -rf dtc usr
mkdir -p dtc/etc/zones
cp -rf ../admin ../client ../shared ../doc ../email dtc/
echo "<?php
\$conf_dtc_version= \""$VERSION"\";
\$conf_dtc_release= \""$RELEASE"\";
\$conf_unix_type= \""$UNIX_TYPE"\";

?>" > dtc/shared/dtc_version.php
find dtc/ -iname 'CVS' -exec rm -rf {} \; &>/dev/null
find dtc/ -iname '*~' -exec rm -rf {} \; &>/dev/null
mkdir -p dtc/shared/imgcache
if ! [ -e dtc/admin/gfx ]
then
	ln -s ../shared/gfx dtc/admin/gfx
fi
if ! [ -e dtc/email/gfx ]
then
	ln -s ../shared/gfx dtc/email/gfx
fi
if ! [ -e dtc/client/gfx ]
then
	ln -s ../shared/gfx dtc/client/gfx
fi
if ! [ -e dtc/admin/imgcache ]
then
        ln -s ../shared/imgcache dtc/admin/imgcache
fi
if ! [ -e dtc/client/imgcache ]
then
        ln -s ../shared/imgcache dtc/client/imgcache
fi
if ! [ -e dtc/email/imgcache ]
then
        ln -s ../shared/imgcache dtc/email/imgcache
fi
# 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
        if [ -e /usr/bin/php4 ] ;then
	        /usr/bin/php4 backup_db.php $conf_dbpass
	else
		/usr/bin/php backup_db.php $conf_dbpass
	fi
        cd $curdir
fi
cp sources/dtc_db.php sources/restor_db.php dtc/admin

find ./dtc -type d | xargs chmod 755
find debian/ -iname 'CVS' -exec rm -rf {} \; &>/dev/null
find debian/ -iname '*~' -exec rm -rf {} \; &>/dev/null
chown -R root:root dtc
chown nobody:65534 dtc/shared dtc/client/imgcache dtc/admin/imgcache dtc/shared/imgcache dtc/email/imgcache
chown nobody:65534 dtc/client/gfx dtc/admin/gfx dtc/shared/gfx dtc/email/gfx

cat sources/redhat/install.sh >dtc/admin/dtc.rpm-install.sh
cat sources/interactive_installer.sh >>dtc/admin/dtc.rpm-install.sh
cat sources/setup_mysql_db.sh >>dtc/admin/dtc.rpm-install.sh
cat sources/create_chroot.sh >>dtc/admin/dtc.rpm-install.sh
cat sources/configure_deamons.sh >>dtc/admin/dtc.rpm-install.sh

cat sources/redhat/uninstall.sh >dtc/admin/dtc.rpm-uninstall.sh
cat sources/uninstall_deamons.sh >>dtc/admin/dtc.rpm-uninstall.sh

# Specific RPM package actions :
echo -n "===> Creating tar file and move it to build tree: "
echo -n "tar..."
tar -cf dtc.tar.gz dtc
echo -n "mv..."
mkdir -p ~/rpmbuild/SOURCES
mkdir -p ~/rpmbuild/BUILD
mkdir -p ~/rpmbuild/RPMS
mv dtc.tar.gz ~/rpmbuild/SOURCES/
echo -n "rm..."
rm -rf dtc
echo "done!"

echo "===> Building specfile"
echo "Summary: Domain Technologie Control: a web admin interface for hosting virtual domain-names.
Name: dtc
Version: "$VERSION"
Release: "$RELEASE"
" >dtc.spec
cat sources/redhat/specfile_1 >>dtc.spec

echo "%changelog
* Mon Sep 23 2003 Thomas GOIRAND

1st redhat attempt
" >>dtc.spec

echo "===> Building dtc-"$VERSION"-"$RELEASE".noarch.rpm"
if [ ! -f ~/.rpmmacros ]; then
	echo "%HOME %{expand:%%(cd; pwd)}
 %_topdir %{HOME}/rpmbuild" > ~/.rpmmacros
fi
rpmbuild -bb --target=noarch dtc.spec
echo " -- Made dtc-"$VERSION"-"$RELEASE".noarch.rpm ---"
