#! /bin/sh
# Copyright 2006 Kari Pahula <kaol@debian.org>
# License GNU GPL version 2 or, at your option, later, as published
# by Free Software Foundation.

save_opts () {
    echo "$BINARYNAME" > debian/wsp.opt.binary
    echo "$MAINTAINER" > debian/wsp.opt.maintainer
}

check_dir () {
    until [ -e debian/wsp.opt.maintainer ] ; do
	if [ "`pwd`" = / ] || [ -z $ACTIVE_CHECKDIR ] ; then
	    echo "make-klone-package: not in project directory" >&2
	    exit 1
	fi
	cd ..
    done
}

seed_debian () {
    mkdir -p debian/
    for TEMPLATE in `cd /usr/share/webserver-package/klone/debian; ls`; do
	if [ ! -e debian/$TEMPLATE ] ; then
	    if [ -x /usr/share/webserver-package/klone/debian/$TEMPLATE ] ; then
		/usr/share/webserver-package/klone/debian/$TEMPLATE > debian/$TEMPLATE
	    else
		cp /usr/share/webserver-package/klone/debian/$TEMPLATE debian
	    fi
	fi
    done
    chmod +x debian/rules
}

clean_debian () {
    find debian -mindepth 1 -maxdepth 1 ! -name "*changelog" ! -name "*copyright" ! -wholename "debian/wsp.*" -exec rm -rf '{}' \;
}

seed_source () {
    ln -s . klone-source
    if [ -e webapp ] ; then EXCLUDE_WEBAPP="--exclude=*webapp/*" ; fi
    tar xf /usr/src/klone-source.tar.bz2 --bzip2 $EXCLUDE_WEBAPP
    rm klone-source
}

clean_source () {
    find . -mindepth 1 -maxdepth 1 ! -wholename "./debian" ! -wholename "./webapp" ! -wholename "./userdata" ! -wholename "./patches" -exec rm -rf '{}' \;
}

default_packagename () {
    if [ -z "$PACKAGE" ] ; then
	PACKAGE=customkloneapp
    fi
}

default_binaryname () {
    if [ -z "$BINARYNAME" ] ; then
	BINARYNAME=$PACKAGE
    fi
}

guess_package () {
    PACKAGE_GUESS="`pwd | grep -Eo '[^/]*-[0-9]' | grep -Eo '[^-0-9]*'`"
}

usage () {
    cat >&2 <<EOF
make-klone-project {create|refresh|clean} [options]
create creates a new project directory
refresh injects the newest KLone source in the project directory
clean removes the klone source and leaves the user files in the project dir

  -b NAME	Name the generated klone binary as NAME.  By default it is
		the same as the package name.
  -l		Search for the project root directory from parent directories
  -m NAME	Set the maintainer name as NAME.
  -p NAME	Name the generated package as NAME.  By default
		"customkloneapp".
EOF
}

export WSPVER=0.3
ACTIVE_CHECKDIR=""

if [ -e debian/wsp.opt.maintainer ] ; then
    MAINTAINER="`cat debian/wsp.opt.maintainer`"
fi

if [ -e debian/wsp.opt.binary ] ; then
    BINARYNAME="`cat debian/wsp.opt.binary`"
fi

OPTS=`getopt -o b:lm:p:r:u: -n make-klone-project -- "$@"`

if [ $? != 0 ] ; then
    usage
    exit 1
fi

eval set -- "$OPTS"

while :; do
    case "$1" in
	-b)
	BINARYNAME="$2"
	shift 2
	;;
	-l)
	ACTIVE_CHECKDIR="1"
	shift
	;;
	-m)
	MAINTAINER="$2"
	shift 2
	;;
	-p)
	PACKAGE="$2"
	shift 2
	;;
	--)
	shift
	COMMAND="$1"
	break
	;;
	*)
	echo "make-klone-project: internal error" >&2
	exit 1
	;;
    esac
done

set -e

if [ -z "$MAINTAINER" ] ; then
    MAINTAINER="Undefined Maintainer <not@set.invalid>"
fi

export PACKAGE BINARYNAME MAINTAINER

case "$COMMAND" in
    create)
    default_packagename
    default_binaryname
    mkdir "$PACKAGE-0.1"
    cd "$PACKAGE-0.1"
    seed_source
    seed_debian
    save_opts
    exit 0
    ;;

    clean)
    check_dir
    clean_source
    clean_debian
    save_opts
    exit 0
    ;;

    refresh)
    check_dir
    guess_package || true
    if [ -z "$PACKAGE" ] ; then
	PACKAGE="$PACKAGE_GUESS"
	if [ -z "$PACKAGE" ] ; then
	    echo "make-klone-project: failed to deduce package name" >&2
	    exit 1
	fi
    fi
    default_binaryname
    clean_source
    clean_debian
    seed_source
    seed_debian
    save_opts
    exit 0
    ;;
    *)
    usage
    exit 1
esac
