# Install GRUB on your drive.
# Copyright (C) 1999,2000,2001,2002,2003,2004,2005,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/>.

# Initialize some variables.
prefix="@prefix@"
exec_prefix="@exec_prefix@"
datarootdir="@datarootdir@"
bindir="@bindir@"
libdir="@libdir@"
PACKAGE_NAME=@PACKAGE_NAME@
PACKAGE_TARNAME=@PACKAGE_TARNAME@
PACKAGE_VERSION=@PACKAGE_VERSION@
host_os=@host_os@
datadir="@datadir@"
if [ "x$pkgdatadir" = x ]; then
    pkgdatadir="${datadir}/@PACKAGE@"
fi

self=`basename $0`

grub_mkimage="${bindir}/@grub_mkimage@"
rootdir=/srv/tftp
modules=

no_floppy=
recheck=no
debug=no
debug_image=
subdir="`echo '/@bootdirname@/@grubdirname@' | sed "s,//*,/,g"`"
pc_dir="${libdir}/@PACKAGE@/i386-pc"
ppc_dir="${libdir}/@PACKAGE@/powerpc-ieee1275"
sparc_dir="${libdir}/@PACKAGE@/sparc64-ieee1275"
i386_ieee1275_dir="${libdir}/@PACKAGE@/i386-ieee1275"
efi32_dir="${libdir}/@PACKAGE@/i386-efi"
efi64_dir="${libdir}/@PACKAGE@/x86_64-efi"
itanium_dir="${libdir}/@PACKAGE@/ia64-efi"

export TEXTDOMAIN=@PACKAGE@
export TEXTDOMAINDIR="@localedir@"

. "${pkgdatadir}/grub-mkconfig_lib"

# Usage: usage
# Print the usage.
usage () {
    gettext_printf "Usage: %s [OPTION]\n" "$self"
    gettext; echo "Install GRUB on your drive."; echo
    echo
    print_option_help "-h, --help" "$(gettext "print this message and exit")"
    print_option_help "-v, --version" "$(gettext "print the version information and exit")"
    print_option_help "--modules=$(gettext "MODULES")" "$(gettext "pre-load specified modules MODULES")"
    grub_print_install_files_help
    print_option_help "--net-directory=$(gettext "DIR")" "$(gettext "root directory of TFTP server")"
    print_option_help "--subdir=$(gettext "DIR")" "$(gettext "relative subdirectory on network server")"
    print_option_help "--grub-mkimage=$(gettext "FILE")" "$(gettext "use FILE as grub-mkimage")"
    # TRANSLATORS: platform here isn't identifier. It can be translated.
    dir_msg="$(gettext_printf "use images and modules under DIR [default=%s/<platform>]" "${libdir}/@PACKAGE@")"
    print_option_help "-d, --directory=$(gettext "DIR")" "$dir_msg"
    echo
    gettext_printf "%s copies GRUB images into net_directory/subdir/target_cpu-platform\n" "$self" 
    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 ;;
    -v | --version)
	echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
	exit 0 ;;

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

    --net-directory)
	rootdir=`argument $option "$@"`; shift;;
    --net-directory=*)
	rootdir=`echo "$option" | sed 's/--net-directory=//'` ;;

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

    --grub-mkimage)
	grub_mkimage=`argument $option "$@"`; shift;;
    --grub-mkimage=*)
	grub_mkimage=`echo "$option" | sed 's/--grub-mkimage=//'` ;;

    # This is an undocumented feature...
    --debug)
	debug=yes ;;
    --debug-image)
	debug_image=`argument $option "$@"`; shift;;
    --debug-image=*)
	debug_image=`echo "$option" | sed 's/--debug-image=//'` ;;

    --directory | -d)
	source_directory=`argument $option "$@"`; shift ;;
    --directory=*)
	source_directory=`echo "$option" | sed 's/--directory=//'` ;;

    -*)
	gettext_printf "Unrecognized option \`%s'\n" "$option" 1>&2
	usage
	exit 1
	;;
    *)
	gettext_printf "Unknown extra argument \`%s'." "$option" 1>&2
	echo 1>&2
	usage
	exit 1
	;;
    esac
done

set $grub_mkimage dummy
if test -f "$1"; then
    :
else
    gettext_printf "%s: Not found.\n" "$1" 1>&2
    exit 1
fi

# Create the GRUB directory if it is not present.
mkdir -p "${rootdir}/${subdir}" || exit 1

process_input_dir ()
{ 
    input_dir="$1"
    platform="$2"
    grubdir="${rootdir}/${subdir}/${platform}"
    config_opt=

    grub_install_files "${input_dir}" "${rootdir}/${subdir}" "${platform}"

    rm -f "${grubdir}"/load.cfg

    if [ "x${debug_image}" != x ]; then
       echo "set debug='${debug_image}'" >> ${grubdir}/load.cfg
       config_opt="-c ${grubdir}/load.cfg "
    fi

    prefix="/${subdir}";
    case "${platform}" in
    	 i386-pc) mkimage_target=i386-pc-pxe;
	 	  netmodules="pxe";
		  ext=0 ;;
    	 sparc64-ieee1275) mkimage_target=sparc64-ieee1275-aout;
	 	  netmodules="ofnet";
		  ext=img ;;
    	 *-ieee1275) mkimage_target="${platform}";
	 	  netmodules="ofnet";
		  ext=elf ;;
    	 *-efi) mkimage_target="${platform}";
	 	  netmodules="efinet";
		  ext=efi ;;
    	 *) gettext_printf "Unsupported platform %s\n" ${platform};
    	    exit 1;;
    esac

    cat << EOF > ${grubdir}/grub.cfg
source ${subdir}/grub.cfg
EOF

    "$grub_mkimage" ${config_opt} -d "${input_dir}" -O ${mkimage_target} "--output=${grubdir}/core.$ext" "--prefix=$prefix" $modules $netmodules tftp || exit 1
    # TRANSLATORS: First %s is replaced by platform name. Second one by filename.
    gettext_printf "Netboot directory for %s created. Configure your DHCP server to point to %s\n" "${platform}" "${subdir}/${platform}/core.$ext"
}

if [ "${source_directory}" = "" ] ; then
    if test -e "${pc_dir}" ; then
        process_input_dir "${pc_dir}" i386-pc
    fi
    if test -e "${ppc_dir}" ; then
        process_input_dir "${ppc_dir}" powerpc-ieee1275
    fi
    if test -e "${sparc_dir}" ; then
        process_input_dir ${sparc_dir} sparc64-ieee1275
    fi
    if test -e "${i386_ieee1275_dir}" ; then
        process_input_dir "${i386_ieee1275_dir}" i386-ieee1275
    fi
    if test -e "${efi32_dir}" ; then
        process_input_dir "${efi32_dir}" i386-efi
    fi
    if test -e "${efi64_dir}" ; then
        process_input_dir "${efi64_dir}" x86_64-efi
    fi
    if test -e "${itanium_dir}" ; then
        process_input_dir "${itanium_dir}" ia64-efi
    fi
else
    . "${source_directory}"/modinfo.sh
    process_input_dir "${source_directory}" ${grub_modinfo_target_cpu}-${grub_modinfo_platform}
fi


# Bye.
exit 0