#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | Unsupported Contributions for OpenFOAM 
#  \\    /   O peration     |
#   \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation 
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
# 2013-10-26 Bruno Santos (wyldckat@github): Updated code and comments.
# 2024-09-03 Bruno Santos (FS Dynamics Portugal): Updated for modern Gcc stacks.
#------------------------------------------------------------------------------
# License
#     This file is a derivative work of OpenFOAM. 
#
#     OpenFOAM 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.
#
#     OpenFOAM 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 OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
#
# Script
#     getGcc
#
# Description
#     Get Gcc, GMP, MPFR and MPC and unpack them.
#
#------------------------------------------------------------------------------
gmpPACKAGE=gmp-5.0.1
mpfrPACKAGE=mpfr-2.4.2
mpcPACKAGE=mpc-0.8.1
gccPACKAGE=gcc-4.5.1
gccTarExt=bz2

#------------------------------------------------------------------------------
# run from third-party directory only
wmakeCheckPwd "$WM_THIRD_PARTY_DIR" || {
    echo "Error: Current directory is not \$WM_THIRD_PARTY_DIR"
    echo "    The environment variables are inconsistent with the installation."
    echo "    Check the OpenFOAM entries in your dot-files and source them."
    exit 1
}
. etc/tools/ThirdPartyFunctions
#------------------------------------------------------------------------------

usage() {
    exec 1>&2
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat<<USAGE

usage: ${0##*/} [gmp-VERSION] [mpfr-VERSION] [mpc-VERSION] [gcc-VERSION]

* build combinations of gmp, mpfr, mpc and gcc
      $gmpPACKAGE
      $mpfrPACKAGE
      $mpcPACKAGE
      $gccPACKAGE

USAGE
    exit 1
}

getFile() {
  packageDir=$1
  tarFile=$2
  url=$3

  if [ ! -d ${packageDir} ]
  then
      downloadFile $tarFile $url

      if [ -e download/$tarFile ]
      then
          echo "unpack download/$tarFile"
          tar -xf download/$tarFile
      else
          echo "no download/$tarFile to unpack"
      fi
  fi
}

# parse options
while [ "$#" -gt 0 ]
do
    case "$1" in
    -h | -help)
        usage
        ;;
    gmp-[4-9]*)
        gmpPACKAGE="${1%%/}"
        shift
        ;;
    mpfr-[2-9]*)
        mpfrPACKAGE="${1%%/}"
        shift
        ;;
    mpc-[0-9]*)
        mpcPACKAGE="${1%%/}"
        shift
        ;;
    gcc-[4-6]*)
        gccPACKAGE="${1%%/}"
        gccTarExt=bz2
        shift
        ;;
    gcc-[7-9]*|gcc-1[0-9]*)
        gccPACKAGE="${1%%/}"
        gccTarExt=xz
        shift
        ;;
    *)
        usage "unknown option/argument: '$*'"
        ;;
    esac
done

gmpFile=$gmpPACKAGE.tar.bz2
mpfrFile=$mpfrPACKAGE.tar.bz2
mpcFile=$mpcPACKAGE.tar.gz
gccFile=$gccPACKAGE.tar.$gccTarExt

MIRROR="http://ftpmirror.gnu.org"
#MIRROR="http://mirrors.up.pt/pub/gnu"

gmpUrl="${MIRROR}/gmp/$gmpFile"
mpfrUrl="${MIRROR}/mpfr/$mpfrFile"
mpcUrl="${MIRROR}/mpc/$mpcFile"
gccUrl="${MIRROR}/gcc/$gccPACKAGE/$gccFile"

getFile ${gmpPACKAGE} ${gmpFile} ${gmpUrl}
getFile ${mpfrPACKAGE} ${mpfrFile} ${mpfrUrl}
getFile ${mpcPACKAGE} ${mpcFile} ${mpcUrl}
getFile ${gccPACKAGE} ${gccFile} ${gccUrl}

# ----------------------------------------------------------------- end-of-file