#!/usr/bin/env bash
# ==========================================================================
#         ____            _                     _____           _
#        / ___| _   _ ___| |_ ___ _ __ ___     |_   _|__   ___ | |___
#        \___ \| | | / __| __/ _ \ '_ ` _ \ _____| |/ _ \ / _ \| / __|
#         ___) | |_| \__ \ ||  __/ | | | | |_____| | (_) | (_) | \__ \
#        |____/ \__, |___/\__\___|_| |_| |_|     |_|\___/ \___/|_|___/
#               |___/
#                             --- System-Tools ---
#                  https://www.nntb.no/~dreibh/system-tools/
# ==========================================================================
#
# Configure-GRUB
# Copyright (C) 2013-2025 by Thomas Dreibholz
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: thomas.dreibholz@gmail.com

set -e


if [ $# -lt 1 ] ; then
   echo >&2 "Usage: $0 grub-defaults-template-file [--input|-i input-grub-defaults-file] [--output|-o output-grub-defaults-file] [--video|-V GRUB_GFXMODE] [--tune|-T GRUB_INIT_TUNE]"
   exit 1
fi


# ====== Check input files ==================================================
INPUT_CONFIGURATION="/etc/default/grub"
TEMPLATE_CONFIGURATION="$1"
OUTPUT_CONFIGURATION=""
REPLACE_GRUB_GFXMODE=""
REPLACE_GRUB_INIT_TUNE=""
shift
while [ $# -gt 0 ] ; do
   if [ "$1" == "--output" ] || [ "$1" == "-o" ] ; then
      OUTPUT_CONFIGURATION="$2"
      shift
   elif [ "$1" == "--input" ] || [ "$1" == "-i" ] ; then
      INPUT_CONFIGURATION="$2"
      shift
   elif [ "$1" == "--video" ] || [ "$1" == "-V" ] ; then
      REPLACE_GRUB_GFXMODE="$2"
      shift
   elif [ "$1" == "--tune" ] || [ "$1" == "-T" ] ; then
      REPLACE_GRUB_INIT_TUNE="$2"
      shift
   else
      echo >&2 "ERROR: Unknown option $1!"
      exit 1
   fi
   shift
done

if [ ! -e "${INPUT_CONFIGURATION}" ] ; then
   echo >&2 "ERROR: Template configuration ${INPUT_CONFIGURATION} not found!"
   exit 1
fi
if [ ! -e "${TEMPLATE_CONFIGURATION}" ] ; then
   echo >&2 "ERROR: Template configuration ${TEMPLATE_CONFIGURATION} not found!"
   exit 1
fi
if [ "${OUTPUT_CONFIGURATION}" == "" ] ; then
   OUTPUT_CONFIGURATION="grub-defaults.new"
fi


# ====== Get options ========================================================
OPTION_DISABLE_BIOSDEVNAME=0
if grep "biosdevname=0" >/dev/null 2>&1 /proc/cmdline ; then
   OPTION_DISABLE_BIOSDEVNAME=1
fi
OPTION_DISABLE_NETIFNAMES=0
if grep "net.ifnames=0" >/dev/null 2>&1 /proc/cmdline ; then
   OPTION_DISABLE_NETIFNAMES=1
fi
OPTION_CMDLINE=$(grep "^GRUB_CMDLINE_LINUX=" "${INPUT_CONFIGURATION}" | sed -e "s/biosdevname=0[ ]*//g" -e "s/net.ifnames=0[ ]*//g" || true)
if [ ${OPTION_DISABLE_BIOSDEVNAME} -eq 1 ] ; then
   # shellcheck disable=SC2001
   OPTION_CMDLINE=$(echo "${OPTION_CMDLINE}" | sed -e "s/^GRUB_CMDLINE_LINUX=\"/GRUB_CMDLINE_LINUX=\"biosdevname=0 /g")
fi
if [ ${OPTION_DISABLE_NETIFNAMES} -eq 1 ] ; then
   # shellcheck disable=SC2001
   OPTION_CMDLINE=$(echo "${OPTION_CMDLINE}" | sed -e "s/^GRUB_CMDLINE_LINUX=\"/GRUB_CMDLINE_LINUX=\"net.ifnames=0 /g")
fi

OPTION_ENABLE_CRYPTODISK=$(grep "^GRUB_ENABLE_CRYPTODISK=" "${INPUT_CONFIGURATION}" || true)
if [ "${REPLACE_GRUB_GFXMODE}" == "" ] ; then
   OPTION_GFXMODE=$(grep -E "^[#[:space:]]*GRUB_GFXMODE=" "${INPUT_CONFIGURATION}" | sed -e "s/#[ ]*GRUB/# GRUB/g" || true)
else
   OPTION_GFXMODE="GRUB_GFXMODE=\"${REPLACE_GRUB_GFXMODE}\""
fi
if [ "${REPLACE_GRUB_INIT_TUNE}" == "" ] ; then
   OPTION_INIT_TUNE=$(grep -E "^[#[:space:]]*GRUB_INIT_TUNE=" "${INPUT_CONFIGURATION}" | sed -e "s/#[ ]*GRUB/# GRUB/g" || true)
else
   OPTION_INIT_TUNE="GRUB_INIT_TUNE=\"${REPLACE_GRUB_INIT_TUNE}\""
fi


# ====== Print configuration ================================================
echo "Configuration:"
echo "* Input file:                 ${INPUT_CONFIGURATION}"
echo "* Template file:              ${TEMPLATE_CONFIGURATION}"
echo "* Output file:                ${OUTPUT_CONFIGURATION}"
echo "* OPTION_DISABLE_BIOSDEVNAME: ${OPTION_DISABLE_BIOSDEVNAME}"
echo "* OPTION_DISABLE_NETIFNAMES:  ${OPTION_DISABLE_NETIFNAMES}"
echo "* OPTION_CMDLINE:             ${OPTION_CMDLINE}"
echo "* OPTION_ENABLE_CRYPTODISK:   ${OPTION_ENABLE_CRYPTODISK}"
echo "* OPTION_INIT_TUNE:           ${OPTION_INIT_TUNE}"
echo "* OPTION_GFXMODE:             ${OPTION_GFXMODE}"


# ====== Set configuration ==================================================
awk \
   -v OPTION_CMDLINE="${OPTION_CMDLINE}" \
   -v OPTION_GFXMODE="${OPTION_GFXMODE}" \
   -v OPTION_INIT_TUNE="${OPTION_INIT_TUNE}" \
   -v OPTION_ENABLE_CRYPTODISK="${OPTION_ENABLE_CRYPTODISK}" \
'
   BEGIN {
      wroteCmdlineOption=0;
      wroteGfxModeOption=0;
      wroteInitTuneOption=0;
      wroteCryptodiskOption=0;
   }

   /^GRUB_CMDLINE_LINUX=/ {
      print OPTION_CMDLINE;
      wroteCmdlineOption=1;
      next;
   }

   /^[#[:space:]]*GRUB_GFXMODE=/ {
      print OPTION_GFXMODE;
      wroteGfxModeOption=1;
      next;
   }

   /^[#[:space:]]*GRUB_INIT_TUNE=/ {
      print OPTION_INIT_TUNE;
      wroteInitTuneOption=1;
      next;
   }

   /^GRUB_ENABLE_CRYPTODISK=/ {
      print OPTION_ENABLE_CRYPTODISK;
      wroteCryptodiskOption=1;
      next;
   }

   /^.*$/ {
      print $0;
   }

   END {
      if( (!wroteCmdlineOption) && (OPTION_CMDLINE != "")) {
         print OPTION_CMDLINE;
      }
      if( (!wroteGfxModeOption) && (OPTION_GFXMODE != "")) {
         print OPTION_GFXMODE;
      }
      if( (!wroteInitTuneOption) && (OPTION_INIT_TUNE != "")) {
         print OPTION_INIT_TUNE;
      }
      if( (!wroteCryptodiskOption) && (OPTION_ENABLE_CRYPTODISK != "")) {
         print OPTION_ENABLE_CRYPTODISK;
      }
   }
' <"${TEMPLATE_CONFIGURATION}" >"${OUTPUT_CONFIGURATION}.new"

echo "Differences between ${INPUT_CONFIGURATION} and ${OUTPUT_CONFIGURATION}:"
diff --color "${INPUT_CONFIGURATION}" "${OUTPUT_CONFIGURATION}.new" || true
mv "${OUTPUT_CONFIGURATION}.new" "${OUTPUT_CONFIGURATION}"
