#! /bin/bash
# Compiler setup script.  Links up the cramfs dev image so that it is suitable for
# compiling QT stuff.
#
# Bruce Kroeze <bruce@zefamily.org>
# ver 1.0

HOME_DIR=/home/zaurus
MOUNT_DIR="$HOME_DIR/develop"
DO_PROMPT="true"

function mklink ()
{
  if [ ! -e "$1" ]
  then
    if [ ! -f $2 ]
    then
      echo "Warning link source $1 not found, linking anyway"
    else
      echo "Warning link source $1 not found, leaving existing target alone"
    fi
  fi
#  if [ -f $2 ]
#  then
#    mv $2 $2.bak
#  fi
  ln -sf $1 $2
}

function fixprofile ()
{
cat >>/etc/profile <<EOF
# added by compiler_setup.sh
export QTDIR=/opt/qtopia
EOF
}

# -- script entry point --

while [ -n "$1" ]
do
  case "$1" in
  -h)
      echo "USAGE: compiler_setup [-h] [-r /mnt/path]"
      echo "  -h display this help"
      echo "  -r path to the root of your /lib, /bin/ and /include dirs"
	  exit 1
	  ;;
  -r)
      shift
      MOUNT_DIR=$1
	  ;;
  esac
  shift
done

if [ ! -d $MOUNT_DIR ]
then
  echo "Could not find root dir: $MOUNT_DIR"
  exit 1
fi

for F in bin lib include
do
  if [ ! -d ${MOUNT_DIR}/$F ]
  then
    echo "Could not find $F dir at ${MOUNT_DIR}/$F"
	exit 1
  fi
done

if [ -z "$QTDIR" ]
then
  if [ -d /opt/Qtopia ]
  then
    export QTDIR=/opt/Qtopia
    if [ -f /etc/profile ]
    then
      if grep -qv "QTDIR" /etc/profile
      then
        echo "No QTDIR set in environment, probably 'cause it is not in your /etc/profile."
	echo "Hit CTRL-C to abort adding it to the profile, or ENTER to continue"
	read
	fixprofile
      fi
    fi
    export QTDIR=/opt/Qtopia
  else
    echo "No QTDIR set in environment and it isn't in it's normal location. Please set one before running this script."
    exit 1
  fi
fi

if [ ! -d $QTDIR ]
then
  echo "I can't find the QTDIR directory [${QTDIR}]"
  exit 1
fi

echo Linking...

echo "Setting up /usr/bin..."
FILES=`ls $MOUNT_DIR/usr/bin`

for FILE in $FILES
do
    ln -sf $MOUNT_DIR/usr/bin/$FILE /usr/bin/$FILE
done

echo "Setting up /usr/local/bin..."
FILES=`ls $MOUNT_DIR/usr/local/bin`

for FILE in $FILES
do
    ln -sf $MOUNT_DIR/usr/local/bin/$FILE /usr/local/bin/$FILE
done


#mklink $MOUNT_DIR/usr/bin/gcc /usr/bin/gcc
#mklink $MOUNT_DIR/usr/bin/g++ /usr/bin/g++

mklink $MOUNT_DIR/include /usr/local/include
mklink $MOUNT_DIR/lib/libstdc++-3-libc6.1-2-2.10.0.so /usr/lib/libstdc++-libc6.1-2.so.3
mklink $MOUNT_DIR/lib/libopcodes-2.9.5.0.14.so /usr/lib/libopcodes-2.9.5.0.14.so
mklink $MOUNT_DIR/lib/libbfd-2.9.5.0.14.so /usr/lib/libbfd-2.9.5.0.14.so
mklink $MOUNT_DIR/lib/gcc-lib /usr/lib/gcc-lib
mklink $MOUNT_DIR/lib/gcc-lib/armv4l-redhat-linux/2.95.1/crtn.o /usr/lib/crtn.o
mklink $MOUNT_DIR/lib/gcc-lib/armv4l-redhat-linux/2.95.1/crti.o /usr/lib/crti.o
mklink $MOUNT_DIR/lib/gcc-lib/armv4l-redhat-linux/2.95.1/crt1.o /usr/lib/crt1.o
mklink $MOUNT_DIR/lib/Mcrt1.o /usr/lib/Mcrt1.o

# flex setup
echo "Setting up flex..."
if [ ! -d /usr/local/lib ]
then 
  mkdir /usr/local/lib
fi

#mklink $MOUNT_DIR/usr/local/bin/flex /usr/local/bin/flex
#mklink $MOUNT_DIR/usr/local/bin/flex++ /usr/local/bin/flex++

#mklink $MOUNT_DIR/usr/local/lib/libfl.a /usr/local/lib/libfl.a

echo "Setting up /usr/local/lib..."
FILES=`ls $MOUNT_DIR/usr/local/lib`

for FILE in $FILES
do
    ln -sf $MOUNT_DIR/usr/local/lib/$FILE /usr/local/lib/$FILE
done


# bison setup
echo "Setting up flex..."
if [ ! -d /usr/local/share ]
then 
  mkdir /usr/local/share
fi

#mklink $MOUNT_DIR/usr/local/bin/bison /usr/local/bin/bison
mklink $MOUNT_DIR/usr/local/share/bison /usr/local/share/bison
mklink $MOUNT_DIR/usr/local/share/awk /usr/local/share/awk
mklink $MOUNT_DIR/usr/local/share/autoconf /usr/local/share/autoconf
mklink $MOUNT_DIR/usr/local/share/automake-1.8 /usr/local/share/automake-1.8
mklink $MOUNT_DIR/usr/local/share/aclocal-1.8 /usr/local/share/aclocal-1.8
#mklink $MOUNT_DIR/usr/local/share/aclocal /usr/local/share/aclocal
mklink $MOUNT_DIR/usr/local/share/libtool /usr/local/share/libtool

mklink $MOUNT_DIR/usr/local/share/vim /usr/local/share/vim

mkdir $HOME_DIR/automake
ln -sf /usr/local/bin $HOME_DIR/automake/bin
ln -sf /usr/local/share $HOME_DIR/automake/share

mkdir $HOME_DIR/autoconf
ln -sf /usr/local/bin $HOME_DIR/autoconf/bin
ln -sf /usr/local/share $HOME_DIR/autoconf/share

mkdir $HOME_DIR/libtool
ln -sf /usr/local/bin $HOME_DIR/libtool/bin
ln -sf /usr/local/share $HOME_DIR/libtool/share

# perl setup
echo "Setting up perl..."
ln -sf $MOUNT_DIR/usr/lib/perl /usr/lib/perl
ln -sf $MOUNT_DIR/usr/lib/perl5 /usr/lib/perl5
mkdir /mnt/pkgs
mkdir /mnt/pkgs/perl
mkdir /mnt/pkgs/perl/perl5.8.0

ln -sf /usr/bin /mnt/pkgs/perl/perl5.8.0/bin 
ln -sf /usr/lib/perl /mnt/pkgs/perl/perl5.8.0/lib 

echo "Setting up tmake..."
ln -sf $MOUNT_DIR/usr/local/tmake /usr/local/tmake

echo "Setting up libraries..."
mklink /usr/lib/libstdc++-libc6.1-2.so.3 /usr/lib/libstdc++.so
mklink /lib/libm.so.6 /usr/lib/libm.so
mklink /lib/libc.so.6 /usr/lib/libc.so
mklink $MOUNT_DIR/lib/libc_nonshared.a /usr/lib/libc_nonshared.a

mklink $MOUNT_DIR/include/qt $QTDIR/include
mklink $MOUNT_DIR/bin/moc $QTDIR/bin/moc
mklink $MOUNT_DIR/lib/libstdc++-3-libc6.1-2-2.10.0.so $QTDIR/lib/libstdc++-libc6.2-2.so.3
mklink $MOUNT_DIR/lib/libmng.a $QTDIR/lib/libmng.a
mklink $MOUNT_DIR/lib/libpng.a $QTDIR/lib/libpng.a
mklink $MOUNT_DIR/lib/libqte.a $QTDIR/lib/libqte.a
mklink $MOUNT_DIR/lib/libz.a $QTDIR/lib/libz.a

mklink $QTDIR/lib/libqpe.so.1.5.0 $QTDIR/lib/libqpe.a

BIN=$MOUNT_DIR/bin
echo $PATH | grep -qv "${BIN}" && export PATH=$BIN:$PATH

echo "You are now set up for dev using $MOUNT_DIR"