#!/bin/bash
#
# setup the environment for quartus 
#
#
cd $HOME
# Have we already done this, if so bail out otherwise students 
# keep repeating this and end up with stupidly long paths. :(
# I'll think of a better test at some point
if [ -d $HOME/.sopc_builder -o -d $HOME/.altera.quartus ]; then
    echo
    echo 
    echo "You've already run this script, shouldn't need to do it again"
    echo 
    echo
    exit 0
fi
if [ ! -f $HOME/.bashrc ]; then touch .bashrc; fi
echo " moving .bashrc .old_bashrc"
echo "You will need to remerge any customisations"
echo
#mv .bash_profile .old_bash_profile
mv .bashrc .old_bashrc
echo
echo
echo "writing new .bashrc"
echo
cat >$HOME/.bashrc <<'EOF'
# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
# Path to local altera root on new fedora machines
ALTERA_ROOT=/altera/11.0
export ALTERA_ROOT

QUARTUS_ROOTDIR=$ALTERA_ROOT/quartus
export QUARTUS_ROOTDIR

export sopc_builder=$QUARTUS_ROOTDIR/sopc_builder


# only needed for the cpu_sdk makefiles for NiosI
export sopc_kit_nios=$ALTERA_ROOT/ip/local/kits/nios
#


export PATH=$PATH:$QUARTUS_ROOTDIR/bin:$QUARTUS_ROOTDIR/sopc_builder/bin:$ALTERA_ROOT/ip/local/kits/nios/bin:$ALTERA_ROOT/ip/local/kits/nios/bin/nios-gnupro/bin

export PATH=$PATH:$ALTERA_ROOT/nios2eds/bin/nios2-gnutools/H-i686-pc-linux-gnu/bin/:$ALTERA_ROOT/nios2eds/sdk2/bin:$ALTERA_ROOT/nios2eds/bin


# Altera have their own custom PERL stuff.
export PERL5LIB=$PERL5LIB:$QUARTUS_ROOTDIR/sopc_builder/bin/:$QUARTUS_ROOTDIR/quartus/sopc_builder/bin/europa/:$QUARTUS_ROOTDIR/sopc_builder/bin/perl_lib/

# set up a sensible prompt
export PS1="\[\033]0;\u@\h: \W\007\]bash$ "

# and turn the keyboard beep off that results from the above
echo "set bell-style none" >> $HOME/.inputrc

export LC_COLLATE=C

#required by Quartus
export QUARTUS_64BIT=0
export MWOS=linux
export MWARCH=i86
export QUARTUS_MWWM=allwm

# uncomment the next line if you are using the Excalibur board
# export SOPC_BUILDER_CLASSIC=1
#
SOPC_KIT_NIOS2=$ALTERA_ROOT/nios2eds
export SOPC_KIT_NIOS2

export SOPC_BUILDER_CLASSIC=
EOF
echo "writing new .bash_profile"
echo
echo
cat > $HOME/.bash_profile<<EOF
if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi
EOF
echo
echo
echo "copying quartus registry"
echo
cd $HOME
   wget www.cems.uwe.ac.uk/~ngunton/vhdl/q_reg.tgz
  /bin/tar -xzvf q_reg.tgz
   echo
   echo "should all be ready now"
   echo "You may need to manually add back any .bashrc customisations"
   echo " that you added to the original .bashrc"
   rm q_reg.tgz
#fi

