#!/bin/bash
. ~/oe-env-setup

function packages_to_install {
   echo $(dpkg -s $1 2>&1|grep 'not installed'|grep "not installed"|cut -d" " -f2|sed s-[\`\']--g)
}

#-- Required Debian Packages
echo
echo Checking for required Debian Packages ...

pkgs=(
# BitBake prereqs
"python python-dev python-psyco ccache patch m4 sed bison make wget bzip2 cvs gawk libc6-dev g++ subversion sharutils coreutils docbook openjade quilt "
# Kernel and compile stuff
"bc console-tools"
# OE Konq Bug
"libpcre3-dev"
# ReiserFS 4 - fastest FS for small files - compiling
"reiser4progs"
)

for pkg in "${pkgs[@]}"; do
  pkglist=`packages_to_install "$pkg"`
  if [ -n "$pkglist" ]; then 
    sudo apt-get install $pkglist
  fi
done

echo

#-- Cross compile toolchain for compiling the kernel
if [ "`which arm-linux-gcc-2.95 | wc -l`" -eq "0" ]; then
  echo Installing ARM Cross compiler toolchain ... 
  sudo mkdir -p /usr/local/arm/
  cd /usr/local/arm/
  sudo wget http://handhelds.org/download/projects/toolchain/archive/cross-2.95.3.tar.bz2
  sudo tar -jxvf cross-2.95.3.tar.bz2
  # GCC 2.95 and LD 2.11.2 are used to compile the kernel only.  All packages in OE are compile with GCC 3.3+
  # so we must move (rather then link or copy) arm-linux-gcc in the crosscompile tool chain so it doesn't 
  # get called by accident when compiling packages which should be using arm-linux-gcc-3.3
  sudo mv /usr/local/arm/2.95.3/bin/arm-linux-gcc /usr/local/arm/2.95.3/bin/arm-linux-gcc-2.95
  sudo mv /usr/local/arm/2.95.3/bin/arm-linux-ld /usr/local/arm/2.95.3/bin/arm-linux-ld-2.11.2
  cd ~
else
  echo Cross compiler toolchain is already installed.
fi

echo

#-- Install opensource bk-client
if [ ! -d "$OE_TOOLS/bk_client-1.1" ]; then
  echo Installing Bitkeeper client ... 
  cd $OE_TOOLS
  wget http://www.bitmover.com/bk-client.shar
  chmod +x bk-client.shar
  ./bk-client.shar
  cd bk_client-1.1/
  make
  make install
else
 echo Bitkeeper client already installed.
fi

#-- Set-up OE build directories, retrieve OE package data  
if [ ! -d "$OE_BUILD_MNT" ]; then
   mkdir -p $OE_BUILD_MNT
fi

if [ "`mount|grep $OE_BUILD_MNT|wc -l`" -eq "0" ]; then
  OE_MNT_PART=$(whiptail --title 'OE Build Partition' --inputbox 'Specify a partion to be used for building OE.  The partition must be a minimum size of 5GB to build "opie-image" (32GB for "world")' 10 60 '/dev/hdb1' 2>&1 </dev/tty >/dev/tty)
  if [ $? -eq 0 ]; then 
    echo
    echo Mounting $OE_BUILD_MNT on partition $OE_MNT_PART ... 
    
    MOUNT_RESULT=`sudo mount $OE_MNT_PART $OE_BUILD_MNT 2>&1`
    if [ "$(echo $MOUNT_RESULT|grep "not a valid block device"|wc -l)" -eq "1" ]; then
      MOUNT_DEV=$(echo $MOUNT_RESULT|cut -d" " -f2 | sed -e "s-[1234567890]*\$--")
      echo
      echo Partition is invalid -- $MOUNT_RESULT
      echo 
      echo Creating new partition $OE_MNT_PART on $MOUNT_DEV ...
      # --- Create new partition
      echo "n
            p
            1


            w" | sudo fdisk $MOUNT_DEV
      # --- End of create new partition
      echo yes|sudo mkfs.reiser4 $OE_MNT_PART
      sudo mount $OE_MNT_PART $OE_BUILD_MNT
    fi
  else
    echo
    echo Aborted.
    exit
  fi
fi

cd $OE_BUILD_MNT
sudo chown -R `whoami`:`whoami` .

if [ ! -d "$OE_BUILD_MNT/sources" ]; then
  mkdir -p $OE_BUILD_MNT/sources
fi

if [ ! -d "$OE_BUILD_MNT/build/conf" ]; then
  mkdir -p $OE_BUILD_MNT/build/conf
fi

if [ ! -e "$OE_BUILD_MNT/build/conf/local.conf" ]; then
  ln -s ~/local.conf $OE_BUILD_MNT/build/conf/local.conf
fi

echo

~/refresh-toolchain.sh
