Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
- SCRIPT_PATH="${0}"
- #==================================================
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 3 of the License.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU Library General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
- #
- # http://www.gnu.org/licenses/gpl.txt
- #
- #==================================================
- URL="http://${HTTP_HOST}/cgi-bin/player"
- MYSQL='mysql --defaults-file=my.cnf tools -Be'
- echo "Content-type: text/html"
- # exit function
- function die()
- {
- echo -e "<div style=\"padding:4px; border:1px solid red; color:red;\">Error in ${SCRIPT_PATH}:\n${1}</div>"
- exit 1
- }
- # not there yet function
- function fNotYet()
- {
- NODE="${1}"
- COMM="${2}"
- echo -e "<div style=\"padding:4px; border:1px solid orange; color:orange;\">Warning: Command \"${COMM}\" is not yet implemented in ${DOCUMENT_URI}\n</div>"
- return 0
- }
- # check for.......
- function fCheck()
- {
- # checken binaries
- [ -x /usr/bin/mysql ] || die "This script depends on mysql-common!"
- # check op .my.cnf
- [ -f "./my.cnf" ] || die "my.cnf Not found!"
- # checken op connectiviteit
- ( mysql --defaults-file="./my.cnf" --connect-timeout=2 -Be "SHOW DATABASES" |grep -Eq '(information_schema|mysql)' ) || die "Failed to connect to database!"
- # checken op staat
- [ "$( ${MYSQL} "DESC nodes" | wc -l )" -eq 9 ] || die "Database state unknown!"
- return 0
- }
- # Create command
- function fStart()
- {
- # array met input args
- local ARG=( $( echo "${QUERY_STRING}" | sed -e 's/&/\ /g' ) )
- # set node var
- NODE="$( echo "${ARG[0]}" | sed -e 's/node=//g' )"
- # set comm var
- COMM="$( echo "${ARG[1]}" | sed -e 's/comm=//g' )"
- # output if
- if [ ! -z "${NODE}" ] && [ ! -z "${COMM}" ] ; then
- # case off commands
- case "${COMM}"
- in
- start)
- LINE1="$( fNotYet "${NODE}" "${COMM}" )"
- LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
- return 0
- ;;
- stop)
- LINE1="$( fNotYet "${NODE}" "${COMM}" )"
- LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
- return 0
- ;;
- next)
- LINE1="$( fNotYet "${NODE}" "${COMM}" )"
- LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
- return 0
- ;;
- prev)
- LINE1="$( fNotYet "${NODE}" "${COMM}" )"
- LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
- return 0
- ;;
- slower)
- LINE1="$( fNotYet "${NODE}" "${COMM}" )"
- LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
- return 0
- ;;
- faster)
- LINE1="$( fNotYet "${NODE}" "${COMM}" )"
- LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
- return 0
- ;;
- blackout)
- LINE1="$( fNotYet "${NODE}" "${COMM}" )"
- LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
- return 0
- ;;
- effect)
- LINE1="$( fNotYet "${NODE}" "${COMM}" )"
- LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
- return 0
- ;;
- connect)
- LINE1="$( fNotYet "${NODE}" "${COMM}" )"
- LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
- return 0
- ;;
- reboot)
- LINE1="$( fNotYet "${NODE}" "${COMM}" )"
- LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
- return 0
- ;;
- admin)
- LINE1="$( fNotYet "${NODE}" "${COMM}" )"
- LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
- return 0
- ;;
- *)
- LINE1="$( echo -e "<div style=\"padding:4px; border:1px solid red; color:red;\">An error appeared! Command \"${COMM}\" was not send to node: ${NODE}\n</div>" )"
- return 1
- ;;
- esac
- else
- LINE1="$( echo -e "<div style=\"padding:4px; border:1px solid red; color:red;\">An error appeared! Command \"${COMM}\" was not send to node: ${NODE}\n</div>" )"
- return 1
- fi
- return 0
- }
- # function fGetStatus ( to do, now just for demo )
- function fGetStatus()
- {
- NODE="${1}"
- STATE="$( ${MYSQL} "SELECT state FROM nodes WHERE host = '${NODE}'" | sed 1d )"
- if [ "${STATE}" = online ]; then
- echo -e "<td bgcolor=#00FF00><center><b>${NODE}</b><br><br>Node is Online</center></td>"
- elif [ "${STATE}" = offline ]; then
- echo -e "<td bgcolor=#FF0000><center><b>${NODE}</b><br><br>Node is Offline</center></td>"
- else
- echo -e "<td bgcolor=\"orange\"><center><b>${NODE}</b><br><br>Node is Unknown</center></td>"
- fi
- return 0
- }
- # function fGetPreview ( to do, now just for demo )
- function fGetPreview()
- {
- echo -e "<td bgcolor=#333><font color=\"#FFF\"/><center>Preview<br><br><b>Not available<b></center></td>"
- return 0
- }
- # create index
- function fIndex()
- {
- echo -e "
- <HTML>
- <HEAD>
- <TITLE>CGI Script Controller Matrix</TITLE>
- </HEAD>
- <BODY>
- <body bgcolor=\"#000000\"
- <table border=\"5\" color=\"black\" width=\"100%\" height=\"100%\" cellpadding=\"10\" cellspacing=\"10\">
- </table>
- <table border=\"5\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
- <table border=\"2\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
- <tr>
- <td colspan=\"1\" bgcolor=\"#000\">
- <font color=\"#CFCFCF\"/>
- <center><b>CGI Panel</b><br>
- Scripting Based Matrix Controller<br>
- For interactive video installations<br><center>
- </td>
- <td colspan=\"8\" bgcolor=\"#000\">
- <font color=\"#CFCFCF\"/>
- <center><h1>CGI Script Controller Panel</h1></center>
- <center><i>Control Raspberry Pi nodes by pressing the corresponding button!</i></center>
- </td>
- </tr>"
- }
- # create status window
- function fStatusWindow()
- {
- # check for query string
- if [ "X${QUERY_STRING}" != "X" ]; then
- fCheck
- fStart
- echo -e "
- <table border=\"5\" color=\"black\" width=\"100%\" height=\"100%\" cellpadding=\"10\" cellspacing=\"10\">
- </table>
- <table border=\"5\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
- <table border=\"2\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
- <tr>
- <td colspan=\"1\" bgcolor=\"#000\">
- <font color=\"#CFCFCF\"/>
- ${LINE1}
- ${LINE2}
- </td>
- </tr>
- <table>"
- else
- echo -e "
- <table border=\"5\" color=\"black\" width=\"100%\" height=\"100%\" cellpadding=\"10\" cellspacing=\"10\">
- </table>
- <table border=\"5\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
- <table border=\"2\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
- <tr>
- <td colspan=\"1\" bgcolor=\"#000\">
- <font color=\"#CFCFCF\"/>
- ${LINE1}
- ${LINE2}
- </td>
- </tr>
- <table>"
- fi
- return 0
- }
- # create buttons
- function fCreateButtons()
- {
- for NODE in $( ${MYSQL} "SELECT host FROM nodes" | sed 1d )
- do
- STATUS="$( fGetStatus "${NODE}" )"
- PREVIEW="$( fGetPreview "${NODE}" )"
- echo -e "
- <table border=\"2\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
- <td bgcolor=#555 colspan=\"2\"><h3><font color=\"#FFF\">
- ${PREVIEW}
- ${STATUS}
- </td>
- <td bgcolor=#777>
- <font color=\"#FFF\"/>
- <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=start\" >
- <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\"start playlist\"></form>
- <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=stop\" >
- <br>
- <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\"stop playlist\"></form>
- </td>
- <td bgcolor=#777>
- <font color=\"#FFF\"/>
- <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=next\">
- <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\">>\"></form>
- <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=prev\">
- <br>
- <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\"<<\"></form>
- </td>
- <td bgcolor=#777>
- <font color=\"#FFF\"/>
- <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=slower\">
- <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\"Play slower\"></form>
- <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=faster\">
- <br>
- <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\"Play faster\"></form>
- </td>
- <td bgcolor=#777>
- <font color=\"#FFF\"/>
- <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=blackout\">
- <center><input style=\"height: 12px; width: 80px\" type=\"submit\" value=\"Blackout\"></form>
- <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=effect\">
- <center><input style=\"height: 12px; width: 80px\" type=\"submit\" value=\"Effect\"></form>
- <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=connect\">
- <center><input style=\"height: 12px; width: 80px\" type=\"submit\" value=\"Connect\"></form>
- <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=reboot\">
- <center><input style=\"height: 12px; width: 80px\" type=\"submit\" value=\"Reboot\"></form>
- </td>
- <td bgcolor=#777>
- <font color=\"#000\"/>
- <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=admin\">
- <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\"Admin\"></form><br>
- </td>
- </table>"
- done
- return 0
- }
- # More layout and EOF
- function fEnd()
- {
- echo -e "
- <table border=\"2\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
- <tr>
- <td colspan=\"1\">
- <font color=\"#FFF\"/>
- <b>By Flip Hess</b><br>
- <br>
- Augustus 2012<br>
- CGI Bash Script Controller Panel Version 1.1<br>
- <br>
- Made for controlling video and multimedia, but adjustable to any need.<br>
- <br>
- </td>
- <td colspan=\"1\">
- <font color=\"#FFF\"/>
- <i>This program is free software</i><br>
- <i>You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation</i><br>
- <br>
- <i>This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of</i><br>
- <i>MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.</i><br>
- </td>
- </tr>
- </table>
- </table>
- </BODY>
- </HTML>"
- return 0
- }
- # Do tha magic
- fIndex
- fStatusWindow
- fCreateButtons
- fEnd
Advertisement
Add Comment
Please, Sign In to add comment