#! /bin/sh -e

# grub-mkconfig helper script.
# Copyright (C) 2006,2007,2008,2009,2010  Free Software Foundation, Inc.
#
# 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/>.

transform="s,x,x,"

prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
grub_prefix=`echo /boot/burg | sed ${transform}`
locale_dir=`echo /boot/burg/locale | sed ${transform}`
grub_lang=`echo $LANG | cut -d _ -f 1`
sysconfdir=/etc
pass_file=${sysconfdir}/default/burg-passwd

. ${libdir}/burg/burg-mkconfig_lib

# Do this as early as possible, since other commands might depend on it.
# (e.g. the `loadfont' command might need lvm or raid modules)
for i in ${GRUB_PRELOAD_MODULES} ; do
  echo "insmod $i"
done

if [ "x${GRUB_DEFAULT}" = "x" ] ; then GRUB_DEFAULT=0 ; fi
if [ "x${GRUB_DEFAULT}" = "xsaved" ] ; then GRUB_DEFAULT='${saved_entry}' ; fi
if [ "x${GRUB_TIMEOUT}" = "x" ] ; then GRUB_TIMEOUT=5 ; fi
if [ "x${GRUB_GFXMODE}" = "x" ] ; then GRUB_GFXMODE=640x480 ; fi

if test "x${GRUB_USERS}" != "x" ; then
  if test ! -r $pass_file ; then
    echo "Password file not found" >&2 ; exit 1
  fi

  awk -F: 'BEGIN {super=""} {print "password_" $3,$1,$4; if ($2=="s") super=super $1 ",";} END {if (super=="") exit 1; else print "set superusers=" super;}' $pass_file || { echo "No superuser is found" >&2 ; exit 1; }
fi

default_theme="$(echo ${GRUB_DISTRIBUTOR} | tr '[A-Z]' '[a-z]' | cut -d' ' -f1)"
if test ! -r "${grub_prefix}/themes/${default_theme}/theme"; then
  default_theme=burg
fi

if test "x${GRUB_THEME}" = "xsaved"; then
  echo "set theme_name=${default_theme}"
fi
if test "x${GRUB_GFXMODE}" = "xsaved"; then
  echo "set gfxmode=640x480"
fi
cat << EOF
if [ -s \$prefix/burgenv ]; then
  load_env
fi
set default="${GRUB_DEFAULT}"
if [ \${prev_saved_entry} ]; then
  set saved_entry=\${prev_saved_entry}
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
fi

function savedefault {
  if [ -z \${boot_once} ]; then
    saved_entry=\${chosen}
    save_env saved_entry
  fi
}
EOF
if test "x${GRUB_GFXMODE}" != "xsaved"; then
  echo "set gfxmode=${GRUB_GFXMODE}"
fi

serial=0;
gfxterm=0;
for x in ${GRUB_TERMINAL_INPUT} ${GRUB_TERMINAL_OUTPUT}; do
    if [ xserial = "x$x" ]; then
	serial=1;
    fi
    if [ xgfxterm = "x$x" ]; then
	gfxterm=1;
    fi
done

if [ "x$serial" = x1 ]; then
    if ! test -e ${grub_prefix}/serial.mod ; then
	echo "Serial terminal not available on this platform." >&2 ; exit 1
    fi

    if [ "x${GRUB_SERIAL_COMMAND}" = "x" ] ; then
	grub_warn "Requested serial terminal but GRUB_SERIAL_COMMAND is unspecified. Default parameters will be used."
	GRUB_SERIAL_COMMAND=serial
    fi
    echo "${GRUB_SERIAL_COMMAND}"
fi

if [ x$GRUB_THEME != x ] && [ ! -f $GRUB_THEME ] ; then
  cat << EOF
function select_menu {
  if menu_popup -t template_popup theme_menu ; then
    free_config template_popup template_subitem menu class screen
    load_config \${prefix}/themes/\${theme_name}/theme \${prefix}/themes/custom/theme_\${theme_name}
EOF
  
  if test "x${GRUB_THEME}" = "xsaved"; then
    cat << EOF
    save_env theme_name
    menu_refresh
  fi
}
EOF
  else
    cat << EOF
    menu_refresh
  fi
}
set theme_name=${GRUB_THEME}
EOF
  fi

  cat << EOF
function toggle_fold {
  if test -z \$theme_fold ; then
    set theme_fold=1
  else
    set theme_fold=
  fi
EOF
  
  if test "x${GRUB_FOLD}" = "xsaved"; then
    cat << EOF
  save_env theme_fold
  menu_refresh
}
EOF
  else
    cat << EOF
  menu_refresh
}
EOF
    if test "x${GRUB_FOLD}" = "xtrue"; then
      echo "set theme_fold=1"
    else
      echo "set theme_fold="
    fi
  fi
  
  cat << EOF
function select_resolution {
  if menu_popup -t template_popup resolution_menu ; then
    menu_reload_mode
EOF

  if test "x${GRUB_GFXMODE}" = "xsaved"; then
    echo "    save_env gfxmode"
  fi

  cat << EOF
  fi
}
EOF

  echo "if test -f \${prefix}/themes/\${theme_name}/theme ; then"

  cat << EOF
  insmod coreui
  menu_region.text
EOF

  for i in ${grub_prefix}/themes/* ; do
    if test -r "$i/theme"; then
      theme_name=`echo $i | sed 's,.*/,,'`
      echo "  load_string '+theme_menu { -${theme_name} { command=\"set theme_name=${theme_name}\" }}'"
    fi
  done  

  for i in ${grub_prefix}/themes/conf.d/* ; do
    case "$i" in
      *~*) ;;
      *)
        if grub_file_is_not_garbage "$i" && test -r "$i" ; then
	  echo "  load_config \${prefix}/themes/conf.d/`echo $i | sed 's,.*/,,'`"
        fi
      ;;
    esac
  done

  echo "  load_config \${prefix}/themes/\${theme_name}/theme \${prefix}/themes/custom/theme_\${theme_name}"

  if [ "x$gfxterm" = x1 ]; then
    cat << EOF
  insmod ${GRUB_VIDEO_BACKEND}
  insmod png
  insmod jpeg
  set gfxfont="Unifont Regular 16"
  menu_region.gfx
  vmenu resolution_menu
EOF
  fi

  cat << EOF
  controller.ext
fi
EOF

  gfxterm=0
  term_output=
  for x in ${GRUB_TERMINAL_OUTPUT}; do
    if [ xgfxterm != "x$x" ]; then
      term_output=${term_output} $x
    fi
  done
  GRUB_TERMINAL_OUTPUT=${term_output}
fi

if [ "x$gfxterm" = x1 ]; then
    # Make the font accessible
    prepare_grub_to_access_device `${grub_probe} --target=device "${GRUB_FONT_PATH}"`

    cat << EOF
if loadfont `make_system_path_relative_to_its_root "${GRUB_FONT_PATH}"` ; then
  insmod gfxterm
  insmod ${GRUB_VIDEO_BACKEND}
EOF
    if [ "x$GRUB_THEME" != x ] && [ -f "$GRUB_THEME" ] \
	&& is_path_readable_by_grub "$GRUB_THEME"; then
	echo "Found theme: $GRUB_THEME" >&2
	prepare_grub_to_access_device `${grub_probe} --target=device "$GRUB_THEME"` | sed -e "s/^/  /"
	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`
EOF
    elif [ "x$GRUB_BACKGROUND" != x ] && [ -f "$GRUB_BACKGROUND" ] \
	    && is_path_readable_by_grub "$GRUB_BACKGROUND"; then
	echo "Found background: $GRUB_BACKGROUND" >&2
	case "$GRUB_BACKGROUND" in
	    *.png)         reader=png ;;
	    *.tga)         reader=tga ;;
	    *.jpg|*.jpeg)  reader=jpeg ;;
	    *)             echo "Unsupported image format" >&2; exit 1 ;;
	esac
	prepare_grub_to_access_device `${grub_probe} --target=device "$GRUB_BACKGROUND"` | sed -e "s/^/  /"
	cat << EOF
  insmod $reader
  background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"`
EOF
    fi
    cat << EOF
fi
EOF
fi

case x${GRUB_TERMINAL_INPUT} in
  x)
    # Just use the native terminal
  ;;
  x*)
    cat << EOF
if terminal_input ${GRUB_TERMINAL_INPUT} ; then true ; else
  # For backward compatibility with versions of terminal.mod that don't
  # understand terminal_input
  terminal ${GRUB_TERMINAL_INPUT}
fi
EOF
  ;;
esac

case x${GRUB_TERMINAL_OUTPUT} in
  x)
    # Just use the native terminal
  ;;
  x*)
    cat << EOF
if terminal_output ${GRUB_TERMINAL_OUTPUT} ; then true ; else
  # For backward compatibility with versions of terminal.mod that don't
  # understand terminal_output
  terminal ${GRUB_TERMINAL_OUTPUT}
fi
EOF
  ;;
esac

# Gettext variables and module
if [ "x${LANG}" != "xC" ] ; then
    prepare_grub_to_access_device $(${grub_probe} --target=device ${locale_dir})
  cat << EOF
set locale_dir=(\$root)$(make_system_path_relative_to_its_root ${locale_dir})
set lang=${grub_lang}
insmod gettext
EOF
fi

if [ "x${GRUB_HIDDEN_TIMEOUT}" != "x" ] ; then
  if [ "x${GRUB_HIDDEN_TIMEOUT_QUIET}" = "xtrue" ] ; then
    verbose=
  else
    verbose=" --verbose"
  fi
  cat << EOF
if sleep$verbose --interruptible ${GRUB_HIDDEN_TIMEOUT} ; then
  set timeout=${GRUB_TIMEOUT}
fi
EOF
else
  cat << EOF
set timeout=${GRUB_TIMEOUT}
EOF
fi

# Play an initial tune
if [ "x${GRUB_INIT_TUNE}" != "x" ] ; then
  cat << EOF
insmod play
play ${GRUB_INIT_TUNE}
EOF
fi
