#!/bin/sh
set -e

# grub-mkconfig helper script.
# Copyright (C) 2010  Alexander Kurtz <kurtz.alex@googlemail.com>
#
# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GRUB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.

# Include the GRUB helper library for grub-mkconfig.
. /usr/share/grub/grub-mkconfig_lib

# We want to work in /boot/grub/ only.
test -d /boot/grub; cd /boot/grub

# Set the location of a possibly necessary cache file for the background image.
# NOTE: This MUST BE A DOTFILE to avoid confusing it with user-defined images.
GRUB_THEME=/usr/share/grub/themes/UKUI/theme.txt

BACKGROUND_CACHE=".background_cache"
gfxterm=0
for x in ${GRUB_TERMINAL_INPUT} ${GRUB_TERMINAL_OUTPUT}; do
    if [ xgfxterm = "x$x" ]; then
        gfxterm=1;
    fi
done
if [ "x$gfxterm" = x1 ]; then
    if [ "x$GRUB_THEME" != x ] && [ -f "$GRUB_THEME" ] \
        && is_path_readable_by_grub "$GRUB_THEME"; then
        gettext_printf "Found theme: %s\n" "$GRUB_THEME" >&2

        prepare_grub_to_access_device `${grub_probe} --target=device "$GRUB_THEME"`
        cat << EOF
insmod gfxmenu
EOF
        themedir="`dirname "$GRUB_THEME"`"
        for x in "$themedir"/*.pf2 "$themedir"/f/*.pf2; do
            if [ -f "$x" ]; then
                cat << EOF
loadfont (\$root)`make_system_path_relative_to_its_root $x`
EOF
            fi
        done
        if [ x"`echo "$themedir"/*.jpg`" != x"$themedir/*.jpg" ] || [ x"`echo "$themedir"/*.jpeg`" != x"$themedir/*.jpeg" ]; then
            cat << EOF
insmod jpeg
EOF
        fi
        if [ x"`echo "$themedir"/*.png`" != x"$themedir/*.png" ]; then
            cat << EOF
insmod png
EOF
        fi
        if [ x"`echo "$themedir"/*.tga`" != x"$themedir/*.tga" ]; then
            cat << EOF
insmod tga
EOF
        fi

        cat << EOF
set theme=(\$root)`make_system_path_relative_to_its_root $GRUB_THEME`
export theme
EOF
    elif [ "x$GRUB_BACKGROUND" != x ] && [ -f "$GRUB_BACKGROUND" ] \
            && is_path_readable_by_grub "$GRUB_BACKGROUND"; then
        gettext_printf "Found background: %s\n" "$GRUB_BACKGROUND" >&2
        case "$GRUB_BACKGROUND" in
            *.png)         reader=png ;;
            *.tga)         reader=tga ;;
            *.jpg|*.jpeg)  reader=jpeg ;;
            *)             gettext "Unsupported image format" >&2; echo >&2; exit 1 ;;
        esac
        prepare_grub_to_access_device `${grub_probe} --target=device "$GRUB_BACKGROUND"`
        cat << EOF
insmod $reader
background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"`
EOF
    fi
fi
