Automatic commit of irc logs
[matches/MCTX3420.git] / server / run.sh
index 18af55b..1890059 100755 (executable)
 #!/bin/bash
-# Use this to quickly test run the server in valgrind
-#spawn-fcgi -p9005 -n ./valgrind.sh
-# Use this to run the server normally
-#./stream &
+
+# Check existence of program
+if [ ! -e "server" ]; then
+        (echo "Rebuild server.") 1>&2;
+        exit 1
+fi
+
+# Get the parameters
+. parameters
+echo "Parameters are: $parameters"
+
+if [[ "$(uname -m)" != *arm*  ]]; then
+        echo Not running on the BBB
+        # Use this to quickly test run the server in valgrind
+        #spawn-fcgi -p9005 -n ./valgrind.sh
+        # Use this to run the server normally
+        spawn-fcgi -p9005 -n -- ./server $parameters
+        exit 0
+fi
 
 # Check running as root
 if [ "$(whoami)" != "root" ]; then
-       (echo "Run $0 as root.") 1>&2
-       exit 1
+        (echo "Run $0 as root.") 1>&2
+        exit 1
 fi
 
-# Check existence of program
-if [ ! -e "server" ]; then
-       (echo "Rebuild server.") 1>&2;
-       exit 1
-fi
+# Rotate the logs
+echo Rotating the system logs
+logrotate -f /etc/logrotate.d/mctxserv.conf
 
 # Identify cape-manager slots
 slot=$(echo /sys/devices/bone_capemgr.*/slots | awk '{print $1}')
 pwm=/sys/class/pwm/
 
 # Load PWM module
-#modprobe pwm_test
-(echo am33xx_pwm > $slot) 1>&2 >> /dev/null
-#for port in P9_21 P9_22 P9_14 P9_16 P9_29 P9_31 P9_42 P8_13 P8_19 P8_34 P8_36 P8_45 P8_46; do
-#      echo bone_pwm_$port > $slot
-#done
-echo 0 > $pwm/export
-echo 1 > $pwm/export
-echo bone_pwm_P9_21 > $slot
-echo bone_pwm_P9_22 > $slot
+if [ $(cat $slot | grep am33xx_pwm -c) -gt 0 ]; then 
+        echo am33xx_pwm already loaded, not loading again; 
+else
+        echo Enabling PWM driver am33xx_pwm
+        echo am33xx_pwm > $slot
+fi;
+
+# Safe pins that won't interfere with one another
+#ports=(P9_22 P9_42 P9_16 P8_13 P9_28);
+#portnumbers=(0 2 4 6 7);
+#Full correspondence from pwm0-pwm7
+ports=(P9_22 P9_21 P9_42 P9_14 P9_16 P8_19 P8_13 P9_28);
+portnumbers=(0 1 2 3 4 5 6 7);
+# Enable PWM pins
+# They must be exported at this stage, before the device tree
+# overlay for that pin is enabled.
+for ((c=0; c < ${#ports[*]}; c++)); do
+        if [ ! -d $pwm/pwm${portnumbers[$c]} ]; then
+                echo Exporting PWM ${portnumbers[$c]} \(${ports[$c]}\)
+                echo ${portnumbers[$c]} > $pwm/export
+        else
+                echo PWM ${portnumbers[$c]} already enabled
+        fi;
+                
+        if [ $(cat $slot | grep ${ports[$c]} -c) -gt 0 ]; then
+                echo PWM pin ${ports[$c]} already enabled, not enabling again
+        else
+                (echo bone_pwm_${ports[$c]} > $slot) 1>&2 >> /dev/null
+        fi;
+done;
 
 # Load ADCs
-(echo cape-bone-iio > $slot) 1>&2 >> /dev/null
+if [ $(cat $slot | grep BB-ADC -c) -gt 0 ]; then 
+        echo BB-ADC already loaded, not loading again; 
+else 
+        echo Enabling BB-ADC
+        (echo BB-ADC > $slot) 1>&2 >> /dev/null
+fi;
+
 # Find adc_device_path
 # NOTE: This has to be passed as a parameter, because it is not always the same. For some unfathomable reason. Hooray.
-adc_device_path=$(dirname $(find /sys -name *AIN0))
+#adc_device_path=$(dirname $(find /sys -name *AIN0))
+
+
 
 
 # Run the program with parameters
 # TODO: Can tell spawn-fcgi to run the program as an unprivelaged user?
 # But first will have to work out how to set PWM/GPIO as unprivelaged user
+# NOTE: Having the program automatically restart itself after a *FATAL ERROR* doesn't seem like such a good idea now
+# (Some things that call Fatal happen under circumstances where they will just keep calling Fatal every time the program starts)
+# Change the number of fails to 1 for now. We can potentially use different error codes for different types of errors, but that seems overkill.
 fails=0
-while [ $fails -lt 10 ]; do
-       spawn-fcgi -p9005 -n -- ./server -a "$adc_device_path"
-       if [ "$?" == "0" ]; then
-               exit 0
-       fi
-       fails=$(( $fails + 1 ))
-       (echo "Restarting server after Fatal Error #$fails") 1>&2
-       
+while [ $fails -lt 1 ]; do
+        spawn-fcgi -p9005 -n -- ./server $parameters
+               error=$?
+        if [ "$error" == "0" ]; then
+                exit 0
+        fi
+        fails=$(( $fails + 1 ))
+        (echo "Restarting server after Fatal Error #$fails") 1>&2
+        
 done
 (echo "Server had too many Fatal Errors ($fails)") 1>&2
 exit $fails
-

UCC git Repository :: git.ucc.asn.au