#!/bin/sh
#
# Copyright 2007 (C) Albin Tonnerre (Lutin) <lut1n.tne@gmail.com>
#
# ##################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# See file /usr/share/common-licenses/GPL-2 for more details.
#
# ##################################################################
#
# This script is used to update the Maintainer field of an Ubuntu package
# to match the DebianMaintainerField specification.

usage() {
cat <<EOF
Usage: $0 [--path=PATH] [--section=SECTION]
--path=PATH 		Specify the path of the source directory.
--section=SECTION 	Specify the section of the package (if the package is
			not yet in the archive.
EOF
}

for argv in "$@"; do
    param=${argv%=*}
    value=${argv#*=}
    case $param in
        "--path")
            path="$value"
        ;;
        "--section")
            [ "$value" != "main" ] &&
            [ "$value" != "restricted" ] &&
            [ "$value" != "universe" ] &&
            [ "$value" != "multiverse" ] && echo "Invalid section. Valid sections: main restricted universe multiverse" >&2 && exit 1
            section=$value
        ;;
        "--help")
            usage
            exit 0
        ;;
        *)
            echo "Bad parameter."
            usage
            exit 1
        ;;
    esac
done

cd ${path-.}

# look for the debian directory
if [ -f debian/control ] && [ -f debian/changelog ]; then
    DEBIANDIR=debian
elif [ -f control ] && [ -f changelog ]; then
    DEBIANDIR=.
else
    echo "Please execute «$0» in the source folder." >&2
    exit 1
fi

if [ ! -r $DEBIANDIR/changelog ]
then
    echo "Make sure that the changelog file is readable." >&2 && exit 1
fi

for file in control control.in control.real; do
    if [ -f $DEBIANDIR/$file ]; then
        if [ ! -r $DEBIANDIR/$file ] || [ ! -w $DEBIANDIR/$file ]; then
            echo "Make sure that the control file(s) is (are) readable and writable." >&2 && exit 1
        fi
        controls="$controls $file"
    fi
done

if ! head -1 $DEBIANDIR/changelog | grep -q '(*ubuntu.*)'
then
	echo "Latest changelog entry has no Ubuntu version number." >&2
	exit 1
fi

if grep -E "^Maintainer: .*@.*ubuntu.*>" $DEBIANDIR/control >/dev/null
then
	echo "Package already maintained by the Ubuntu team." >&2
	exit 1
fi

if [ -z "$section" ]; then
    DISTRO=$(sed -r '1!d;s/.*\) (.*);.*/\1/' $DEBIANDIR/changelog | cut -d'-' -f1)
    pkgline=$(rmadison -u ubuntu -a source -s $DISTRO $(head -1 $DEBIANDIR/changelog | cut -d' ' -f1))
    if [ -z "$pkgline" ]; then
        echo "The package doesn't exist in the $DISTRO archive. You may have to use the" >&2
        echo "--section argument. Run $0 --help for more information." >&2
        exit 1
    fi
    section=$(echo $pkgline | grep -Eo "main|universe|multiverse|restricted") || section=main
fi

case $section in
    "main"|"restricted")        email="Ubuntu Core Developers <ubuntu-devel-discuss@lists.ubuntu.com>"  ;;
    "universe"|"multiverse")    email="Ubuntu MOTU Developers <ubuntu-motu@lists.ubuntu.com>"           ;;
    *)      echo "No section found for this package; aborting" >&2; exit 1 ;;
esac

for file in $controls; do
    sed -ri "s/(^Maintainer:) (.*)/\1 $email\nXSBC-Original-\1 \2/" $DEBIANDIR/$file
done