#!/bin/bash

set -x
set -e

PKG=$1

if [ "$PKG" = "" ]; then
    echo "usage: $0 libsome-module-perl"
    exit 1;
fi

export VISUAL=autochangelog
export PATH=/home/jk/prog/Parse-Deb-Control/examples/:$PATH

# rebuild our local repo
OLD_PWD=$PWD
cd /var/cache/pbuilder/result && dpkg-scanpackages . /dev/null > Packages && sudo apt-get update
cd $OLD_PWD

[ -d $PKG-* ] || get-all-source-dependecies --prefix=opt- $PKG
ls | while read F; do [ -d "$F" ] || rm $F; done
ls | while read F; do mv "$F" "opt-$F"; done
ls | while read F; do
    cd $F
    dh_listpackages | while read DPKG; do
        ls debian/$DPKG.* | perl -lane 'next if not -f; die if not m{^debian/(.+)$}; system(qq{mv "$_" "debian/opt-$1" });';
        find debian  -maxdepth 1 -type f -not -name control -not -name changelog -exec perl -i -lane "s{$DPKG([^-]|\$)}{opt-$DPKG\$1}g; print" {} \;
        # perl -i -lane "s{/debian/tmp}{/debian/opt-$DPKG}g; print" debian/rules # no good
    done
    [ "$?" == "0" ] || exit 1
    cd ..
done
[ "$?" == "0" ] || exit 1

ls */debian/control | while read F; do deb-control-prefix-perl --maintainer=automat@mms.ag '--prefix=opt-' "$F" > control.tmp; mv control.tmp "$F"; done
[ "$?" == "0" ] || exit 1

# replace all /usr
find */debian -maxdepth 1 -type f -exec perl -i -lane 's{(^|\s|/)usr}{$1opt/perl}g if not m{(/usr/bin/make|/usr/share/common-licenses|changelog:|/usr/doc|usr/share/doc|/usr/share/dpatch|/usr/share/quilt/|/usr/share/cdbs/|www\.perl\.org|debusr|usr/bin/printf|usr/bin/python)};print;' {} \;
# cdbs
find */debian -type f -exec perl -i -lane 's{(cdbs/1/class/)(perlmodule.*\.mk)}{$1opt$2}g; print' {} \;
# changelog
ls | while read F; do cd $F; DEBEMAIL=automat@mms.ag dch --force-distribution --distribution unstable --bpo; cd ..; done

build-pkg "opt-$PKG"
