update-conf
changeset 0 8baf084f58c5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/update-conf	Wed Jun 18 16:16:36 2014 +0200
@@ -0,0 +1,51 @@
+#!/bin/sh
+
+#
+# This script copies havp.config.default to havp.config,
+# while keeping user set values
+#
+# Command: update-conf /usr/local/etc/havp/havp.config
+#
+# Default config must be found: /usr/local/etc/havp/havp.config.default
+#
+
+if [ ! -f "$1" ]; then exit 0; fi
+if [ ! -f "$1.default" ]; then exit 0; fi
+
+cp "$1" "$1.old"
+
+perl -e '
+	open(OLDCONF, "$ARGV[0]") or die;
+	while (<OLDCONF>)
+	{
+		chomp;
+		unless ( /^\s*?#/ || /^\s*$/ )
+		{
+			if ( /\s*?(\S+?)\s+?(.+)\s*$/ )
+			{
+				$conf{$1} = $2;
+			}
+		}
+	}
+	close(OLDCONF);
+
+	open(NEWCONF, "$ARGV[0].default") or die;
+	open(REPCONF, ">$ARGV[0].tmp") or die;
+
+	while (<NEWCONF>)
+	{
+		foreach $key (keys %conf)
+		{
+			if ( /^(\# )?$key / )
+			{
+				print REPCONF "$key $conf{$key}\n" or die;
+				goto END;
+			}
+		}
+		print REPCONF $_ or die;
+		END:
+	}
+	close(REPCONF);
+	close(NEWCONF);
+	rename("$ARGV[0].tmp", "$ARGV[0]") or die;
+' $1