#!/sbin/openrc-run

# Copyright 1999-2020 Gentoo Foundation
# Distributed under the terms of the GNU General Public Licence v2


name=jellyfin

cfgfile="/etc/conf.d/${name}"

command="/usr/lib/${name}/bin/${name}"
command_user="${JELLYFIN_USER}"
command_background="yes"

pidfile="/run/${name}.pid"


depend() {
        need net
}

start_pre() {
    echo "Running with:"
    echo "   JELLYFIN_ARGS=${JELLYFIN_ARGS}"
    echo "   JELLYFIN_CACHE_DIR=${JELLYFIN_CACHE_DIR}"
    echo "   JELLYFIN_CONFIG_DIR=${JELLYFIN_CONFIG_DIR}"
    echo "   JELLYFIN_DATA_DIR=${JELLYFIN_DATA_DIR}"
    echo "   JELLYFIN_FFMPEG_OPT=${JELLYFIN_FFMPEG_OPT}"
    echo "   JELLYFIN_LOG_DIR=${JELLYFIN_LOG_DIR}"
    echo "   JELLYFIN_NOWEBAPP_OPT=${JELLYFIN_NOWEBAPP_OPT}"
    echo "   JELLYFIN_RESTART_OPT=${JELLYFIN_RESTART_OPT}"
    echo "   JELLYFIN_SERVICE_OPT=${JELLYFIN_SERVICE_OPT}"
    echo "   JELLYFIN_USER=${JELLYFIN_USER}"
    echo "   JELLYFIN_WEB_OPT=${JELLYFIN_WEB_OPT}"

    mkdir -p ${JELLYFIN_CACHE_DIR}
    mkdir -p ${JELLYFIN_CONFIG_DIR}
    mkdir -p ${JELLYFIN_DATA_DIR}
    mkdir -p ${JELLYFIN_LOG_DIR}

    chown -R ${command_user}:${command_user} ${JELLYFIN_CACHE_DIR}
    chown -R ${command_user}:${command_user} ${JELLYFIN_CONFIG_DIR}
    chown -R ${command_user}:${command_user} ${JELLYFIN_DATA_DIR}
    chown -R ${command_user}:${command_user} ${JELLYFIN_LOG_DIR}
}

start() {
    ebegin "Starting ${name}"

    env JELLYFIN_CACHE_DIR=${JELLYFIN_CACHE_DIR} \
        JELLYFIN_CONFIG_DIR=${JELLYFIN_CONFIG_DIR} \
        JELLYFIN_DATA_DIR=${JELLYFIN_DATA_DIR} \
        JELLYFIN_LOG_DIR=${JELLYFIN_LOG_DIR} \
        start-stop-daemon --start --exec ${command} \
                          --background \
                          --pidfile ${pidfile} \
                          --user ${command_user} \
                          --make-pidfile \
                          -- ${JELLYFIN_ARGS}

    eend $?
}

stop() {
    ebegin "Stopping ${name}"

    env JELLYFIN_CACHE_DIR=${JELLYFIN_CACHE_DIR} \
        JELLYFIN_CONFIG_DIR=${JELLYFIN_CONFIG_DIR} \
        JELLYFIN_DATA_DIR=${JELLYFIN_DATA_DIR} \
        JELLYFIN_LOG_DIR=${JELLYFIN_LOG_DIR} \
        start-stop-daemon --stop --exec ${command} \
                          --pidfile ${pidfile} \
                          --user ${command_user} \
                          -- ${JELLYFIN_ARGS}

    eend $?
}