#!/bin/sh
# Part of spindle http://asbradbury.org/projects/spindle
#
# See LICENSE file for copyright and license details

set -ex

. ./common

WORKDIR=work
OUTDIR=out
CURIMG=stage4.$IMGFORMAT


setup_stratux_bootconfig() {
	dotask attach_image_to_nbd $CURIMG $NBD_DEV
	mkdir boot
	dotask sudo mount $BOOT_DEV boot
	#usb power
	echo "max_usb_current=1" >>boot/config.txt
	#i2c
	echo "dtparam=i2c1=on" >>boot/config.txt
	echo "dtparam=i2c1_baudrate=400000" >>boot/config.txt
	echo "dtparam=i2c_arm_baudrate=400000" >>boot/config.txt
	#disable serial console
    sed -i boot/cmdline.txt -e "s/console=ttyAMA0,[0-9]\+ //"
}

setup_stratux() {
#HERE

echo "**** STRATUX SETUP *****"

  ssh_in_to_qemu chroot /mnt sh -l -ex - <<\EOF
#general
apt-get install -y screen
#wifi
apt-get install -y hostapd isc-dhcp-server
#troubleshooting
apt-get install -y tcpdump
#wifi startup
update-rc.d hostapd enable
update-rc.d isc-dhcp-server enable
#disable ntpd autostart
update-rc.d ntp disable
EOF

scp_in_to_qemu /root/spindle/hostapd-edimax /tmp/hostapd.in
scp_in_to_qemu /root/spindle/root /tmp/root.in
scp_in_to_qemu /root/spindle/interfaces /tmp/interfaces.in
scp_in_to_qemu /root/spindle/dhcpd.conf /tmp/dhcpd.conf.in
scp_in_to_qemu /root/spindle/hostapd.conf /tmp/hostapd.conf.in
scp_in_to_qemu /root/spindle/isc-dhcp-server /tmp/isc-dhcp-server.in
scp_in_to_qemu /root/spindle/sshd_config /tmp/sshd_config.in
scp_in_to_qemu /root/spindle/libdump978.so /tmp/libdump978.so.in
scp_in_to_qemu /root/spindle/libimu.so /tmp/libimu.so.in
scp_in_to_qemu /root/spindle/go.tgz /mnt/root/go.tgz


 ssh_in_to_qemu chroot /mnt sh -l -ex - <<\EOF
mv /tmp/libdump978.so.in /usr/lib/libdump978.so
mv /tmp/libimu.so.in /usr/lib/libimu.so
mv -f /tmp/hostapd.in /usr/sbin/hostapd
chown root.root /usr/sbin/hostapd
chmod 755 /usr/sbin/hostapd

mkdir -p /etc/ssh/authorized_keys
mv -f /tmp/root.in /etc/ssh/authorized_keys/root
chown root.root /etc/ssh/authorized_keys/root
chmod 644 /etc/ssh/authorized_keys/root

mv -f /tmp/interfaces.in /etc/network/interfaces
mv -f /tmp/dhcpd.conf.in /etc/dhcp/dhcpd.conf
mv -f /tmp/hostapd.conf.in /etc/hostapd/hostapd.conf
mv -f /tmp/isc-dhcp-server.in /etc/default/isc-dhcp-server
mv -f /tmp/sshd_config.in /etc/ssh/sshd_config
rm -f /usr/share/dbus-1/system-services/fi.epitest.hostap.WPASupplicant.service

echo "DAEMON_CONF=\"/etc/hostapd/hostapd.conf\"" >/etc/default/hostapd

echo blacklist dvb_usb_rtl28xxu >>/etc/modprobe.d/rtl-sdr-blacklist.conf
echo blacklist e4000 >>/etc/modprobe.d/rtl-sdr-blacklist.conf
echo blacklist rtl2832 >>/etc/modprobe.d/rtl-sdr-blacklist.conf


echo "# prevent power down of wireless when idle" >>/etc/modprobe.d/8192cu.conf
echo "options 8192cu rtw_power_mgnt=0 rtw_enusbss=0" >>/etc/modprobe.d/8192cu.conf

#Set up Go1.5.1.
echo export PATH=/root/go/bin:\$\{PATH\} >>/root/.bashrc
echo export GOROOT=/root/go >>/root/.bashrc
echo export GOPATH=/root/go_path >>/root/.bashrc


cd /root
tar -zxvf go.tgz
rm -f go.tgz

#ifndef DEBUG
#rm -rf go
#endif

EOF

echo "*** STRATUX COMPILE/PACKAGE INSTALL ***"
echo " - RTL-SDR tools"

  ssh_in_to_qemu chroot /mnt sh -l -ex - <<\EOF
apt-get install -y git cmake libusb-1.0-0.dev build-essential


cd /root
rm -rf rtl-sdr
git clone https://github.com/jpoirier/librtlsdr rtl-sdr
cd rtl-sdr
mkdir build
cd build
cmake ../
make
make install
ldconfig
EOF

echo " - Stratux"

scp_in_to_qemu /root/spindle/gen_gdl90 /tmp/gen_gdl90

  ssh_in_to_qemu chroot /mnt bash -l -ex - <<\EOF
source /root/.bashrc
cd /root

apt-get install -y mercurial

rm -rf stratux
git clone https://github.com/cyoung/stratux --recursive
cd stratux
make www
make xdump978
make xdump1090
cp -f dump1090/dump1090 /usr/bin/

mv /tmp/gen_gdl90 /usr/bin/gen_gdl90
chmod +x /usr/bin/gen_gdl90
cp start_uat.sh /usr/bin/start_uat
cp init.d-stratux /etc/init.d/stratux
cp start_stratux.sh /usr/sbin/stratux
chmod 755 /usr/bin/start_uat
chmod 755 /usr/sbin/stratux
chmod 755 /etc/init.d/stratux
ln -s /etc/init.d/stratux /etc/rc2.d/S01stratux
ln -s /etc/init.d/stratux /etc/rc6.d/K01stratux

update-rc.d stratux enable

mkdir -p /root/go_path
#FIXME
#export PATH=/root/go/bin:${PATH}
#export GOROOT=/root/go
#export GOPATH=/root/go_path
#make
EOF

ssh_in_to_qemu chroot /mnt sh -l -ex - <<\EOF
#i2c
echo "i2c-bcm2708" >>/etc/modules
echo "i2c-dev" >>/etc/modules



#kalibrate-rl
cd /root
git clone https://github.com/steve-m/kalibrate-rtl
cd kalibrate-rtl
apt-get install -y autoconf fftw3 fftw3-dev
apt-get install -y libtool

./bootstrap
./configure
make
make install

#disable serial console
sed -i /etc/inittab -e "s|^.*:.*:respawn:.*ttyAMA0|#&|"

#Set the keyboard layout to US.
sed -i /etc/default/keyboard -e "/^XKBLAYOUT/s/\".*\"/\"us\"/"





EOF

echo "**** END STRATUX SETUP *****"
#DONE HERE
}




cd $WORKDIR
dotask branch_image ../$OUTDIR/stage3.$IMGFORMAT $CURIMG


dotask run_qemu $CURIMG
dotask mount_apt_cache
dotask disable_starting_services


dotask setup_stratux


dotask save_space_using_hardlink
dotask allow_starting_services
dotask update_issue
dotask fingerprint_debian
dotask shutdown_qemu


dotask setup_stratux_bootconfig
universal_cleanup


dotask finish_image
