#! /bin/sh
CUSTOM_COMMANDS_PATH=/var/custom_commands
FILENAME=$CUSTOM_COMMANDS_PATH/custom_commands_cgi
mkdir $CUSTOM_COMMANDS_PATH
echo "#! /bin/sh" > $FILENAME
echo "" >> $FILENAME
echo "path=$CUSTOM_COMMANDS_PATH" >> $FILENAME
echo "orgcmd=\$path/system_status" >> $FILENAME
echo "" >> $FILENAME
echo "# get Parameter (\$cmd+\$par) z.B. wol=192.168.1.123" >> $FILENAME
echo "cmd=\$(echo \$QUERY_STRING | sed -n \"s/\\(.*\\)=\\(.*\\)/\\\\1/p\")" >> $FILENAME
echo "par=\$(echo \$QUERY_STRING | sed -n \"s/\\(.*\\)=\\(.*\\)/\\\\2/p\")" >> $FILENAME
echo "" >> $FILENAME
echo "# echo \"QS: \$QUERY_STRING, function: \$cmd, param: \$par\" >>\$path/param" >> $FILENAME
echo "" >> $FILENAME
echo "if [ \$cmd = \"wol\" ]; then" >> $FILENAME
echo " " >> $FILENAME
echo " # get mac from multid.leases" >> $FILENAME
echo " set -- \$(grep \$par </var/flash/multid.leases) >/dev/null" >> $FILENAME
echo " if [ \$? = 0 ]; then" >> $FILENAME
echo " mac=\$2" >> $FILENAME
echo " else" >> $FILENAME
echo " mac=\$par " >> $FILENAME
echo " fi" >> $FILENAME
echo " " >> $FILENAME
echo " # call ether-wake" >> $FILENAME
echo " ether-wake \$mac >\$path/output" >> $FILENAME
echo " " >> $FILENAME
echo " echo \"Content-type: text/html\"" >> $FILENAME
echo " echo \"\"" >> $FILENAME
echo " echo \"<html><body>Der Wake-Befehl wurde an die Mac-Adresse \$mac gesendet.<br><br>\"" >> $FILENAME
echo " echo \"<a href=http://meinedyndns.no-ip.org>Zur Weboberflaeche...</a></body></html>\"" >> $FILENAME
echo " echo \"\"" >> $FILENAME
echo " " >> $FILENAME
echo " exit" >> $FILENAME
echo "fi " >> $FILENAME
echo "" >> $FILENAME
echo "\$orgcmd" >> $FILENAME
chmod +x $FILENAME
cp /usr/bin/system_status $CUSTOM_COMMANDS_PATH
mount -o bind $CUSTOM_COMMANDS_PATH/custom_commands_cgi /usr/www/avm/cgi-bin/system_status