openmedialibrary/ctl

193 lines
4.5 KiB
Bash
Executable file

#!/usr/bin/env bash
NAME="openmedialibrary"
PID="/tmp/$NAME.$USER.pid"
cd "`dirname "$0"`"
if [ -e oml ]; then
cd ..
fi
BASE=`pwd`
SYSTEM=`uname -s`
PLATFORM=`uname -m`
DATA="$BASE/data"
if [ ! -e "$DATA" ] && [ -e "$BASE/config" ]; then
if [ "$1" == "stop" ] || [ "$1" == "restart" ]; then
DATA="$BASE/config"
else
mv "$BASE/config" "$DATA"
fi
else
if [ ! -e "$BASE/data/release.json" ] && [ -e "$BASE/config/release.json" ]; then
mv "$BASE/config/release.json" "$BASE/data/release.json"
fi
fi
if [ ! -e "$PID" ]; then
if [ -e "$DATA/tor/hostname" ]; then
onion=$(cat "$DATA/tor/hostname")
id=${onion/.onion/}
PID="/tmp/$NAME.$USER.$id.pid"
fi
fi
if [ ! -e "$PID" ]; then
PID="$DATA/$NAME.pid"
fi
if [ -e "$BASE/local_platform" ]; then
export PLATFORM_ENV="$BASE/local_platform"
else
if [ $SYSTEM == "Linux" ]; then
export PLATFORM_ENV="$BASE/platform/${SYSTEM}_${PLATFORM}"
else
export PLATFORM_ENV="$BASE/platform/$SYSTEM"
fi
fi
PATH="$PLATFORM_ENV/bin:$PATH"
SHARED_ENV="$BASE/platform/Shared"
export SHARED_ENV
PATH="$SHARED_ENV/bin:$PATH"
export PATH
if [ $SYSTEM == "Darwin" ]; then
export DYLD_FALLBACK_LIBRARY_PATH="$PLATFORM_ENV/lib"
export SSL_CERT_FILE="$SHARED_ENV/etc/openssl/cert.pem"
export SSL_CERT_DIR="$SHARED_ENV/etc/openssl/certs"
fi
PYTHONPATH="$PLATFORM_ENV/lib/python3.4/site-packages:$SHARED_ENV/lib/python3.4/site-packages:$BASE/$NAME"
export PYTHONPATH
oxCACHE="$DATA/ox"
export oxCACHE
#must be called to update commands in $PATH
hash -r 2>/dev/null
# allow more open files
ulimit -S -n 2048
function remove_autostart {
if [ $SYSTEM == "Darwin" ]; then
launchd_name="com.openmedialibrary.loginscript"
launchd_plist="$HOME/Library/LaunchAgents/${launchd_name}.plist"
if [ -e "$launchd_plist" ]; then
launchctl stop "$launchd_name"
launchctl unload "$launchd_plist"
rm "$launchd_plist"
fi
fi
if [ $SYSTEM == "Linux" ]; then
if [ -e "$HOME/.config/autostart/openmedialibrary.desktop" ]; then
rm "$HOME/.config/autostart/openmedialibrary.desktop"
fi
fi
}
if [ "$1" == "start" ]; then
remove_autostart
cd "$BASE/$NAME"
if [ -e "$PID" ]; then
if ps -p `cat "$PID"` > /dev/null; then
echo openmedialibrary already running
exit 1
fi
fi
python3 oml server "$PID"
exit $?
fi
if [ "$1" == "debug" ]; then
cd "$BASE/$NAME"
if [ -e "$PID" ]; then
if ps -p `cat "$PID"` > /dev/null; then
echo openmedialibrary already running
exit 1
fi
fi
shift
python3 oml server debug "$PID"
exit $?
fi
if [ "$1" == "stop" ]; then
remove_autostart
if [ -e "$PID" ]; then
_PID=`cat "$PID"`
kill $_PID
waited=0
while ps -p $_PID > /dev/null
do
sleep 1
waited=$(($waited+1))
if [ $waited -gt 11 ]; then
kill -9 $_PID
sleep 1
fi
done
test -e "$PID" && rm "$PID"
fi
exit $?
fi
if [ "$1" == "restart" ]; then
if [ -e "$PID" ]; then
"$0" stop
"$0" start
exit $?
else
"$0" start
exit $?
fi
fi
if [ "$1" == "open" ]; then
if [ $SYSTEM == "Darwin" ]; then
open "/Applications/Open Media Library.app"
fi
if [ $SYSTEM == "Linux" ]; then
if [ -e "$PID" ]; then
if ps -p `cat "$PID"` > /dev/null; then
xdg-open "file://${BASE}/openmedialibrary/static/html/load.html"
else
python3 "$NAME/oml/gtkstatus.py" $@
exit $?
fi
else
python3 "$NAME/oml/gtkstatus.py" $@
exit $?
fi
fi
exit 0
fi
if [ "$1" == "ui" ]; then
shift
python3 "$NAME/oml/ui.py" $@
exit $?
fi
if [ "$1" == "update" ]; then
cd "$BASE/$NAME"
if [ -d "$BASE/$NAME/.git" ]; then
OLD=`"$0" version`
cd "$BASE/platform"
echo Update platform...
git pull
echo Update $NAME...
cd "$BASE/$NAME"
find . -name "*.pyc" -exec rm "{}" \;
git pull
"$0" update_static > /dev/null
NEW=`"$0" version`
"$0" postupdate -o $OLD -n $NEW
else
python3 oml update
fi
exit $?
fi
if [ "$1" == "python" ]; then
cd "$BASE/$NAME"
shift
python3 $@
exit $?
fi
cd "$BASE/$NAME"
python3 oml $@
exit $?