#!/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`
GIT_BASE=https://code.0x2620.org/0x2620

PYTHON=python3

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

PLATFORM_PYTHON=3.4
SHARED_PYTHON=3.7
if [ -e "$BASE/platform_local" ]; then
    export PLATFORM_ENV="$BASE/platform_local"
else
    if [ $SYSTEM == "Linux" ]; then
        if [ $PLATFORM == "x86_64" ]; then
            ARCH=64
            PLATFORM_PYTHON=3.11
        else
            ARCH=32
        fi
        if [ $PLATFORM == "aarch64" ]; then
            ARCH="_aarch64"
            PLATFORM_PYTHON=3.5
        fi
        if [ $PLATFORM == "armv7l" ]; then
            ARCH="_armv7l"
        fi
        PLATFORM="linux$ARCH"
        if [ -e "$BASE/platform_${PLATFORM}/lib/libunrar.so" ]; then
            export UNRAR_LIB_PATH="$BASE/platform_${PLATFORM}/lib/libunrar.so"
        fi
    fi
    if [ $SYSTEM == "Darwin" ]; then
        PLATFORM="darwin64"
        PLATFORM_PYTHON=3.7
        if [ -e "$BASE/platform_${PLATFORM}/lib/libunrar.dylib" ]; then
            export UNRAR_LIB_PATH="$BASE/platform_${PLATFORM}/lib/libunrar.dylib"
        fi
    fi
    export PLATFORM_ENV="$BASE/platform_${PLATFORM}"
fi
PATH="$PLATFORM_ENV/bin:$PATH"

SHARED_ENV="$BASE/platform/Shared"
export SHARED_ENV

if [ -e "$SHARED_ENV/etc/openssl/openssl.cnf" ]; then
    export OPENSSL_CONF="$SHARED_ENV/etc/openssl/openssl.cnf"
fi

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/python${PLATFORM_PYTHON}/site-packages"
PYTHONPATH="${PYTHONPATH}:${SHARED_ENV}/lib/python${PLATFORM_PYTHON}/site-packages"
PYTHONPATH="${PYTHONPATH}:${SHARED_ENV}/lib/python${SHARED_PYTHON}/site-packages"
PYTHONPATH="${PYTHONPATH}:${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 update_gitbase() {
    if [ -e "${BASE}/openmedialibrary/.git/config" ]; then
         GIT_BASE=`grep origin .git/config  -A 1 | grep url | cut -f2 -d=`
    fi
}

if [ "$1" == "start" ]; then
    cd "$BASE/$NAME"
    if [ -e "$PID" ]; then
        if ps -p `cat "$PID"` > /dev/null; then
            echo openmedialibrary already running
            exit 1
        fi
    fi
    $PYTHON 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
    $PYTHON oml server debug "$PID"
    exit $?
fi
if [ "$1" == "stop" ]; then
    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 60 ]; 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

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
}


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
                open_linux
            else
                open_linux
                "$0" start &
            fi
        else
            open_linux
            "$0" start &
        fi
    fi
    exit 0
fi
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
            $PYTHON "$NAME/oml/gtkstatus.py" --autostart
            exit $?
        fi
    fi
    exit 0
fi

if [ "$1" == "ui" ]; then
    shift
    $PYTHON "$NAME/oml/ui.py" "$@"
    exit $?
fi
if [ "$1" == "init" ]; then
    update_gitbase
    if [ ! -e "${BASE}/platform" ]; then
        git clone --depth 1 $GIT_BASE/openmedialibrary_platform.git "${BASE}/platform"
    fi
    if [ ! -e "${PLATFORM_ENV}" ]; then
        git clone --depth 1 $GIT_BASE/openmedialibrary_platform_${PLATFORM}.git "${PLATFORM_ENV}"
    fi
    cd "${BASE}"
    if [ ! -e "${BASE}/ctl" ]; then
        ln -s openmedialibrary/ctl
    fi
    "${BASE}/ctl" update_static
    exit
fi
if [ "$1" == "update" ]; then
    cd "$BASE/$NAME"
    if [ ! -e "${PLATFORM_ENV}" ]; then
        update_gitbase
        git clone --depth 1 $GIT_BASE/openmedialibrary_platform_${PLATFORM}.git "${PLATFORM_ENV}"
    fi
    if [ -d "$BASE/$NAME/.git" ]; then
        OLD=`"$0" version`
        cd "$BASE/platform"
        echo Update shared platform...
        git pull
        cd "${PLATFORM_ENV}"
        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
        $PYTHON oml update
    fi
    exit $?
fi
if [ "$1" == "python" ]; then
    cd "$BASE/$NAME"
    shift
    $PYTHON $@
    exit $?
fi

cd "$BASE/$NAME"
$PYTHON oml $@
exit $?
