#!/bin/sh
export DAEMON="dtmfbox"
export WEBSERVER_PORT="6767"
export DTMFBOX_WWW="http://fritz.v3v.de/dtmfbox/dtmfbox-dl/0.5.0"
export DTMFBOX_WWW_DL_FILE="dtmfbox-0.5.0-dl.tar.gz"
# dtmfbox already installed?
if [ -d /var/dtmfbox ]; then
DTMFBOX_INSTALLED="1"
if [ -z "$DTMFBOX_PATH" ]; then
# first, try to get real path with realpath command
export DTMFBOX_PATH="`realpath /var/dtmfbox 2>/dev/null`"
# when this does not work, try to extract realpath with 'ls' command and sed ...
if [ -z "$DTMFBOX_PATH" ] || [ ! -d "$DTMFBOX_PATH" ]; then
export DTMFBOX_PATH="`ls -l /var/dtmfbox 2>/dev/null | sed 's/^.*->.\(.*\)$/\1/g' 2>/dev/null`"
if [ -z "$DTMFBOX_PATH" ] || [ ! -d "$DTMFBOX_PATH" ]; then DTMFBOX_INSTALLED="0"; fi
fi
fi
else
[ -z "$DTMFBOX_PATH" ] && DTMFBOX_INSTALLED="0" || DTMFBOX_INSTALLED="1"
fi
if [ "$DTMFBOX_INSTALLED" = "1" ]; then export PATH=$PATH:/var/dtmfbox; export LD_LIBRARY_PATH=/var/dtmfbox; fi
# Using Freez-Mod?
if [ -d "/etc/default.dtmfbox" ]; then FREETZ="1"; else FREETZ="0"; fi
# Check, if dtmfbox is already installed
#
check_installed() {
if [ "$DTMFBOX_INSTALLED" = "0" ]; then
echo "$DAEMON not installed!"
exit 1;
fi
}
# Check for required busybox commands and link them to /var/tmp/busybox-tools
#
check_busybox() {
if [ ! -s /var/tmp/busybox-tools ] || [ ! -s /var/tmp/sh ] || [ "$1" = "FORCE" ]; then
BB_CMDS="du ftpput gzip gunzip head httpd mkfifo nc tail tar uudecode uuencode"
BB_CMD_MISSING="0"
BB=`which busybox`
if [ ! -f "$BB" ]; then BB="`pwd`/busybox-tools"; fi
if [ ! -f "$BB" ]; then BB="/var/dtmfbox/busybox-tools"; fi
if [ ! -f "$BB" ]; then BB_CMD_MISSING="1"; fi
if [ "$BB_CMD_MISSING" = "0" ]; then
echo "Testing $BB..."
for cmd in $BB_CMDS;
do
echo -n "$cmd..."
if [ -z "`$BB --help | grep -e \"\b$cmd\"`" ]; then
echo "missing!"
BB_CMD_MISSING="1";
break;
else
echo "found!"
fi
done
fi
if [ "$BB_CMD_MISSING" = "0" ]; then
echo "Congratulations! Your busybox has all required commands!"
else
echo "Using shipped busybox(-tools) ...";
BB="/var/dtmfbox/busybox-tools"
if [ ! -f "$BB" ]; then BB="`pwd`/busybox-tools"; fi
if [ ! -f "$BB" ]; then echo "Error: $BB not found! "; exit 1; fi
fi
if [ -s /var/tmp/busybox-httpd ]; then rm -f /var/tmp/busybox-httpd; fi
if [ -s /var/tmp/busybox-tools ]; then rm -f /var/tmp/busybox-tools; fi
if [ -s /var/tmp/sh ]; then rm -f /var/tmp/sh; fi
ln -sf "$BB" /var/tmp/busybox-httpd
ln -sf "$BB" /var/tmp/busybox-tools
ln -sf "$BB" /var/tmp/sh
fi
}
# Restore defaults
#
restore_defaults() {
FORCE="$1"
# restore from default, when some directories/files are missing
REQ_DIRS="tmp play record script"
REQ_FILES="dtmfbox.cfg menu.cfg script.cfg"
for REQ_DIR in $REQ_DIRS; do
if [ ! -d "$DTMFBOX_PATH/$REQ_DIR" ] || [ "$FORCE" = "FORCE" ]; then
mkdir -p "/var/dtmfbox/$REQ_DIR"
# restore default scripts
if [ "$REQ_DIR" = "script" ]; then
# (compressed)
if [ -f /var/dtmfbox/default/dtmfbox_script.tar.gz ]; then
cat /var/dtmfbox/default/dtmfbox_script.tar.gz | /var/tmp/busybox-tools gunzip -c - | tar xvf - -C /var/dtmfbox
fi
# (uncompressed)
if [ -d /var/dtmfbox/default/script ]; then
cp -Rf /var/dtmfbox/default/script/* /var/dtmfbox/script
fi
fi
fi
done
for REQ_FILE in $REQ_FILES; do
if [ ! -f "$DTMFBOX_PATH/$REQ_FILE" ] || [ "$FORCE" = "FORCE" ]; then
# (compressed)
if [ -f /var/dtmfbox/default/dtmfbox_cfg.tar.gz ]; then
cat /var/dtmfbox/default/dtmfbox_cfg.tar.gz | /var/tmp/busybox-tools gunzip -c - | tar xvf - -C /var/dtmfbox $REQ_FILE
fi
# (uncompressed)
if [ -d /var/dtmfbox/default/cfg ]; then
cp -Rf /var/dtmfbox/default/cfg/* /var/dtmfbox
fi
fi
done
}
# Install dtmfbox
#
install() {
INSTALL_MODE="$1"
INSTALL_PATH="$2"
if [ "$INSTALL_MODE" = "bypath" ]; then
if [ "$DTMFBOX_PATH" = "" ]; then
echo "Error: no DTMFBOX_PATH specified. export \$DTMFBOX_PATH=/path/to/dtmfbox"
exit 1;
fi
if [ "$DTMFBOX_PATH" = "/var/dtmfbox-bin" ]; then
INSTALL_MODE="ram"
else
if [ -f "$DTMFBOX_PATH/dtmfbox" ]; then
INSTALL_MODE="usb"
else
if [ "$FREETZ" = "0" ]; then
INSTALL_MODE="ram"
DTMFBOX_PATH=/var/dtmfbox-bin
else
INSTALL_MODE="usb"
fi
fi
fi
INSTALL_PATH="$DTMFBOX_PATH"
fi
# Check path (usb/apache)
if [ "$INSTALL_MODE" = "usb" ] || [ "$INSTALL_MODE" = "apache" ]; then
if [ "$FREETZ" = "0" ]; then
if [ "$INSTALL_PATH" = "" ]; then echo "Error: No path specified!"; exit 1; fi
if [ "$INSTALL_PATH" = "/var/dtmfbox" ] || [ "$INSTALL_PATH" = "/var/dtmfbox-bin" ] || [ "$INSTALL_PATH" = "/var/tmp" ] || [ "$INSTALL_PATH" = "/usr/bin/dtmfbox-apache" ]; then echo "Error: Path $INSTALL_PATH is not allowed!"; exit 1; fi
if [ ! -d "$INSTALL_PATH" ] && [ "$INSTALL_MODE" = "usb" ]; then echo "Error: $INSTALL_PATH does not exist!"; exit 1; fi
if [ ! -f "$INSTALL_PATH/dtmfbox" ] && [ "$INSTALL_MODE" = "usb" ]; then echo "Error: $INSTALL_PATH/dtmfbox does not exist!"; exit 1; fi
export DTMFBOX_PATH=$INSTALL_PATH;
fi
fi
# Apache (i386)
if [ "$INSTALL_MODE" = "apache" ] && [ "$FREETZ" = "0" ]; then
if [ `id -g` != "0" ]; then echo "Error: You must be root to run this script!"; exit 1; fi
if [ -f "./dtmfbox" ] && [ "./libmenu.plugin.so" ]; then
echo -n "Install dtmfbox binary and menu plugin now [Y/N]? "
read copy_to_bin
if [ "$copy_to_bin" = "y" ] || [ "$copy_to_bin" = "Y" ]; then
echo "Install /bin/dtmfbox"
cp ./dtmfbox /bin
echo "Install /lib/libmenu.plugin.so"
cp ./libmenu.plugin.so /lib
else
echo "Aborting!"
exit 1
fi
fi
LDTMFBOX=`which dtmfbox`
if [ ! -d "`pwd`/default" ]; then echo "Error: Unable to find `pwd`/default"; exit 1; fi
if [ ! -d "`pwd`/httpd" ]; then echo "Error: Unable to find `pwd`/httpd"; exit 1; fi
if [ ! -f $LDTMFBOX ] || [ -z "$LDTMFBOX" ]; then echo "Error: Unable to find dtmfbox binary!"; exit 1; fi
echo ""
echo "Installing $DAEMON for apache to $DTMFBOX_PATH ... ";
#echo "Creating user 'dtmfbox' ... "
#useradd -G audio,www-data dtmfbox
echo "Creating /usr/bin/dtmfbox-apache ..."
mkdir -p /usr/bin/dtmfbox-apache
mkdir -p /usr/bin/dtmfbox-apache/extras
cp -R `pwd`/httpd /usr/bin/dtmfbox-apache
cp -R `pwd`/default /usr/bin/dtmfbox-apache
cp `pwd`/rc.dtmfbox /usr/bin/dtmfbox-apache
ln -sf $LDTMFBOX /usr/bin/dtmfbox-apache/dtmfbox
ln -sf /usr/bin/dtmfbox-apache /var/dtmfbox
ln -sf /usr/bin/dtmfbox-apache/httpd $DTMFBOX_PATH
echo "Link extra applications ..."
LMADPLAY=`type madplay 2>/dev/null`; LMADPLAY="${LMADPLAY##* }"
LESPEAK=`type espeak 2>/dev/null`; LESPEAK="${LESPEAK##* }"
LCHECKMAILD=`type checkmaild 2>/dev/null`; LCHECKMAILD="${LCHECKMAILD##* }"
ln -sf $LMADPLAY /usr/bin/dtmfbox-apache/extras/madplay
ln -sf $LESPEAK /usr/bin/dtmfbox-apache/extras/speak
ln -sf $LCHECKMAILD /usr/bin/dtmfbox-apache/extras/checkmaild
echo "Creating wrapper scripts ..."
echo "#!/bin/sh" > /var/dtmfbox/busybox-tools
echo "\$@" >> /var/dtmfbox/busybox-tools
chmod +x /var/dtmfbox/busybox-tools
# create urldecode.pl (used as busybox-httpd)
echo "#!/usr/bin/perl" > /var/dtmfbox/urldecode.pl
echo "\$str = \$ARGV[2];" >> /var/dtmfbox/urldecode.pl
echo "\$str =~ s/\+/ /g;" >> /var/dtmfbox/urldecode.pl
echo "\$str =~ s/\%([a-fA-F0-9]{2})/chr(hex \$1)/eg;" >> /var/dtmfbox/urldecode.pl
echo "print \"\$str\";" >> /var/dtmfbox/urldecode.pl
chmod +x /var/dtmfbox/urldecode.pl
# create /var/dtmfbox/dtmfbox.apache.conf
echo "" > /var/dtmfbox/dtmfbox.apache.conf
echo " Order deny,allow" >> /var/dtmfbox/dtmfbox.apache.conf
echo " AllowOverride None" >> /var/dtmfbox/dtmfbox.apache.conf
echo " Options +ExecCGI" >> /var/dtmfbox/dtmfbox.apache.conf
echo " Deny from all" >> /var/dtmfbox/dtmfbox.apache.conf
echo " Allow from 192.168" >> /var/dtmfbox/dtmfbox.apache.conf
echo " Allow from 127" >> /var/dtmfbox/dtmfbox.apache.conf
echo " AddHandler cgi-script .cgi .sh" >> /var/dtmfbox/dtmfbox.apache.conf
echo "" >> /var/dtmfbox/dtmfbox.apache.conf
echo "" >> /var/dtmfbox/dtmfbox.apache.conf
echo "" >> /var/dtmfbox/dtmfbox.apache.conf
echo " AllowOverride None" >> /var/dtmfbox/dtmfbox.apache.conf
echo " Options +ExecCGI" >> /var/dtmfbox/dtmfbox.apache.conf
echo " Order deny,allow" >> /var/dtmfbox/dtmfbox.apache.conf
echo " Deny from all" >> /var/dtmfbox/dtmfbox.apache.conf
echo " Allow from 192.168" >> /var/dtmfbox/dtmfbox.apache.conf
echo " Allow from 127" >> /var/dtmfbox/dtmfbox.apache.conf
echo " AddHandler cgi-script .cgi .sh" >> /var/dtmfbox/dtmfbox.apache.conf
echo "" >> /var/dtmfbox/dtmfbox.apache.conf
fi
# USB
if [ "$INSTALL_MODE" = "usb" ] && [ "$FREETZ" = "0" ]; then
echo "Installing $DAEMON (USB -> $DTMFBOX_PATH) ..."
check_busybox
mkdir -p "$DTMFBOX_PATH"
fi
# RAM
if [ "$INSTALL_MODE" = "ram" ] && [ "$FREETZ" = "0" ]; then
echo "Installing $DAEMON (RAM -> /var/dtmfbox-bin) ...";
export DTMFBOX_PATH=/var/dtmfbox-bin
if [ ! -f "./busybox-tools" ]; then
wget $DTMFBOX_WWW/busybox-tools;
chmod +x busybox-tools
fi
check_busybox
mkdir -p "$DTMFBOX_PATH"
/var/tmp/busybox-tools mkfifo /var/tmp/dtmfbox-dl.tar
wget $DTMFBOX_WWW/$DTMFBOX_WWW_DL_FILE -O - > /var/tmp/dtmfbox-dl.tar &
cat /var/tmp/dtmfbox-dl.tar | /var/tmp/busybox-tools gunzip -f -c - | /var/tmp/busybox-tools tar xv -f - -C $DTMFBOX_PATH
rm /var/tmp/dtmfbox-dl.tar
fi
# FREETZ (RAM/USB)
if [ "$FREETZ" = "1" ]; then
check_busybox
if [ "$INSTALL_MODE" = "usb" ]; then
echo "Installing $DAEMON using Freetz (USB -> $INSTALL_PATH) ...";
else
echo "Installing $DAEMON using Freetz (RAM -> /var/dtmfbox-bin) ...";
export DTMFBOX_PATH=/var/dtmfbox-bin
fi
# link/copy required binaries
mkdir -p "$INSTALL_PATH"
ln -sf "$INSTALL_PATH" /var/dtmfbox
ln -sf "/usr/sbin/dtmfbox" "$INSTALL_PATH/dtmfbox" 2>/dev/null
ln -sf "/usr/lib/libmenu.plugin.so" "$INSTALL_PATH/libmenu.plugin.so" 2>/dev/null
ln -sf "/etc/init.d/rc.dtmfbox" "$INSTALL_PATH/rc.dtmfbox" 2>/dev/null
ln -sf "/etc/default.dtmfbox" "$INSTALL_PATH/default" 2>/dev/null
if [ ! -f "$INSTALL_PATH/dtmfbox" ]; then cp -f "/usr/sbin/dtmfbox" "$INSTALL_PATH"; fi
if [ ! -f "$INSTALL_PATH/libmenu.plugin.so" ]; then cp -f "/usr/lib/libmenu.plugin.so" "$INSTALL_PATH"; fi
if [ ! -f "$INSTALL_PATH/rc.dtmfbox" ]; then cp -f "/etc/init.d/rc.dtmfbox" "$INSTALL_PATH"; fi
if [ ! -d "$INSTALL_PATH/default" ]; then cp -Rf "/etc/default.dtmfbox" "$INSTALL_PATH/default"; fi
fi
echo "Restoring defaults ..."
restore_defaults
if [ "$INSTALL_MODE" = "apache" ]; then
echo "Set permissions ..."
chown www-data -R /var/dtmfbox
chown www-data -R /var/dtmfbox/*
chmod 777 /var/dtmfbox/tmp
echo "Finished!\n\nAttention:\nInclude /var/dtmfbox/dtmfbox.apache.conf in your configuration\nand restart apache after that!\n"
fi
}
start_httpd() {
if [ "$FREETZ" = "1" ]; then return 1; fi
if [ -d /usr/bin/dtmfbox-apache ]; then
/etc/init.d/apache2 start
else
echo -n "Starting Webinterface on port $WEBSERVER_PORT ... "
(
cd /var/dtmfbox
/var/tmp/busybox-httpd httpd -p $WEBSERVER_PORT -r "admin" -h /var/dtmfbox/httpd
)>/dev/null
if [ ! -z "$(pidof "busybox-httpd")" ]; then
echo 'done!'
else
echo 'failed!'
fi
fi
}
stop_httpd() {
if [ "$FREETZ" = "1" ]; then return 1; fi
if [ -d /usr/bin/dtmfbox-apache ]; then
/etc/init.d/apache2 stop
else
echo -n "Stopping Webinterface ... "
if [ ! -z "$(pidof "busybox-httpd")" ]; then
killall -15 busybox-httpd
sleep 1
fi
if [ ! -z "$(pidof "busybox-httpd")" ]; then
killall -9 busybox-httpd
fi
if [ -z "$(pidof "busybox-httpd")" ]; then
echo 'done!'
else
echo 'failed!'
fi
fi
}
start() {
START_MODE="$1"
echo -n "Start $DAEMON ... "
cd /var/dtmfbox
rm -f /var/dtmfbox/tmp/* 2>/dev/null
# Refresh voipd register (if started)
if [ ! -z "$(pidof "voipd")" ]; then
voipd -R 2>/dev/null
fi
if [ "$START_MODE" = "foreground" ];
then
# Run foreground
echo ""
$DAEMON -cfg /var/dtmfbox/dtmfbox.cfg
fi
if [ "$START_MODE" = "log" ];
then
# Run daemon (logging) ...
$DAEMON -daemon -cfg /var/dtmfbox/dtmfbox.cfg -log /var/dtmfbox/tmp/dtmfbox.log
fi
if [ "$START_MODE" = "" ];
then
# Run daemon ...
$DAEMON -daemon -cfg /var/dtmfbox/dtmfbox.cfg
fi
if [ ! -z "$(pidof "$DAEMON")" ];
then
echo 'done!'
else
echo 'failed!'
fi
}
stop() {
echo -n "Stop $DAEMON ... "
# Stop daemon
if [ ! -z "$(pidof "$DAEMON")" ]; then
cd /var/dtmfbox
$DAEMON -stop daemon >/dev/null &
# max. wait 10 sec until stop
(
let wait=9;
while [ ! -z "$(pidof "$DAEMON")" ] && [ $wait -ge 1 ];
do
echo -n "$wait,"
let wait=wait-1
sleep 1
done
) 2>/dev/null
# kill -15 dtmfbox
if [ ! -z "$(pidof "$DAEMON")" ];
then
killall -15 $DAEMON > /dev/null 2>&1
sleep 2
fi
# kill -9 dtmfbox
if [ ! -z "$(pidof "$DAEMON")" ];
then
killall -9 $DAEMON > /dev/null 2>&1
fi
fi
if [ -z "$(pidof "$DAEMON")" ];
then
echo 'done!'
else
echo 'failed!'
exit 0;
fi
# Refresh voipd register (if started)
if [ ! -z "$(pidof "voipd")" ]; then
voipd -R 2>/dev/null
fi
}
if [ "$FREETZ" = "1" ];
then
case "$1" in
""|load)
ln -sf /usr/lib/cgi-bin/dtmfbox.cgi /mod/usr/lib/dtmfbox.cgi
touch /mod/etc/conf/dtmfbox.cfg
modreg daemon dtmfbox
if [ -z "$DTMFBOX_PATH" ]; then DTMFBOX_PATH="/var/dtmfbox-bin"; fi
check_busybox
;;
unload)
modunreg daemon dtmfbox
stop
;;
esac
fi
case "$1" in
check_busybox)
check_busybox "FORCE"
;;
defaults)
check_installed
check_busybox
restore_defaults "FORCE"
;;
install)
if [ "$2" != "usb" ] && [ "$2" != "ram" ] && [ "$2" != "bypath" ] && [ "$2" != "apache" ];
then
echo "Please specify if you want to install on USB, into RAM (mipsel) or for Apache (i386):"
echo "./rc.dtmfbox install ram"
echo "./rc.dtmfbox install usb /var/media/path/to/dtmfbox"
echo "./rc.dtmfbox install apache /var/www/dtmfbox"
exit 1;
fi
install "$2" "$3"
;;
uninstall)
check_installed
check_busybox
echo "Uninstall dtmfbox ..."
echo "" |
(
cd /var/dtmfbox/httpd/cgi-bin/
. ./dtmfbox_cfg.cgi
show_page "dtmfbox_scriptedit.cgi" "UNINSTALL"
) > /dev/null 2>/dev/null
if [ -d /usr/bin/dtmfbox-apache ]; then rm -Rf /usr/bin/dtmfbox-apache; fi
# stop webserver
if [ "$FREETZ" = "0" ] && [ -f /var/dtmfbox/rc.dtmfbox ]; then
/var/dtmfbox/rc.dtmfbox stop_httpd >/dev/null
fi
rm /var/dtmfbox
echo "Finished! You can reboot now!"
;;
start_httpd)
check_installed
check_busybox
start_httpd
;;
stop_httpd)
check_installed
stop_httpd
;;
restart_httpd)
check_installed
stop_httpd
check_busybox
start_httpd
;;
start)
check_installed
check_busybox
start
;;
stop)
check_installed
stop
;;
log)
check_installed
check_busybox
start "log"
;;
foreground)
check_installed
check_busybox
start "foreground"
;;
restart)
check_installed
check_busybox
stop
start
;;
status)
if [ -z "$(pidof "$DAEMON")" ]; then
echo "stopped"
else
echo "running"
fi
;;
"")
echo "usage: "
if [ "$FREETZ" = "0" ];
then
echo "./rc.dtmfbox install apache /path/to/www - Install for Apache (bash/i386)"
echo "./rc.dtmfbox install usb /path/to/dtmfbox - Install on USB (busybox/mipsel)"
echo "./rc.dtmfbox install ram - Install to RAM (busybox/mipsel)"
echo ""
fi
echo "./rc.dtmfbox defaults - Restore default cfg/scripts"
echo "./rc.dtmfbox uninstall - Uninstall dtmfbox"
echo ""
echo "./rc.dtmfbox start - Start dtmfbox as daemon"
echo "./rc.dtmfbox foreground - Start dtmfbox in foreground"
echo "./rc.dtmfbox log - Start dtmfbox logged"
echo "./rc.dtmfbox stop - Stop dtmfbox"
echo "./rc.dtmfbox restart - Restart dtmfbox"
echo "./rc.dtmfbox status - Daemon status"
echo ""
if [ "$FREETZ" = "0" ];
then
echo "./rc.dtmfbox start_httpd - Start Webinterface"
echo "./rc.dtmfbox stop_httpd - Stop Webinterface"
echo "./rc.dtmfbox restart_httpd - Restart Webinterface"
echo ""
fi
exit 1
;;
esac
exit 0