#!/bin/ksh
set -u #all var must be initilized
#save_node node
#should get a form data on stdin
#pull it apart and check in node data
#rebuilding current data

NODE=$1

##just dump node data
#cat - <& 0 > base/${NODE}
#return 0

#change '=' and '&' to \n first
KEY="none"
NAME="Anonymous"
REASON="none"
REVISED="off" #change major version (official revision)
REVNUM="" # the -r[rev] flag to ci (if any)

#co lock done here as file will get overwritten with post data
if test -a base/RCS/${NODE},v
then /usr/bin/co -q -f -l base/${NODE}
fi

while test X"${KEY}" != "X"
do {
	read KEY
	case X"${KEY}" in
		( "Xname" ) read NAME ;;
		( "Xsecret" ) read SECRET ;;
		( "Xreason" ) read REASON ;;
		( "Xrevised" ) read REVISED ;;
		( "Xnode_data" ) {
			read NODE_DATA; 
			echo ${NODE_DATA} | sed -f template/post_data.sed > base/${NODE} 
			} ;;
		( * ) read VALUE ;;
		esac
	} done

#
if test "X${NAME}" != "X" -a "X${SECRET}" != "X"
then
	COMPARE=$(grep ${NAME} base/secret.file | awk ' { print $2 }' )
	if test "X${COMPARE}" != "X"
	then
		if test "X$(sha1 -q -b -s ${SECRET})" == "X${COMPARE}"
		then
			NAME="${NAME}-athoritive"
		else
			NAME="${NAME}-bogus"
			fi
	else
		echo "${NAME} $(sha1 -q -b -s ${SECRET})" >> base/secret.file
		NAME="${NAME}-registered"
		fi
	fi

NAME=${NAME:-Anonymous}"-"${REMOTE_ADDR}
REASON="$(echo -n ${REASON:-no reason} | sed -f template/post_data.sed)"

#check for an official revision
if test -a base/RCS/${NODE},v
then { #prev revisions
	if test X"${REVISED}" == "Xon"
	then { #inc major revision number manually for ci
		REVNUM="-f"$(rlog -t base/${NODE} | grep head | \
			awk 'BEGIN { FS="[\. ]" }; { print $2 + 1} ' )
		}
	else REVNUM="" #let it auto rev
	fi
	}
else { #no previous revs
	#unable to properaly quote the node description
	#thus the filesystem jigery pokery going on
	echo "${REASON}" > base/${NODE}.description
	if test "X${REVISED}" == "Xon"
	then 
		REVNUM="-r1 -tbase/${NODE}.description" #first rev 1
	else {
		REVNUM="-r0 -tbase/${NODE}.description" #start revs at 0
		} fi
	REASON="Added ${NODE}"
	} fi


#check in changes
/usr/bin/ci -q -m"${REASON}" \
	-w"$( echo -n ${NAME} | sed -f template/post_data.sed ) " \
	${REVNUM} base/${NODE}

#echo ${NODE} Checked in >& 2

#generate current
bin/build_node ${NODE}

#save new official version
if test X"${REVISED}" == "Xon"
then { #save "official version"
	cp current/${NODE}.html current/official/${NODE}.html
	} fi

