#!/bin/sh
#
#
# Now with new improved error checking (sort of)
#
# Version 2
#########################################################
# setup dedicated folders and  core quartus stuff first #
# if it hasn't already been done.
#########################################################

cd

file=$HOME/.altera.quartus

mkdir -vp Altera_Systems_Labs
if [ ! -d $file ]; then
    wget --no-proxy  http://www.cems.uwe.ac.uk/~ngunton/vhdl/quartus_11_fed_setup.sh
    chmod 700 ./quartus_11_fed_setup.sh 
    ./quartus_11_fed_setup.sh 
#    ERRNO=$?
#    if [ $ERRNO -eq "0" ]; then
#	. .bashrc
#    else 
#	echo "Something went wrong with first stage setup"
#	echo "quartus_11_fed_setup.sh returned $ERRNO"
#	exit
#    fi
fi
cd
source $HOME/.bashrc #source the new paths to the altera tools

#############################################
#                                           #
# setup RTOS workshop directories and files #
#                                           #
#############################################
cd
mkdir -vp Altera_Systems_Labs/New_FreeRTOS
pushd Altera_Systems_Labs/New_FreeRTOS

########################################
# fetch and unpack the quartus project #
########################################
mkdir -vp CycloneIII
pushd CycloneIII
wget --no-proxy  http://www.cems.uwe.ac.uk/~ngunton/Altera_docs/cycloneIII_3c25_niosII_rtos.qar
ERRNO=$?
if [ $ERRNO -eq "0" ]; then
echo "unpacking first archive"
echo
quartus_sh --unqar cycloneIII_3c25_niosII_rtos.qar
else 
echo "Something went wrong with wget niosII_rtos.qar"
echo "wget returned $ERRNO"
exit
fi
# back up a level
popd

##############################################
# fetch and unpack the local FreeRTOS distro #
##############################################
wget --no-proxy  http://www.cems.uwe.ac.uk/~ngunton/worksheets/new_local_freertos.tgz
ERRNO=$?
if [ $ERRNO -eq "0" ]; then
echo "unpacking free_rtos"
echo
tar --checkpoint-action="echo=busy"  -zxf new_local_freertos.tgz
pushd FreeRTOS/Demo/NiosII_CycloneIII_DBC3C40_GCC/
./CreateProjectDirectoryStructure.sh
else 
echo "Something went wrong with wget freertos"
echo "wget returned $ERRNO"
exit
fi

# pop back up to Altera_Systems_Labs/New_FreeRTOS
popd
# pop back to ~
popd
#########################################################
#Add the GPIB interface hardware and example software   #
#########################################################
cd
mkdir -vp Altera_Systems_Labs/Gpib_v2_from
mkdir -vp Altera_Systems_Labs/New_FreeRTOS/FreeRTOS/Demo/GPIB

wget --no-proxy  http://www.cems.uwe.ac.uk/~ngunton/worksheets/gpib.tbz
ERRNO=$?
if [ $ERRNO -ne "0" ]; then
  echo "Bailing out, wget gpib.tbz failed"
  exit
fi
wget --no-proxy   http://www.cems.uwe.ac.uk/~ngunton/worksheets/interface1_with_dac.qar
ERRNO=$?
if [ $ERRNO -ne "0" ]; then
  echo "Bailing out, wget dac_interface failed"
  exit
fi
wget --no-proxy  http://www.cems.uwe.ac.uk/~ngunton/worksheets/gpib_edm1.ptf
ERRNO=$?
if [ $ERRNO -ne "0" ]; then
  echo "Bailing out, wget ptf failed"
  exit
fi
wget --no-proxy  http://www.cems.uwe.ac.uk/~ngunton/worksheets/gpib_edm1.cdf
ERRNO=$?
if [ $ERRNO -ne "0" ]; then
  echo "Bailing out, wget gpib cdf file failed"
  exit
fi
mv gpib.tbz Altera_Systems_Labs/New_FreeRTOS/FreeRTOS/Demo
pushd Altera_Systems_Labs/New_FreeRTOS/FreeRTOS/Demo
tar --checkpoint-action="echo=busy" -xjf gpib.tbz
rm gpib.tbz
popd
mv interface1_with_dac.qar Altera_Systems_Labs/Gpib_v2_from
pushd Altera_Systems_Labs/Gpib_v2_from
 quartus_sh --restore interface1_with_dac.qar
rm interface1_with_dac.qar
mv $HOME/gpib_edm1.ptf ./
mv $HOME/gpib_edm1.cdf ./
popd

######################################################
#
# Finally do the DE0 stuff
#
######################################################

#!/bin/sh
#
# Set up and unpack first demo for DE0 board
# and simple plain C.
#
cd
mkdir -vp Altera_Systems_Labs/De0_Counter/software/
pushd Altera_Systems_Labs/De0_Counter
wget --no-proxy http://www.cems.uwe.ac.uk/~ngunton/Altera_docs/DE0_TOP_v1.qar
ERRNO=$?
if [ $ERRNO -ne "0" ]; then
  echo "Bailing out, wget DE0 archive failed"
  exit
fi
quartus_sh --restore DE0_TOP_v1.qar
rm DE0_TOP_v1.qar
popd
pushd Altera_Systems_Labs/De0_Counter/software
wget --no-proxy http://www.cems.uwe.ac.uk/~ngunton/Altera_docs/counter.tgz 
ERRNO=$?
if [ $ERRNO -ne "0" ]; then
  echo "Bailing out, wget DE0 software failed"
  exit
fi
tar --checkpoint-action="echo=busy"  -zxf counter.tgz
popd
echo "All done, new quartus project and archive of software."



echo "Log out and back in again to reset your environment"



