#!/bin/sh
# Install - GCC4TI source installation wizard
# (created from TIGCC source installation wizard)
#
# Copyright (C) 2004 Kevin Kofler
# Copyright (C) 2009 Lionel Debroux
# Copyright (C) 2009 Patrick Pelissier
#
# This program 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.
#
# This program 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, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

oldecho=`echo -n test | grep '\-n'`
if [ "x$oldecho" = "x" ]; then
	#This echo supports '-n' (no newline)
        echon="echo -n"
else
        echon=echo
fi

# Move into the script directory
cd $(dirname $0)

echo "GCC4TI Installation Wizard"

# Get GCC4TI prefix directory
if [ -z "$PREFIX_GCC4TI" ]
then PREFIX_GCC4TI="/usr/local/share/gcc4ti"
fi
${echon} "Destination directory ($PREFIX_GCC4TI) - will become \$TIGCC: "
read PREFIX_GCC4TInew
# PpHd: use eval so that the user can enter e.g. $HOME/gcc4ti
PREFIX_GCC4TInew=`eval echo "$PREFIX_GCC4TInew"`
if [ ! -z "$PREFIX_GCC4TInew" ]
then PREFIX_GCC4TI="$PREFIX_GCC4TInew"
fi
export PREFIX_GCC4TI

# Get Bootstrap compiler
if [ -z "$CC" ]
then CC="gcc"
fi
${echon} "Bootstrap compiler ($CC): "
read CCnew
if [ ! -z "$CCnew" ]
then CC="$CCnew"
fi
export CC
# Check if this is Apple GCC
if [ ! -z "`$CC -v 2>&1 | grep Apple`" ]
then echo "Apple GCC detected, adding -no-cpp-precomp and -DHAVE_DESIGNATED_INITIALIZERS=0 to \$CC"
export CC="$CC -no-cpp-precomp -DHAVE_DESIGNATED_INITIALIZERS=0"
fi

if [ -z "$CFLAGS" ]
then CFLAGS="-Os -s -fno-exceptions -fomit-frame-pointer"
fi
${echon} "Optimization flags ($CFLAGS): "
read CFLAGSnew
if [ ! -z "$CFLAGSnew" ]
then CFLAGS="$CFLAGSnew"
fi
export CFLAGS

${echon} "Host platform for the generated executables ($GCC4TIHOST), leave empty if not cross-compiling: "
read GCC4TIHOSTnew
if [ ! -z "$GCC4TIHOSTnew" ]
then GCC4TIHOST="$GCC4TIHOSTnew"
fi
export GCC4TIHOST

# Apply GCC4TI patch if desired
if ./Install_step_1 ; then
    echo "Patching done."
else
    echo Patching failed
    exit 1
fi

# Complete the installation
if ./Install_All_nopatch ; then
    echo "Installation done."
else
    echo Installation failed
    exit 1
fi

# Now offer to install environment variables
${echon} "Add environment settings (\$TIGCC, \$PATH) to bashrc [Y/n]? "
read AddEnvSettings
echo
if [ -z "$AddEnvSettings" -o "x$AddEnvSettings" = xy -o "x$AddEnvSettings" = xY ]
then
    TIGCC="$PREFIX_GCC4TI"
    export TIGCC
    "$PREFIX_GCC4TI/bin/envreg"
    echo "Done. You must restart bash for the new environment settings to take effect."
    exit 0;
fi

echo "Done. Make sure you set \$TIGCC to \"$PREFIX_GCC4TI\" and append \$TIGCC/bin to your \$PATH before using GCC4TI."
exit 0
