#!/bin/bash
# autoproject.  Generated from autoproject.in by configure.
#
# Script to start a programing project
#
# Jim Van Zandt <jrv@vanzandt.mv.com> 1999-06-09, based on dh_make by:
# Craig Small, <csmall@debian.org> 5 March 1998, based on deb-make by:
# Christoph Lameter, <clameter@debian.org> October 10, 1996

set -e
#set -v

prefix=/usr
DATADIR=${prefix}/share
AWK=awk
NAME=
EMAIL=
theoptions=a:d:e:i:l:n:o:p:vVh

# for testing: SKELETONS="/home/local/src/autoproject/lib"
SKELETONS="$HOME/.autoproject /etc/autoproject $DATADIR/autoproject"

# The version number for the new project is set in configure.in, and
# available in a C program as the #defined macro VERSION.
VERSION=0.1.0

# check what parsing facilities are available
if getopt -T; then 
    gnu_getopt=0;
else 
    if [ $? = 4 ]; then gnu_getopt=1; else gnu_getopt=0; fi
fi

if [ $gnu_getopt = 1 ]; then 

# Parse the autoproject options

    TEMP=`getopt -n autoproject -o L::$theoptions --longoptions author:,debug,description:,email:,interface:,language:,name:,option:,parser:,verbose,help,version -- "$@"`
    one=1; two=2;

    if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

    # Note the quotes around `$TEMP': they are essential!
    eval set -- "$TEMP"
else
    echo "WARNING - GNU getopt extensions not available, so autoproject's long options and optional arguments won't work"
    one=0; two=0;
fi

STDOPTS=" dry-run no-warn output brief quiet verbose directory cd interactive "
STDLANGS=" c sh c++ fortran lex yacc awk "
STDIFS=" cli "
STDPARSERS=" argp autogen clig none "

while 
    if [ $gnu_getopt = 1 ]; then 
	c=$1; OPTARG=$2; true; 
    else 
	getopts L:$theoptions c ; 
    fi; do

    case "$c" in
	a|-a|--author) AUTHOR="$OPTARG"; shift $two ;;
	D|-D|--debug)
	    DEBUGGING=yes; shift $one ;;
	d|-d|--description)
	    DESCRIPTION="$OPTARG"; shift $two ;;
	e|-e|--email) EMAIL="$OPTARG"; shift $two ;;
	i|-i|--interface)
	    if echo "$STDIFS"|grep -q -- " $OPTARG "; then
		IFACE="$OPTARG"; shift $two;
	    else
		echo "unrecognized interface $OPTARG";
		echo "the known interfaces are: $STDIFS";
		exit 1;
	    fi ;;
	L|-L)
	    if [ "$OPTARG" = "" ]; then 
		SKELETONS="";
	    else
		if (echo $OPTARG|grep ^/ >/dev/null); then true; \
		else OPTARG=`pwd`/$OPTARG; fi
		SKELETONS="$OPTARG $SKELETONS";
	    fi;
	    shift $two;;
	l|-l|--language)
	    if echo "$STDLANGS"|grep -F -q -- " $OPTARG "; then
		PROJECT_LANG="$PROJECT_LANG $OPTARG "; shift $two;
	    else
		echo "unrecognized language $OPTARG";
		echo "the known languages are: $STDLANGS";
		exit 1;
	    fi ;;
	n|-n|--name)
	    NAME=$OPTARG; shift $two ;;
	o|-o|--option)
	    if echo "$STDOPTS"|grep -q -- " $OPTARG "; then
		OPTS="$OPTS $OPTARG"; shift $two;
	    else
		echo "unrecognized option $OPTARG";
		echo "the known options are: $STDOPTS";
		exit 1;
	    fi ;;
	p|-p|--parser)
	    if echo "$STDPARSERS"|grep -q -- " $OPTARG "; then
		PARSER="$OPTARG"; shift $two;
	    else
		echo "unrecognized parser generator $OPTARG";
		echo "the known interfaces are: $STDPARSERS";
		exit 1;
	    fi ;;
	v|-v|--verbose) VERBOSE=yes; shift $one ;;
	V|-V|--version)
	    echo "autoproject 0.20"
	    exit 0
	    ;;
	\?|h|-h|--help)
	    cat <<EOF
autoproject - start a programming project
usage: autoproject  [options]  [program_name]
options:
-a, --author NAME       specify the author of the program
-e, --email ADDR        specify the email address of the author
-d, --description TEXT  supply the short description
--debug                 leave intermediate files
-i, --interface TYPE    specify the type of user interface
                        (currently only cli, for command line interface)
-l, --language LANG     add LANG to the list of languages used
                        (currently accepted: $STDLANGS)
-L[DIR]                 Prepend DIR to the list of directories to
                        search for skeleton files.  If LIB is missing,
                        then the path is cleared.  
                        (default path: $SKELETONS)
-n, --name NAME         specify the name of the program
-o, --option OPT        add OPT to the list of long options accepted by 
                        the program, from among these: 
                       $STDOPTS
-p, --parser PARSER     specify the command line parser generator
                        (currently supported: $STDPARSERS)
-v, --verbose           display more information
-h, --help              display this help
-V, --version           display autoproject name and version
EOF
	    exit 0
	    ;;
	--) shift $one ; break ;;
	*) echo "Internal error!  no case for option \"$c\"" ; exit 1 ;;
    esac
done
if [ $gnu_getopt != 1 ]; then shift `expr $OPTIND - 1`; fi

# Prompt user to select from a list
choose_from_list(){
QUERY=$1; CHOICES=$2; DEFAULT=$3; HELP=$4;
finished=no
while [ $finished != yes ]; do
    echo "$QUERY";
    echo -n "    select from: $CHOICES [$DEFAULT]: "; read ans;
    if [ "$ans" = "" ]; then ans="$DEFAULT"; fi
    if [ "$ans" = "?" ]; then 
	if [ "$HELP" = "" ]; then
	    echo "    sorry, no help is available"
	else
	    echo "$HELP";
	fi
    else if echo "$CHOICES" | grep -q -- " $ans "; then
	finished=yes;
    else echo "    unrecognized choice $ans"; fi; fi
done
}

# prompt user for a string
type_string() {
    QUERY=$1; DEFAULT=$2; HELP=$3;
    finished=no
    while [ $finished != yes ]; do
	echo -n "$QUERY [$DEFAULT]: "; read ans;
	if [ "$ans" = "" ]; then ans="$DEFAULT"; fi
	if [ "$ans" = "?" ]; then
	    if [ "$HELP" = "" ]; then
		echo "sorry, no help is available"
	    else
		echo "$HELP";
	    fi
	else 
	    finished=yes;
	fi
    done
}

# Get all the values we need

LEGALNAME=no
if [ "$NAME" = "" ];  then
    if [ "$1" != "" ]; then
	NAME="$1"
    fi
fi
while [ $LEGALNAME = no ]; do
    if [ "$NAME" = "" ]; then
	echo -n "What is the program name? "; read NAME;
    fi
    if expr "$NAME" : '[a-z][_a-z0-9]*$' >/dev/null = 0; then
	echo "    Illegal program name $NAME. Must be lowercase letters and digits or \`_'."
	NAME=""
    else
	LEGALNAME=yes
    fi
done
CAPNAME=`echo "$NAME"|tr '[a-z]' '[A-Z]'`

if [ "$DESCRIPTION" = "" ]; then
    echo ""
    echo "Please describe in one line what $NAME will do:"
    read DESCRIPTION
fi

DESCRIPTION=`echo $DESCRIPTION | sed 's,/,\\\\/,g'`
DESCRIPTIONC=`echo $DESCRIPTION | sed 's,",\\\\\\\\",g'`

if [ "$PROJECT_LANG" = "" ]; then 
    choose_from_list "The main program will be written in what language?" \
	"$STDLANGS" "c" ""
    PROJECT_LANG=" $ans "; 

    until
	choose_from_list "What other languages will be used in the package?" \
	    "$STDLANGS none " "none" ""
	[ "$ans" = "none" ]; do
	PROJECT_LANG="$PROJECT_LANG $ans "; 
    done
fi

PRIMARY_LANG=`echo $PROJECT_LANG | $AWK '{print $1}'`

if [ "$IFACE" = "" ]; then 
# this section will be enabled only after a GUI is supported
#    choose_from_list "What type of interface will the program use?" \
#	 "$STDIFS" "cli" \
#	 "cli = command line interface"
#    IFACE="$ans"; 
    IFACE=cli;
fi

if [ "$PARSER" = "" ]; then
    choose_from_list "What command line parser generator will be used?" \
	"$STDPARSERS" "none" ""
    PARSER="$ans";
fi	


echo "Please indicate which of the following standard options $NAME will use:"
for op in $STDOPTS; do
    if echo " $OPTS "|grep -q -- " $op "; then
	true;
    else
	echo -n "    $op? [yN] "; read ans;
	if [ "$ans" = "y" -o "$ans" = "Y" ]; then
	    OPTS="$OPTS $op";
	fi
    fi
done

# accept zero padding only if the GNU extension is not available
if [ `date +%-d` = %-d ]; then 
    # prevent user's locale settings from affecting %B
    DATE=`LC_ALL=POSIX date "+%B %d, %Y"`
else
    DATE=`LC_ALL=POSIX date "+%B %-d, %Y"`
fi
YEAR=`date "+%Y"`
ISODATE=`date "+%Y-%m-%d"`


if [ "$AUTHOR" = "" ]; then
    AUTHOR=`$AWK -F: -vUSER=$USER '$1 == USER { print $5; }' /etc/passwd`

    if [ "$AUTHOR" = "" -a -x /usr/bin/ypmatch ]; then
	# Give NIS a try
	AUTHOR=`ypmatch $USER passwd.byname|$AWK -F: '{ print $5; }'`
    fi
    if echo $AUTHOR | grep -q -- "\,"; then
    	X=`expr index "$AUTHOR" ","`
   	X=`expr $X - 1` || true
   	AUTHOR=`expr substr "$AUTHOR" 1 $X` || true
    fi
    type_string "What is the name of the author?" "$AUTHOR" ""
    AUTHOR="$ans"
fi


if [ "$EMAIL" = "" ]; then
    if [ -s /etc/mailname ]; then
	EMAIL="$USER@`cat /etc/mailname`"
	else if [ -s /etc/news/whoami ]; then
	    EMAIL="$USER@`cat /etc/news/whoami`"
	else EMAIL="`whoami`@`hostname`"
	fi
    fi
    type_string "What is the email address of the author?" "$EMAIL" ""
    EMAIL="$ans"
fi
# `@' must be escaped in a .texinfo file
EEMAIL=`echo $EMAIL|sed s/@/@@/`


echo "Program Name		: $NAME"
echo "Description	        : $DESCRIPTION"
echo "Version			: $VERSION"
echo "Language		:$PROJECT_LANG"
echo "Interface		: $IFACE"
echo "Parser Generator	: $PARSER"
echo "Long Options		:$OPTS"
echo "Author      		: $AUTHOR"
echo "Email-Address		: $EMAIL"
echo "Date		        : $DATE"



# Create the source directory
mkdir "$NAME"
cd "$NAME"


# Create a small awk script to select the appropriate options fragments

for op in $STDOPTS; do
    if echo " $OPTS " | grep -q -- " $op " ; then
	# lines like "@item --output=@var{name}" are not special
	cat >>optionsub <<EOF
/^@$op@\$/,/^@@\$/{if(\$0~/^@.*@\$/)next}
EOF
    else
	cat >>optionsub <<EOF
/^@$op@\$/,/^@@\$/{next}
EOF
    fi
done
cat >>optionsub <<EOF
{print}
EOF

# Customize files

process_file()
{
	sed -e "s/#NAME#/$NAME/g"			\
		-e "s/#CAPNAME#/$CAPNAME/g"		\
		-e "s/#VERSION#/$VERSION/g"		\
		-e "s/#EMAIL#/$EMAIL/g"			\
		-e "s/#EEMAIL#/$EEMAIL/g"		\
		-e "s/#DATE#/$DATE/g"			\
		-e "s/#ISODATE#/$ISODATE/g"		\
		-e "s/#YEAR#/$YEAR/g"			\
		-e "s/#AUTHOR#/$AUTHOR/g"		\
		-e "s/#DESCRIPTION#/$DESCRIPTION/g"	\
		-e "s/#DESCRIPTIONC#/$DESCRIPTIONC/g"
}

PROCESSED=0
for LIB in $SKELETONS; do
    if [ "$VERBOSE" = "yes" ]; then
	echo "looking for skeletons in: $LIB"
    fi
    for IF in $IFACE all; do
	for L in $PRIMARY_LANG all; do
	    for P in $PARSER all; do
    		DIR=$LIB/$IF/$L/$P
    		if [ -d $DIR ]; then
    		    X=`(cd $DIR;ls)`
    		    for i in $X; do
    			if [ ! -f $i ]; then
    			    if [ "$VERBOSE" = "yes" ]; then
    				echo "  processing: $DIR/$i"
    			    fi
    			    process_file <$DIR/$i |$AWK -f optionsub >$i
			    if [ -x $DIR/$i ]; then chmod +x $i; fi
			    PROCESSED=`awk "BEGIN{print $PROCESSED+1}"`
    			fi
    		    done
    		fi
	    done
	done
    done
done

echo "$PROCESSED files processed"
if [ ! -f configure.in ]; then
    echo "configure.in not found - either skeleton library was not found, 
or requested combination of options is not available"
fi

if [ "$DEBUGGING" = "yes" ]; then true; else rm optionsub; fi

# rename some files
X="`ls program* 2>/dev/null`"
if [ "$X" ]; then
    for i in $X; do
	mv $i `echo $i|sed -e "s/^program/$NAME/"`
    done
fi

# insert language-specific macros into configure.in
insert(){
if echo "$PROJECT_LANG"|grep -F -q -- " $1 "; then
    echo "using $1 - adding $2 to configure.in"
    $AWK "{print}/^AC_PROG_CC/{print \"$2\"}" \
	configure.in >tmp && mv tmp configure.in
fi
}

insert c++ AC_PROG_CXX
insert fortran AC_PROG_F77
insert fortran AC_F77_LIBRARY_LDFLAGS
insert yacc AC_PROG_YACC
insert lex AM_PROG_LEX
insert awk AC_PROG_AWK

# ensure .def file is newer than the skeleton man page
if [ -f checkopt.def ]; then
    if [ "$VERBOSE" = "yes" ]; then
	echo touching checkopt.def
    fi
    sleep 1; 
    touch checkopt.def; 
    mv checkopt.def $NAME-opt.def
    sed "s,checkopt,$NAME-opt,g" Makefile.am > Makefile.tmp
    mv -f Makefile.tmp Makefile.am
fi

if [ -x postinst ]; then
    ./postinst && rm postinst
fi

aclocal
autoheader
automake --add-missing
autoconf
./configure

exit 0
