#!/bin/bash -f
######################################################################
## Filename:      E3D_wizard.sh
## Version:       0.1
## Description:   Create
## Author:        Yannick Copin <y.copin@ipnl.in2p3.fr>
## Created at:    Fri Apr 25 14:18:46 2003
## Modified at:   Tue Sep  2 14:49:10 2003
## Modified by:   Yannick Copin <y.copin@ipnl.in2p3.fr>
######################################################################

# TODO:
# add a project-wide incl/ (instead of package)

wizvers=0.1

# Template package in Template project
temppack=Template

# Usage: usage ==============================================================
usage () {
    echo "
E3D_wizard helps you to create a new project from the E3D_Template
project. Go to the E3D_PATH directory (${E3D_PATH:-WARNING: It should be defined!}), 
untar the E3D_Template project, and launch the E3D_wizard script. The
-t, -P, -V and -p options are mandatory, use option -a if you just
want to add an extra package to an existing project.

Usage: E3D_wizard -t|--template template_dir    \\
                  -P|--Project  project_name    \\
                  -V|--Version  project_version \\
		  -p|--package  package_name
		 [-a|--add]
		 [-h|--help]

Example: $0 -t E3D_Template-1.0/ -P MyProject -V 1.0 -p MyPackage

Contact: y.copin@ipnl.in2p3.fr"
}

# Parsing arguments: parse_arg $* ===========================================
parse_arg () {
    if [ $# -eq 0 ]; then
	usage; exit 1;
    fi
    while [ $# -gt 0 ]; do
	case "$1" in
	    -t|--template) template=${2:?"Option -t incomplete"}; shift 2 ;;
	    -P|--Project)  project=${2:? "Option -P incomplete"}; shift 2 ;;
	    -V|--Version)  version=${2:? "Option -V incomplete"}; shift 2 ;;
	    -p|--package)  package=${2:? "Option -p incomplete"}; shift 2 ;;
	    -a|--add)      add=1; shift ;;
	    -h|--help)     usage; exit 0 ;;
	    *) echo "Unknown option \`$1'"; usage; exit 1 ;;
	esac
    done

    dir=${project:?}-${version:?}
    echo ""
    if [ ${add=0} -eq 0 ]; then
	echo "Brand new project creation"
	regexp=$temppack
    else
	echo "Adding extra package to pre-existing project"
	regexp="$"
    fi
    echo "Template directory:     ${template?}"
    echo "Project directory:      $dir"
    echo "Package name:           ${package?}"
    echo ""
}

# Remove previous version of project: rm_project ============================
rm_project () {
    if [ -d $dir ]; then
	read -p "Project \`$dir' already exists, remove it? [y|n] " answer
	case $answer in
	    n*) echo "Project \`$dir' already exists"; exit 1 ;;
	    *)  rm -rf $dir ; echo "Project \`$dir' removed" ;;
	esac
    fi
}

# Copy Template to new project: cp_project ==================================
cp_project () {
    if [ -d $template ]; then
	echo "Copying \`$template' to \`$dir'"
        cp -a $template $dir
    else
	echo "ERROR: Template project \`$template' does not exist"
        exit 1
    fi
}

# Rename Template package: mv_package =======================================
mv_package () {
    if [ -d $template/pkg/$temppack ]; then
	echo "Renaming package \`$temppack' to \`$package'";
	mv $dir/pkg/$temppack $dir/pkg/$package
    else
	echo "ERROR: Project \`$template' does not have a \`$temppack' package"
	exit 1
    fi
}

# Add package: add_package ==================================================
add_package () {
    if [ ! -d $template ]; then
	echo "ERROR: Project \`$template' does not exist"
        exit 1
    elif [ ! -d $template/pkg/$temppack ]; then
	echo "ERROR: Project \`$template' does not have a \`$temppack' package"
	exit 1
    elif [ -d $dir/pkg/$package ]; then
	echo "ERROR: Project \`$dir' already has a \`$package' package"
        exit 1
    fi
    echo "Copying package \`$temppack' to \`$package'"
    cp -a $template/pkg/$temppack $dir/pkg/$package
}

# Update pkg/Makefile.am: update_am =========================================
update_am () {
    file=pkg/Makefile.am
    if [ -w $template/$file ]; then
	echo "Updating $dir/$file:"
	cat $dir/$file | \
            sed "/^SUBDIRS/s/$regexp/ $package/" > $dir/$file.tmp && \
            mv -f $dir/$file.tmp $dir/$file
	grep "^SUBDIRS" $dir/$file
    else
	echo "ERROR: Project \`$template' does not have a $file"
	exit 1
    fi
}

# Update configure.in: update_in ============================================
update_in () {
    file=configure.in
    if [ -w $template/$file ]; then
	echo "Updating $dir/$file:"
        # Update AC_INIT and AM_INIT_AUTOMAKE if new project
        if [ $add -eq 0 ]; then
            # If configure.in contains embedded newlines, remove them
            cat $dir/$file | sed "\
                :b;/\\\\$/{N;s/\\\\\n//;bb;};\
                /^AC_INIT/s|(.*)|(pkg/$package/source/template.c)|;\
                /^AM_INIT_AUTOMAKE/s|(.*)|($project,$version)|;" > \
                    $dir/$file.tmp && mv -f $dir/$file.tmp $dir/$file
            grep "^AC_INIT\|^AM_INIT_AUTOMAKE" $dir/$file
        fi
        # Update AC_OUTPUT in any case
	ac_output=`find $dir -name "[Mm]akefile.??" | \
	    sed 's|^[^/]*/||;s|\...$| |' | uniq | tr -d "\n"`
	cat $dir/$file | sed "\
	    :b;/\\\\$/{N;s/\\\\\n//;bb;};\
	    /^AC_OUTPUT/s|(.*)|($ac_output)|;" > $dir/$file.tmp && \
            mv -f $dir/$file.tmp $dir/$file
	grep "^AC_OUTPUT" $dir/$file
    else
	echo "ERROR: Project \`$template' does not have a $file"
	exit 1
    fi
}

# Main ##############################

echo "E3D Wizard v$wizvers (y.copin@ipnl.in2p3.fr)"

parse_arg $*
if [ $add -eq 0 ]; then
    rm_project
    cp_project
    mv_package
else
    add_package
fi
update_am
update_in

exit 0

