#!/usr/bin/env bash
set -e
declare -r pkgname="koreader"
declare -r pkgdesc="An ebook reader application supporting PDF, DjVu, EPUB, FB2 and many more formats"
declare -r url="https://github.com/koreader/koreader"
declare -r pkgver="2020.12-3"
declare -r timestamp="2020-10-10T20:13Z"
declare -r section="readers"
declare -r maintainer="raisjn <of.raisjn@gmail.com>"
declare -r license="AGPL-3.0-or-later"
declare -ar depends=([0]="fbink" [1]="fbdepth")
declare -ar conflicts=()
declare -r arch="armv7-3.2"
declare -r image=""
declare -ar source=([0]="https://build.koreader.rocks/download/stable/v2020.12/koreader-remarkable-v2020.12.zip" [1]="koreader.draft" [2]="KOReader.oxide" [3]="rm2-support.patch")
declare -ar noextract=()
declare -ar sha256sums=([0]="38db8f3895472828d7c1d63fc54db426135a8dbf8425633e299a1a9abfdb69b2" [1]="SKIP" [2]="SKIP" [3]="SKIP")
declare -ar flags=()
#!/usr/bin/env bash
# Copyright (c) 2020 The Toltec Contributors
# SPDX-License-Identifier: MIT

#
# install-lib
#
# Common functions used by the install scripts
#

# Check whether a systemd unit exists and is in an enabled-like state
# ("enabled", "enabled-runtime", "alias", "static", "indirect", "generated"
# or "transient")
#
# Arguments:
#
# $1 - Name of the systemd unit, e.g. "xochitl.service" or "xochitl"
#
# Exit code:
#
# 0 if the unit exists and is enabled, 1 otherwise
is-enabled() {
    systemctl --quiet is-enabled "$1" 2> /dev/null
}

# Check whether a systemd unit is in an active state
# ("running")
#
# Arguments:
#
# $1 - Name of the systemd unit, e.g. "xochitl.service" or "xochitl"
#
# Exit code:
#
# 0 if the unit exists and is enabled, 1 otherwise
is-active() {
    systemctl --quiet is-active "$1" 2> /dev/null
}

# Get a list of systemd units with which the given unit conflicts
#
# Arguments:
#
# $1 - Full name of the systemd unit, e.g. "xochitl.service"
#
# Output:
#
# List of conflicting units
get-conflicts() {
    systemctl show "$1" | awk -F'=' '/^Conflicts=/{print $2}'
}

# Print instructions about how to enable a given systemd service and disable
# the services that conflict with it
#
# Arguments:
#
# $1 - Full name of the systemd unit, e.g. "draft.service"
#
# Output:
#
# Commands to run to achieve the desired result
how-to-enable() {
    for conflict in $(get-conflicts "$1"); do
        if is-enabled "$conflict"; then
            echo "$ systemctl disable --now ${conflict/.service/}"
        fi
    done

    echo "$ systemctl enable --now ${1/.service/}"
}

# Reload Oxide applications if tarnish is running
#
# Output:
#
# Status message
reload-oxide-apps() {
    if ! is-active tarnish.service; then
        return
    fi
    echo -n "Reloading Oxide applications: "
    if ! /opt/bin/rot apps call reload 2> /dev/null; then
        echo "Failed!"
    else
        echo "Done!"
    fi
}
if [[ $1 = configure ]]; then
    reload-oxide-apps
fi
