#!/bin/sh

# Project configuration script used by Anjuta
# Should not be used manually

# Copyright (c) 2000 Kh. Nabakumar
# This is a free software; the Free Software Foundation
# gives unlimited permission to copy, distribute and modify it.

# Defaults:

project=
program=
version=0.1
author=$USER
srcdir=src
docdir=doc
pixdir=pixmaps
menu_entry=
menu_comment=
need_terminal=yes
use_header=no
file_header=
icon_file=
app_group=Applications
input_file=
output_file=
cur_year=`date +%Y 2>/dev/null`
cur_date=`date 2>/dev/null`

anj_prev=
for anj_option
do

  # If the previous option needs an argument, assign it.
  if test -n "$anj_prev"; then
    eval "$anj_prev=\"\$anj_option\""
    anj_prev=
    continue
  fi

  case "$anj_option" in
  -*=*) anj_optarg=`echo "$anj_option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) anj_optarg= ;;
  esac

  case "$anj_option" in

  -project | --project | -project-name | --project-name)
    anj_prev=project ;;
  -project | --project | project-name=* | --project-name=*)
    project="$anj_optarg" ;;

  -program | --program | -program-name | --program-name)
    anj_prev=program ;;
  -program=* | --program=* | -program-name=* | --program-name=*)
    program="$anj_optarg" ;;

  -author | --author | -author-name | --author-name)
    anj_prev=author ;;
  -author=* | --author=* | -author-name=* | --author-name=*)
    author="$anj_optarg" ;;

  -version | --version)
    anj_prev=version ;;
  -version=* | --version=*)
    version="$anj_optarg" ;;

  -srcdir | --srcdir)
    anj_prev=srcdir ;;
  -srcdir=* | --srcdir=*)
    srcdir="$anj_optarg" ;;

  -docdir | --docdir)
    anj_prev=docdir ;;
  -docdir=* | --docdir=*)
    docdir="$anj_optarg" ;;

  -pixdir | --pixdir)
    anj_prev=pixdir ;;
  -pixdir=* | --pixdir=*)
    pixdir="$anj_optarg" ;;

  -menu | --menu | -menu-entry | --menu-entry)
    anj_prev=menu_entry ;;
  -menu=* | --menu=* | -menu-entry=* | --menu-entry=*)
    menu_entry="$anj_optarg" ;;

  -menu-comment | --menu-comment)
    anj_prev=menu_comment ;;
  -menu-comment=* | --menu-comment=*)
    menu_comment="$anj_optarg" ;;

  -icon | --icon | -icon-file | --icon-file)
    anj_prev=icon_file ;;
  -icon=* | --icon=* | -icon-file=* | --icon-file=*)
    icon_file="$anj_optarg" ;;

  -app-group | --app-group)
    anj_prev=app_group ;;
  -app-group=* | --app-group=*)
    app_group="$anj_optarg" ;;

  -need-terminal | --need-terminal)
    anj_prev=need_terminal ;;
  -need-terminal=* | --need-terminal=*)
    need_terminal="$anj_optarg" ;;

  -use-header | --use-header)
    anj_prev=use_header ;;
  -use-header=* | --use-header=*)
    use_header="$anj_optarg" ;;

  -file-header | --file-header)
    anj_prev=file_header ;;
  -file-header=* | --file-header=*)
    file_header="$anj_optarg" ;;

  -input | --input)
    anj_prev=input_file ;;
  -input=* | --input=*)
    input_file="$anj_optarg" ;;

  -output | --output)
    anj_prev=output_file ;;
  -output=* | --output=*)
    output_file="$anj_optarg" ;;

  -show-version | --show-version | --show-ver | --show-ver | --show-v | -show-v)
    echo "anjuta_prjconf version 0.1.1";
    exit 0 ;;

  -help | --help | --hel | --he | --h | -h)
    cat << EOF

*WARNING* To be used by anjuta. Not to be used manually *WARNING*

Usage: anjuta_prjconf [options]
Options: [defaults in brackets after descriptions]

  --help                    print this message
  --project-name=NAME       Name of the project
  --program-name=NAME       Name of the program
  --author-name=NAME        Name of the author
                             [Can be used multiple times
                              for multiple author]
  --srcdir=DIR              Program sources in DIR
  --docdir=DIR              Program documents in DIR
  --pixdir=DIR              Program pixmaps in DIR
  --version=VERSION         Version of the project
  --menu_entry=TEXT         Gnome menu entry for the program
  --menu_comment=TEXT       Menu Tooltip for the menu entry
  --need_terminal=[yes/no]  Program needs terminal to run
  --use-header=[yes/no]     Use file heading [along with the next option] 
  --file-header=FILE        File heading with copyright statements
  --input=FILE              Input file
  --output=FILE             Output file
  
EOF
    exit 0 ;;

  -*=* | --*=*)
    unknown_opt="$anj_optarg" ;;
  -*)
    anj_prev=unknown_opt ;;

  *)
    nonopt="$ac_option" ;;

  esac
done

if test -n "$anj_prev"; then
  { echo "anjuta_prjconf: error: missing argument to --`echo $anj_prev | sed 's/_/-/g'`" 1>&2; exit 1; }
fi

if test -z "$project"; then
      { echo "anjuta_prjconf: error: No project name supplied" 1>&2; exit 1; }
fi

if test -z "$program"; then
      { echo "anjuta_prjconf: error: No program name supplied" 1>&2; exit 1; }
fi

if test -z "$input_file"; then
      { echo "anjuta_prjconf: error: No input file name supplied" 1>&2; exit 1; }
fi

if test -z "$output_file"; then
    output_file="$input_file"
fi

if test -z "$menu_entry"; then
    menu_entry="$project Version $version"
fi

if test -z "$menu_comment"; then
    menu_comment="$project Version $version"
fi

echo "Creating $output_file"

if test "x$use_header" = "xtrue" && test -f "$input_file" \
         && test -f "$file_header" ; then
    cat "$file_header" > "$input_file_$$.tmp" ;
    cat "$input_file" >> "$input_file_$$.tmp" ;
    rm -f "$input_file";
    mv -f "$input_file_$$.tmp" "$input_file";
fi

if test -f "$input_file" ; then
    cat "$input_file" | \
    sed s/@PROJECT__NAME@/"$project"/g | \
    sed s/@PROJECT__VERSION@/"$version"/g | \
    sed s/@PROJECT__AUTHOR@/"$author"/g | \
    sed s/@PROGRAM__NAME@/"$program"/g | \
    sed s/@SOURCE__DIRECTORY@/"$srcdir"/g | \
    sed s/@PIXMAPS__DIRECTORY@/"$pixdir"/g | \
    sed s/@DOCUMENTS__DIRECTORY@/"$docdir"/g | \
    sed s/@MENU__ENTRY@/"$menu_entry"/g | \
    sed s/@MENU__COMMENT@/"$menu_comment"/g | \
    sed s/@APP__GROUP@/"$app_group"/g | \
    sed s/@NEED__TERMINAL@/"$need_terminal"/g | \
    sed s/@ICON__FILE@/"$icon_file"/g | \
    sed s/@CURRENT__FILE@/"$output_file"/g | \
    sed s/@CURRENT__DATE@/"$cur_date"/g | \
    sed s/@CURRENT__YEAR@/"$cur_year"/g \
    > "$input_file_$$.sed";
    rm -f "$input_file";
    mv -f "$input_file_$$.sed" "$output_file";
else 
    echo "anjuta_prjconf: error: Cannot find $input_file"
fi
