#!/bin/sh

# (c) 2003 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=apache2-$upsrcversion

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

cd ..

mkdir $tmpdir

cd $tmpdir

echo "N: Unpacking upstream..."

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

( cd $upsrcdir/upstream/tarballs; tar zxf httpd-*.tar.gz )

mkdir $srcdir

cp -a \
    $upsrcdir/upstream/tarballs/httpd-*/modules/aaa/mod_auth.c \
    $srcdir/mod_auth_plain.c

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

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

cp -a $srcdir/mod_auth_plain.c $pkgdir

cd ..

rm -rf $tmpdir

echo "N: Done."
