#!/bin/bash

#
# install_avr_gcc.sh, avr-gcc build and installation script
# Last updated: 05/28/06
#
# run this script with "sh ./install_avr_gcc.sh"
#
# The default installation target is /usr/local/avr. If you don't
# have the necessary rights to create files there you may change 
# the installation target to e.g. $HOME/avr in your home directory
#

# select where to install the software
PREFIX=/usr/local/avr
#PREFIX=$HOME/avr

# tools need each other and must therefore be in path
export PATH=$PATH:$PREFIX/bin

# specify here where to find/install the source archives
ARCHIVES=$HOME/src

# specify here where to compile (i.e. to avoid compiling over nfs)
COMPILE_DIR=/tmp/$USER

# tools required for build process
REQUIRED="wget bison flex gcc g++"
# uncomment if you want to build avrdude from cvs
#REQUIRED="${REQUIRED} automake-1.9 cvs autoconf"

# download sites
GNU_MIRROR="ftp://ftp.gnu.org/pub/gnu"
NONGNU_MIRROR="http://savannah.nongnu.org/download"

# toolchain definition
# updated for latest versions on 28may06
BINUTILS=binutils-2.16.1
BINUTILS_PACKAGE=${BINUTILS}.tar.bz2
BINUTILS_DOWNLOAD=${GNU_MIRROR}/binutils/${BINUTILS_PACKAGE}
BINUTILS_CHECKSUM="6a9d529efb285071dad10e1f3d2b2967"

GCC=gcc-4.0.2
GCC_CHECKSUM="a659b8388cac9db2b13e056e574ceeb0"
GCC=gcc-4.1.1
GCC_PACKAGE=${GCC}.tar.bz2
GCC_DOWNLOAD=${GNU_MIRROR}/gcc/${GCC}/${GCC_PACKAGE}
GCC_CHECKSUM="ad9f97a4d04982ccf4fd67cb464879f3"

GDB=gdb-6.4
GDB_PACKAGE=${GDB}.tar.bz2
GDB_DOWNLOAD=${GNU_MIRROR}/gdb/${GDB_PACKAGE}
GDB_CHECKSUM="f62c14ba0316bc88e1b4b32a4e901ffb"

AVRLIBC=avr-libc-1.4.1
AVRLIBC_CHECKSUM="970244f82015f75a51e8f05749ef05ec"
AVRLIBC=avr-libc-1.4.4
AVRLIBC_PACKAGE=${AVRLIBC}.tar.bz2
AVRLIBC_DOWNLOAD=${NONGNU_MIRROR}/avr-libc/${AVRLIBC_PACKAGE}
AVRLIBC_CHECKSUM="04f774841b9dc9886de8120f1dfb16e3"

UISP=uisp-20050207
UISP_PACKAGE=${UISP}.tar.gz
UISP_DOWNLOAD=${NONGNU_MIRROR}/uisp/${UISP_PACKAGE}
UISP_CHECKSUM="b1e499d5a1011489635c1a0e482b1627"

AVRDUDE=avrdude-5.1
AVRDUDE_PACKAGE=${AVRDUDE}.tar.gz
AVRDUDE_DOWNLOAD=${NONGNU_MIRROR}/avrdude/${AVRDUDE_PACKAGE}
AVRDUDE_CHECKSUM="6c3005709983f65043529c6eda5de314"
# uncomment if you want to build avrdude from cvs
#AVRDUDE_CVS="cvs -z3 -d:pserver:anonymous@cvs.savannah.nongnu.org:/sources/avrdude co avrdude"

##
################################################################################
# end of configuration
################################################################################
##

OLDPWD=$PWD

echo "--------------------------------------"
echo "- Installation of avr-gcc tool chain -"
echo "--------------------------------------"
echo
echo "Tools to build:"
echo "  binutils - collection of binary tools"
echo "  gcc      - GNU Compiler Collection"
echo "  gdb      - GNU Project debugger"
echo "  avr-libc - C library for use with GCC on Atmel AVR microcontrollers."
echo "  uisp     - tool for AVR microcontrollers which can interface to many hardware"
echo "             in-system programmers"
echo "  avrdude  - software for programming Atmel AVR Microcontrollers"
echo
echo "Installation into target directory:"
echo "${PREFIX}"
echo

# Search for required tools
echo "Checking for required tools ..."
for i in ${REQUIRED}
do 
	echo -n "Checking for $i ..."
	TOOL=`which $i 2>/dev/null`
	if [ "${TOOL}" == "" ]
	then
		echo " not found, please install it!"
		exit 1
	else
		echo " ${TOOL}, ok"
	fi
done
echo

# Check if target is already there
if [ -x $PREFIX ]
then 
	echo "Target $PREFIX already exists! This may be due to"
	echo "a previous incomplete build attempt. Please remove"
	echo "it and restart."
	exit 1
fi

# Check if target can be created
install -d $PREFIX >/dev/null
if [ "$?" != "0" ]
then
	echo "Unable to create target $PREFIX, please check permissions"
	exit 1
fi
rmdir $PREFIX

# Check if there's already a avr-gcc
TOOL=`which avr-gcc 2>/dev/null`
if [ "${TOOL}" != "" ]
then
	echo "There's already a avr-gcc in ${TOOL},"
	echo "please remove it, as it will conflict"
	echo "with the build process!"
	exit 1
fi

# crate ARCHIVE directory if it doesn't exist
if [ ! -d ${ARCHIVES} ]
then
	echo "Archive directory ${ARCHIVES} does not exist, creating it."
	install -d ${ARCHIVES}
fi

# download and check the source packages
download_and_check() {
	if [ ! -f ${ARCHIVES}/$1 ]
	then
		echo "Archive ${ARCHIVES}/$1 not found, downloading..."
		wget --quiet -O ${ARCHIVES}/$1 $2
	fi

	echo -n "Verifying md5sum of ${ARCHIVES}/${1}... "
	MD5=`md5sum ${ARCHIVES}/$1 | awk '{print $1}'`
	if [ "$MD5" != "$3" ]
	then
		echo "Error: ${1} corrupted!"
		echo "MD5 is: ${MD5}, but should be ${3}"
		exit 1
	fi
}

echo "Check / download source packages..."
download_and_check $BINUTILS_PACKAGE  $BINUTILS_DOWNLOAD   $BINUTILS_CHECKSUM &
download_and_check $GCC_PACKAGE       $GCC_DOWNLOAD        $GCC_CHECKSUM &
download_and_check $GDB_PACKAGE       $GDB_DOWNLOAD        $GDB_CHECKSUM &
download_and_check $AVRLIBC_PACKAGE   $AVRLIBC_DOWNLOAD    $AVRLIBC_CHECKSUM &
download_and_check $UISP_PACKAGE      $UISP_DOWNLOAD       $UISP_CHECKSUM &
[ -z "$AVRDUDE_CVS" ] && download_and_check $AVRDUDE_PACKAGE   $AVRDUDE_DOWNLOAD    $AVRDUDE_CHECKSUM &

# wait until the subshells are finished
wait

echo
echo "Build environment seems fine!"
echo
echo "After successful installation add $PREFIX/bin to"
echo "your PATH by e.g. adding"
echo "   export PATH=\$PATH:$PREFIX/bin"
echo "to your $HOME/.bashrc file."
echo
echo "Press return to continue, CTRL-C to abort ..."
read


# binutils
################################################################################
echo "Building Binutils..."
cd $COMPILE_DIR
tar xvfj ${ARCHIVES}/${BINUTILS}.tar.bz2
cd ${BINUTILS}
mkdir obj-avr
cd obj-avr
../configure  --prefix=$PREFIX --target=avr --disable-nls --enable-install-libbfd
make
make install
cd $COMPILE_DIR
rm -rf ${BINUTILS}


# gcc
################################################################################
echo "Building GCC ..."
cd $COMPILE_DIR
tar xvfj ${ARCHIVES}/${GCC}.tar.bz2
cd ${GCC}
mkdir obj-avr
cd obj-avr
../configure --prefix=$PREFIX --target=avr --enable-languages=c,c++ --disable-nls
make
make install
cd $COMPILE_DIR
rm -rf ${GCC}


# avr-libc
################################################################################
echo "Building AVR-Libc ..."
cd $COMPILE_DIR
tar xvfj ${ARCHIVES}/${AVRLIBC}.tar.bz2
cd ${AVRLIBC}
mkdir obj-avr
cd obj-avr
PATH=$PATH:$PREFIX/bin
../configure --prefix=$PREFIX --build=`./config.guess` --host=avr
make
make install
cd $COMPILE_DIR
rm -rf ${AVRLIBC}


# gdb
################################################################################
echo "Building GDB ..."
cd $COMPILE_DIR
tar xvfj ${ARCHIVES}/${GDB}.tar.bz2
cd ${GDB}
mkdir obj-avr
cd obj-avr
../configure --prefix=$PREFIX --target=avr
make
make install
cd $COMPILE_DIR
rm -rf ${GDB}


# uisp
################################################################################
echo "Building UISP ..."
cd $COMPILE_DIR
tar xvfz ${ARCHIVES}/${UISP}.tar.gz
cd ${UISP}
mkdir obj-avr
cd obj-avr
../configure --prefix=$PREFIX
make
make install
cd $COMPILE_DIR
rm -rf ${UISP}


# avrdude
################################################################################
echo "Building AVRDUDE..."
if [ -z "${AVRDUDE_CVS}" ]
then
	cd $COMPILE_DIR
	tar xvfz ${ARCHIVES}/${AVRDUDE}.tar.gz
	cd ${AVRDUDE}
	mkdir obj-avr
	cd obj-avr
	../configure --prefix=$PREFIX
	make
	make install
	cd $COMPILE_DIR
	rm -rf ${AVRDUDE}
else
	# building avrdude from cvs
	cd $COMPILE_DIR
	`$AVRDUDE_CVS`
	cd avrdude
	./bootstrap
	mkdir obj-avr
	cd obj-avr
	../configure --prefix=$PREFIX
	make
	make install
	cd $COMPILE_DIR
	rm -rf avrdude
fi


################################################################################
echo "--------------------------------------------------------------------------------"
echo "Installation script finished!"
# show versions
echo "--------------------------------------------------------------------------------"
echo "These are the current installed versions of the avr tool chain:"
echo
echo "--binutils--"
avr-ld --version | head -1
avr-as --version | head -1
echo
echo "--gcc--"
avr-gcc -dumpversion
echo
echo "--gdb--"
avr-gdb --version | head -1
echo
echo "--avrlibc--"
ls $PREFIX/lib/gcc/avr
echo
echo "--uisp--"
uisp --version | head -1
echo
echo "--avrdude--"
avrdude  -v 2>&1 | grep Version
# show path / manpath hints
echo "--------------------------------------------------------------------------------"
echo "Make sure to have $PREFIX/bin in your PATH"
echo "You can do this by adding the following"
echo "line to your $HOME/.bashrc file:"
echo "    export PATH=\$PATH:$PREFIX/bin"
echo
echo "And for manpages, add the following line:"
echo "    export MANPATH=\$MANPATH:$PREFIX/man"
echo
echo "--------------------------------------------------------------------------------"
# return to users current directory
cd $OLDPWD