#!/bin/sh

# Copyright 2003 Neil Gorsuch
#
# This file is part of pfilter.
#
# pfilter is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# pfilter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# uncomment for verbose/debug output
#verbose=yes

function usage() {
cat <<EOF
usage: $0 CONFIG_PROTO CONFIG_SYSTEM
where CONFIG_PROTO is the path name of the new prototype
pfilter.conf file, and CONFIG_SYSTEM is the path name of
the (possibly already existing) system pfilter.conf file
EOF
}

if [ $# != 2 ] ; then
    usage
    exit 1
fi

# make sure the prototype file is readable
if [ ! -r $1 ] ; then
    echo $0: error - cannot read prototype file $1
    usage
    exit 1
fi

# if the system config file doesnt exist yet,
# go ahead and write a new one using the prototype
# file (getting rid of the section markers),
# and then we are done
if [ ! -e $2 ] ; then
    if [ -n "$verbose" ] ; then
	echo $0: writing new configuration file $2 ...
    fi
    # the pipe to the cat is there because the exit status of the
    # cat is wanted not the exit status of the egrep
    if ! egrep <$1 -v '^\#SECTIONBEGIN|^\#SECTIONEND' | cat >$2 ; then
	echo $0: error - cannot write to location $2
	usage
	exit 1
    else
	if [ -n "$verbose" ] ; then
	    echo ... done writing new configuration file $2
        fi
	exit 0
    fi
fi

# if the system config file exists, it has to be 
# a writable file (not a directory or whatever)
if [ ! -f $2 ] ; then
    echo $0: error config $2 has to be a file
    usage
    exit 1
else
    if [ ! -w $2 ] ; then
        echo $0: error config file $2 is not writable
        usage
        exit 1
   fi
fi

# find the list of sections in the prototype file
if [ -n "$verbose" ] ; then
    echo merging prototype file $1 into $2 ...
    echo finding list of sections to merge ...
fi
sections=`egrep '^\#SECTIONBEGIN' $1 | awk '{print $2}' | egrep -v '^COPYRIGHT$'`
if [ -n "$verbose" ] ; then
    echo ... found sections:
    for section in $sections ; do 
	echo "        $section"
    done
fi

# start building new configuration file
NEWFILE=/tmp/`basename $0`.$$
rm -f $NEWFILE
touch $NEWFILE
chmod 644 $NEWFILE

# make sure the copyright section is there
# (hardcoded test here) and put it in at the start
# of the new file if not in the previous file
if ! egrep 'Copyright [0-9][0-9][0-9][0-9] Neil Gorsuch' $2 >/dev/null ; then
    if [ -n "$verbose" ] ; then
	echo adding back in missing copyright section to new file ...
    fi
    section=COPYRIGHT
    awk <$1 "\$1==\"#SECTIONBEGIN\"&&\$2==\"$section\",\$1==\"#SECTIONEND\"&&\$2==\"$section\"{print}" \
    | egrep -v '^\#SECTIONBEGIN|^\#SECTIONEND' >> $NEWFILE
fi

# put the current configuration file in next
if [ -n "$verbose" ] ; then
    echo merging current configuration text
fi
cat $2 >> $NEWFILE

# now for every section type add that section from the prototype
# file if there aren't any commands from that section already 
# in the configuration file
added=0
if ! sed <$2 's,[:space:]*\#.*$,,g' | awk '{print $1}' | egrep -i '^nomerge$' >/dev/null ; then
    for section in $sections ; do
        if [ -n "$verbose" ] ; then
            echo testing for $section in current file ...
        fi
        section_found=0
        commands="`echo $section | tr , ' '`"
        for command in $commands ; do
            if tr -d '#' <$2 | tr '[:space:]' '\n' | \
            egrep "^$command\$" >/dev/null ; then
                if [ -n "$verbose" ] ; then
                    echo "        found command $command"
                fi
                section_found=1
            fi
        done
        if [ $section_found == 0 ] ; then
            echo none of the commands $commands were found
            echo adding section for $section to end of new file
            if [ $added == 0 ] ; then
                if ! tail -1 $NEWFILE | egrep '^[:space:]*$' >/dev/null ; then 
                    echo >> $NEWFILE
                fi
                echo \# UPGRADE ADDED FOLLOWING MISSING SECTIONS ON `date`: >> $NEWFILE
                echo >> $NEWFILE
            fi
            awk <$1 "\$1==\"#SECTIONBEGIN\"&&\$2==\"$section\",\$1==\"#SECTIONEND\"&&\$2==\"$section\"{print}" \
            | egrep -v '^\#SECTIONBEGIN|^\#SECTIONEND' >> $NEWFILE
            added=1
        fi
    done
fi

if cmp -s $NEWFILE $2 ; then
    if [ -n "$verbose" ] ; then
	echo configuration file $2 needed no changes
    fi
    rm -f $NEWFILE
else
    if [ -n "$verbose" ] ; then
	echo saving old configuration file as $2.previous
	echo cp -p $2 $2.previous
    fi
    cp -p $2 $2.previous
    if [ -n "$verbose" ] ; then
	echo writing new merged configuration file $2
	echo mv $NEWFILE $2
    fi
    mv $NEWFILE $2
fi    

exit 0








