Merge pull request #42 from jtanx/master
[matches/MCTX3420.git] / server / run.sh
1 #!/bin/bash
2 # Use this to quickly test run the server in valgrind
3 #spawn-fcgi -p9005 -n ./valgrind.sh
4 # Use this to run the server normally
5 #./stream &
6
7 # Check running as root
8 if [ "$(whoami)" != "root" ]; then
9         (echo "Run $0 as root.") 1>&2
10         exit 1
11 fi
12
13 # Check existence of program
14 if [ ! -e "server" ]; then
15         (echo "Rebuild server.") 1>&2;
16         exit 1
17 fi
18
19 # Identify cape-manager slots
20 slot=$(echo /sys/devices/bone_capemgr.*/slots | awk '{print $1}')
21
22 # Load PWM module
23 modprobe pwm_test
24 (echo am33xx_pwm > $slot) 1>&2 >> /dev/null
25 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
26         echo bone_pwm_$port > $slot
27 done
28
29 # Load ADCs
30 (echo cape-bone-iio > $slot) 1>&2 >> /dev/null
31 # Find adc_device_path
32 # NOTE: This has to be passed as a parameter, because it is not always the same. For some unfathomable reason. Hooray.
33 adc_device_path=$(dirname $(find /sys -name *AIN0))
34
35
36 # Run the program with parameters
37 # TODO: Can tell spawn-fcgi to run the program as an unprivelaged user?
38 # But first will have to work out how to set PWM/GPIO as unprivelaged user
39 fails=0
40 while [ $fails -lt 10 ]; do
41         spawn-fcgi -p9005 -n -- ./server -a "$adc_device_path"
42         if [ "$?" == "0" ]; then
43                 exit 0
44         fi
45         fails=$(( $fails + 1 ))
46         (echo "Restarting server after Fatal Error #$fails") 1>&2
47         
48 done
49 (echo "Server had too many Fatal Errors ($fails)") 1>&2
50 exit $fails
51

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