#! /bin/sh

# $Id: configure,v 1.29 2007/08/23 07:12:48 akf Exp $
#
# This file is part of AKFQuiz
#
# AKFQuiz is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# AKFQuiz is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

#####################################################################
# configure here:

PREFIX=/usr/local
CGIDIR=/usr/lib/cgi-bin
EXAMDIR=/var/lib/akfquiz

# for modified versions use a variant-name please
VARIANT=""
#VARIANT="-cvs"
NAME="AKFQuiz"

MAINVERSION=4
SUBVERSION=4
REVISION=1
PKGVERSION=0

VERSION=$MAINVERSION.$SUBVERSION.$REVISION
SVERSION=$MAINVERSION$SUBVERSION$REVISION

SRCDIR="."

# either a E-Mail address, or empty
BUGMAIL=bug-akfquiz@akfoerster.de

#####################################################################
# don't change these values

SOUND=no

#####################################################################
# functions

help() {
cat <<EOF
./configure [options]

options:
  -h | --help
  --srcdir=
  --prefix=
  --cgidir=
  --examdir=
  --variant=
  --arch=
  --sys=
  --with-fpc
  --with-gpc
  --with-grx
  --with-xw32
  --with-xarm
  --enable-exam=no
  --disable-exam
  --enable-sound
  --disable-sound
  
The options "--enable-sound" or "--disable-sound" have no influence on 
grquiz
EOF
exit
}

warn() {
  echo "$@" 1>&2
  }

error() {
  echo "error: $@" 1>&2
  exit 1
  }
  
getcompiler() {
  # search for available compiler
  echo "checking for fpc or gpc..."
  if which fpc
  then COMPILER=fpc
  elif which gpc
  then COMPILER=gpc
  else error "neither gpc nor fpc found"
  fi
  }

create_rc() {
  sed < "srcbin/w32/rc.in" > "srcbin/w32/$1.rc" \
    -e "s/^\(FILEVERSION\).*/\1 $MAINVERSION, $SUBVERSION, $REVISION, 0/" \
    -e "s/^\(PRODUCTVERSION\).*/\1 $MAINVERSION, $SUBVERSION, $REVISION, 0/" \
    -e "s/\(.*VALUE \"FileDescription\", *\)\".*\"/\1\"$2\"/" \
    -e "s/\(.*VALUE \"FileVersion\", *\)\".*\"/\1\"$VERSION$VARIANT\"/" \
    -e "s/\(.*VALUE \"InternalName\", *\)\".*\"/\1\"$1\"/" \
    -e "s/\(.*VALUE \"OriginalFilename\", *\)\".*\"/\1\"$1.exe\"/" \
    -e "s/\(.*VALUE \"Comments\", *\)\".*\"/\1\"$3\"/" \
    -e "s/\(.*VALUE \"ProductName\", *\)\".*\"/\1\"$NAME$VARIANT\"/" \
    -e "s/\(.*VALUE \"ProductVersion\", *\)\".*\"/\1\"$VERSION\"/"
    }

notsupported() {
  warn "configure: warning: $1 not supported"
  }

#####################################################################
# main program:

SYS=`uname -s`
ARCH=`uname -m`
# rename architecture
case "$ARCH" in
   i?86) ARCH=x86 ;;
esac

# system specific settings
case "$SYS" in
  *Linux*) NEEDPTHREAD="yes" ;;
  *) NEEDPTHREAD="no" ;;
esac

# check parameter
while [ $# -gt 0 ] 
do
  case "$1" in
    -h | --help)   help ;;
    --target=gpc | --target=fpc | --target=xw32 | --target=xarm)
                   error "old usage for --target no longer supported!" ;;
    #--srcdir=*)    SRCDIR=`echo "$1" | sed -e "s/--srcdir=\(.*\)/\1/"` ;;
    --srcdir=*)    notsupported --srcdir ;; # yet to be implemented
    --build=*)     notsupported --build ;; # yet to be implemented
    --host=*)      notsupported --host ;;  # yet to be implemented
    --target=*)    notsupported --target ;; # yet to be implemented
    --with-gpc)    COMPILER=gpc ;;
    --with-grx)    COMPILER=grx ;;
    --with-fpc)    COMPILER=fpc ;;
    --with-xw32)   COMPILER=xw32 ;;
    --with-xarm)   COMPILER=xarm ;;
    --with-*)      ;; # ignore (see GNU Coding Standards)
    --gpc)         COMPILER=gpc ;;
    --grx)         COMPILER=grx ;;
    --fpc)         COMPILER=fpc ;;
    --xw32)        COMPILER=xw32 ;;
    --xarm)        COMPILER=xarm ;;
    --enable-sound|--enable-sound=yes|--disable-sound=no) SOUND=yes ;;
    --disable-sound|--disable-sound=yes|--enable-sound=no) SOUND=no ;;
    --enable-exam=no) EXAMDIR="" ;;
    --enable-exam=yes) ;; # default behaviour
    --disable-exam)   EXAMDIR="" ;;
    --disable-exam=yes) EXAMDIR="" ;;
    --disable-exam=no) ;; # default behaviour
    --enable-*)    ;; # ignore (see GNU Coding Standards)
    --disable-*)    ;; # ignore (see GNU Coding Standards)
    --prefix=*)    PREFIX=`echo "$1" | sed -e "s/--prefix=\(.*\)/\1/"` ;;
    --mandir=*)    notsupported --mandir ;; # yet to be implemented
    --infodir=*)    ;; # ignore - not used
    --sysconfdir=*) ;; # ignore - not used
    --libexecdir=*) ;; # ignore - not used
    --cgidir=*)    CGIDIR=`echo "$1" | sed -e "s/--cgidir=\(.*\)/\1/"` ;;
    --cgipath=*)   CGIDIR=`echo "$1" | sed -e "s/--cgipath=\(.*\)/\1/"` ;;
    --examdir=*)   EXAMDIR=`echo "$1" | sed -e "s/--examdir=\(.*\)/\1/"` ;;
    --variant=*)   VARIANT=-`echo "$1" | sed -e "s/--variant=\(.*\)/\1/"` ;;
    --arch=*)      ARCH=`echo "$1" | sed -e "s/--arch=\(.*\)/\1/"` ;;
    --sys=*)       SYS=`echo "$1" | sed -e "s/--sys=\(.*\)/\1/"` ;;
    -*) error "unknown parameter $1" ;;
    *) ;; # ignore for now
  esac
  shift
done

test -z "$COMPILER" && getcompiler

test "$COMPILER" = "xarm" && ARCH=arm
test "$COMPILER" = "xw32" && { SYS=w32; NEEDPTHREAD="no"; }

# disable exam-mode on some systems
case $SYS in
  w32|mingw32*|MINGW32*) EXAMDIR="" ;;
esac
  
# check for needed tools
case $COMPILER in
  grx) echo "checking for binobj... "
       which binobj || error "binobj not found - should come with gpc" ;;
esac

VERSION=$MAINVERSION.$SUBVERSION.$REVISION
SVERSION=$MAINVERSION$SUBVERSION$REVISION

#if [ ! -r $SRCDIR/srcbin/uakfquiz.pas ]
#then
#  error cannot find sources in srcdir "$SRCDIR"
#fi

# show settings:
cat << EOF
prefix:   $PREFIX
cgidir:   $CGIDIR
examdir:  $EXAMDIR
Name:     $NAME
Variant:  $VARIANT
Version:  $VERSION
SVersion: $SVERSION
Compiler: $COMPILER
Sys:      $SYS
Arch:     $ARCH
BugMail:  $BUGMAIL
Sound:    $SOUND
NeedPthread: $NEEDPTHREAD
EOF

# change to default directory
cd $(dirname $0)

sed < Makefile.in > Makefile \
    -e "s%^\(srcdir *= *\).*%\1$SRCDIR%" \
    -e "s/ARCH=.*/ARCH=$ARCH/" \
    -e "s/SYS=.*/SYS=$SYS/" \
    -e "s/^\(VERSION *= *\).*/\1$VERSION/" \
    -e "s/^\(VARIANT *= *\).*/\1$VARIANT/"


SOUNDCOMMENTED=""
test "$SOUND" = "no" && SOUNDCOMMENTED="#"
test "$NEEDPTHREAD" = "no" && PTHREADCOMMENTED="#"
sed < srcbin/Makefile.$COMPILER > srcbin/Makefile \
   -e "s/.*\(DEFINES.*SdlSoundForAll\)$/$SOUNDCOMMENTED\1/" \
   -e "s/.*\(DEFINES.*NEEDPTHREAD\)$/$PTHREADCOMMENTED\1/"

sed < srcbin/common.in > srcbin/common.mak \
    -e "s%^\(srcdir *= *\).*%\1$SRCDIR%" \
    -e "s%^\(prefix *= *\).*%\1$PREFIX%" \
    -e "s%^\(cgidir *= *\).*%\1$CGIDIR%" \
    -e "s%^\(examdir *= *\).*%\1$EXAMDIR%" \
    -e "s/^\(SVERSION *= *\).*/\1$SVERSION/"

cat > srcbin/config.inc <<EOF
{ this file was generated automatically using configure
  do not edit this by hand }

const AKFQuizName = '$NAME$VARIANT';
const AKFQuizVersion = '$VERSION';
const BugMail = '$BUGMAIL';
const ExamDir = '$EXAMDIR';

EOF

cat > srcbin/getquiz <<EOF
#!/bin/sh
cp -i $PREFIX/share/akfquiz/html/* .
EOF

if [ -w ../autopackage/default.apspec ]
then
  mv ../autopackage/default.apspec ../autopackage/default.apspec.old
  sed < ../autopackage/default.apspec.old > ../autopackage/default.apspec \
      -e "s/^\(SoftwareVersion: *\).*/\1$VERSION/"
fi

# Windows stuff

cat > srcbin/w32/akfquiz.nsh <<EOF
!define NAME "$NAME$VARIANT"
!define MAINNAME "$NAME"
!define VERSION $VERSION
!define PKGVERSION $PKGVERSION
!define VARIANT "$VARIANT"
EOF

create_rc scrquiz "scrquiz: AKFQuiz in text-mode" "ported from GNU\/Linux"
create_rc mkquiz "mkquiz: make HTML quiz-file for JavaScript" \
          "ported from GNU\/Linux"
create_rc cgiquiz "cgiquiz: CGI program for AKFQuiz" "ported from GNU\/Linux"
create_rc linequiz "linequiz: AKFQuiz line oriented" "ported from GNU\/Linux"
create_rc grquiz "grquiz: AKFQuiz in graphic-mode" "ported from GNU\/Linux"
