#!/bin/sh
#
# codaconfedit, edits Coda configuration files.
#
# arguments: <configuration file> <variable> <newvalue>
#
# It looks for the configuration file specified by the user as is, and
# by prepending the path of the coda configuration directory (/etc/coda,
# /usr/pkg/etc/coda, /usr/local/etc/coda, depending on where Coda was
# installed). If the variable already has the new value, nothing is
# modified. Otherwise any existing entries are commented out and the new
# entry is appended right behind the last existing previous entry (or at
# the end of the configuration file if the variable didn't exist before.
#

set -e

prefix="/home/guest/cmason@cmu.edu/"

if [ $# != 3 ] ; then
    echo "Usage: $0 <conffile> <variable> <newvalue>"
    exit 1
fi

conffile="$1"
variable="$2"
newvalue="$3"

for cpath in "" /home/guest/cmason@cmu.edu//etc/coda/ ; do
    if [ -f "$cpath$conffile" ] ; then
	break
    fi
    if [ -f "$cpath$conffile.ex" ] ; then
	/bin/cp "$cpath$conffile.ex" "$cpath$conffile"
	break
    fi
done

if [ ! -f "$cpath$conffile" ] ; then
    echo "Couldn't find $conffile"
    exit 1
fi

if `/bin/grep -q "^$variable=\"\?$newvalue\"\?$" $cpath$conffile` ; then
    exit 0
fi

# Use ed to comment all previous declarations of the variable,
# and append the new value.
 -s $cpath$conffile << EOF
g/^#*${variable}=/s/^#*/#/
a
${variable}="${newvalue}"
.
w
EOF

