#!/bin/sh -e

#
# Original script by J.C.Wren <jcwren@jcwren.com>,
# with horrible hacks by Rick Jenkins <rick@hartmantech.com> to update it.
# 
# At present does not make the python bootstrap loader support (pybsl).
#

#
# This is the directory in which the build takes place, it should be
# distinct from the one below
#
export BUILD_DIR=/usr/local/src/msp430

#
#  This is the directory where the tools will wind up
#
export PREFIXROOT=/usr/local/msp430
export PATH=$PREFIXROOT/bin:$PATH

#
# Current versions of sources to be patched.
#
export BINUTILS_VERSION=2.14
export GCC_VERSION=3.2.3

#
# This is the CVS directory version with the patches.
# It does not always match the GCC_VERSION above.
#

export GCC_PATCH_VERSION=3.3

######## For an ordinary update, you should not need to change anything below this ########

#
#  Gotta be root for installs
#
if [ ! $UID == 0 ] ; then
	echo "Must be root to do this"
	exit ;
fi

#
# Go to the build directory
#

if [ ! -s $BUILD_DIR ] ; then
  mkdir $BUILD_DIR ;
fi
cd $BUILD_DIR
echo "$0: Building in $BUILD_DIR" 

#
#  Some systems alias cp, rm and mv for our protection.  We don't want that.
#  My system doesn't (rlj), so I removed the \ before these commands 'cos
#  if it's there the script fails.
#

#
# Check we have working wget and cvs commands
#
command -V wget >/dev/null 2>&1 ; retval=$? ; if [ $retval -ne 0 ] ; then
	echo "$0: Can't find wget!" 
	exit 1 ;
fi

command -V cvs >/dev/null 2>&1 ; retval=$? ; if [ $retval -ne 0 ] ; then
	echo "$0: Can't find cvs!" 
	exit 1 ;
fi

#
#  Blow away the old version, if it exists.
#  Safest thing to do, according to Dmitry
#  Keep a rotating backup, so that after a failure there is a way
#  to have a working compiler.
#
if [ -s $PREFIXROOT ] ; then
  echo "$0: Backing up existing version as $PREFIXROOT.old"
  if [ -s $PREFIXROOT.old ] ; then
    rm -rf $PREFIXROOT.old ;
  fi
  mv $PREFIXROOT $PREFIXROOT.old ;
fi

#
#  In theory, you can do an update to make the CVS pull more efficient.
#  Safer this way, tho
#
echo "Deleting old patches"
rm -rf gcc
rm -rf msp430-libc
rm -rf pybsl 
rm -rf pyjtag 
rm -rf jtag 
ls

#
#  If these already exist, don't fetch again
#
if [ ! -s binutils-$BINUTILS_VERSION.tar.bz2 ] ; then
  echo "$0: Downloading binutils source"
  wget ftp://sources.redhat.com/pub/binutils/releases/binutils-$BINUTILS_VERSION.tar.bz2 ;
fi

if [ ! -s gcc-core-$GCC_VERSION.tar.bz2 ] ; then
  echo "$0: Downloading gcc core source"
  wget http://mirrors.rcn.net/pub/sourceware/gcc/releases/gcc-$GCC_VERSION/gcc-core-$GCC_VERSION.tar.bz2 ;
fi

#
#  Login to CVS, pull the patches down. 
#  We don't need the binutils stuff any more.
#
echo "$0: Fetching patches from sourceforge CVS"
echo "Hit <return> when prompted for password"
cvs -d:pserver:anonymous@cvs.mspgcc.sourceforge.net:/cvsroot/mspgcc login
cvs -z3 -d:pserver:anonymous@cvs.mspgcc.sourceforge.net:/cvsroot/mspgcc co gcc
cvs -z3 -d:pserver:anonymous@cvs.mspgcc.sourceforge.net:/cvsroot/mspgcc co msp430-libc
cvs -z3 -d:pserver:anonymous@cvs.mspgcc.sourceforge.net:/cvsroot/mspgcc co pybsl
cvs -z3 -d:pserver:anonymous@cvs.mspgcc.sourceforge.net:/cvsroot/mspgcc co pyjtag
cvs -z3 -d:pserver:anonymous@cvs.mspgcc.sourceforge.net:/cvsroot/mspgcc co jtag

#
#  Build the binutils and install.
#  As of version 2.14 we don't need to patch the binutils,
#  because the required modifications are integrated into the base version.
#
if [ ! -s binutils-$BINUTILS_VERSION.tar.bz2 ] ; then
	echo "$0: Eeek!  Where did the binutils-$BINUTILS_VERSION.tar.bz2 file go?!?"
	exit ;
fi

echo "$0: Unpacking binutils sources"
rm -rf binutils-$BINUTILS_VERSION
tar --bzip2 -xf binutils-$BINUTILS_VERSION.tar.bz2 || exit
echo "$0: Building binutils"
cd binutils-$BINUTILS_VERSION
./configure --target=msp430 --prefix=$PREFIXROOT
(make && make install) || exit
cd $BUILD_DIR

#
#  Build the compiler and install
#
if [ ! -s gcc-core-$GCC_VERSION.tar.bz2 ] ; then
	echo "$0: Eeek!  Where did the gcc-core-$GCC_VERSION.tar.bz2 go?!?"
	exit ;
fi

echo "$0: Unpacking gcc sources"
rm -rf gcc-$GCC_VERSION
tar --bzip2 -xf gcc-core-$GCC_VERSION.tar.bz2 || exit
echo "$0: Patching gcc sources"
cp -a gcc/gcc-$GCC_PATCH_VERSION/gcc/* gcc-$GCC_VERSION/gcc
echo "$0: Building gcc"
cd gcc-$GCC_VERSION
./configure --target=msp430 --prefix=$PREFIXROOT
(make && make install) || exit
cd $BUILD_DIR

#
#  Build the libraries and install
#
echo "$0: Building libraries"
cd msp430-libc/src
sed -e "s#^prefix =.*#prefix=$PREFIXROOT#" Makefile >Makefile.tmp
mv Makefile.tmp Makefile
(make && make install) || exit
cd $BUILD_DIR

#
# Get jtag.py into a useable directory:
#
echo "$0: Moving pyjtag script into place" 
cd pyjtag/
cp jtag.py $PREFIXROOT/bin/
chmod 755 $PREFIXROOT/bin/jtag.py
cd $BUILD_DIR

#
# Get jtag hardware support
#
echo "$0: Building jtag hardware support"
cd jtag
make
cd python/
cp _parjtag.so /usr/local/lib
ldconfig
cd ../hardware_access/HILppdev/
make libHIL.so
mv libHIL.so /usr/local/lib
ldconfig
cd $BUILD_DIR

echo "$0: MSP430 compiler build is complete"
