#!/bin/sh

# Copyright (C) 1998-2004 Free Software Foundation, Inc.
#
# Author: Frank Heckenbach <frank@pascal.gnu.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 [ $# -lt 4 ]; then
  echo "Usage: `basename "$0"` major minor zipfilename files ..." >&2
  echo '  $GPC_STABILITY must be alpha, beta or gamma' >&2
  exit 1
fi

zipname="$3"
date="`echo $zipname | sed -e 's/^.*-\([0-9]*\)-.*$/\1/'`"
target="`echo $zipname | sed -e 's/^.*-\([^-]*\)$/\1/'`"

case "$GPC_STABILITY" in
  alpha) a=ab; al=" alpha $date" ;;
  beta)  a=bb; al=" beta $date"  ;;
  gamma) a=""; al=""             ;;
  *)     echo '$GPC_STABILITY must be alpha, beta or gamma' >&2; exit 1
esac

mftversion="$1$2$a"
version="$1.$2$al"

if [ x"$target" = x"djgpp" ]; then
  cross=""
  crosstarget=""
  mftname="gpc$mftversion"
else
  cross=" cross"
  crosstarget=" for `echo $target | cut -c 1 | tr a-z A-Z``echo "$target" | cut -c 2-`"
  mftname="gpc`echo "$target" | cut -c 1`$mftversion"
fi

(
  cd ./info
  mmv \*.info "#1.inf" || exit 1
  mmv \*.info-\* "#1.i#2" || exit 1
  for file in *; do
    sed -e 's/\.info-/.i/g;s/\.info/.inf/g' < "$file" > tempfile || exit 1
    mv -f tempfile "$file" || exit 1
  done
)
(
  cd ./lib/gcc-lib/*/*
  rm libgcc.a # @@
  # "Fix" file names with base names longer than 8 characters
  # (e.g. crtbeginS.o -> cbeginS.o for a DJGPP -> Linux cross compiler)
  for a in `find -maxdepth 1 -name "?????????*.*"`; do
    a="`echo "$a" | sed -e 's,^./,,'`"
    b="`echo "$a" | sed -e 's,\([^/]\)[^/][^/]\([^/]*\)$,\1\2,'`"
    echo "  Renaming \`$a' to \`$b'" >&2
    sed -e "s,$a,$b,g" < specs > specs.new || exit 1
    mv -f specs.new specs || exit 1
    mv "$a" "$b"
  done
)

mkdir manifest
echo "$mftname GNU Pascal$cross compiler$crosstarget version $version, binaries and documentation" > "manifest/$mftname.ver"
find * -type f > "manifest/$mftname.mft"
shift 3
rm -f "$zipname.zip"
zip -Drq "$zipname.zip" $* manifest -x "man/*" || exit 1
