From e4e56bf1fbe8c3e5e11ed7da9b5d27c54de9de67 Mon Sep 17 00:00:00 2001 From: Alex Bradbury Date: Fri, 13 Jul 2012 17:01:48 +0100 Subject: [PATCH] be more intelligent about inserting config options If a config option is already in the file but commented out, modify that line rather than inserting a new one. --- debian/control | 2 +- raspi-config | 30 ++++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/debian/control b/debian/control index 16e2f9c..b44d291 100644 --- a/debian/control +++ b/debian/control @@ -10,5 +10,5 @@ Homepage: https://github.com/asb/raspi-config Package: raspi-config Architecture: all -Depends: ${misc:Depends}, whiptail, parted +Depends: ${misc:Depends}, whiptail, parted, lua5.1 Description: Simple configuration for Raspberry Pi diff --git a/raspi-config b/raspi-config index 7165e44..4e602ba 100755 --- a/raspi-config +++ b/raspi-config @@ -70,6 +70,28 @@ EOF The filesystem will be enlarged upon the next reboot" 20 60 2 } +set_config_var() { + lua - "$1" "$2" "$3" < "$3.bak" +local key=assert(arg[1]) +local value=assert(arg[2]) +local fn=assert(arg[3]) +local file=assert(io.open(fn)) +local made_change=false +for line in file:lines() do + if line:match("^#?%s*"..key.."=.*$") then + line=key.."="..value + made_change=true + end + print(line) +end + +if not made_change then + print(key.."="..value) +end +EOF +mv "$3.bak" "$3" +} + # $1 is 0 to disable overscan, 1 to disable it set_overscan() { # Stop if /boot is not a mountpoint @@ -81,13 +103,9 @@ set_overscan() { if [ "$1" -eq 0 ]; then # disable overscan sed /boot/config.txt -i -e "s/^overscan_/#overscan_/" - if grep -q "^disable_overscan" /boot/config.txt; then - sed -i /boot/config.txt -e "s/^disable_overscan.*$/disable_overscan=1/" - else - printf "disable_overscan=1\n" >> /boot/config.txt - fi + set_config_var disable_overscan 1 /boot/config.txt else # enable overscan - sed -i /boot/config.txt -e "s/^disable_overscan.*$/disable_overscan=0/" + set_config_var disable_overscan 0 /boot/config.txt fi }