#!/bin/bash

#
# original setup-rtlsdr script PLUS stuff for APRS iGate from:
# https://sq7mru.blogspot.com/2013/08/aprs-igate-rx-z-tunera-dvb-t.html
#

SRCDIR="/var/local/src"
PKGSDIR="/var/local/pkgs"

APRSFREQ="144.390M"




SEPARATOR="##############################################################################"



echo
echo
echo $SEPARATOR
echo "# Getting base packages"
echo

sudo apt-get update

sudo apt-get install cmake build-essential python-pip libusb-1.0-0-dev python-numpy git git-core pandoc

# add'l packages from sq7mru

sudo apt-get install libusb-1.0-0 libusb-1.0-0-dev libpulse-dev libx11-dev python-pkg-resources qtcreator libtool autoconf automake libfftw3-dev





echo
echo
echo $SEPARATOR
echo "# Building RTL-SDR"
echo

read -p "Install base RTL-SDR?  " install_rtlsdr

if [[ "$install_rtlsdr" == "y" ]]; then

   cd $SRCDIR

   git clone git://git.osmocom.org/rtl-sdr.git
   
   cd rtl-sdr
   
   mkdir build
   
   cd build
   
   cmake ../ -DINSTALL_UDEV_RULES=ON   # Pi
   #cmake ../ -DINSTALL_UDEV_RULES=ON -DDETACH_KERNEL_DRIVER=ON
   
      # Note that faster execution can be had on more recent Raspberry Pi 
      # models (B+, 3) by using four processors. The command becomes:
      # make -j5
   make
   
   sudo make install
   
   sudo ldconfig
      # if it was successful you should see no return on the previous command.
   
   sudo cp ../rtl-sdr.rules /etc/udev/rules.d/01-rtl-sdr.rules   # Pi
   #sudo cp ../rtl-sdr.rules /etc/udev/rules.d/
   
   #  blacklist and unload kernel modules
      cat > /etc/modprobe.d/no-dvb.conf << EOF
   blacklist dvb_usb_rtl28xxu
   blacklist rtl2832
   blacklist rtl2830
EOF

   # maybe for Pi
   ## rmmod dvb_usb_rtl28xxu 
   ## rmmod rtl2830
   ## rmmod rtl2832

else

   echo -e "...skipping\n\n"

fi





echo
echo
echo $SEPARATOR
echo "# DireWolf "
echo

read -p "Install DireWolf?  " install_direwolf

if [[ "$install_direwolf" == "y" ]]; then

   cd $SRCDIR

   sudo apt-get install libasound2-dev

   git clone https://www.github.com/wb2osz/direwolf

   cd direwolf

   # to get rid of the blinky screen, check out the dev branch
   # maybe... fucking ridiculous
   # 

   git checkout dev 

   mkdir build && cd build
   cmake ..
   make -j4

   sudo make install
   make install-conf


   # 
   # the previous command installs conf files in user's home dir
   # because why the fuck would you put this in the user's home dir??? WHYYYYY???
   # we have perfectly fucking good etc/ dirs for conf files jfc people
   #

   if [[ -d /usr/local/etc ]]; then

      mkdir -v -p /usr/local/etc/direwolf

      mv -v ~/direwolf.conf /usr/local/etc/direwolf/
      mv -v ~/sdr.conf /usr/local/etc/direwolf/
      mv -v ~/telem-m0xer-3.txt /usr/local/etc/direwolf/
      mv -v ~/telem-*.conf /usr/local/etc/direwolf/
      mv -v ~/dw-start.sh /usr/local/sbin/
      chmod +x /usr/local/sbin/dw-start.sh
   
   fi

else

   echo -e "...skipping\n\n"

fi





echo
echo
echo $SEPARATOR
echo "# multimonNG "
echo

read -p "Install multimonNG?  " install_multimonNG

if [[ "$install_multimonNG" == "y" ]]; then

   cd $SRCDIR

   git clone https://github.com/EliasOenal/multimonNG.git

   cd multimonNG

   mkdir build

   cd build

   #qmake ../multimon-ng.pro
   cmake ..

   make

   sudo make install

else

   echo -e "...skipping\n\n"

fi





echo
echo
echo $SEPARATOR
echo "# pymultimonaprs"
echo

read -p "Install pymultimonAPRS?  " install_pymultimonaprs

if [[ "$install_pymultimonaprs" == "y" ]]; then

   cd $SRCDIR

   git clone https://github.com/asdil12/pymultimonaprs.git

   cd pymultimonaprs

   chmod a+x setup.py

   ./setup.py build

   sudo ./setup.py install



   #
   # keygen
   #

   echo "--- Generating key:\n\n"

   read -p "    Callsign: " CALLSIGN

   ./keygen.py $CALLSIGN



   #
   # edit config
   #

   if [[ -e /etc/pymultimonaprs.json ]]; then

      if [[ -d /usr/local/etc ]]; then

         echo "Hard linking /etc/pymultimonaprs.json to /usr/local/etc/pymultimonaprs.json"

         sudo ln /etc/pymultimonaprs.json /usr/local/etc/pymultimonaprs.json

      fi 

      read -p  "--- Edit config file to specify your parameters... OK\n" poop

      sudo vi /etc/pymultimonaprs.json

   fi



   #
   # run automatically?
   #

   read -p "--- Run automatically as INIT service?  " pymultimonaprs_init

   if [[ "$pymultimonaprs_init" == "y" ]]; then

      #sudo cp pymultimonaprs.init /etc/init.d/pymultimonaprs

      echo "systemctl :: reloading daemons"

      sudo systemctl daemon-reload

      echo

      # pymultimonaprs.service is there as well

      echo "systemctl :: enabling pymultimonaprs service"

      sudo systemctl enable pymultimonaprs

      echo

      echo "systemctl :: starting  pymultimonaprs service"

      sudo systemctl start pymultimonaprs

      echo

      echo "systemctl :: verifying status of pymultimonaprs service"

      sudo systemctl status pymultimonaprs

      echo

   else

      echo -e "--- Keeping at manual init. \n"

   fi

else

   echo -e "...skipping\n\n"

fi





echo
echo
echo $SEPARATOR
echo "# kalibrate-rtl"
echo

read -p "Install kalibrate-rtl?  " install_kalibratertl

if [[ "$install_kalibratertl" == "y" ]]; then

   cd $SRCDIR

   git clone https://github.com/steve-m/kalibrate-rtl.git 

   cd kalibrate-rtl

   ./bootstrap

   ./configure

   make

else

   echo -e "...skipping\n\n"

fi




echo "Next Steps"
echo 

echo "Test APRS reception \n\n
 1) rtl_fm -f $APRSFREQ -s 22050 | multimon-ng -t raw -a AFSK1200 -f alpha /dev/stdin \n
\n
 2) kal -- to calibrate rtl-sdr \n
\n
 3) edit /etc/pymultimonaprs -- to use pymultimonaprs \n
\n
 4) pymultimonaprs -v for testing \n
\n
"

echo "Le Fin."




# 
# for compiling rtl_ais from osmocom rtl_sdr, since osmocom puts
# things in /usr/local instead of /
#
#
#sudo apt-get install librtlsdr0 librtlsdr-dev
#



