#! /bin/sh
#
# A simple script file to restore the entries in the NET/ROM routing from a
# NET/ROM routing file creted by nodesave.
#

if [ "$#" = "0" ]; then
	echo "Usage: noderest <filename>"
	exit 1
fi

if [ ! -f $1 ]; then
	echo "noderest: cannot open file $1"
	exit 1
fi

if [ ! -f /proc/net/nr ]; then
	echo "noderest: NET/ROM not configured into the kernel - aborting"
	exit 1
fi

awk '{
	if ($1 == "addr") {
		mode = 1
	} else if ($1 == "callsign") {
		mode = 2
	} else if (mode == 1) {
		call[$1] = $2
		dev[$1]  = $3
		qual[$1] = $4
		lock[$1] = $5
		if ($5 == "1") {
			system("nrparms -routes " $3 " " $2 " + " $4)
		}
	} else if (mode == 2) {
		if (NF == 13) {
			system("nrparms -nodes " $1 " + \"" $2 "\" " $11 " " $12 " " dev[$13] " " call[$13])
		}
		if (NF >= 10) {
			system("nrparms -nodes " $1 " + \"" $2 "\" " $8  " " $9  " " dev[$10] " " call[$10])
		}
		if (NF >= 7) {
			system("nrparms -nodes " $1 " + \"" $2 "\" " $5  " " $6  " " dev[$7]  " " call[$7])
		}
	}
}' $1

exit 0
