#!/bin/bash
#
# FREE STAR* System Build and Configuration Script
# 
# Pre-Requisites...
#
#   CENTOS 5.x (32 bit) installation
#   
#   In order to run this script, you will need:
#
#   Repeater or Gateway Callsign
#   Your callsign - registered on USROOT in order to connect to REF reflectors
#   IP address of eth0 (we recommend a static IP address)
#   Your gateway should be registered with the ircDDB network and a password assigned
#   Knowledge of your intended RF setup - module, A, B, or C, and duplex type (full or half duplex)
#
#
#  Version 0.50 - Ramesh Dhami, 2012.05.01 - Updated to include option to install DBVRPTR modem / Red DVAP Device - also update to pull latest version of all packages
#  Version 0.43 - Ramesh Dhami, 2012.01.26 - Startup all services after the install
#  Version 0.42 - Ramesh Dhami, 2012.01.20 - Update to pull latest packages
#  Version 0.41 - Ramesh Dhami, 2011.12.17 - Pull rptr v3.10 off VA3UV Site
#  Version 0.40 - Ramesh Dhami, 2011.11.23 - Fixed typo
#  Version 0.39 - Ramesh Dhami, 2011.11.13 - Update to pull rptr v3.07
#  Version 0.38 - Ramesh Dhami, 2011.11.01 - Update to download g2_link - v2.79
#  Version 0.37 - Ramesh Dhami, 2011.10.28 - Fix typtos with PACKAGE_REV for rptr
#  Version 0.36 - Ramesh Dhami, 2011.10.23 - Disabled the reboot so that the script returns to the phase2-setup script
#  Version 0.35 - Ramesh Dhami, 2011.10.20 - Copy rptr init script to /etc/init.d
#  Version 0.34 - Ramesh Dhami, 2011.10.19 - Update RPTR_VIRTUAL_IP for GMSK rptr
#  Version 0.33 - Ramesh Dhami, 2011.10.16 - Updated pcrepeater dstarrepeaterd init script
#  Version 0.32 - Ramesh Dhami, 2011.10.07 - Updated package info to show Linux as well as reflect 20111004 of sound card s/w
#  Version 0.31 - Ramesh Dhami, 2011.10.01 - Change the package info in g2_ircddb.cfg when installing the sound card s/w
#  Version 0.3 -  Ramesh Dhami, 2011.09.30 - Added option to install sound card s/w
#
# Ramesh@va3uv.com



# Installation directories

g2_ircddb_home="/root/g2_ircddb"
g2_link_home="/root/g2_link"
rptr_home="/root/rptr"
dvrptr_home="/root/dvrptr"
dvap_rptr_home="/root/dvap_rptr"


# config variables

time_zone="EST5EDT"

yum -y install gcc-c++

mkdir /root/rptr
mkdir /root/g2_link
mkdir /root/g2_ircddb
mkdir /root/my_scripts

# disable rptr as this may have been set to the default in the original ISO

service rptr stop
chkconfig rptr off


cd /root/g2_link


wget http://va3uv.com/hp/g2_link_v2.83_Linux.zip

wget http://www.va3uv.com/hp/g2link_test.zip


cd /root/g2_ircddb

wget http://www.va3uv.com/hp/g2_ircddb_306_Linux.zip

wget http://www.va3uv.com/hp/ircDDB_124_linux.zip

wget http://www.va3uv.com/hp/libber.zip


cd /root/rptr

wget http://va3uv.com/hp/rptr_v319_Linux.zip


cd /tmp

wget http://www.va3uv.com/hp/bakefiles.zip

wget http://www.va3uv.com/hp/disable_useless_Linux_services.sh

wget http://www.va3uv.com/hp/free-star-cron.txt


cd /root/my_scripts

wget http://www.va3uv.com/hp/get_gwy_list.sh



yum -y install ntp

chkconfig ntpd on

cd /usr/share/zoneinfo

cp $time_zone /etc/localtime

service ntpd stop

ntpdate pool.ntp.org

service ntpd start

hwclock --systohc

service ntpd restart

# Disable Linux Firewall

service iptables stop
chkconfig iptables off

cd /tmp
chmod +x disable_useless_Linux_services.sh
./disable_useless_Linux_services.sh
 
cd /root/g2_ircddb

unzip g2_ircddb_306_Linux.zip

unzip ircDDB_124_linux.zip

unzip libber.zip



while [ ! $GW_TMP ]; do
    echo " "
    echo "Please enter the callsign of your Gateway (Repeater):"
    echo " "
    read GW_TMP
done

GW_CALL=`echo $GW_TMP | tr '[:lower:]' '[:upper:]'`


echo " "
echo "OK, Gateway Callsign will be set to '$GW_CALL'"


while [ ! $IRCPWD ]; do
    echo " "
    echo "Please enter the password provided by the ircDDB-team to enter the system:"
    echo " "
    read IRCPWD
done

echo " "
echo "OK, Password will be set to '$IRCPWD'"


echo " "


while [ ! $ETH0_IP_ADDR ]; do
    echo " "
    echo "Please enter the IP Addreess for eth0 (internal IP - e.g., 192.x.x.x):"
    echo " "
    read ETH0_IP_ADDR
done

echo " "
echo "OK, eth0 IP will be set to '$ETH0_IP_ADDR'"


# Now go and enter the above variables into the /root/g2_ircddb/g2_ircddb.cfg file


sed -i "s|OWNER=XX0XXX|OWNER=$GW_CALL|g" $g2_ircddb_home/g2_ircddb.cfg

sed -i "s|G2_INTERNAL_IP=0.0.0.0|G2_INTERNAL_IP=$ETH0_IP_ADDR|g" $g2_ircddb_home/g2_ircddb.cfg

sed -i "s|IRC_PASS=1111111111111111|IRC_PASS=$IRCPWD|g" $g2_ircddb_home/g2_ircddb.cfg


# Now enter the variables into the /root/g2_ircddb/proc_g2_dtfms.sh script


sed -i "s|G2_INT_IP=127.0.0.1|G2_INT_IP=$ETH0_IP_ADDR|g" $g2_ircddb_home/proc_g2_ircddb_dtmfs.sh

sed -i "s|G2=|G2=$GW_CALL|g" $g2_ircddb_home/proc_g2_ircddb_dtmfs.sh



# Now download and build the wxBase package

cd /tmp

wget http://sourceforge.net/projects/wxwindows/files/2.8.11/wxBase-2.8.11.tar.gz/download

tar xvf wxBase-2.8.11.tar.gz
cd wxBase-2.8.11
./configure --disable-gui --without-expat
/usr/bin/make


mv /tmp/bakefiles.zip /tmp/wxBase-2.8.11/build/bakefiles.zip

cd /tmp/wxBase-2.8.11/build
                      
unzip bakefiles.zip

cd ..

/usr/bin/make install

echo "/usr/local/lib" >> /etc/ld.so.conf

/sbin/ldconfig -v


# Now build the ircddb toolkit library

cd /root/g2_ircddb
/usr/bin/make > /root/g2_ircddb_make.log


#Build the libber.a library

chmod +x build_ber.doit
#/root/g2_ircddb/build_ber.doit

/usr/bin/g++ -c /root/g2_ircddb/golay23.cpp
/usr/bin/g++ -c /root/g2_ircddb/dstar_dv.cpp

/usr/bin/ar rcv /root/g2_ircddb/libber.a /root/g2_ircddb/golay23.o /root/g2_ircddb/dstar_dv.o
/usr/bin/ranlib /root/g2_ircddb/libber.a


# Build g2_ircddb

chmod +x g2_ircddb.doit
#/root/g2_ircddb/g2_ircddb.doit >> /root/g2_ircddb_make.log


/usr/bin/g++ -W -Wall -g -O2 -Wall `wx-config --cppflags base` -o /root/g2_ircddb/g2_ircddb /root/g2_ircddb/g2_ircddb.cpp /root/g2_ircddb/libircddb.a /root/g2_ircddb/libber.a -I/usr/include -L/usr/lib `wx-config --libs base` -pthread

 
# Install the Linux init script

chmod +x g2_ircddb.SVC
mv g2_ircddb.SVC /etc/init.d/g2_ircddb
chkconfig --add g2_ircddb
chkconfig g2_ircddb on


chmod +x proc*

mv proc_g2_ircddb_dtmfs.SVC /etc/init.d/proc_g2_ircddb_dtmfs

chkconfig --add proc_g2_ircddb_dtmfs
chkconfig proc_g2_ircddb_dtmfs on


# Build g2_link

cd $g2_link_home

unzip g2_link_v2.83_Linux.zip


while [ ! $ADMIN_CALL_TMP ]; do      
    echo " "
    echo "Please enter your personal callsign (as reg'd on USROOT):"
    echo " "
    read ADMIN_CALL_TMP      
done

ADMIN_CALL=`echo $ADMIN_CALL_TMP | tr '[:lower:]' '[:upper:]'`

echo " "

echo "Admin callsign will be set to '$ADMIN_CALL'"

sed -i "s|#LOGIN_CALL=|LOGIN_CALL=$ADMIN_CALL|g" $g2_link_home/g2_link.cfg

sed -i "s|ADMIN=XX0XXX|ADMIN=$ADMIN_CALL|g" $g2_link_home/g2_link.cfg

sed -i "s|#OWNER=|OWNER=$GW_CALL|g" $g2_link_home/g2_link.cfg

sed -i "s|MAX_DONGLES=5|MAX_DONGLES=1|g" $g2_link_home/g2_link.cfg


# Build the g2_link Software

chmod +x g2_link.doit
./g2_link.doit
unzip g2link_test.zip
chmod +x g2link_test.doit
./g2link_test.doit


# Install the g2_link init script

chmod +x g2_link.SVC
mv g2_link.SVC /etc/init.d/g2_link
chkconfig --add g2_link
chkconfig g2_link on

# Build the gwys.txt file for g2_link

cd /root/my_scripts

chmod +x *.sh

./get_gwy_list.sh


# Now build and configure the GMSK repeater software

cd /root/rptr

unzip rptr_v319_Linux.zip

echo " "

echo "Now configuring the GMSK Repeater Software"

echo " "

while [ ! $RPTR_MODULE_TMP ]; do
    echo " "      
    echo "Please Enter the RF Band -->"
    echo " "
    echo "Enter A for 1.2GHz"
    echo "Enter B for 440MHz"
    echo "Enter C for 144MHz"
    echo " "
    read RPTR_MODULE_TMP      
done

RPTR_MODULE=`echo $RPTR_MODULE_TMP | tr '[:lower:]' '[:upper:]'`

case "$RPTR_MODULE" in
A)
  RPTR_PORT='19998';
;;
B)
  RPTR_PORT='19999';
;;
C)
  RPTR_PORT='20000';
;;
esac


     
while [ ! $DUPLEX_TMP ]; do      
    echo " "
    echo "Is your system Half Duplex (Simplex) or Full Duplex?"
    echo " "
    echo "Enter H for Half Duplex OR F for Full Duplex"
    echo " "
    read DUPLEX_TMP      
done

DUPLEX=`echo $DUPLEX_TMP | tr '[:lower:]' '[:upper:]'`

case $DUPLEX in

F)
  DUPLEX_TYPE='Full Duplex';
  DUPLEX_FLAG='Y';
;;
H)
  DUPLEX_TYPE='Half Duplex';
  DUPLEX_FLAG='N';
;;
esac

# Now ask which type of repeater s/w to install


echo " "
echo "**************************************************************"
echo " "
echo " " 
echo "FREE STAR* supports four types of repeater interface"
echo " "
echo "A hardware based GMSK Modem (Satoshi, Fred, Mark, Jim, etc...)"
echo " "
echo "the DVRPTR board from Germany"
echo " "
echo "the Red DVAP Access Point"
echo " "
echo "or the Soundcard pcrepeater software from Jonathan"
echo " "

while [ ! $RPTR_TYPE_TMP ]; do

    echo "Please select which one of the four scenarios is to be used on your system"
    echo " "
    echo "Enter G for GMSK Modem"
    echo " "
    echo "Enter D for DVRPTR modem"
    echo " "
    echo "Enter P for Red DVAP Access Point"
    echo " "
    echo "Enter S for Soundcard"
    echo " "
    read RPTR_TYPE_TMP
done

RPTR_TYPE=`echo $RPTR_TYPE_TMP | tr '[:lower:]' '[:upper:]'`

case $RPTR_TYPE in

G)

echo " ";

sed -i "s|RPTR=XX0XXX|RPTR=$GW_CALL|g" $rptr_home/rptr.cfg;

sed -i "s|OWNER=XX0XXX|OWNER=$GW_CALL|g" $rptr_home/rptr.cfg;

sed -i "s|RPTR_MOD=B|RPTR_MOD=$RPTR_MODULE|g" $rptr_home/rptr.cfg;

sed -i "s|RPTR_PORT=19999|RPTR_PORT=$RPTR_PORT|g" $rptr_home/rptr.cfg;

sed -i "s|G2_INTERNAL_IP=127.0.0.1|G2_INTERNAL_IP=$ETH0_IP_ADDR|g" $rptr_home/rptr.cfg;

sed -i "s|DUPLEX=N|DUPLEX=$DUPLEX_FLAG|g" $rptr_home/rptr.cfg;

sed -i "s|RPTR_ACK=N|RPTR_ACK=Y|g" $rptr_home/rptr.cfg;

cd /root/rptr;

yum -y install libusb-devel;

chmod +x /root/rptr/rptr.doit;

/root/rptr/rptr.doit;

chmod +x rptr.SVC;
cp rptr.SVC /etc/init.d/rptr;

chkconfig --add rptr;
chkconfig rptr on;


echo " ";

echo "GMSK rptr software configuration completed";


while [ ! $RETURN ]; do      
    echo " ";
    echo "Please plug in your GMSK Node Adapter and press 'c' followed by Enter to continue...";
    echo " ";
    read RETURN;      
done;


service rptr start;


# Now update the s/w version in the g2_ircddb.cfg file


sed -i "s|PACKAGE_REV=rptr_318|PACKAGE_REV=Linux-rptr_3.19|g" /root/g2_ircddb/g2_ircddb.cfg;

;;


D)

echo " ";

echo "Now installing the DVRPTR modem package";

mkdir /root/dvrptr;

cd /root/dvrptr;

wget http://www.va3uv.com/hp/dvrptr_176_Linux.zip;

unzip dvrptr_176_Linux.zip;

echo " ";

echo "Now updating config file";

sed -i "s|DVCALL=XX0XXX|DVCALL=$GW_CALL|g" $dvrptr_home/dvrptr.cfg;

sed -i "s|RPTR=XX0XXX|RPTR=$GW_CALL|g" $dvrptr_home/dvrptr.cfg;

sed -i "s|DVRPTR_MOD=B|DVRPTR_MOD=$RPTR_MODULE|g" $dvrptr_home/dvrptr.cfg;

sed -i "s|DVRPTR_INTERNAL_PORT=19999|DVRPTR_INTERNAL_PORT=$RPTR_PORT|g" $dvrptr_home/dvrptr.cfg;

sed -i "s|GATEWAY_IP=127.0.0.1|GATEWAY_IP=$ETH0_IP_ADDR|g" $dvrptr_home/dvrptr.cfg;

sed -i "s|DUPLEX=N|DUPLEX=$DUPLEX_FLAG|g" $dvrptr_home/dvrptr.cfg;

chmod +x dvrptr.SVC;

cp dvrptr.SVC /etc/init.d/dvrptr;

chkconfig --add dvrptr;

chkconfig dvrptr on;

while [ ! $RETURN ]; do      
    echo " ";
    echo "Please plug in your DVRPTR Modem and press 'c' followed by Enter to continue...";
    echo " ";
    read RETURN;      
done;


service dvrptr start;

echo " ";

echo "Please confirm the dvrptr modem serial number in the dvrptr.log file and correct the serial number in the dvrptr.cfg file";

echo " ";

echo "Please restart the dvrptr service with 'service dvrptr restart' after updating the serial number";

# Now update the s/w version in the g2_ircddb.cfg file


sed -i "s|PACKAGE_REV=rptr_318|PACKAGE_REV=Linux-dvrptr_1.76|g" /root/g2_ircddb/g2_ircddb.cfg;


;; 


P)

echo " ";


echo "Now installing the DVAP Access Point package";

mkdir /root/dvap_rptr;

cd /root/dvap_rptr;

wget http://www.va3uv.com/hp/dvap_rptr_230_Linux.zip;

unzip dvap_rptr_230_Linux.zip;

echo " ";

echo "Now updating config file";

sed -i "s|RPTR=XX0XXX|RPTR=$GW_CALL|g" $dvap_rptr_home/dvap_rptr.cfg;

sed -i "s|OWNER=XX0XXX|OWNER=$GW_CALL|g" $dvap_rptr_home/dvap_rptr.cfg;

sed -i "s|RPTR_MOD=B|RPTR_MOD=$RPTR_MODULE|g" $dvap_rptr_home/dvap_rptr.cfg;

sed -i "s|RPTR_PORT=19999|RPTR_PORT=$RPTR_PORT|g" $dvap_rptr_home/dvap_rptr.cfg;

sed -i "s|G2_INTERNAL_IP=127.0.0.1|G2_INTERNAL_IP=$ETH0_IP_ADDR|g" $dvap_rptr_home/dvap_rptr.cfg;

sed -i "s|RPTR_ACK=N|RPTR_ACK=Y|g" $dvap_rptr_home/dvap_rptr.cfg;



chmod +x dvap_rptr.SVC;

cp dvap_rptr.SVC /etc/init.d/dvap_rptr;

chkconfig --add dvap_rptr;

chkconfig dvap_rptr on;

while [ ! $RETURN ]; do      
    echo " ";
    echo "Please plug in your DVAP and press 'c' followed by Enter to continue...";
    echo " ";
    read RETURN;      
done;


service dvap_rptr start;

echo " ";

echo "Please confirm the DVAP serial number in the dvap_rptr.log file and correct the serial number in the dvap_rptr.cfg file";

echo " ";

echo "You will also need to edit the frequency and power level settings in the dvap_rptr.cfg file";

echo " ";

echo "Please restart the dvap_rptr service with 'service dvap_rptr restart' after updating the serial number, frequency and power level";


# Now update the s/w version in the g2_ircddb.cfg file


sed -i "s|PACKAGE_REV=rptr_318|#PACKAGE_REV=Linux-rptr_3.19|g" /root/g2_ircddb/g2_ircddb.cfg;

sed -i "s|# PACKAGE_REV=dvap-rptr_228|PACKAGE_REV=Linux-dvap-rptr_230|g" /root/g2_ircddb/g2_ircddb.cfg;


;; 



S)

echo " ";

echo "Now installing pre-requisites for Soundcard - pcrepeater software";

cd /tmp;

yum -y install yum-priorities;

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.i386.rpm;

rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt;

rpm -i rpmforge-release-0.5.1-1.el5.rf.*.rpm;

yum -y check-update;

yum -y install wxGTK-devel libusb-devel portaudio-devel;


wget http://downloads.sourceforge.net/libusb/libusb-1.0.8.tar.bz2;

tar -xvjpf libusb-1.0.8.tar.bz2;

cd libusb-1.0.8;

./configure --prefix=/usr && make;

make install;

yum -y install alsa-utils;

wget www.va3uv.com/hp/dstarrepeater.zip;

unzip dstarrepeater.zip;

mkdir /root/pcrepeater;

chmod +x showcard;
chmod +x dstarrepeaterd;
chmod +x dstarrepeaterd.SVC;

mv showcard /root/pcrepeater;
mv dstarrepeaterd /root/pcrepeater;
mv dstarrepeaterd.SVC /etc/init.d/dstarrepeaterd;
mv D-Star\ Repeater /root/.D-Star\ Repeater;


# Now enter config variables into the /root/.D-star\ Repeater File

sed -i "s|ve3uru-b|$GW_CALL-$RPTR_MODULE|g" /root/.D-Star\ Repeater;

sed -i "s|callsign=VE3URU B|callsign=$GW_CALL $RPTR_MODULE|g" /root/.D-Star\ Repeater;

sed -i "s|gateway=VE3URU G|gateway=$GW_CALL G|g" /root/.D-Star\ Repeater;

sed -i "s|mode=0|mode=$PC_RPTR_MODE|g" /root/.D-Star\ Repeater;

sed -i "s|gatewayAddress=10.11.0.18|gatewayAddress=$ETH0_IP_ADDR|g" /root/.D-Star\ Repeater;

sed -i "s|localPort=19999|localPort=$RPTR_PORT|g" /root/.D-Star\ Repeater;

sed -i "s|va3waj-b|$GW_CALL-$RPTR_MODULE|g" /etc/init.d/dstarrepeaterd;

# Finally change the package info in /root/g2_ircddb.cfg

sed -i "s|PACKAGE_REV=rptr_318|#PACKAGE_REV=rptr_318|g" /root/g2_ircddb/g2_ircddb.cfg;

sed -i "s|# PACKAGE_REV=sound_116|PACKAGE_REV=Linux-pcrepeater_20111004|g" /root/g2_ircddb/g2_ircddb.cfg;


chkconfig -add dstarrepeaterd;
chkconfig dstarrepeaterd on;

service dstarrepeaterd start;

cd /tmp;
rm -rf *.bz2;
rm -rf *.rpm;
;;

esac

echo " "


# Post installation clean-up...

# Add crontab for user root

echo "Do you wish to add crontab entries?"
echo " "
echo "WARNING - this process will overwrite existing entries"

while [ ! $CRON_TMP ]; do

    echo " "
    echo "Press Y to Accept automatic addition of cron entries"
    echo " "
    echo "Press X to skip this step"
    read CRON_TMP      

done

CRON=`echo $CRON_TMP | tr '[:lower:]' '[:upper:]'`

case $CRON in

Y)
  cd /tmp;
  crontab free-star-cron.txt;
;;
X)
  echo "The recommended cron entries can be found in a txt file in /tmp";
  echo " ";
  echo "Please add these to crontab for root";
;;
esac


# disable kernel updates

echo " "
echo "Disabling kernel updates"


service yum-updatesd stop
chkconfig yum-updatesd off

echo "exclude=kernel*" >> /etc/yum.conf

echo " "
echo "Removing temporary files"

cd /tmp
rm -rf *.gz
rm -rf disable*

cd /rptr
rm -rf *.zip

cd /root/g2_ircddb
rm -rf *.zip

cd /root/g2_link
rm -rf *.zip


cd /boot/grub
mv splash.xpm.gz splash.xpm.gz.old
wget www.va3uv.com/hp/splash.xpm.gz


service g2_ircddb start
service g2_link start
service proc_g2_ircddb_dtmfs start

cd /root


echo " "
echo "Congratulations, system build and configuration is complete"
echo " "

echo "Please email Ramesh - Ramesh@va3uv.com - with any feedback / suggestions for improvement"
echo " "
echo "Thank you for your support of the FREE STAR* project"
echo " "

#echo "System will now reboot"

#sleep 5

#reboot


exit 0

