# Exit on error:
set -e

wget http://ftpmirror.gnu.org/grub/grub-2.00.tar.gz
tar xf grub-2.00.tar.gz
cd grub-2.00

# Fix:
# ./stdio.h:456:1: error: 'gets' undeclared here (not in a function)
sed -i.orig '/gets is a security hole/d' grub-core/gnulib/stdio.in.h

./configure --prefix=

# Fix
# grub_script.yy.c:2296:17: error: comparison between signed and
# unsigned integer expressions [-Werror=sign-compare]
cd grub-core/script
EXP1='\n#pragma GCC diagnostic ignored \"-Wsign-compare\"'
sed -i.1 '/Wunsafe-loop-optimizations\"/s,$,'"$EXP1"',' yylex.l

# Fix
# grub_script.yy.c:2351:13: error: 'yy_fatal_error' defined but not
# used [-Werror=unused-function]
EXP2='\n#pragma GCC diagnostic ignored \"-Wunused-function\"'
sed -i.2 '/Wsign-compare\"/s,$,'"$EXP2"',' yylex.l
cd ../..

make
make install
cd ..
rm -rf grub-2.00
rm grub-2.00.tar.gz

mkdir /boot/grub

cat > /boot/grub/grub.cfg << 'EOF'
insmod ext2
set default=0
set timeout=1

menuentry 'GLLFSC 1.7 on /dev/sda1 (3.10.108-gnu)' {
    echo 'Loading linux-libre-3.10.108-gnu...'
    linux (hd0,1)/boot/vmlinuz-3.10.108-gnu root=/dev/sda1 ro quiet
}

menuentry 'OS2 on /dev/sda3 (5.10.0-17-686)' {
    echo 'Loading linux-5.10.0-17-686...'
    linux (hd0,3)/boot/vmlinuz-5.10.0-17-686 root=/dev/sda3 ro quiet
    echo 'Loading initial ramdisk...'
    initrd (hd0,3)/boot/initrd.img-5.10.0-17-686
}

menuentry 'GLLFSC 1.8 on /dev/sda4 (3.10.108-gnu)' {
    echo 'Loading linux-libre-3.10.108-gnu...'
    linux (hd0,4)/boot/vmlinuz-3.10.108-gnu root=/dev/sda4 ro quiet
}
EOF

echo
echo Now:
echo 1. Backup your data.
echo 2. Edit /boot/grub/grub.cfg to your taste.
echo ..  \(Run grub-mkconfig for an automatically-generated grub.cfg\).
echo 3. Add the Boot flag to the GLLFSC partition.
echo 4. Run: grub-install /dev/sda.
echo

exit 0
