#!/bin/sh
# Filename:      ${GRML_FAI_CONFIG}/scripts/GRMLBASE/47-update-wallpaper
# Purpose:       Update the grml wallpaper
# Authors:       grml-team (grml.org)
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2 or any later version.
################################################################################

set -e
set -u

# FAI sets $target, but shellcheck does not know that.
target=${target:?}

# shellcheck source=/dev/null
. "$GRML_LIVE_CONFIG"

TITLE_FONTSIZE=${TITLE_FONTSIZE:-200}
VERSION_FONTSIZE=${VERSION_FONTSIZE:-100}
GRML_WALLPAPER=${GRML_WALLPAPER:-"$target"/usr/share/grml/desktop.jpg}

fcopy -M -i -r /usr/share/grml-wallpaper
FONTFILE="${target}/usr/share/grml-wallpaper/font.otf"
GRML_BG="${target}/usr/share/grml-wallpaper/input.png"

echo "Creating GRMLBASE wallpaper"

if [ ! -x "$(which convert)" ]; then
    echo "convert not installed, skipping wallpaper."
    exit 0
fi

if [ ! -f "$GRML_BG" ]; then
    echo "Could not find input image $GRML_BG, skipping wallpaper"
    exit 0
fi

if [ ! -f "$FONTFILE" ]; then
    echo "Could not find font $FONTFILE, skipping wallpaper"
    exit 0
fi

convert "$GRML_BG" -gravity center \
    -fill white -font "$FONTFILE" \
    -pointsize "$TITLE_FONTSIZE" \
    -draw "text 0,0 \"$GRML_NAME\"" \
    -pointsize "$VERSION_FONTSIZE" \
    -draw "text 0,$((TITLE_FONTSIZE+50)) \"$VERSION\"" "$GRML_WALLPAPER"

rm -rf "${target}/usr/share/grml-wallpaper"
