#! /bin/sh
set -e

# Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012  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/>.

prefix="/usr"
exec_prefix="${prefix}"
datarootdir="${prefix}/share"
bindir="${exec_prefix}/bin"

libdir="${exec_prefix}/lib"
PACKAGE_NAME=GRUB2
PACKAGE_TARNAME=grub2
PACKAGE_VERSION=2.00
datadir="${datarootdir}"
if [ "x$pkgdatadir" = x ]; then
    pkgdatadir="${datadir}/grub2"
fi

self=`basename $0`

export TEXTDOMAIN=grub2
export TEXTDOMAINDIR="${datarootdir}/locale"

. "${pkgdatadir}/grub-mkconfig_lib"

modules=

pkglib_DATA="moddep.lst command.lst fs.lst partmap.lst parttool.lst \
handler.lst video.lst crypto.lst terminal.lst"

grub_mkimage="${bindir}/grub2-mkimage"

grub_compress_file () {
    if [ "$compressor" != "" ] ; then
        "$compressor" $compressor_opts "$1" > "$2"
    else
        cp -f "$1" "$2"
    fi
}

grub_install_files () {
    grub_install_files_source_directory="$1"
    grub_install_files_target_directory="$2"
    grub_install_files_platform="$3"
    
    mkdir -p "${grub_install_files_target_directory}"/"${grub_install_files_platform}"
    
    for file in "${grub_install_files_target_directory}"/*.mod \
"${grub_install_files_target_directory}"/*.lst \
"${grub_install_files_target_directory}"/*.img \
"${grub_install_files_target_directory}"/efiemu??.o \
"${grub_install_files_target_directory}"/"${grub_install_files_platform}"/*.mod \
"${grub_install_files_target_directory}"/"${grub_install_files_platform}"/*.lst \
"${grub_install_files_target_directory}"/"${grub_install_files_platform}"/*.img \
"${grub_install_files_target_directory}"/"${grub_install_files_platform}"/efiemu??.o;
    do
	if test -f "$file" && [ "`basename $file`" != menu.lst ]; then
	    rm -f "$file" || exit 1
	fi
    done

    if [ x"$install_modules" = xall ]; then
	for file in "${grub_install_files_source_directory}/"*.mod; do
	    grub_compress_file "$file" "${grub_install_files_target_directory}"/"${grub_install_files_platform}/$(basename "$file")"
	done
    else
	modules1=
	modules2="$install_modules"
	while [ x"$modules2" != x ]; do
	    modules3=
	    for x in $modules2; do
		modules3="$modules3 $(grep "^$x:" "${grub_install_files_source_directory}/moddep.lst" | sed 's,^[^:]*:,,')"
	    done
	    modules1="$modules1 $modules2"
	    modules2="$modules3"
	done
	for file in $(echo "$modules1" | sed 's, ,\n,g' |sort -u); do
	    grub_compress_file "${grub_install_files_source_directory}/$file.mod" "${grub_install_files_target_directory}"/"${grub_install_files_platform}/$file.mod"
	done
    fi
    
    for file in ${pkglib_DATA} efiemu32.o efiemu64.o; do
	if test -f "${grub_install_files_source_directory}/${file}"; then
            grub_compress_file "${grub_install_files_source_directory}/${file}" "${grub_install_files_target_directory}"/"${grub_install_files_platform}/${file}"
	fi
    done
    
    # Copy gettext files
    mkdir -p "${grub_install_files_target_directory}"/locale

    for file in "${grub_install_files_target_directory}"/locale/*.mo; do
	if test -f "$file"; then
	    rm -f "$file" || exit 1
	fi
    done

    if [ x"$install_locales" = xall ]; then
	for file in "${grub_install_files_source_directory}"/po/*.mo; do
	    if test -f "$file"; then
		grub_compress_file "$file" "${grub_install_files_target_directory}"/locale/"$(basename "$file")"
	    fi
	done
	for dir in "${localedir}"/*; do
	    if test -f "$dir/LC_MESSAGES/grub2.mo" && ! test -f "${grub_install_files_target_directory}"/locale/"${dir##*/}.mo"; then
		grub_compress_file "$dir/LC_MESSAGES/grub2.mo" "${grub_install_files_target_directory}"/locale/"${dir##*/}.mo"
	    fi
	done
    else
	for locale in $install_locales; do
	    if test -f "${grub_install_files_source_directory}"/po/$locale.mo; then
		grub_compress_file "${grub_install_files_source_directory}"/po/locale.mo "${grub_install_files_target_directory}"/locale/$locale.mo
	    elif test -f "${localedir}/$locale/LC_MESSAGES/grub2.mo"; then
		grub_compress_file "${localedir}/$locale/LC_MESSAGES/grub2.mo" "${grub_install_files_target_directory}"/locale/$locale.mo
	    fi
	done
    fi
    for theme in ${install_themes} ; do
	if test -f "${pkgdatadir}"/themes/"${theme}"/theme.txt; then
	    mkdir -p "${grub_install_files_target_directory}"/themes/"${theme}"
	    for file in "${pkgdatadir}"/themes/"${theme}"/*; do
		grub_compress_file "$file" "${grub_install_files_target_directory}"/themes/"${theme}"/"$(basename "$file")"
	    done
	fi
    done

    for font in ${install_fonts} ; do
	if test -f "${pkgdatadir}"/"$font".pf2; then
	    mkdir -p "${grub_install_files_target_directory}"/fonts
	    grub_compress_file "${pkgdatadir}"/"$font".pf2 "${grub_install_files_target_directory}"/fonts/"$font".pf2
	fi
    done
}

grub_print_install_files_help () {
    print_option_help "--modules=$(gettext "MODULES")" "$(gettext "pre-load specified modules MODULES")"
    print_option_help "--install-modules=$(gettext "MODULES")" "$(gettext "install only MODULES and their dependencies [default=all]")"
    print_option_help "--themes=THEMES" "$(gettext_printf "install THEMES [default=%s]" "starfield")"
    print_option_help "--fonts=FONTS" "$(gettext_printf "install FONTS [default=%s]" "unicode")"
    print_option_help "--locales=LOCALES" "$(gettext_printf "install only LOCALES [default=all]")"
    print_option_help "--compress[=no,xz,gz,lzo]" "$(gettext "compress GRUB files [optional]")"
    # TRANSLATORS: platform here isn't identifier. It can be translated.
    dir_msg="$(gettext_printf "use images and modules under DIR [default=%s/<platform>]" "${libdir}/grub2")"
    print_option_help "-d, --directory=$(gettext "DIR")" "$dir_msg"
    print_option_help  "--grub-mkimage=$(gettext "FILE")" "$(gettext "use FILE as grub-mkimage")"
    print_option_help "-v, --version" "$(gettext "print the version information and exit")"
}

install_modules=all
install_themes=starfield
install_fonts=unicode
install_locales=all
compress=no
grub_decompression_module=""
compressor=""
compressor_opts=""
source_directory=""

argument () {
  opt=$1
  shift

  if test $# -eq 0; then
      gettext_printf "%s: option requires an argument -- \`%s'\n" "$0" "$opt" 1>&2
      exit 1
  fi
  echo $1
}

grub_parse_compress () {
    compress="$1"
    case x"$compress" in
	xno) ;;
	xgz)
	    compressor=`which gzip || true`
	    grub_decompression_module="gzio"
	    compressor_opts="--best --stdout";;
	xxz)
	    compressor=`which xz || true`
	    grub_decompression_module="xzio gcry_crc"
	    compressor_opts="--lzma2=dict=128KiB --check=none --stdout";;
	xlzo)
	    compressor=`which lzop || true`
	    grub_decompression_module="lzopio adler32 gcry_crc"
	    compressor_opts="-9 -c";;
	*)
	    gettext_printf "Unrecognized compression \`%s'\n" "$compress" 1>&2
	    usage
	    exit 1
    esac
}

grub_process_install_options () {
    option=$1
    shift

    grub_process_install_options_consumed=0

    case "$option" in
	--install-modules)
	    install_modules=`argument $option "$@"`; grub_process_install_options_consumed=2; return ;;
	--install-modules=*)
            install_modules=`echo "$option" | sed 's/--install-modules=//'`; grub_process_install_options_consumed=1; return ;;
	--themes)
	    install_themes=`argument $option "$@"`; grub_process_install_options_consumed=2; return ;;
	--themes=*)
            install_themes=`echo "$option" | sed 's/--themes=//'`; grub_process_install_options_consumed=1; return ;;
	--fonts)
	    install_fonts=`argument $option "$@"`; grub_process_install_options_consumed=2; return ;;
	--fonts=*)
            install_fonts=`echo "$option" | sed 's/--fonts=//'`; grub_process_install_options_consumed=1; return ;;
	--locales)
	    install_locales=`argument $option "$@"`; grub_process_install_options_consumed=2; return ;;
	--locales=*)
            install_locales=`echo "$option" | sed 's/--locales=//'`; grub_process_install_options_consumed=1; return ;;
	--compress)
	    grub_parse_compress `argument $option "$@"`; grub_process_install_options_consumed=2; return ;;
        --compress=*)
            grub_parse_compress `echo "${option}" | sed 's/--compress=//'`; grub_process_install_options_consumed=1; return ;;
	--directory | -d)
	    source_directory=`argument $option "$@"`; grub_process_install_options_consumed=2 ;;
	--directory=*)
	    source_directory=`echo "$option" | sed 's/--directory=//'` grub_process_install_options_consumed=1;;

	# For backwards compatibility
	--override-directory)
	    source_directory=`argument $option "$@"`; grub_process_install_options_consumed=2 ;;
	--override-directory=*)
	    source_directory=`echo "$option" | sed 's/--override-directory=//'` grub_process_install_options_consumed=1;;

	--grub-mkimage)
	    grub_mkimage=`argument $option "$@"`; grub_process_install_options_consumed=2 ;;
        --grub-mkimage=*)
	    grub_mkimage=`echo "$option" | sed 's/--grub-mkimage=//'`;grub_process_install_options_consumed=1 ;;
        --modules)
	    modules=`argument $option "$@"`; grub_process_install_options_consumed=2;;
        --modules=*)
	    modules=`echo "$option" | sed 's/--modules=//'` grub_process_install_options_consumed=1;;
        -v | --version)
	    echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
	    exit 0 ;;
    esac
}

export grub_decompression_module
#!/bin/sh

# Make GRUB rescue image
# Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012  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/>.

# Initialize some variables.

compression=auto
format=
source=

# Usage: usage
# Print the usage.
usage () {
    formats="i386-coreboot i386-multiboot i386-pc i386-pc-pxe i386-efi i386-ieee1275 i386-qemu x86_64-efi mipsel-yeeloong-flash mipsel-fuloong2f-flash mipsel-loongson-elf powerpc-ieee1275 sparc64-ieee1275-raw sparc64-ieee1275-aout ia64-efi mips-arc mipsel-qemu_mips-elf mips-qemu_mips-flash  mipsel-qemu_mips-flash  mips-qemu_mips-elf"
    gettext_printf "Usage: %s [OPTION] SOURCE...\n" "$self"
    gettext "Generate a standalone image (containing all modules) in the selected format"
    echo
    print_option_help "-h, --help" "$(gettext "print this message and exit")"
    print_option_help "-o, --output=$(gettext FILE)" "$(gettext "save output in FILE [required]")"
    print_option_help "-O, --format=$(gettext "FORMAT")" "$(gettext "generate an image in FORMAT")"; echo
    print_option_help "" "$(gettext "available formats:") $formats"
    echo
    print_option_help "-C, --compression=(xz|none|auto)" "$(gettext "choose the compression to use for core image")"
    grub_print_install_files_help
    echo
    gettext "Report bugs to <bug-grub@gnu.org>."; echo
}

# Check the arguments.
while test $# -gt 0
do
    grub_process_install_options "$@"
    case "$grub_process_install_options_consumed" in
	1) shift; continue;;
	2) shift; shift; continue;;
    esac

    option=$1
    shift

    case "$option" in
    -h | --help)
	usage
	exit 0 ;;

    -o | --output)
	output_image=`argument $option "$@"`; shift ;;
    --output=*)
	output_image=`echo "$option" | sed 's/--output=//'` ;;

    --compression | -C)
	compression=`argument $option "$@"`; shift ;;
    --compression=*)
	compression=`echo "${option}" | sed 's/--compression=//'` ;;

    --format | -O)
	format=`argument $option "$@"`; shift ;;
    --format=*)
	format=`echo "${option}" | sed 's/--format=//'` ;;

    *)
	source="${source} ${option} $@"; break ;;
    esac
done

if [ "x${output_image}" = x ] ; then
  gettext "output file must be specified" >&2
  echo >&2
  usage
  exit 1
fi

if [ "x${format}" = x ] ; then
  gettext "Target format not specified (use the -O option)." >&2
  echo >&2
  exit 1
fi

if [ "x$source_directory" = x ] ; then
    cpu="`echo $format | awk -F - '{ print $1; }'`"
    platform="`echo $format | awk -F - '{ print $2; }'`"
    case "$platform" in
	yeeloong | fuloong | fuloong2f | fuloong2e)
	    platform=loongson ;;
    esac
    case "$cpu-$platform" in
	mips-loongson)
	    cpu=mipsel ;;
    esac
    source_directory="${libdir}/grub2/$cpu-$platform"
fi

. "${source_directory}"/modinfo.sh

set $grub_mkimage dummy
if test -f "$1"; then
    :
else
    echo "$1: Not found." 1>&2
    exit 1
fi

memdisk_dir="`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"`" || exit 1
grub_install_files "${source_directory}" "${memdisk_dir}"/boot/grub "${grub_modinfo_target_cpu}-${grub_modinfo_platform}"
for file in $source; do
    cp -f "$file" "${memdisk_dir}"/"$file";
done

memdisk_img=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1

(cd "${memdisk_dir}"; tar -cf - * $source) > "${memdisk_img}"
rm -rf "${memdisk_dir}"
"$grub_mkimage" -O "${format}" -C "$compression" -d "${source_directory}" -m "${memdisk_img}" -o "$output_image" --prefix='(memdisk)/boot/grub' memdisk tar $grub_decompression_module $modules
rm -rf "${memdisk_img}"

exit 0
