#!/bin/bash
# Generated automatically from autoproject.in by configure.
#
# Script to start a programming 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

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

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

# Parse the autoproject options

TEMP=`getopt -n autoproject -o a:d:e:i:l:L::n:o:p:vVh --longoptions author:,debug,description:,email:,interface:,language:,name:,option:,parser:,verbose,help,version -- "$@"`

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

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

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

while true ; do
    case "$1" in
	-a|--author) AUTHOR="$2"; shift 2 ;;
	--debug)
	    DEBUGGING=yes; shift ;;
	-d|--description)
	    DESCRIPTION="$2"; shift 2 ;;
	-e|--email) EMAIL="$2"; shift 2 ;;
	-i|--interface)
	    if echo "$STDIFS"|grep -q -- " $2 "; then
		IFACE="$2"; shift 2;
	    else
		echo "unrecognized interface $2";
		echo "the known interfaces are: $STDIFS";
		exit 1;
	    fi ;;
	-L)
	    if [ "$2" = "" ]; then LIBS=""; else LIBS="$2 $LIBS"; fi;
	    shift 2;;
	-l|--language)
	    if echo "$STDLANGS"|grep -F -q -- " $2 "; then
		PROJECT_LANG="$PROJECT_LANG $2 "; shift 2;
	    else
		echo "unrecognized language $2";
		echo "the known languages are: $STDLANGS";
		exit 1;
	    fi ;;
	-n|--name)
	    NAME=$2; shift 2 ;;
	-o|--option)
	    if echo "$STDOPTS"|grep -q -- " $2 "; then
		OPTS="$OPTS $2"; shift 2;
	    else
		echo "unrecognized option $2";
		echo "the known options are: $STDOPTS";
		exit 1;
	    fi ;;
	-p|--parser)
	    if echo "$STDPARSERS"|grep -q -- " $2 "; then
		PARSER="$2"; shift 2;
	    else
		echo "unrecognized parser generator $2";
		echo "the known interfaces are: $STDPARSERS";
		exit 1;
	    fi ;;
	-v|--verbose) VERBOSE=yes; shift ;;
	-V|--version)
	    echo "autoproject 0.6.2"
	    exit 0
	    ;;
	-h|--help)
	    echo "autoproject - start a programming project"
	    echo "usage: autoproject  [options]  program_name"
	    echo "options:"
	    echo "-a, --author NAME       specify the author of the program"
	    echo "-e, --email ADDR        specify the email address of the author"
	    echo "-d, --description TEXT  supply the short description"
	    echo "--debug                 leave intermediate files"
	    echo "-i, --interface TYPE    specify the type of user interface"
	    echo "                        (currently only cli, for command line interface)"
	    echo "-l, --language LANG     add LANG to the list of languages used"
	    echo "                        (currently accepted: $STDLANGS)"
	    echo "-L[DIR]                 Prepend DIR to the list of directories to"
	    echo "                        search for skeleton files.  If LIB is missing,"
	    echo "                        then the path is cleared."
	    echo "-n, --name NAME         specify the name of the program"
	    echo "-o, --option OPT        add OPT to the list of long options accepted by the program"
	    echo "                        (from among these: $STDOPTS)"
	    echo "-p, --parser PARSER     specify the command line parser generator"
	    echo "                        (currently supported: $STDPARSERS)"
	    echo "-v, --verbose           display more information"

	    echo "-h, --help              display this help"
	    echo "-V, --version           display autoproject name and version"
	    exit 0
	    ;;
	--) shift ; break ;;
	*) echo "Internal error!  no case for option \"$1\"" ; exit 1 ;;
    esac
done

# 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
while [ $LEGALNAME = no ]; do
    if [ "$NAME" = "" ];  then
	if [ "$1" != "" ]; then
	    NAME="$1"
	fi
    fi
    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



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=`$AWK 'BEGIN{print ARGV[1]}' $PROJECT_LANG`

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


DATE=`date "+%B %-d, %Y"`
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`
    	AUTHOR=`expr substr "$AUTHOR" 1 $X`
    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


echo "Program Name		: $NAME"
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
	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/#DATE#/$DATE/g" \
		-e "s/#ISODATE#/$ISODATE/g" \
		-e "s/#YEAR#/$YEAR/g" \
		-e "s/#AUTHOR#/$AUTHOR/g" \
		-e "s/#DESCRIPTION#/$DESCRIPTION/g"
}

for LIB in $LIBS; 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
    			fi
    		    done
    		fi
	    done
	done
    done
done

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

# `@' must be escaped in a .texinfo file
if [ "*.texinfo" != "\*.texinfo" ]; then
    for f in *.texinfo; do
	EEMAIL=`echo $EMAIL|sed s/@/@@/`
	sed "s/$EMAIL/$EEMAIL/g" $f >foo && mv foo $f
    done
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 AC_PROG_LEX
insert lex AC_DECL_YYTEXT
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; 
fi

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

automake --add-missing
aclocal
autoconf
./configure

exit 0
