From 7e6fa05f858e1a99bfafa1c955bddf3425cda0c0 Mon Sep 17 00:00:00 2001
From: Daniel Dehennin <daniel.dehennin@baby-gnu.org>
Date: Sat, 12 Nov 2011 01:02:16 +0100
Subject: [PATCH] Make "${GRUB_PREFIX}" variable user configurable

* util/grub-mkconfig.in: Load "${sysconfdir}/default/grub" after
  providing default value of "${GRUB_PREFIX}".

* util/grub-install.in (GRUB_PREFIX): New name of "${grubdir}", this
  permit user configuration with "${sysconfdir}/default/grub".
  (grub_setup): Permit to overwrite this variable with ${GRUB_SETUP} from
  user default variable file.
---
 util/grub-install.in  |   79 +++++++++++++++++++++++++++---------------------
 util/grub-mkconfig.in |    8 ++++-
 2 files changed, 50 insertions(+), 37 deletions(-)

diff --git a/util/grub-install.in b/util/grub-install.in
index 26a2523..614cb3d 100644
--- a/util/grub-install.in
+++ b/util/grub-install.in
@@ -45,7 +45,7 @@ grub_editenv="${bindir}/`echo grub-editenv | sed ${transform}`"
 grub_mkrelpath="${bindir}/`echo grub-mkrelpath | sed ${transform}`"
 rootdir=
 bootdir=
-grubdir="`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`"
+GRUB_PREFIX="`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`"
 modules=
 
 install_device=
@@ -65,6 +65,10 @@ if test -f "${sysconfdir}/default/grub" ; then
     . "${sysconfdir}/default/grub"
 fi
 
+if test -n ${GRUB_SETUP} ; then
+    grub_setup=${GRUB_SETUP}
+fi
+
 bootloader_id="$(echo "$GRUB_DISTRIBUTOR" | tr 'A-Z' 'a-z' | cut -d' ' -f1)"
 if test -z "$bootloader_id"; then
     bootloader_id=grub
@@ -98,7 +102,7 @@ Install GRUB on your drive.
   -v, --version           print the version information and exit
   --modules=MODULES       pre-load specified modules MODULES
   --boot-directory=DIR    install GRUB images under the directory DIR/@grubdirname@
-                          instead of the $grubdir directory
+                          instead of the $GRUB_PREFIX directory
   --grub-setup=FILE       use FILE as grub-setup
   --grub-mkimage=FILE     use FILE as grub-mkimage
   --grub-mkrelpath=FILE   use FILE as grub-mkrelpath
@@ -130,7 +134,7 @@ fi
 
 INSTALL_DEVICE can be a GRUB device name or a system device filename.
 
-$self copies GRUB images into $grubdir, and uses grub-setup
+$self copies GRUB images into $GRUB_PREFIX, and uses grub-setup
 to install grub into the boot sector.
 
 Report bugs to <bug-grub@gnu.org>.
@@ -293,11 +297,16 @@ if [ -z "$bootdir" ]; then
     if [ -n "$rootdir" ] ; then
         # Initialize bootdir if rootdir was initialized.
         bootdir="${rootdir}/@bootdirname@"
+
+        # Command line overwrite @sysconfdir@/default/grub
+	GRUB_PREFIX="`echo "${bootdir}/@grubdirname@" | sed 's,//*,/,g'`"
     fi
+else
+    # Command line overwrite @sysconfdir@/default/grub
+    GRUB_PREFIX="`echo "${bootdir}/@grubdirname@" | sed 's,//*,/,g'`"
 fi
 
-grubdir="`echo "${bootdir}/@grubdirname@" | sed 's,//*,/,g'`"
-device_map="${grubdir}/device.map"
+device_map="${GRUB_PREFIX}/device.map"
 
 
 # Check if GRUB is installed.
@@ -441,37 +450,37 @@ if test -n "$tmp"; then
 fi
 
 # Copy the GRUB images to the GRUB directory.
-for file in "${grubdir}"/*.mod "${grubdir}"/*.lst "${grubdir}"/*.img "${grubdir}"/efiemu??.o; do
+for file in "${GRUB_PREFIX}"/*.mod "${GRUB_PREFIX}"/*.lst "${GRUB_PREFIX}"/*.img "${GRUB_PREFIX}"/efiemu??.o; do
     if test -f "$file" && [ "`basename $file`" != menu.lst ]; then
 	rm -f "$file" || exit 1
     fi
 done
 for file in "${pkglibdir}"/*.mod "${pkglibdir}"/*.lst; do
-    cp -f "$file" "${grubdir}" || exit 1
+    cp -f "$file" "${GRUB_PREFIX}" || exit 1
 done
 if [ "${target_cpu}-${platform}" = "i386-pc" ] || [ "${target_cpu}-${platform}" = "sparc64-ieee1275" ] ; then
     for file in "${pkglibdir}"/*.img "${pkglibdir}"/efiemu??.o; do
 	if test -f "$file"; then
-	    cp -f "$file" "${grubdir}" || exit 1
+	    cp -f "$file" "${GRUB_PREFIX}" || exit 1
 	fi
     done
 fi
 
 # Copy gettext files
-mkdir -p "${grubdir}"/locale/
+mkdir -p "${GRUB_PREFIX}"/locale/
 for dir in "${localedir}"/*; do
     if test -f "$dir/LC_MESSAGES/grub.mo"; then
-        cp -f "$dir/LC_MESSAGES/grub.mo" "${grubdir}/locale/${dir##*/}.mo"
+        cp -f "$dir/LC_MESSAGES/grub.mo" "${GRUB_PREFIX}/locale/${dir##*/}.mo"
     fi
 done
 
-is_path_readable_by_grub "${grubdir}" || (echo "${grubdir}" not readable 1>&2 ; exit 1)
+is_path_readable_by_grub "${GRUB_PREFIX}" || (echo "${GRUB_PREFIX}" not readable 1>&2 ; exit 1)
 
 # Write device to a variable so we don't have to traverse /dev every time.
-grub_device="`"$grub_probe" --device-map="${device_map}" --target=device "${grubdir}"`" || exit 1
+grub_device="`"$grub_probe" --device-map="${device_map}" --target=device "${GRUB_PREFIX}"`" || exit 1
 
-if ! test -f "${grubdir}"/grubenv; then
-    "$grub_editenv" "${grubdir}"/grubenv create
+if ! test -f "${GRUB_PREFIX}"/grubenv; then
+    "$grub_editenv" "${GRUB_PREFIX}"/grubenv create
 fi
 
 # Create the core image. First, auto-detect the filesystem module.
@@ -479,7 +488,7 @@ fs_module="`"$grub_probe" --device-map="${device_map}" --target=fs --device "${g
 if test "x$fs_module" = x ; then
     echo "Auto-detection of a filesystem of ${grub_device} failed." 1>&2
     echo "Try with --recheck." 1>&2
-    echo "If the problem persists please report this together with the output of \"$grub_probe --device-map=\"${device_map}\" --target=fs -v ${grubdir}\" to <bug-grub@gnu.org>" 1>&2
+    echo "If the problem persists please report this together with the output of \"$grub_probe --device-map=\"${device_map}\" --target=fs -v ${GRUB_PREFIX}\" to <bug-grub@gnu.org>" 1>&2
     exit 1
 fi
 
@@ -516,7 +525,7 @@ fi
 modules="$modules $disk_module"
 modules="$modules $fs_module $partmap_module $devabstraction_module"
 
-relative_grubdir="`"$grub_mkrelpath" "${grubdir}"`" || exit 1
+relative_grubdir="`"$grub_mkrelpath" "${GRUB_PREFIX}"`" || exit 1
 if [ "x${relative_grubdir}" = "x" ] ; then
     relative_grubdir=/
 fi
@@ -524,11 +533,11 @@ fi
 prefix_drive=
 config_opt=
 
-rm -f "${grubdir}/load.cfg"
+rm -f "${GRUB_PREFIX}/load.cfg"
 
 if [ "x${debug_image}" != x ]; then
-    echo "set debug='${debug_image}'" >> "${grubdir}/load.cfg"
-    config_opt="-c ${grubdir}/load.cfg "
+    echo "set debug='${debug_image}'" >> "${GRUB_PREFIX}/load.cfg"
+    config_opt="-c ${GRUB_PREFIX}/load.cfg "
 fi
 
 if [ "x${devabstraction_module}" = "x" ] ; then
@@ -550,19 +559,19 @@ if [ "x${devabstraction_module}" = "x" ] ; then
         uuid="`"$grub_probe" --device-map="${device_map}" --target=fs_uuid --device "${grub_device}"`"
         if [ "x${uuid}" = "x" ] ; then
           if [ "x$platform" != xefi ] && [ "x$platform" != xpc ] && [ x"${platform}" != x"ieee1275" ]; then
-             echo "UUID needed with $platform, but the filesystem containing ${grubdir} does not support UUIDs." 1>&2
+             echo "UUID needed with $platform, but the filesystem containing ${GRUB_PREFIX} does not support UUIDs." 1>&2
           elif [ "$disk_module" = ata ]; then
-             echo "UUID needed with ata mod, but the filesystem containing ${grubdir} does not support UUIDs." 1>&2
+             echo "UUID needed with ata mod, but the filesystem containing ${GRUB_PREFIX} does not support UUIDs." 1>&2
           else
-             echo "UUID needed with cross-disk installs, but the filesystem containing ${grubdir} does not support UUIDs." 1>&2
+             echo "UUID needed with cross-disk installs, but the filesystem containing ${GRUB_PREFIX} does not support UUIDs." 1>&2
           fi
              
           exit 1
         fi
 
-        echo "search.fs_uuid ${uuid} root " >> "${grubdir}/load.cfg"
-	echo 'set prefix=($root)'"${relative_grubdir}" >> "${grubdir}/load.cfg"
-	config_opt="-c ${grubdir}/load.cfg "
+        echo "search.fs_uuid ${uuid} root " >> "${GRUB_PREFIX}/load.cfg"
+	echo 'set prefix=($root)'"${relative_grubdir}" >> "${GRUB_PREFIX}/load.cfg"
+	config_opt="-c ${GRUB_PREFIX}/load.cfg "
         modules="$modules search_fs_uuid"
     elif [ "x$platform" = xefi ] || [ "x$platform" = xpc ] || [ "x$platform" = xieee1275 ]; then
         # we need to hardcode the partition number in the core image's prefix.
@@ -575,9 +584,9 @@ if [ "x${devabstraction_module}" = "x" ] ; then
 else
     if [ x$GRUB_CRYPTODISK_ENABLE = xy ]; then
 	for uuid in "`"${grub_probe}" --device "${grub_device}" --target=cryptodisk_uuid`"; do
-	    echo "cryptomount -u $uuid" >> "${grubdir}/load.cfg"
+	    echo "cryptomount -u $uuid" >> "${GRUB_PREFIX}/load.cfg"
 	done
-	config_opt="-c ${grubdir}/load.cfg "
+	config_opt="-c ${GRUB_PREFIX}/load.cfg "
     fi
 
     prefix_drive=`"$grub_probe" --device-map="${device_map}" --target=drive --device "${grub_device}"` || exit 1
@@ -597,22 +606,22 @@ case "${target_cpu}-${platform}" in
 esac
 
 
-"$grub_mkimage" ${config_opt} -d "${pkglibdir}" -O ${mkimage_target} --output="${grubdir}/core.${imgext}" --prefix="${prefix_drive}${relative_grubdir}" $modules || exit 1
+"$grub_mkimage" ${config_opt} -d "${pkglibdir}" -O ${mkimage_target} --output="${GRUB_PREFIX}/core.${imgext}" --prefix="${prefix_drive}${relative_grubdir}" $modules || exit 1
 
 # Backward-compatibility kludges
 if [ "${target_cpu}-${platform}" = "mipsel-loongson" ]; then
-    cp "${grubdir}/core.${imgext}" "${bootdir}"/grub.elf
+    cp "${GRUB_PREFIX}/core.${imgext}" "${bootdir}"/grub.elf
 elif [ "${target_cpu}-${platform}" = "i386-ieee1275" ] || [ "${target_cpu}-${platform}" = "powerpc-ieee1275" ]; then
-    cp "${grubdir}/core.${imgext}" "${grubdir}/grub"
+    cp "${GRUB_PREFIX}/core.${imgext}" "${GRUB_PREFIX}/grub"
 elif [ "${target_cpu}-${platform}" = "i386-efi" ] || [ "${target_cpu}-${platform}" = "x86_64-efi" ]; then
-    "$grub_mkimage" ${config_opt} -d "${pkglibdir}" -O ${mkimage_target} --output="${grubdir}/grub.efi" --prefix="" $modules || exit 1
+    "$grub_mkimage" ${config_opt} -d "${pkglibdir}" -O ${mkimage_target} --output="${GRUB_PREFIX}/grub.efi" --prefix="" $modules || exit 1
 fi
 
 
 # Perform the platform-dependent install
 if [ "${target_cpu}-${platform}" = "i386-pc" ] || [ "${target_cpu}-${platform}" = "sparc64-ieee1275" ] ; then
     # Now perform the installation.
-    "$grub_setup" ${allow_floppy} ${setup_verbose} ${setup_force} --directory="${grubdir}" \
+    "$grub_setup" ${allow_floppy} ${setup_verbose} ${setup_force} --directory="${GRUB_PREFIX}" \
 	--device-map="${device_map}" "${install_device}" || exit 1
 elif [ "${target_cpu}-${platform}" = "i386-ieee1275" ] || [ "${target_cpu}-${platform}" = "powerpc-ieee1275" ]; then
     if [ x"$update_nvram" = xyes ]; then
@@ -642,7 +651,7 @@ elif [ "${target_cpu}-${platform}" = "i386-ieee1275" ] || [ "${target_cpu}-${pla
 	}
 
         # Point boot-device at the new grub install
-	boot_device="$ofpath:$partno,"`"$grub_mkrelpath" "${grubdir}/core.${imgext}" | sed 's,/,\\\\,g'`
+	boot_device="$ofpath:$partno,"`"$grub_mkrelpath" "${GRUB_PREFIX}/core.${imgext}" | sed 's,/,\\\\,g'`
 	"$nvsetenv" boot-device "$boot_device" || {
 	    echo "$nvsetenv failed."
 	    echo "You will have to set boot-device manually.  At the Open Firmware prompt, type:"
@@ -654,10 +663,10 @@ elif [ x"${target_cpu}-${platform}" = xmips-arc ]; then
     dvhtool -d "${install_device}" --unix-to-vh "{grubdir}/core.${imgext}" grub
     echo "You will have to set SystemPartition and OSLoader manually."
 elif [ x"$platform" = xefi ]; then
-    cp "${grubdir}/core.${imgext}" "${efidir}/${efi_file}"
+    cp "${GRUB_PREFIX}/core.${imgext}" "${efidir}/${efi_file}"
     # For old macs. Suggested by Peter Jones.
     if [ x$target_cpu = xi386 ]; then
-	cp "${grubdir}/core.${imgext}" "${efidir}/boot.efi"
+	cp "${GRUB_PREFIX}/core.${imgext}" "${efidir}/boot.efi"
     fi
 
     # Try to make this image bootable using the EFI Boot Manager, if available.
diff --git a/util/grub-mkconfig.in b/util/grub-mkconfig.in
index e317279..3dcd255 100644
--- a/util/grub-mkconfig.in
+++ b/util/grub-mkconfig.in
@@ -31,16 +31,20 @@ host_os=@host_os@
 datarootdir=@datarootdir@
 datadir=@datadir@
 pkgdatadir=${datadir}/`echo @PACKAGE_TARNAME@ | sed "${transform}"`
-grub_cfg=""
 grub_mkconfig_dir=${sysconfdir}/grub.d
 
+GRUB_PREFIX="`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`"
+grub_cfg="${GRUB_PREFIX}/grub.cfg"
+
 self=`basename $0`
 
 grub_mkdevicemap=${sbindir}/`echo grub-mkdevicemap | sed "${transform}"`
 grub_probe=${sbindir}/`echo grub-probe | sed "${transform}"`
 grub_script_check="${bindir}/`echo grub-script-check | sed "${transform}"`"
 
-GRUB_PREFIX=`echo '/@bootdirname@/@grubdirname@' | sed "s,//*,/,g"`
+if test -f ${sysconfdir}/default/grub ; then
+  . ${sysconfdir}/default/grub
+fi
 
 # Usage: usage
 # Print the usage.
-- 
1.7.7.2

