#!/bin/sh

# Make GPC
#
# Copyright (C) 1998-2006 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"
  srcparentdir="$GPC_SRCDIR/gcc"
elif [ -e "$GPC_SRCDIR/p/gpc.c" ]; then
  srcdir="$GPC_SRCDIR/p"
  srcparentdir="$GPC_SRCDIR"
  dose=i386-pc-go32
else
  echo "Invalid GPC_SRCDIR" >&2
  exit 1
fi

if [ x"$1" = x"-b" ]; then
  bindist=y
  shift
else
  bindist=""
fi

DEFAULT_MAKEFLAGS="-s"
DEFAULT_CFLAGS="-O2 -s"

if [ x"$1" = x-h -o x"$1" = x--help -o $# -gt 2 ]; then
  echo "Usage: `basename "$0"` [-b] [Makeflags] [CFlags]
  -b: make a pascal.bindist at the end
  Makeflags default to $DEFAULT_MAKEFLAGS
  CFlags default to $DEFAULT_CFLAGS
  " >&2
  exit 1
fi

if [ ! -e cfgpc.platforms ]; then
  echo "cfgpc.platforms not found. Please run cfgpc first." >&2
  exit 1
fi

. cfgpc.platforms

if [ x"$1" != x ]; then
  MAKEFLAGS="$1"
else
  MAKEFLAGS="$DEFAULT_MAKEFLAGS"
fi

#MAKEFLAGS="`echo "$MAKEFLAGS" | sed -e 's/-s//'`"

if [ x"$target" != x"$host" ]; then
  MAKEFLAGS="$MAKEFLAGS GPCSOLIBDIR=$target_alias/lib"
fi

# @@ Must be passed to gcc/Makefile
if [ x"$host" != x"$build" ]; then
  MAKEFLAGS="$MAKEFLAGS \
    HOST_CC=gcc \
    CC=$GPC_DEFAULT_PREFIX/bin/$host-gcc \
    GCC_FOR_TARGET=$GPC_DEFAULT_PREFIX/bin/$target-gcc \
    GPC_FOR_TARGET=$GPC_DEFAULT_PREFIX/bin/$target-gpc \
    AR_FOR_TARGET=$GPC_DEFAULT_PREFIX/$target/bin/ar \
    RANLIB=$GPC_DEFAULT_PREFIX/$target/bin/ranlib \
    FLOAT_H=$GPC_DEFAULT_PREFIX/$target/include/float.h"
fi

if [ x"$host" = x"$dose" ]; then
  MAKEFLAGS="$MAKEFLAGS SYMLINK=cp exeext=.exe target_alias=$target_alias version=`sed -e 's/.*\"\([^ \"]*\)[ \"].*/\1/;s/\./#dot#/;s/\.//g;s/#dot#/./' < "$srcparentdir/version.c"` program_transform_cross_name=s,^,$target_alias, GPC_EXTRA_INSTALL_FLAGS_TO_PASS=prefix="
  MAKEGPCBINDIST='mkdjgppgpczip >&2 $(gpc_major) $(gpc_minor) ../gpc-$$version-$(target_alias)'
fi

if [ x"$2" != x ]; then
  CFLAGS="$CFLAGS $2"
else
  CFLAGS="$DEFAULT_CFLAGS"
fi

#EF_ALLOW_MALLOC_0=1; export EF_ALLOW_MALLOC_0 # for libefence

ALL_MAKEFLAGS='$MAKEFLAGS LANGUAGES="C pascal" CFLAGS="$CFLAGS" GPC_TARFLAGS="--owner=root --group=root --mode=u=rwX,go=rX -czf"'
if [ x"$MAKEGPCBINDIST" != x ]; then
  ALL_MAKEFLAGS="$ALL_MAKEFLAGS "'MAKEGPCBINDIST="$MAKEGPCBINDIST"'
fi

logflags ()
{
  : > mkgpc.makeflags
  for flag; do
    echo -n "'$flag' " >> mkgpc.makeflags
  done
  echo >> mkgpc.makeflags
}

eval logflags $ALL_MAKEFLAGS

eval make $ALL_MAKEFLAGS $MG_MAKEFLAGS || exit 1

if [ x"$host" != x"$build" -a x"$target" != x"$host" ]; then
  # GCC's Makefile doesn't seem to support this case completely,
  # as it knows only all.cross and all.build, but not "all.build.cross".
  # So we make the missing pascal.all.cross here.
  if [ ! -e gpc-cross* ]; then
    eval make pascal.all.cross $ALL_MAKEFLAGS $MG_MAKEFLAGS || exit 1
  fi
fi

if [ x"$host" = x"$dose" ]; then
  STUBEDIT="$GPC_DEFAULT_PREFIX/$dose/bin/stubedit"
  for file in gpc1.exe cc1.exe; do
    # Don't stubedit files that weren't changed to avoid triggering
    # unnecessary dependencies in the next make run.
    if $STUBEDIT -v "$file" 2>&1 | grep '2048k.*stack space' > /dev/null; then
      :
    else
      $STUBEDIT "$file" minstack=2m || exit 1
    fi
  done
fi

if [ x"$bindist" != x ]; then
  #if [ x"$silent" = x ]; then echo "  Making binary distribution."; fi
  umask 022
  eval make pascal.bindist $ALL_MAKEFLAGS $MG_MAKEFLAGS || exit 1
fi
