#!/bin/sh

set -e

if [ "$(id -u)" != "0" ]; then
    exec sudo "${0}" "${@}"
fi

themes_dir="/usr/share/plymouth/themes"
theme_name="elive-vortex-glow"

install() {
    set -x
    rsync -av --delete "$(dirname "${0}")/" "${themes_dir}/${theme_name}/"
    update-alternatives \
        --install \
        "${themes_dir}/default.plymouth" \
        default.plymouth \
        "${themes_dir}/${theme_name}/${theme_name}.plymouth" \
        100

    update-alternatives \
        --set \
        default.plymouth \
        "${themes_dir}/${theme_name}/${theme_name}.plymouth"

    update-initramfs -u
}

uninstall() {
    set -x
    update-alternatives \
        --remove \
        default.plymouth \
        "${themes_dir}/${theme_name}/${theme_name}.plymouth"
    # Prompt user for a different theme
    update-alternatives \
        --config \
        default.plymouth
    update-initramfs -u
    rm -rvf "${themes_dir:?}/${theme_name:?}/"
}

usage() {
    echo "Usage: ${0} [uninstall]" >&2
    exit 1
}

if [ "${1}" = "-h" ] || [ "${1}" = "--help" ] || [ "${1}" = "help" ]; then
    usage
fi

if [ ! -d "${themes_dir}" ]; then
    echo "\"${themes_dir}\" directory does not exist" >&2
    exit 1
fi

case "${1}" in
    "")
        install;;
    uninstall)
        uninstall;;
    *)
        usage;;
esac
