#!/bin/bash -x
#
# Gemaakt voor RuG-CIT 2017-05-27                 Door: Rein van Weerden
# ======================================================================
# getAndBuild                                Copyright dVR H&S Sys. 2017
# ======================================================================
#
# getAndBuild is used to check a flag file set by a webhook and if
# go and build a kernel in a virtualbox
#
#    ____
#   <.  .\
#     \..|
#      \ |___(\--/)
#    __/    (  . . )
#   "'._.    '-.O.'
#        '-.  \ "|\
#           '.,,/'.,     dVR
#
if [[ -n \${DX} ]]
 then
  exec 1>>/tmp/\${0##*/}.log 2>&1
  set -\${DX}    # Are you going to debug? (DX is set to 'x' (set DX='x') or unset)
fi
#
date > $HOME/getAndBuild.run.time
cd $HOME/bin
mv mvGet2here mvGet2here.done
sudo mv /media/sf_VM-sfolder/build/vr_genetlink.c.new $HOME/src
sudo chown rein:rein $HOME/src/vr_genetlink.c.new
sudo chmod a-wx,a+r,u+w $HOME/src/vr_genetlink.c.new
cd $HOME
echo 'debconf debconf/frontend select Noninteractive' | sudo debconf-set-selections
sudo dpkg --configure -a
sudo apt-get update
# sudo apt-get -y install autoconf automake bison debhelper flex libcurl4-openssl-dev libexpat1-dev libgettextpo0 libprotobuf-dev libtool libxml2-utils make protobuf-compiler python-all python-lxml python-setuptools python-sphinx ruby-ronn scons unzip vim-common python-netsnmp librdkafka-dev librdkafka1
sudo apt-get -y install autoconf automake bison debhelper flex libcurl4-openssl-dev libexpat1-dev libgettextpo0 libprotobuf-dev libtool libxml2-utils make protobuf-compiler python-all python-lxml python-setuptools python-sphinx ruby-ronn scons unzip vim-common python-netsnmp
sudo apt-get -y install python-dev
sudo apt-get -y install libfixbuf3-dev
sudo apt-get -y install xz-utils cpio pax
sudo apt-get -y install libncurses5-dev
sudo apt-get -y install libssl-dev
sudo mv /media/sf_VM-sfolder/build/libssl1.0.0_1.0.2g-1ubuntu4_amd64.deb .
sudo apt-get -f install
sudo apt-get -y install git
sudo apt-get -y install g++ g++-multilib
# sudo apt-get -y install gcc-4.9-plugin-dev
sudo apt-get -y install gcc-5-plugin-dev
sudo apt-get -y --no-install-recommends install dselect build-essential fakeroot kernel-package
sudo apt-get -y install libboost-dev libboost-chrono-dev libboost-date-time-dev libboost-filesystem-dev libboost-program-options-dev libboost-python-dev libboost-regex-dev libboost-system-dev libcurl4-openssl-dev google-mock libgoogle-perftools-dev liblog4cplus-dev libtbb-dev libhttp-parser-dev libxml2-dev libicu-dev
# sudo apt-get -y install libuv1 libuv1-dev
sudo apt-get -y install libpcap-dev
sudo apt-get -y install libnl-3-200 libnl-3-dev libnl-genl-3-200 libnl-genl-3-dev
sudo apt-get -y install cmake cmake-data cmake-extras
sudo apt-get -y install w3m
sudo apt-get -y install repo
sudo apt-get -y install nmon
sudo apt-get -y -f install
sudo apt-get update
sudo apt-get -y dist-upgrade
sudo apt-get -y autoremove 
sudo rm -f /media/sf_VM-sfolder/build/dpkg-gencontrol
date >> $HOME/getAndBuild.run.time
MAKEOPTIES="--jobs \$(nproc) V=1 -d" 
mkdir -p src/Linux
cd src/Linux
# git clone ${KERNELSET}
# git clone ${CONFIGFILE}
# cd ~/src/Linux/build
# ln -s ../Kernel source
# if make \${MAKEOPTIES} prepare > make.prepare.lst 2>&1
#  then
#   if make \${MAKEOPTIES} modules > make.modules.lst 2>&1
#    then
#     if make \${MAKEOPTIES} all > make.all.lst 2>&1
#      then
#       if sudo make \${MAKEOPTIES} modules_install > make.modules_install.lst 2>&1
#        then
#         if sudo make \${MAKEOPTIES} headers_install > make.headers_install.lst 2>&1
#          then
#           if sudo make \${MAKEOPTIES} install > make.install.lst 2>&1
#            then
#             VERSION=\$(cat include/config/kernel.release)
#             cd /boot
#             sudo update-grub
#             sudo update-initramfs -c -k \${VERSION}
#             sudo update-grub
#             sudo update-initramfs -u -k \${VERSION}
#             sudo update-grub
#           fi
#         fi
#       fi
#     fi
#   fi
# fi
date >> $HOME/getAndBuild.run.time
# cd ~/src/Linux/build
# KR=\$(cat include/config/kernel.release)
KR=$(uname -r)
echo ${KR} > ~/kernel.version
# sudo mkdir -p /usr/src/linux-headers-\${KR}
# sudo cp -a * /usr/src/linux-headers-\${KR}/
# cd /usr/src/
# sudo chown -R root:root linux-headers-\${KR}
date >> $HOME/getAndBuild.run.time
echo "JuniperControllerBuild" > next
sleep 5
sudo reboot
