#!/bin/bash

set -e

[ -z "$LIBEXECDIR" ] && LIBEXECDIR="/usr/libexec"
[ -z "$DATADIR" ] && DATADIR="/usr/share"
if [ -z "$LIB" ]; then
  if $(echo nash-showelfinterp /proc/$$/exe | /sbin/nash --forcequiet | grep -q lib64); then
    LIB="lib64"
  else
    LIB="lib"
  fi
fi
[ -z "$LIBDIR" ] && LIBDIR="/usr/$LIB"
[ -z "$BINDIR" ] && BINDIR="/usr/bin"

function usage ()
{
  echo "usage: plymouth-set-default-theme { --list | --reset | <theme-name> [ --rebuild-initrd ] }"
}

function list_themes ()
{
        for theme in ${DATADIR}/plymouth/themes/*/*.plymouth; do
                [ -f $theme ] || continue;
                echo "$(basename $theme .plymouth)"
        done
}

function get_default_theme ()
{
        THEME_NAME=$(basename $(readlink ${DATADIR}/plymouth/themes/default.plymouth) .plymouth)
        if [ "$THEME_NAME" = ".plymouth" ]; then
                $0 --reset
                THEME_NAME=$(basename $(readlink ${DATADIR}/plymouth/themes/default.plymouth) .plymouth)
        fi
        [ "$THEME_NAME" = ".so" ] || echo $THEME_NAME && exit 1
}

DO_RESET=0
DO_INITRD_REBUILD=0
DO_LIST=0
THEME_NAME=""
while [ $# -gt 0 ]; do
        case "$1" in

        --list)
                if [ -n "$THEME_NAME" ]; then
                        echo "You can only specify --list or a theme name, not both" > /dev/stderr
                        echo $(usage) > /dev/stderr
                        exit 1
                fi

                if [ $DO_RESET -ne 0 ]; then
                        echo "You can only specify --reset or --list, not both" > /dev/stderr
                        echo $(usage) > /dev/stderr
                        exit 1
                fi

                DO_LIST=1
        ;;

        --rebuild-initrd)
                DO_INITRD_REBUILD=1
        ;;

        --reset|default)
                if [ -n "$THEME_NAME" ]; then
                        echo "You can only specify --reset or a theme name, not both" > /dev/stderr
                        echo $(usage) > /dev/stderr
                        exit 1
                fi

                if [ $DO_LIST -ne 0 ]; then
                        echo "You can only specify --reset or --list, not both" > /dev/stderr
                        echo $(usage) > /dev/stderr
                        exit 1
                fi

                DO_RESET=1
        ;;

        *)
                if [ -n "$THEME_NAME" ]; then
                        echo "You can only specify one theme at a time" > /dev/stderr
                        echo $(usage) > /dev/stderr
                        exit 1
                fi

                if [ $DO_RESET -ne 0 ]; then
                        echo "You can only specify --reset or a theme name, not both" > /dev/stderr
                        echo $(usage) > /dev/stderr
                        exit 1
                fi

                if [ $DO_LIST -ne 0 ]; then
                        echo "You can only specify --list or a theme name, not both" > /dev/stderr
                        echo $(usage) > /dev/stderr
                        exit 1
                fi

                THEME_NAME="$1"
        ;;
  esac
  shift
done

if [ $DO_LIST -ne 0 ]; then
        list_themes
        exit $?
fi

if [ $DO_RESET -eq 0 ] && [ $DO_INITRD_REBUILD -eq 0 ] && [ -z $THEME_NAME ]; then
        get_default_theme
        exit $?
fi

if [ `id -u` -ne 0 ]; then
        echo "This program must be run as root" > /dev/stderr
        exit 1
fi

if [ $DO_RESET -ne 0 ]; then
        THEME_NAME=$(basename $(ls -1 -t ${DATADIR}/plymouth/themes/*/*.plymouth 2> /dev/null | tail -n 1) .plymouth)
        if [ $THEME_NAME = .plymouth ]; then
                rm -f ${DATADIR}/plymouth/themes/default.plymouth
                exit 0
        fi
fi

if [ ! -e ${DATADIR}/plymouth/themes/${THEME_NAME}/${THEME_NAME}.plymouth ]; then
        echo "${DATADIR}/plymouth/themes/${THEME_NAME}/${THEME_NAME}.plymouth does not exist" > /dev/stderr
        exit 1
fi

MODULE_NAME=$(grep "ModuleName *= *" ${DATADIR}/plymouth/themes/${THEME_NAME}/${THEME_NAME}.plymouth | sed 's/ModuleName *= *//')

if [ ! -e ${LIBDIR}/plymouth/${MODULE_NAME}.so ]; then
        echo "${LIBDIR}/plymouth/${MODULE_NAME}.so does not exist" > /dev/stderr
        exit 1
fi

(cd ${DATADIR}/plymouth/themes;
        ln -sf ${THEME_NAME}/${THEME_NAME}.plymouth default.plymouth && \
        ([ $DO_INITRD_REBUILD -ne 0 ] && \
           $LIBEXECDIR/plymouth/plymouth-update-initrd) || :)

