#!/bin/bash

# Set kernel command line to disable 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 off"
    else
        sed -i -e "/^append/s/$/ maxcpus=1/" -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 off"
    else
        sed -i -e "/# defoptions=/s/$/ maxcpus=1/" /boot/grub/menu.lst
        update-grub
    fi
elif [ "${G2}" = "1" ];then
    if grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*maxcpus=1' /etc/default/grub;then
        echo "Already off"
    else
        sed -i -e '/^GRUB_CMDLINE_LINUX_DEFAULT=/s/" *$/ maxcpus=1"/' -e '/^GRUB_CMDLINE_LINUX_DEFAULT=/s/  */ /g' /etc/default/grub
       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
