#!/bin/bash
# This script configures a SIAM node based on user selection
# of primary RF link, ethernet enabled, SIAM autostart, etc.


# root=~/tmp; echo; echo WARNING - DEBUG mode, putting output file in $root; echo

echo Select primary PPP communications link:
select primaryLink in globalstar freewave nullModem NONE; do
  if [ $REPLY -lt 1 -o $REPLY -gt 4 ]; then
    echo Invalid reply
  else
    if [ $REPLY -eq 1 ]; then
      pppScript=globalstar
    elif [ $REPLY -eq 4 ]; then
      # "NONE" means no ppp script
      pppScript=""
    else
      # Freewave, null modem, and 'NO radio' all invoke 'longhaul' ppp script
      pppScript=longhaul
    fi
    break;
  fi
done

echo
echo Enable ethernet?
select foo in 'Ethernet enabled' 'Ethernet disabled'; do
  if [ $REPLY -lt 1 -o $REPLY -gt 2 ]; then
    echo Invalid reply
  else
    if [ $REPLY -eq 1 ]; then
      ethernetEnabled=TRUE
    else
      ethernetEnabled=FALSE
    fi
    break;
  fi
done

echo
echo Autostart SIAM application on reboot?
select foo in 'Autostart' 'Do not autostart'; do
  if [ $REPLY -lt 1 -o $REPLY -gt 2 ]; then
    echo Invalid reply
  else
    if [ $REPLY -eq 1 ]; then
      autostart=TRUE
    else
      autostart=FALSE
    fi
    break;
  fi
done

echo
echo Select Log4J level:
select log4jLevel in 'DEBUG' 'INFO'; do
  if [ $REPLY -lt 1 -o $REPLY -gt 2 ]; then
    echo Invalid reply
  else
    break;
  fi
done

echo 
echo "Enter portal name or IP:"
read portalIP

echo --------------------------------
echo Your choices:
echo primary link: $primaryLink
echo ethernet enabled: $ethernetEnabled
echo SIAM autostart: $autostart
echo Log4J threshold: $log4jLevel
echo portal: $portalIP

echo
echo Proceed? 
select proceed in 'Yes; THIS WILL MODIFY SYSTEM CONFIGURATION FILES' 'No, exit'; do
  if [ $REPLY -lt 1 -o $REPLY -gt 2 ]; then
    echo Invalid reply

  elif [ $REPLY -eq 2 ]; then
    echo "Exit..."
    exit

  else
    break;
  fi

done

echo "Configuring..."
# Set appropriate property values in $SIAM_HOME/properties/siamPort.cfg
properties=$SIAM_HOME/properties/siamPort.cfg

echo "Modifying $properties..."

# If RF link specified, add appropriate 

if [ $primaryLink == "globalstar" ]; then
    protocolWaitTime=15000
    processWaitTime=240000
  else
    protocolWaitTime=5000
    processWaitTime=60000
fi

awkFile=/tmp/awkFile1.$$

cat > $awkFile <<AWKFILE1
/^[ \t]*CommsManager.onString/ {
  next;
}
/^[ \t]*CommsManager.offString/ {
  next;
}
/^[ \t]*CommsManager.protocolWaitTime/ {
  next;
}
/^[ \t]*CommsManager.processWaitTime/ {
  next;
}

/# Added by config script:/ {
  next;
}

{
  print;
}

END {
  printf "# Added by config script: user specified link '$primaryLink'\n"
  printf "CommsManager.onString = pon $pppScript\n";
  printf "CommsManager.offString = commsOff $pppScript\n";
  printf "CommsManager.protocolWaitTime = $protocolWaitTime\n";
  printf "CommsManager.processWaitTime = $processWaitTime\n";
}
AWKFILE1


awk -f $awkFile $properties > /tmp/siamPort.cfg.$$
mv /tmp/siamPort.cfg.$$ $properties
# Make sure everyone has read/write permission
chmod a+w $properties

# Create SIAM initialization file, based on user input
rcLocal=$root/etc/init.d/rc.local
echo "Generating $rcLocal..."

cat > $rcLocal <<EOF
#!/bin/bash
# Called at boot time to configure node appropriately.
# THIS FILE CREATED AUTOMATICALLY - DO NOT MODIFY

# Mount everything
mount -a

# Start log size management utilities. Args are:
#          logfile       maxBytes  checkInterval (sec)
/etc/siam/manageLog /var/log/syslog 500000 60 &
/etc/siam/manageLog /var/log/messages 500000 60 &

# Set groups and permissions for RXTX Java serial port package
chgrp lock /var/lock
chmod 775 /var/lock

# Get time from Ricoh 
/root/ricohRTC -s

# Try to synchronize time with shore
/usr/sbin/ntpdate -b ocean.shore.mbari.org

EOF

if [ "$ethernetEnabled" == "FALSE" ]; then  # User specified ethernet disabled
cat >> $rcLocal <<EOF2
# Disable ethernet, set hostname
ifdown eth0
hostname mooring

EOF2
fi


# Modify /etc/siam/siamEnv to reflect user inputs
siamEnv=$root/etc/siam/siamEnv

awkFile=/tmp/awkFile2.$$

cat > $awkFile <<AWKFILE2
/SIAM_AUTOSTART/ {
  next;
}
/SIAM_PORTAL/ {
  next;
}
/LOG4J_THRESHOLD/ {
  next;
}

{
  print;
}

END {
  printf "\n# SIAM_AUTOSTART indicates whether to automatically start SIAM on boot\n";
  printf "export SIAM_AUTOSTART=$autostart\n";
  printf "\n# SIAM_PORTAL is the name of the portal to contact\n";
  printf "export SIAM_PORTAL=$portalIP\n";
  printf "\n# LOG4J_THRESHOLD; DEBUG, INFO, WARN, or ERROR:\n";
  printf "export LOG4J_THRESHOLD=$log4jLevel\n";
  printf "\n# ppp script for wireless comms\n";
  printf "export SIAM_PPP_SCRIPT=$pppScript\n";
}
AWKFILE2

echo "Modifying $siamEnv ..."
awk -f $awkFile $siamEnv > /tmp/siamEnv.$$
mv /tmp/siamEnv.$$ $siamEnv
# Make sure everyone has read/write permission
chmod a+w $siamEnv

echo
echo "*** NOTE ***"
echo System must be rebooted in order for changes to take effect.



