#!/bin/sh -e
#
#  Copyright (C) 2015 ScyllaDB

. /etc/os-release

if [ ! -f /etc/default/grub ]; then
    echo "Unsupported bootloader"
    exit 1
fi
if [ "`grep hugepagesz /etc/default/grub`" != "" ] || [ "`grep hugepages /etc/default/grub`" != "" ]; then
    sed -e "s#hugepagesz=2M ##" /etc/default/grub > /tmp/grub
    mv /tmp/grub /etc/default/grub
    sed -e "s#hugepages=[0-9]* ##" /etc/default/grub > /tmp/grub
    mv /tmp/grub /etc/default/grub
fi
sed -e "s#^GRUB_CMDLINE_LINUX=\"#GRUB_CMDLINE_LINUX=\"hugepagesz=2M hugepages=$NR_HUGEPAGES #" /etc/default/grub > /tmp/grub
mv /tmp/grub /etc/default/grub
if [ "$ID" = "ubuntu" ]; then
    grub-mkconfig -o /boot/grub/grub.cfg
else
    grub2-mkconfig -o /boot/grub2/grub.cfg
fi
