#!/bin/bash
# vim:set sw=4 ts=4:
#
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2000-2002 SuSE Linux Solutions AG, Eschborn, Germany
#               2002-2004 SuSE Linux AG, Eschborn, Germany
#               2005           SUSE GmbH, Nuernberg, Germany
#
# This program 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.
#
# This program 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
#
#############################################################################
#
# Author: 
##############################################################
#
#  ALICE
#
##############################################################
#
#	change_netcfg - change network configuration patterns 
#   $Id: change_netcfg,v 1.4 2005/04/11 14:14:34 fabian Exp $
#	Copyright (c) 
#             2004 SuSE Linux AG. All rights reserved.
#             2004,2005 SUSE LINUX GmbH
#       
#	GNU Public License (GPL).
#
#############################################################
# Patterns could be
#    IP-ADDR
#    NET-ADDR
#    NET-MASK
#    ROUTER-IP
#    NET-ADDR/NET-BITS
#
#    or any others :-=
############################################################
#
# an example for the pattern table file
#
# ---------- snip ----------
# # change the ip address
# 10.70.1.234:->:192.1.100.20
# # change netadress
# 10.70.1.0:->:192.1.100.0
# # change broad cast
# 10.70.1.255:->:192.1.100.255
# # change netaddr and net-bits
# 10.70.1.0/24:->:192.1.100.0/24 
# # chnage default router
# 10.70.1.250:->:192.1.100.1
# ---------- snap ----------
#
#
PN=`basename $0`
FS=":->:"
UnixTmp=/tmp
TmpDir=$UnixTmp/ci$$
E=/dev/null
DebugLevel=0

case $1 in
	-d|--debug)
		DebugLevel=9
		# TODO: Konstanten zeigen
		#for v in PN FS UnixTmp T E VB; do
		#	echo $v="$\$v"
		#done
		set -x
		shift
	;;
	-h|--help|"")
		echo -e "Usage: $PN [option] <table> <src dir> <dst dir>\n"
		echo "Option:"
		echo "	-d debug"
		echo "	-h help"
		echo "	-v verbose"
		echo -e "\n$PN - change machine identifiers\n"
		echo "$PN scans files in source directory for patterns, replace them"
		echo "and writes to destination directory."
		echo "Patterns are defined in a table."
		echo "Lines are of the form <search pattern>$FS<replacement string>."
		echo "Lines beginning with # are ignored."
		exit 1
	;;
	-v|--verbose)
		DebugLevel=1
		shift
	;;
esac

PatternTable=$1
SRC=$2
DST=$3

#
# creating temporary directory and files and destination directory
#
mkdir -p $TmpDir $DST
>$TmpDir/pat; >$TmpDir/sed; >$TmpDir/fil

#
# sort pattern table
#
sort -u $PatternTable >$TmpDir/rep
#
# TODO: Muster fr Class A IP autom. erzeugen (pat)
# 192\.[0-9]\{1,3\}\.[0-9]\{1,3\}...
# TODO: Dateisuche optimieren: Nicht Muster suchen (pat), die andere enthalten
#
#
# Strip comments and only write SEARCH pattern to file pat
#    this is done to find the files, which needs to be prepared
#
awk -F"$FS" '$1!~"#"{print $1}' <$TmpDir/rep >$TmpDir/pat
find $SRC -type f -exec grep -lf $TmpDir/pat >>$TmpDir/fil {} \; 
#
# Create the sed syntax out of the lars pinne syntax
#
awk -F"$FS" 'BEGIN{print "#longest first"};$1!~"#"{print "s/"$1"/"$2"/g"}' <$TmpDir/rep >$TmpDir/sed
#
# dedug output file list, sed edit file and symlinks
#
[ $DebugLevel -gt 0 ] && (cat $TmpDir/fil $TmpDir/sed; find $SRC -type l -exec echo "symlink:" {} \; 1>&2) 
#
# copy involved files and do the edit using sed
#
cp -af --parent `cat $TmpDir/fil` $DST
for F in `cat $TmpDir/fil`; do
	sed -f $TmpDir/sed <$F >$DST/$F
done
#
# remove temporary files
#
[ $DebugLevel -lt 9 ] && rm -rf $TmpDir
#
# The end :-)
#
