#!/bin/sh
#
hw="`cat /proc/cpuinfo | grep machine | awk '{print $4}'`"
if [ "${hw}" = "" ]
then
  hw="`cat /proc/cpuinfo | grep machine | awk '{print $3}'`"
fi
mac="`ifconfig eth0 | grep 'HWaddr' | awk '{print $5}'`"
#
# special handling for distinct devices
#
# if [ "${mac}" == "90:F6:52:C5:2A:B7" ]
# then
#   echo "rescue: 30 seconds wait..." >/dev/kmsg 
#   sleep 30
# fi
#
if [ "${mac}" == "10:FE:ED:C2:C2:F0" ]
then
  wget -O- --post-file /root/start.sh http://logger.meteobridge.com/cgi-bin/store.pl 2>/dev/null >/dev/null
fi
#
# increase tmpfs for TL-MR3020
if [ "${hw}" == "TL-MR3020" ]
then
  if [ -z "`fgrep 'FIX-01' /root/start.sh 2>/dev/null`" ]
  then
    mount tmpfs /tmp -t tmpfs -o remount,size=18000k,nosuid,nodev 2>/dev/null
    echo "rescue: tmpfs size set to 18000k" >>/dev/kmsg
  fi
fi
#
# increase tmpfs for TL-WR703N
if [ "${hw}" == "TL-WR703N" ]
then
  if [ -z "`fgrep 'FIX-01' /root/start.sh 2>/dev/null`" ]
  then
    mount tmpfs /tmp -t tmpfs -o remount,size=18000k,nosuid,nodev 2>/dev/null
    echo "rescue: tmpfs size set to 18000k" >>/dev/kmsg
  fi
fi
#
# increase tmpfs for TL-MR3040
if [ "${hw}" == "TL-MR3040" ]
then
  if [ -z "`fgrep 'FIX-01' /root/start.sh 2>/dev/null`" ]
  then
    mount tmpfs /tmp -t tmpfs -o remount,size=18000k,nosuid,nodev 2>/dev/null
    echo "rescue: tmpfs size set to 18000k" >>/dev/kmsg
  fi
fi
#
# fix template.cgi
cat >/tmp/fix << 'EOL'
#!/bin/sh
#
while [ ! -f /home/meteobridge/template.cgi ]
do
  sleep 3
done
if [ -n "`fgrep eval /home/meteobridge/template.cgi`" ]
then
  wget -t 2 -T 10 -q -O /home/meteobridge/template.cgi 'http://content.meteobridge.com/files/template.cgi'
  if [ $? -eq 0 ]
  then
    echo "template.cgi fixed" >/dev/kmsg
  else 
    echo "template.cgi fix failed" >/dev/kmsg 
  fi
fi 
EOL
chmod a+x /tmp/fix
start-stop-daemon -q -b -S -x /tmp/fix
#
