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.
This commit is contained in:
Alex Bradbury 2012-07-13 17:01:48 +01:00
parent 053c55300b
commit e4e56bf1fb
2 changed files with 25 additions and 7 deletions

2
debian/control vendored
View File

@ -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

View File

@ -70,6 +70,28 @@ EOF
The filesystem will be enlarged upon the next reboot" 20 60 2
}
set_config_var() {
lua - "$1" "$2" "$3" <<EOF > "$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
}