#!/bin/sh

# (c) 2003-2004 Piotr Roszatycki <dexter@debian.org>, GPL

# This utility converts original tgz archive into Debian source package
# into *.orig.tar.gz

set -e

if [ -f ../debian/changelog ]; then
    cd ..
elif [ ! -f debian/changelog ]; then
    echo "can't find changelog file"
    exit 1
fi


srcname=`head -n 1 debian/changelog | sed 's/ .*//'`
srcversion=`head -n 1 debian/changelog | sed -e 's/.*(//' -e 's/-[^-]*).*//'`
srcdir=${srcname}-`echo $srcversion | sed -e 's/.*://'`
srcorig=${srcname}_`echo $srcversion | sed -e 's/.*://'`.orig.tar.gz

upsrcversion=`echo $srcversion | sed 's/-.*//'`
upsrcdir=apache-$upsrcversion

tmpdir=debsource-$srcname
pkgdir=$(pwd)

cd ..

mkdir $tmpdir

cd $tmpdir

echo "N: Unpacking upstream..."

dpkg-source -x ../apache_$srcversion.dsc

( cd $upsrcdir; debian/rules source.make )

mkdir $srcdir

cp -a \
    $upsrcdir/build-tree-apache/apache_*/src/modules/standard/mod_auth.c \
    $srcdir/mod_auth_curdir.c
cp -a \
    $upsrcdir/build-tree-apache/apache_*/src/modules/standard/mod_auth_db.c \
    $srcdir/mod_auth_db_curdir.c
cp -a \
    $upsrcdir/build-tree-apache/apache_*/src/modules/standard/mod_auth_dbm.c \
    $srcdir/mod_auth_dbm_curdir.c

( cd $srcdir; patch < $pkgdir/patches/mod_auth_curdir.c.patch
    find -name '*.orig' | xargs rm -f )

echo "N: Copying new files to package directory..."

cp -a $srcdir/*.c $pkgdir


cd ..

rm -rf $tmpdir

echo "N: Done."
