#!/bin/sh
#
# Installer for support files needed by Pacemaker's Cluster Test Suite
#
# Copyright 2018-2020 the Pacemaker project contributors
#
# The version control history for this file may have further details.
#
# This source code is licensed under the GNU General Public License version 2
# or later (GPLv2+) WITHOUT ANY WARRANTY.
#

USAGE_TEXT="Usage: $0 <install|uninstall|--help>"

HELP_TEXT="$USAGE_TEXT
Commands (must be run as root):
 install      Install support files needed by Pacemaker CTS
 uninstall    Remove support files needed by Pacemaker CTS"

# These constants must track crm_exit_t values
CRM_EX_OK=0
CRM_EX_ERROR=1
CRM_EX_USAGE=64

UNIT_DIR="@systemdsystemunitdir@"
RUNTIME_UNIT_DIR="@runstatedir@/systemd/system"
LIBEXEC_DIR="@libexecdir@/pacemaker"
INIT_DIR="@INITDIR@"
PCMK__FENCE_BINDIR="@PCMK__FENCE_BINDIR@"
DATA_DIR="@datadir@/pacemaker/tests/cts"
UPSTART_DIR="/etc/init"

DUMMY_DAEMON="pacemaker-cts-dummyd"
DUMMY_DAEMON_UNIT="pacemaker-cts-dummyd@.service" 
COROSYNC_RUNTIME_UNIT="corosync.service.d"
COROSYNC_RUNTIME_CONF="cts.conf"

LSB_DUMMY="LSBDummy"
UPSTART_DUMMY="pacemaker-cts-dummyd.conf"
FENCE_DUMMY="fence_dummy"
FENCE_DUMMY_ALIASES="fence_dummy_auto_unfence fence_dummy_no_reboot"

# If the install directory doesn't exist, assume we're in a build directory.
if [ ! -d "$DATA_DIR" ]; then
    # If readlink supports -e (i.e. GNU), use it.
    readlink -e / >/dev/null 2>/dev/null
    if [ $? -eq 0 ]; then
        DATA_DIR="$(dirname "$(readlink -e "$0")")"
    else
        DATA_DIR="$(dirname "$0")"
    fi
fi

usage() {
    echo "Error:" "$@"
    echo "$USAGE_TEXT"
    exit $CRM_EX_USAGE
}

must_be_root() {
    if ! [ "$(id -u)" = "0" ]; then
        usage "this command must be run as root"
        return $CRM_EX_ERROR
    fi
    return $CRM_EX_OK
}

support_uninstall() {
    must_be_root || return $CRM_EX_ERROR

    if [ -e "$UNIT_DIR/$DUMMY_DAEMON_UNIT" ]; then
        echo "Removing $UNIT_DIR/$DUMMY_DAEMON_UNIT ..."
        rm -f "$UNIT_DIR/$DUMMY_DAEMON_UNIT"
        systemctl daemon-reload # Ignore failure
    fi

    if [ -e "$RUNTIME_UNIT_DIR/$COROSYNC_RUNTIME_UNIT" ]; then
        echo "Removing $RUNTIME_UNIT_DIR/$COROSYNC_RUNTIME_UNIT ..."
        rm -rf "$RUNTIME_UNIT_DIR/$COROSYNC_RUNTIME_UNIT"
        systemctl daemon-reload # Ignore failure
    fi

    for FILE in \
        "$LIBEXEC_DIR/$DUMMY_DAEMON" \
        "$UPSTART_DIR/$UPSTART_DUMMY" \
        "$PCMK__FENCE_BINDIR/$FENCE_DUMMY" \
        "$INIT_DIR/$LSB_DUMMY"
    do
        if [ -e "$FILE" ]; then
            echo "Removing $FILE ..."
            rm -f "$FILE"
        fi
    done
    for ALIAS in $FENCE_DUMMY_ALIASES; do \
        FILE="$PCMK__FENCE_BINDIR/$ALIAS"
        if [ -L "$FILE" ] || [ -e "$FILE" ]; then
            echo "Removing $FILE ..."
            rm -f "$FILE"
        fi
    done

    return $CRM_EX_OK
}

support_install() {
    support_uninstall || return $CRM_EX_ERROR
    cd "$DATA_DIR" || return $CRM_EX_ERROR
    if [ -d "$UNIT_DIR" ]; then

        echo "Installing $DUMMY_DAEMON ..."
        mkdir -p "$LIBEXEC_DIR"
        install -m 0755 "$DUMMY_DAEMON" "$LIBEXEC_DIR" || return $CRM_EX_ERROR

        echo "Installing $DUMMY_DAEMON_UNIT ..."
        install -m 0644 "$DUMMY_DAEMON_UNIT" "$UNIT_DIR" || return $CRM_EX_ERROR
        systemctl daemon-reload # Ignore failure
    fi

    if [ -d "$RUNTIME_UNIT_DIR" ]; then

        echo "Installing $COROSYNC_RUNTIME_CONF to $RUNTIME_UNIT_DIR/$COROSYNC_RUNTIME_UNIT ..."
        mkdir -p "$RUNTIME_UNIT_DIR/$COROSYNC_RUNTIME_UNIT"
        install -m 0644 "$COROSYNC_RUNTIME_CONF" "$RUNTIME_UNIT_DIR/$COROSYNC_RUNTIME_UNIT" || return $CRM_EX_ERROR

        systemctl daemon-reload # Ignore failure
    fi

    echo "Installing $FENCE_DUMMY to $PCMK__FENCE_BINDIR ..."
    mkdir -p "$PCMK__FENCE_BINDIR"
    install -m 0755 "$FENCE_DUMMY" "$PCMK__FENCE_BINDIR" || return $CRM_EX_ERROR
    for alias in $FENCE_DUMMY_ALIASES; do \
        echo "Installing $alias to $PCMK__FENCE_BINDIR ..."
        ln -s "$FENCE_DUMMY" "$PCMK__FENCE_BINDIR/$alias" || return $CRM_EX_ERROR
    done

    echo "Installing $LSB_DUMMY to $INIT_DIR ..."
    mkdir -p "$INIT_DIR"
    install -m 0755 "$LSB_DUMMY" "$INIT_DIR" || return $CRM_EX_ERROR

    if [ -d "$UPSTART_DIR" ] && [ -f "$UPSTART_DUMMY" ]; then
        echo "Installing $UPSTART_DUMMY to $UPSTART_DIR ..."
        install -m 0644 "$UPSTART_DUMMY" "$UPSTART_DIR" || return $CRM_EX_ERROR
    fi
    return $CRM_EX_OK
}

COMMAND=""
while [ $# -gt 0 ] ; do
    case "$1" in
        --help)
            echo "$HELP_TEXT"
            exit $CRM_EX_OK
            ;;
        install|uninstall)
            COMMAND="$1"
            shift
            ;;
        *)
            usage "unknown option '$1'"
            ;;
    esac
done
case "$COMMAND" in
    install)   support_install              ;;
    uninstall) support_uninstall            ;;
    *)         usage "must specify command" ;;
esac
