#! /bin/zsh -f
#                               -*- Mode: Sh -*-
# arch_create_config ---
# Author           : Manoj Srivastava ( srivasta@glaurung.internal.golden-gryphon.com )
# Created On       : Mon Sep 25 10:51:27 2006
# Created On Node  : glaurung.internal.golden-gryphon.com
# Last Modified By : Manoj Srivastava
# Last Modified On : Mon May  7 11:27:38 2007
# Last Machine Used: glaurung.internal.golden-gryphon.com
# Update Count     : 57
# Status           : Unknown, Use with caution!
# HISTORY          :
# Description      :
#
#
progname="`basename \"$0\"`"
set -e

workdir_top=/usr/local/src/arch/packages--debian
#workdir_top=/usr/local/src/arch/packages--debian.lenny
curdir=$(pwd)
pkg=$(pwd | sed -e 's,^'$workdir_top'/,,' -e 's,/.*$,,')
TLA=baz

withecho () {
    echo " $@" >&2
    "$@"
}
action='withecho'
#action='echo'
opt_pkg=

usageversion () {
        cat >&2 <<END
Usage: $progname  [options]
Options:
  -h           print this message
  -f           Forcibly overwrite configuration files
  -n           "Dry-run" mode - No action taken, only print commands.
  -p name      Over ride the package name
  -v           Make the command verbose

END
}

# parse Command line
# Note that we use `"$@"' to let each command-line parameter expand to a
# separate word. The quotes around `$@' are essential!
# We need TEMP as the `eval set --' would nuke the return value of getopt.
TEMP=$(getopt -a -s bash -o fhnvp: -n 'arch_update' -- "$@")
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
    case "$1" in
        -h|--help) usageversion; exit 0   ; shift   ;;
        -n)      action='echo';docmd='NO' ; shift   ;;
        -p)      opt_pkg="$2"             ; shift 2 ;;
        -f)      FORCE=1                ; shift   ;;
        -v)      VERBOSE=1                ; shift   ;;
        --)      shift ; break ;;
        *) echo >&2 "Internal error!($i)"
            usageversion; exit 1           ;;
    esac
done

if [[ -n "$opt_pkg" ]]; then
    pkg="$opt_pkg"
fi

if [[ ! -d $workdir_top/configs ]]; then
    echo >&2 No configs directory found
    exit 2
fi


test -n "$pkg" || echo >&2 No package name provided
test -n "$pkg" || exit 1

if [[ ! -d "$workdir_top/$pkg" ]]; then
    echo >&2 No working directory found for "$pkg"
    exit 3
fi

cd $workdir_top
target_version=$($TLA tree-version | sed -e 's,[^/]*/,,')
cd $workdir_top/$pkg

changelog=$(find $pkg* -wholename '*/debian/changelog')
debdir=$(dirname $changelog)
topdir=$(dirname $debdir)

archive=$($TLA tree-id | sed -e 's,/.*,,g')
test -n "$archive" || echo >&2 Could not compute archive
test -n "$archive" || exit 4


version=$(dpkg-parsechangelog -l$changelog | sed -ne 's/^Version: //p' )
non_epoch_version=$(echo -n "$version" | perl -pe 's/^\d+://')
upstream_version=$(echo -n "$non_epoch_version" | sed  -e 's/-[^-]*$//')
debian_version=$(echo -n $non_epoch_version |  perl -nle 'm/-([^-]*)$/ && print $1')
config_file="configs/$pkg/debian/$pkg-$version"
debian_config="$workdir_top/$config_file"
upstream_config="$workdir_top/configs/$pkg/upstream/$pkg-$version"
grab_file="$workdir_top/configs/$pkg/grab"

NEW_CONFIG=
if [[ -d "$pkg" ]]; then
    if [[ -n "$debian_version" ]]; then
        echo >&2 We have an unversioned source dir, and yet a debian version
        echo >&2 "[ $debian_version ]"
        echo >&2 This is confusing
        exit 4
    fi
    if [[ -e "$debian_config" ]]; then
        echo >&2 Regenerating $debian_config
    else
        echo >&2 Creating $debian_config
        NEW_CONFIG=YES
    fi
    (
        cd $pkg
        tree_root=$($TLA tree-root)
        archive=$($TLA tree-id | sed -e 's,/.*,,g')
        echo "./$pkg/$pkg $($TLA tree-id)"
        top=$(pwd)
        for nested in $($TLA inventory -t --nested "$tree_root"); do
            (cd $nested; here=$(echo $nested |sed -e "s,$top/,,");
                echo "./$pkg/${pkg}/$here $($TLA tree-id)")
        done
        )        > $debian_config
    if [[ -n "$NEW_CONFIG" ]]; then
        $TLA add $debian_config   || true
    fi
elif [[ -d "$pkg-$upstream_version" ]]; then
    if [[ -z "$debian_version" ]]; then
        echo >&2 We have an versioned source dir, and yet no debian version
        echo >&2 This is confusing
        exit 5
    fi
    test -d "$workdir_top/$pkg/upstream" || mkdir -p $workdir_top/$pkg/upstream
    if [[ -e "$debian_config" ]]; then
        echo >&2 Regenerating $debian_config
    else
        echo >&2 Creating $debian_config
        NEW_CONFIG=YES
    fi
    (
        cd "$pkg-$upstream_version"
        echo "./$pkg/$pkg-$upstream_version $($TLA tree-id)"
        tree_root=$($TLA tree-root)
        archive=$($TLA tree-id | sed -e 's,/.*,,g')
        top=$(pwd)
        for nested in $($TLA inventory -t --nested "$tree_root"); do
            (cd $nested; here=$(echo $nested |sed -e "s,$top/,,");
                echo "./$pkg/${pkg}-${upstream_version}/$here $($TLA tree-id)")
        done
        )     > $debian_config
    if [[ -n "$NEW_CONFIG" ]]; then
        $TLA add $debian_config  || true
    fi
    NEW_CONFIG=
    if [[ -e "$upstream_config" ]]; then
        echo >&2 Regenerating $upstream_config
    else
        echo >&2 Creating $upstream_config
        NEW_CONFIG=YES
    fi
    (
        top=$(pwd)
        for nested in $($TLA inventory -t --nested "upstream"); do
            (cd $nested; here=$(echo $nested |sed -e "s,$top,,");
                echo "./$pkg/$here $($TLA tree-id)")
        done
        )    > $upstream_config
    if [[ -n "$NEW_CONFIG" ]]; then
        $TLA add $upstream_config || true
    fi
else
    echo >&2 cannot determine source directory
    exit 6
fi

cd $workdir_top
if [[  -e "$grab_file" ]]; then
    echo >&2 Regenerating "$grab_file"
else
    echo >&2 Creating "$grab_file"
    new_grab_file='Yes'
fi
(
    echo "Archive-Name: $archive"
    echo "Archive-Location: http://arch.debian.org/arch/private/srivasta"
    echo "Target-Revision: $target_version"
    echo "Target-Directory: manoj-packages"
    echo "Target-Config: $config_file"
) > $grab_file
test -n "$new_grab_file" && $TLA add $grab_file

cd $workdir_top
echo >&2 Uploading "$grab_file"
arch_upload_grab

cd $workdir_top

#$TLA commit -L "Added config files for  $pkg Version $version"
#$TLA archive-mirror $archive $target_version

exit 0

# Archive-Name: srivasta@debian.org--etch
# Archive-Location: http://arch.debian.org/arch/private/srivasta
# Target-Revision: packages--debian--0.1
# Target-Directory: manoj-packages
# Target-Config: configs/flex/debian/flex-2.5.33-9