Make install script for server configs
[matches/MCTX3420.git] / server / run.sh
1 #!/bin/bash
2
3 # Check existence of program
4 if [ ! -e "server" ]; then
5         (echo "Rebuild server.") 1>&2;
6         exit 1
7 fi
8
9
10 if [[ "$(uname -m)" != *arm*  ]]; then
11         echo Not running on the BBB
12         # Use this to quickly test run the server in valgrind
13         spawn-fcgi -p9005 -n ./valgrind.sh
14         # Use this to run the server normally
15         #spawn-fcgi -p9005 -n ./server
16         exit 0
17 fi
18
19 # Check running as root
20 if [ "$(whoami)" != "root" ]; then
21         (echo "Run $0 as root.") 1>&2
22         exit 1
23 fi
24
25 # Rotate the logs
26 echo Rotating the system logs
27 logrotate -f /etc/logrotate.d/mctxserv.conf
28
29 # Identify cape-manager slots
30 slot=$(echo /sys/devices/bone_capemgr.*/slots | awk '{print $1}')
31 pwm=/sys/class/pwm/
32
33 # Load PWM module
34 if [ $(cat $slot | grep am33xx_pwm -c) -gt 0 ]; then 
35         echo am33xx_pwm already loaded, not loading again; 
36 else
37         echo Enabling PWM driver am33xx_pwm
38         echo am33xx_pwm > $slot
39 fi;
40
41 # Safe pins that won't interfere with one another
42 #ports=(P9_22 P9_42 P9_16 P8_13 P9_28);
43 #portnumbers=(0 2 4 6 7);
44 #Full correspondence from pwm0-pwm7
45 ports=(P9_22 P9_21 P9_42 P9_14 P9_16 P8_19 P8_13 P9_28);
46 portnumbers=(0 1 2 3 4 5 6 7);
47 # Enable PWM pins
48 # They must be exported at this stage, before the device tree
49 # overlay for that pin is enabled.
50 for ((c=0; c < ${#ports[*]}; c++)); do
51         if [ ! -d $pwm/pwm${portnumbers[$c]} ]; then
52                 echo Exporting PWM ${portnumbers[$c]} \(${ports[$c]}\)
53                 echo ${portnumbers[$c]} > $pwm/export
54         else
55                 echo PWM ${portnumbers[$c]} already enabled
56         fi;
57                 
58         if [ $(cat $slot | grep ${ports[$c]} -c) -gt 0 ]; then
59                 echo PWM pin ${ports[$c]} already enabled, not enabling again
60         else
61                 (echo bone_pwm_${ports[$c]} > $slot) 1>&2 >> /dev/null
62         fi;
63 done;
64
65 # Load ADCs
66 if [ $(cat $slot | grep BB-ADC -c) -gt 0 ]; then 
67         echo BB-ADC already loaded, not loading again; 
68 else 
69         echo Enabling BB-ADC
70         (echo BB-ADC > $slot) 1>&2 >> /dev/null
71 fi;
72
73 # Find adc_device_path
74 # NOTE: This has to be passed as a parameter, because it is not always the same. For some unfathomable reason. Hooray.
75 #adc_device_path=$(dirname $(find /sys -name *AIN0))
76
77
78 # Get the parameters
79 . parameters
80
81 echo "Parameters are: $parameters"
82
83 # Run the program with parameters
84 # TODO: Can tell spawn-fcgi to run the program as an unprivelaged user?
85 # But first will have to work out how to set PWM/GPIO as unprivelaged user
86 fails=0
87 while [ $fails -lt 10 ]; do
88         spawn-fcgi -p9005 -n -- ./server $parameters
89                 error=$?
90         if [ "$error" == "0" ]; then
91                 exit 0
92         fi
93         fails=$(( $fails + 1 ))
94         (echo "Restarting server after Fatal Error #$fails") 1>&2
95         
96 done
97 (echo "Server had too many Fatal Errors ($fails)") 1>&2
98 exit $fails

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