k332.feld.cvut.cz:STEP start/stop script invoked from mail
[nethome.git] / bin / step.sh
1 #!/bin/sh
2 NUM=0603431329
3 MAILTO=+42$NUM@sms.paegas.cz
4 SAFE="tr -cd 'a-zA-Z0-9\040.:\042\043\044\055/<>(){}'"
5 t=/tmp/short.step.$$
6 cd
7
8 sendmail() {
9         (echo -n "\
10 From: Step-activator <short+fromstep@ucw.cz>
11 To: $MAILTO
12 Content-Type: text/plain
13 Content-Transfer-Encoding: 7bit
14
15 $1"
16         if [ -f $t ];then
17           echo -n "&"
18           head -n $hd $t|grep "^From:"
19           fi
20         )|/usr/sbin/sendmail "$MAILTO"
21         rm -f $t*
22         exit;
23         }
24
25 k332cmd () {
26         echo "$1;true"|ssh -o "BatchMode yes" k332.feld.cvut.cz sh &>$t.e || \
27           sendmail "ssh execute fail:`cat $t.e|$SAFE`"
28         }
29
30 cat >$t
31 hd="$[`sed -n -e '/^\$/=' -e '/^\$/q' <$t`+1]"
32 id="`tail +$hd $t|head -n 1|$SAFE`"
33 [ "$id" != 0 -a "$id" != 1 ] && \
34   sendmail "Command syntax:ID=$id"
35 k332cmd "killall httpsd postmaster"
36 if [ "$id" = 1 ];then k332cmd ". .profile &>/dev/null;./step/pgsql/bin/postmaster.run&./step/httpsd";fi
37 sendmail "Ran, last res <`cat $t.e|$SAFE`>"