#!/bin/bash
# Super Grub Disk - supergrub-meta-mkrescue
# Copyright (C) 2009,2010,2011,2012,2013,2014,2015  Adrian Gibanel Lopez.
#
# Super Grub Disk 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.
#
# Super Grub Disk 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 Super Grub Disk.  If not, see <http://www.gnu.org/licenses/>.

SUPER_GRUB2_DISK_FILE_PREFIX="supergrub2"

SGD2_SOURCE_DIR="$(pwd)"
SG2D_DIR="${SGD2_SOURCE_DIR}"


function build_sg2d () {

  ./supergrub-mkrescue \
      -o="${SG2D_ISO_RELEASE_DIR}/${SUPER_GRUB2_DISK_FILE_PREFIX}\
-classic-${sgrub_version}-${TARGET_PLATFORM}-${TARGET_DEVICE}.iso" \
      --grub-mkrescue-binary=\
"${GRUB_MKRESCUE_BINARY}"\
      --grub-mkfont-binary=\
"${GRUB_MKFONT_BINARY}"
}

function build_sg2d_sb () {

  ./supergrub-mksbrescue \
      -o="${SG2D_ISO_RELEASE_DIR}/${SUPER_GRUB2_DISK_FILE_PREFIX}\
-${sgrub_version}-${TARGET_PLATFORM}-${TARGET_DEVICE}.iso" \
      --grub-mkrescue-binary=\
"${GRUB_MKRESCUE_BINARY}"\
      --grub-mkfont-binary=\
"${GRUB_MKFONT_BINARY}"\
      --userid="$(id -u)" \
      --groupid="$(id -g)" \
      --efi-platforms=\
"${EFI_PLATFORMS}"
}

function build_sg2d_standalone () {

  ./supergrub-mkstandalone  \
    -O=${STANDALONE_FORMAT} \
    -o="${SG2D_ISO_RELEASE_DIR}/${SUPER_GRUB2_DISK_FILE_PREFIX}\
-classic-${sgrub_version}-${TARGET_PLATFORM}-${TARGET_DEVICE}.EFI" \
    --grub-mkstandalone-binary=\
"${GRUB_MKSTANDALONE_BINARY}"\
      --grub-mkfont-binary=\
"${GRUB_MKFONT_BINARY}"

}

function build_sg2d_sb_usb () {

  sudo ./supergrub-mksbusb \
      -o="${SG2D_ISO_RELEASE_DIR}/${SUPER_GRUB2_DISK_FILE_PREFIX}\
-${sgrub_version}-${TARGET_PLATFORM}-${TARGET_DEVICE}.img" \
      --grub-install-binary=\
"${GRUB_INSTALL_BINARY}"\
      --grub-mkfont-binary=\
"${GRUB_MKFONT_BINARY}"\
      --userid="$(id -u)" \
      --groupid="$(id -g)" \
      --efi-platforms=\
"${EFI_PLATFORMS}"
}

function build_sg2d_usb () {

  sudo ./supergrub-mkusb \
      -o="${SG2D_ISO_RELEASE_DIR}/${SUPER_GRUB2_DISK_FILE_PREFIX}\
-classic-${sgrub_version}-${TARGET_PLATFORM}-${TARGET_DEVICE}.img" \
      --grub-install-binary=\
"${GRUB_INSTALL_BINARY}"\
      --grub-mkfont-binary=\
"${GRUB_MKFONT_BINARY}"\
      --userid="$(id -u)" \
      --groupid="$(id -g)" \
      --efi-platforms=\
"${EFI_PLATFORMS}"
}

function grub2_sg2d_hybrid_build () {

  # Super Grub2 Disk - Hybrid build
  TARGET_DEVICE="CD"
  TARGET_PLATFORM="multiarch"
  GRUB_MKRESCUE_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_HYBRID}\
/bin/grub-mkrescue\
"
  GRUB_MKFONT_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_HYBRID}\
/bin/grub-mkfont\
"
  build_sg2d

}

function grub2_sg2d_sbhybrid_build () {

  # Super Grub2 Disk - Hybrid build
  TARGET_DEVICE="CD"
  TARGET_PLATFORM="multiarch"
  EFI_PLATFORMS="${SG2D_SECUREBOOT_EFI_PLATFORMS}"
  GRUB_MKRESCUE_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_HYBRID}\
/bin/grub-mkrescue\
"
  GRUB_MKFONT_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_HYBRID}\
/bin/grub-mkfont\
"
  build_sg2d_sb

}

function grub2_sg2d_i386_pc_build () {

  # Super Grub2 Disk - i386 - pc build
  TARGET_DEVICE="CD"
  TARGET_PLATFORM="i386_pc"
  GRUB_MKRESCUE_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_I386_PC}\
/bin/grub-mkrescue\
"
  GRUB_MKFONT_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_I386_PC}\
/bin/grub-mkfont\
"
  build_sg2d

}

function grub2_sg2d_x86_64_efi_build () {

  # Super Grub2 Disk - x86_64 - efi build
  TARGET_DEVICE="CD"
  TARGET_PLATFORM="x86_64_efi"
  GRUB_MKRESCUE_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_X86_64_EFI}\
/bin/grub-mkrescue\
"
  GRUB_MKFONT_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_X86_64_EFI}\
/bin/grub-mkfont\
"
  build_sg2d

  TARGET_DEVICE="STANDALONE"
  TARGET_PLATFORM="x86_64_efi"
  STANDALONE_FORMAT="x86_64-efi"
  GRUB_MKSTANDALONE_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_X86_64_EFI}\
/bin/grub-mkstandalone\
"
  GRUB_MKFONT_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_X86_64_EFI}\
/bin/grub-mkfont\
"
  build_sg2d_standalone

}

function grub2_sg2d_i386_efi_build () {

  # Super Grub2 Disk - i386 - efi build
  TARGET_DEVICE="CD"
  TARGET_PLATFORM="i386_efi"
  GRUB_MKRESCUE_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_I386_EFI}\
/bin/grub-mkrescue\
"
  GRUB_MKFONT_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_I386_EFI}\
/bin/grub-mkfont\
"
  build_sg2d
  TARGET_DEVICE="STANDALONE"
  TARGET_PLATFORM="i386_efi"
  STANDALONE_FORMAT="i386-efi"
  GRUB_MKSTANDALONE_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_I386_EFI}\
/bin/grub-mkstandalone\
"
  GRUB_MKFONT_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_I386_EFI}\
/bin/grub-mkfont\
"
  build_sg2d_standalone
}

function grub2_sg2d_sbusb_hybrid_build () {

  # Super Grub2 Disk - Secure Boot Hybrid build for USB
  TARGET_PLATFORM="multiarch"
  TARGET_DEVICE="USB"
  EFI_PLATFORMS="${SG2D_SECUREBOOT_EFI_PLATFORMS}"
  GRUB_INSTALL_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_HYBRID}\
/sbin/grub-install\
"
  GRUB_MKFONT_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_HYBRID}\
/bin/grub-mkfont\
"
  build_sg2d_sb_usb

}

function grub2_sg2d_usb_hybrid_build () {

  # Super Grub2 Disk - Hybrid build for USB
  TARGET_PLATFORM="multiarch"
  TARGET_DEVICE="USB"
  EFI_PLATFORMS="x64 ia32"
  GRUB_INSTALL_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_HYBRID}\
/sbin/grub-install\
"
  GRUB_MKFONT_BINARY="\
${SG2D_GRUB_INSTALLATION_PREFIX}\
/${SG2D_GRUB_INSTALLATION_HYBRID}\
/bin/grub-mkfont\
"
  build_sg2d_usb

}

#function grub2_sg2d_coreboot_build () {
#
#  # Super Grub2 Disk - coreboot build
#  TARGET_PLATFORM="coreboot"
#  build_sg2d
#
#  STANDALONE_FORMAT_STR="i386_coreboot"
#  STANDALONE_FORMAT="i386-coreboot"
#  STANDALONE_EXTENSION="img"
#  build_sg2d_standalone
#
#}

#function grub2_sg2d_ieee1275_build () {
#
#  # Super Grub2 Disk - ieee1275 build
#  TARGET_PLATFORM="ieee1275"
#  build_sg2d
#
#  STANDALONE_FORMAT_STR="i386_ieee1275"
#  STANDALONE_FORMAT="i386-ieee1275"
#  STANDALONE_EXTENSION="img"
#  build_sg2d_standalone
#
#}


# Needed for getting SG2D version
source menus/sgd/version.cfg

source grub-build-config

SG2D_RELEASE_DIR="${SGD2_SOURCE_DIR}/releases/${sgrub_version}"
SG2D_ISO_RELEASE_DIR="${SG2D_RELEASE_DIR}/super_grub2_disk_${sgrub_version}"

if [ ! -d "${SG2D_ISO_RELEASE_DIR}" ] ; then
  mkdir --parents "${SG2D_ISO_RELEASE_DIR}"
fi

grub2_sg2d_hybrid_build

#grub2_sg2d_sbhybrid_build

grub2_sg2d_x86_64_efi_build

grub2_sg2d_i386_pc_build

grub2_sg2d_i386_efi_build

grub2_sg2d_usb_hybrid_build

grub2_sg2d_sbusb_hybrid_build

#grub2_sg2d_coreboot_build

#grub2_sg2d_ieee1275_build
