#!/bin/sh

# Configure GPC
#
# Copyright (C) 1998-2005 Free Software Foundation, Inc.
#
# Authors: Frank Heckenbach <frank@pascal.gnu.de>
#          Peter Gerwinski <peter@gerwinski.de>
#
# This file is part of GNU Pascal.
#
# GNU Pascal 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 2, or (at your option)
# any later version.
#
# GNU Pascal 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 GNU Pascal; see the file COPYING. If not, write to the
# Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

# This script requires bash. Since bash cannot be assumed to be in
# /bin, /usr/bin or any other certain place, we cannot use it in the
# first line. So we use /bin/sh, which can be assumed to exist. Then
# we check if it's actually bash, and if not, try to re-run the
# script with bash.
if [ x"$BASH" = x ]; then
  if [ x"$RERUN_BASH_TRIED" != x ]; then
    echo "`basename "$0"`: cannot run, \`bash' is either not bash or a very old version" >&2
    exit 1
  else
    RERUN_BASH_TRIED=1; export RERUN_BASH_TRIED
    exec bash "$0" "$@"
    echo "`basename "$0"`: cannot run bash" >&2
    exit 1
  fi
fi
RERUN_BASH_TRIED=""

if [ x"$GPC_DEFAULT_PREFIX" = x ]; then
  echo "GPC_DEFAULT_PREFIX must be set" >&2
  exit 1
fi

if [ x"$GPC_SRCDIR" = x ]; then
  echo "GPC_SRCDIR must be set" >&2
  exit 1
fi

dose=i386-pc-msdosdjgpp
if [ -e "$GPC_SRCDIR/gcc/p/gpc.c" ]; then
  srcdir="$GPC_SRCDIR/gcc/p"
elif [ -e "$GPC_SRCDIR/p/gpc.c" ]; then
  srcdir="$GPC_SRCDIR/p"
  dose=i386-pc-go32
else
  echo "Invalid GPC_SRCDIR" >&2
  exit 1
fi

build="`gcc -v 2>&1 | head -1 | sed -ne 's,^.*gcc-lib/\([^/]*\)/.*$,\1,p'`"

if [ x"$build" = x ]; then
  echo "Can't guess build." >&2
  exit 1
fi

if [ x"$1" = x"-h" -o x"$1" = x"--help" -o $# -eq 0 -o $# -gt 4 ]; then
  echo "Usage: `basename "$0"` target [host [target-alias] [prefix]]
  host defaults to $build
  target defaults to host
  prefix defaults to \\\$\$DJDIR for $dose host, $GPC_DEFAULT_PREFIX otherwise
  target-alias (<= 8 chars) is required iff host is $dose" >&2
  exit 1
fi

if [ x"`find .`" != x"." ]; then
  echo "`basename "$0"` should be run in an empty directory" >&2
  exit 1
fi

target="$1"
host="$2"

if [ x"$host" != x ]; then
  CC="$GPC_DEFAULT_PREFIX/bin/$host-gcc"
else
  host="$build"
  CC=""
fi
export CC

if [ x"$target" = x ]; then
  target="$host"
fi

if [ x"$host" = x"$dose" ]; then
  if [ x"$3" = x ]; then
    echo "`basename "$0"`: need at least 3 parameters for $dose host" >&2
    exit 1
  fi
  talias="$3"
  shift
else
  talias="$target"
  if [ x"$4" != x ]; then
    echo "`basename "$0"`: 4 parameters allowed only for $dose host" >&2
    exit 1
  fi
fi

echo "build=$build host=$host target=$target target_alias=$talias" > cfgpc.platforms

prefix="$3"

if [ x"$prefix" = x ]; then
  if [ x"$host" = x"$dose" ]; then
    prefix='\$$DJDIR'
  else
    prefix="$GPC_DEFAULT_PREFIX"
  fi
fi

quiet="> /dev/null --quiet"

eval "$GPC_SRCDIR/configure" $quiet '--prefix=$prefix --build=$build --host=$host --target=$target --enable-languages=c,pascal' || exit 1
