openmedialibrary/ctl

266 lines
6.5 KiB
Text
Raw Normal View History

2014-05-04 19:26:43 +02:00
#!/usr/bin/env bash
NAME="openmedialibrary"
2014-08-17 18:11:19 +02:00
PID="/tmp/$NAME.$USER.pid"
2014-05-04 19:26:43 +02:00
cd "`dirname "$0"`"
2014-05-04 19:26:43 +02:00
if [ -e oml ]; then
cd ..
fi
BASE=`pwd`
SYSTEM=`uname -s`
2014-05-17 20:13:58 +02:00
PLATFORM=`uname -m`
2019-01-18 16:18:41 +05:30
GIT_BASE=https://code.0x2620.org/0x2620
2014-05-04 19:26:43 +02:00
2017-01-06 19:40:33 +01:00
PYTHON=python3
2016-01-18 12:04:20 +05:30
DATA="$BASE/data"
if [ ! -e "$DATA" ] && [ -e "$BASE/config" ]; then
2016-01-20 11:11:50 +05:30
if [ "$1" == "stop" ] || [ "$1" == "restart" ]; then
DATA="$BASE/config"
else
mv "$BASE/config" "$DATA"
fi
2016-01-18 12:04:20 +05:30
else
if [ ! -e "$BASE/data/release.json" ] && [ -e "$BASE/config/release.json" ]; then
mv "$BASE/config/release.json" "$BASE/data/release.json"
fi
fi
2016-01-18 13:07:59 +05:30
if [ ! -e "$PID" ]; then
2016-01-18 12:04:20 +05:30
if [ -e "$DATA/tor/hostname" ]; then
onion=$(cat "$DATA/tor/hostname")
2016-01-05 21:25:19 +05:30
id=${onion/.onion/}
PID="/tmp/$NAME.$USER.$id.pid"
fi
2016-01-05 22:37:14 +05:30
fi
2016-01-18 13:07:59 +05:30
if [ ! -e "$PID" ]; then
2016-01-18 13:04:29 +05:30
PID="$DATA/$NAME.pid"
fi
2016-01-05 21:25:19 +05:30
2016-02-06 16:07:08 +05:30
PLATFORM_PYTHON=3.4
2019-01-29 16:11:35 +05:30
SHARED_PYTHON=3.7
if [ -e "$BASE/platform_local" ]; then
export PLATFORM_ENV="$BASE/platform_local"
2015-11-26 10:38:40 +01:00
else
2016-02-06 16:07:08 +05:30
if [ $SYSTEM == "Linux" ]; then
if [ $PLATFORM == "x86_64" ]; then
ARCH=64
2018-12-15 00:54:49 +01:00
PLATFORM_PYTHON=3.7
2016-02-06 16:07:08 +05:30
else
ARCH=32
fi
if [ $PLATFORM == "aarch64" ]; then
ARCH="_aarch64"
2016-06-24 12:55:10 +02:00
PLATFORM_PYTHON=3.5
fi
if [ $PLATFORM == "armv7l" ]; then
ARCH="_armv7l"
fi
2016-02-06 16:07:08 +05:30
PLATFORM="linux$ARCH"
2019-02-01 16:43:30 +05:30
if [ -e "$BASE/platform_${PLATFORM}/lib/libunrar.so" ]; then
2019-01-29 16:11:35 +05:30
export UNRAR_LIB_PATH="$BASE/platform_${PLATFORM}/lib/libunrar.so"
fi
2016-02-06 16:07:08 +05:30
fi
if [ $SYSTEM == "Darwin" ]; then
PLATFORM="darwin64"
2019-01-01 00:26:03 +01:00
PLATFORM_PYTHON=3.7
2019-02-01 16:05:15 +05:30
if [ -e "$BASE/platform_${PLATFORM}/lib/libunrar.dylib" ]; then
2019-01-29 16:11:35 +05:30
export UNRAR_LIB_PATH="$BASE/platform_${PLATFORM}/lib/libunrar.dylib"
fi
2016-02-06 16:07:08 +05:30
fi
export PLATFORM_ENV="$BASE/platform_${PLATFORM}"
2015-11-26 10:38:40 +01:00
fi
2014-05-04 19:26:43 +02:00
PATH="$PLATFORM_ENV/bin:$PATH"
SHARED_ENV="$BASE/platform/Shared"
export SHARED_ENV
PATH="$SHARED_ENV/bin:$PATH"
export PATH
2016-01-09 12:47:16 +05:30
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
2016-02-06 16:07:08 +05:30
PYTHONPATH="${PLATFORM_ENV}/lib/python${PLATFORM_PYTHON}/site-packages"
2019-01-20 16:46:46 +05:30
PYTHONPATH="${PYTHONPATH}:${SHARED_ENV}/lib/python${PLATFORM_PYTHON}/site-packages"
2016-02-06 16:07:08 +05:30
PYTHONPATH="${PYTHONPATH}:${SHARED_ENV}/lib/python${SHARED_PYTHON}/site-packages"
PYTHONPATH="${PYTHONPATH}:${BASE}/${NAME}"
2014-05-04 19:26:43 +02:00
export PYTHONPATH
2016-01-18 12:04:20 +05:30
oxCACHE="$DATA/ox"
2014-05-14 20:46:31 +02:00
export oxCACHE
2014-05-04 19:26:43 +02:00
#must be called to update commands in $PATH
hash -r 2>/dev/null
2014-05-27 20:10:30 +02:00
# allow more open files
ulimit -S -n 2048
2019-01-18 16:18:41 +05:30
function update_gitbase() {
2019-01-18 16:24:47 +05:30
if [ -e "${BASE}/openmedialibrary/.git/config" ]; then
2019-01-18 16:18:41 +05:30
GIT_BASE=`grep origin .git/config -A 1 | grep url | cut -f2 -d=`
fi
}
2016-01-12 20:30:51 +05:30
if [ "$1" == "start" ]; then
2014-05-20 12:30:53 +02:00
cd "$BASE/$NAME"
2016-01-18 13:07:59 +05:30
if [ -e "$PID" ]; then
2015-03-14 18:06:45 +05:30
if ps -p `cat "$PID"` > /dev/null; then
echo openmedialibrary already running
exit 1
fi
2014-05-04 19:26:43 +02:00
fi
2017-01-06 19:40:33 +01:00
$PYTHON oml server "$PID"
2016-01-13 11:34:56 +05:30
exit $?
2014-05-04 19:26:43 +02:00
fi
if [ "$1" == "debug" ]; then
2014-05-20 12:30:53 +02:00
cd "$BASE/$NAME"
2016-01-18 13:07:59 +05:30
if [ -e "$PID" ]; then
2016-01-13 11:39:41 +05:30
if ps -p `cat "$PID"` > /dev/null; then
echo openmedialibrary already running
exit 1
fi
2014-05-04 19:26:43 +02:00
fi
shift
2017-01-06 19:40:33 +01:00
$PYTHON oml server debug "$PID"
2016-01-13 11:34:56 +05:30
exit $?
2014-05-04 19:26:43 +02:00
fi
if [ "$1" == "stop" ]; then
2016-01-18 13:07:59 +05:30
if [ -e "$PID" ]; then
_PID=`cat "$PID"`
2015-11-18 01:27:53 +01:00
kill $_PID
waited=0
while ps -p $_PID > /dev/null
do
sleep 1
waited=$(($waited+1))
2016-02-13 16:34:48 +05:30
if [ $waited -gt 60 ]; then
2015-11-18 01:27:53 +01:00
kill -9 $_PID
sleep 1
fi
done
2016-01-18 13:07:59 +05:30
test -e "$PID" && rm "$PID"
2015-11-18 01:27:53 +01:00
fi
2014-05-04 19:26:43 +02:00
exit $?
fi
if [ "$1" == "restart" ]; then
2016-01-18 13:07:59 +05:30
if [ -e "$PID" ]; then
2014-05-20 12:30:53 +02:00
"$0" stop
"$0" start
2014-05-04 19:26:43 +02:00
exit $?
else
"$0" start
exit $?
2014-05-04 19:26:43 +02:00
fi
fi
2019-01-31 10:04:01 +05:30
function get_port() {
port=$(grep '"port"' "${BASE}/data/server.json" | cut -f2 -d: | cut -f1 -d, | xargs)
return $port
}
function open_linux() {
port=$(get_port)
if [ -e /usr/bin/gio ]; then
gio open "file://${BASE}/openmedialibrary/static/html/load.html#$port"
else
xdg-open "file://${BASE}/openmedialibrary/static/html/load.html#$port"
fi
}
2014-05-04 19:26:43 +02:00
if [ "$1" == "open" ]; then
if [ $SYSTEM == "Darwin" ]; then
open "/Applications/Open Media Library.app"
fi
2015-11-03 18:44:34 +01:00
if [ $SYSTEM == "Linux" ]; then
2016-01-18 13:07:59 +05:30
if [ -e "$PID" ]; then
if ps -p `cat "$PID"` > /dev/null; then
2019-01-31 10:04:01 +05:30
open_linux
else
#$PYTHON "${NAME}/oml/gtkstatus.py" $@
#exit $?
2019-01-31 10:04:01 +05:30
open_linux
"$0" start &
fi
else
#$PYTHON "$NAME/oml/gtkstatus.py" $@
#exit $?
2019-01-31 10:04:01 +05:30
open_linux
"$0" start &
fi
2014-05-04 19:26:43 +02:00
fi
exit 0
fi
2016-04-14 12:57:45 +02:00
if [ "$1" == "autostart" ]; then
if [ $SYSTEM == "Darwin" ]; then
open "/Applications/Open Media Library.app" --args --autostart
fi
if [ $SYSTEM == "Linux" ]; then
if [ ! -e "$PID" ]; then
2017-01-06 19:40:33 +01:00
$PYTHON "$NAME/oml/gtkstatus.py" --autostart
2016-04-14 12:57:45 +02:00
exit $?
fi
fi
exit 0
fi
2014-05-16 17:48:48 +02:00
if [ "$1" == "ui" ]; then
shift
2019-01-25 14:47:36 +05:30
$PYTHON "$NAME/oml/ui.py" "$@"
2016-01-13 11:34:56 +05:30
exit $?
2014-05-16 17:48:48 +02:00
fi
2016-02-06 16:07:08 +05:30
if [ "$1" == "init" ]; then
2019-01-18 16:18:41 +05:30
update_gitbase
2016-02-06 16:07:08 +05:30
if [ ! -e "${BASE}/platform" ]; then
2019-01-18 16:18:41 +05:30
git clone --depth 1 $GIT_BASE/openmedialibrary_platform.git "${BASE}/platform"
2016-02-06 16:07:08 +05:30
fi
if [ ! -e "${PLATFORM_ENV}" ]; then
2019-01-18 16:18:41 +05:30
git clone --depth 1 $GIT_BASE/openmedialibrary_platform_${PLATFORM}.git "${PLATFORM_ENV}"
2016-02-06 16:07:08 +05:30
fi
2016-03-25 16:59:56 +01:00
cd "${BASE}"
if [ ! -e "${BASE}/ctl" ]; then
ln -s openmedialibrary/ctl
fi
"${BASE}/ctl" update_static
2016-02-06 16:07:08 +05:30
exit
fi
2014-05-17 13:40:51 +02:00
if [ "$1" == "update" ]; then
2014-08-05 12:10:42 +02:00
cd "$BASE/$NAME"
if [ ! -e "${PLATFORM_ENV}" ]; then
2019-01-18 16:18:41 +05:30
update_gitbase
git clone --depth 1 $GIT_BASE/openmedialibrary_platform_${PLATFORM}.git "${PLATFORM_ENV}"
fi
2014-08-05 12:10:42 +02:00
if [ -d "$BASE/$NAME/.git" ]; then
2014-08-05 11:47:16 +02:00
OLD=`"$0" version`
cd "$BASE/platform"
2016-02-06 16:07:08 +05:30
echo Update shared platform...
git pull
cd "${PLATFORM_ENV}"
2016-01-14 11:46:29 +05:30
echo Update platform...
2014-08-05 11:47:16 +02:00
git pull
2016-01-14 11:46:29 +05:30
echo Update $NAME...
2014-08-05 11:47:16 +02:00
cd "$BASE/$NAME"
find . -name "*.pyc" -exec rm "{}" \;
2014-08-09 22:04:23 +02:00
git pull
2014-08-05 18:04:44 +02:00
"$0" update_static > /dev/null
2014-08-05 11:47:16 +02:00
NEW=`"$0" version`
2014-08-05 18:04:44 +02:00
"$0" postupdate -o $OLD -n $NEW
2014-08-05 11:47:16 +02:00
else
2017-01-06 19:40:33 +01:00
$PYTHON oml update
2014-08-05 11:47:16 +02:00
fi
exit $?
2014-05-17 13:40:51 +02:00
fi
2014-05-19 22:14:24 +02:00
if [ "$1" == "python" ]; then
2014-05-20 12:30:53 +02:00
cd "$BASE/$NAME"
2014-05-19 22:14:24 +02:00
shift
2017-01-06 19:40:33 +01:00
$PYTHON $@
2016-01-13 11:34:56 +05:30
exit $?
2014-05-19 22:14:24 +02:00
fi
2014-05-04 19:26:43 +02:00
2014-05-20 12:30:53 +02:00
cd "$BASE/$NAME"
2017-01-06 19:40:33 +01:00
$PYTHON oml $@
2016-01-13 11:34:56 +05:30
exit $?