#!/usr/bin/env bash
name='grub'
release='1'
version='2.06'
url='https://ftp.gnu.org/gnu/grub'
description='GNU GRand Unified Bootloader'
email='parduscix@yandex.ru'
maintainer='sulincix'
license=('GPLv3')
source=("https://ftp.gnu.org/gnu/grub/grub-$version.tar.xz")
depends=()
makedepends=()
md5sums=('cf0fd928b1e5479c8108ee52cb114363')
group=()
uses=()
arch=('x86_64')
dontstrip=1

target=(efi ia32 bios)

unset CFLAGS
unset CXXFLAGS

get_grub_opt(){
    echo -n "--disable-efiemu "
    if [[ "$1" == "efi" ]] ; then
        echo -n "--with-platform=efi --target=x86_64"
    elif [[ "$1" == "ia32" ]] ; then
        echo -n "--with-platform=efi --target=i386"
    elif [[ "$1" == "bios" ]] ; then
        echo -n "--with-platform=pc"
    fi
}

prepare(){
    for tgt in ${target[@]} ; do
        cp -prfv $name-$version $tgt
    done
}

setup(){
    for tgt in ${target[@]} ; do
        cd $tgt
        autoreconf -fvi
        ./configure --prefix=/usr \
            --sysconfdir=/etc \
            --libdir=/usr/lib64/ \
            --disable-nls \
            --disable-werror \
            --disable-grub-themes \
            $(get_grub_opt $tgt)
        cd ..
    done
}

build(){
    for tgt in ${target[@]} ; do
        make $jobs -C $tgt
    done
}

package(){
    for tgt in ${target[@]} ; do
        make $jobs -C $tgt install
    done
    # default grub config
    mkdir -p $DESTDIR/etc/default
    {
	  echo 'GRUB_DISTRIBUTOR=""'
    echo 'GRUB_TERMINAL_OUTPUT=console'
    echo 'GRUB_CMDLINE_LINUX_DEFAULT="quiet"'
    echo 'GRUB_CMDLINE_LINUX=""'
	  echo 'GRUB_DEFAULT=0'
	  echo 'GRUB_TIMEOUT=0'
	  echo 'GRUB_DISABLE_SUBMENU=y'
    echo 'GRUB_DISABLE_OS_PROBER=true'
	  echo 'GRUB_DISABLE_RECOVERY=true'
    } > $DESTDIR/etc/default/grub
}