#!/bin/bash

# Set kernel command line to enable use of multiple cpus

CPU=$(cat /proc/cpuinfo |grep -m 1 'model name.*:'| sed -e 's/Intel(R)//'|sed -e 's/  / /g'|cut -d ' ' -f 3|cut -d '(' -f 1)


if dpkg-query -W -f '${Status}' grub 2>/dev/null| grep -q "install ok installed" ;then
        G1="1"
fi
if dpkg-query -W -f '${Status}' grub-pc 2>/dev/null| grep -q "install ok installed" ;then
        G2="1"
fi


if mount | grep -q '/boot.*(ro,';then
    mount -o remount,rw /boot
    RO=1
fi
    

if [ -e /boot/extlinux/extlinux.conf ];then
    if ! grep -q '^append.*maxcpus=1' /boot/extlinux/extlinux.conf;then
        echo "Already on"
    else
        sed -i -e '/^append/s/maxcpus=1//' -e 's/ *$//' /boot/extlinux/extlinux.conf
        if [ "${CPU}" = "Atom" ]; then
            if ! grep -q '^append.*acpi=force' /boot/extlinux/extlinux.conf ;then
        	sed -i -e '/^append/s/$/ acpi=force/' -e 's/ *$//' /boot/extlinux/extlinux.conf
            fi
        fi
        sed  -i -e '/^append/s/  */ /g' /boot/extlinux/extlinux.conf
    fi
elif [ "${G1}" = "1" ]; then
    if ! grep -q '# defoptions=.*maxcpus=1' /boot/grub/menu.lst;then
        echo "Already on"
    else
        sed -i -e '/# defoptions=/s/maxcpus=1//' -e 's/ *$//' /boot/grub/menu.lst
        if [ "${CPU}" = "Atom" ]; then
            if ! grep -q '# defoptions=.*acpi=force' /boot/grub/menu.lst ; then
        	sed -i -e '/# defoptions=/s/$/acpi=force/' -e 's/ *$//' /boot/grub/menu.lst
            fi
        fi
        update-grub
    fi
elif [ "${G2}" = "1" ];then
    if ! grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*maxcpus=1' /etc/default/grub;then
        echo "Already on"
    else
       sed -i -e '/GRUB_CMDLINE_LINUX_DEFAULT=/s/maxcpus=1//' -e '/^GRUB_CMDLINE_LINUX_DEFAULT=/s/  */ /g' /etc/default/grub
        if [ "${CPU}" = "Atom" ]; then
            if ! grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*acpi=force' /etc/default/grub ; then
        	sed -i -e '/^GRUB_CMDLINE_LINUX_DEFAULT=/s/"$/ acpi=force"/' -e 's/ *$//' /etc/default/grub
            fi
        fi
       update-grub2
    fi
else
    echo "I'm goofed Neither grub nor extlinux found"
fi

if [ "${RO}" = "1" ]; then
    mount -o remount,ro /boot
fi

exit 0
