#!/bin/sh

# Crude validator for game files; assumes that messages only take up a
# single line
cat game.dat | awk '{line++;
	if(line == 2) {items = $0 + 1;print items " items"}
	if(line == 3) {actions = $0 + 1; print actions " actions"}
	if(line == 4) {words = $0 + 1;print words " nouns and verbs"}
	if(line == 5) {rooms = $0 + 1;print rooms " rooms"}
	if(line == 11) {messages = $0 + 1;print messages " messages"}
	if(line == 13) {in_action = 1}
	if(in_action == 1) {action++}
	if(action > actions * 8 && line > 12) {in_action = 2}
	if(in_action == 1 && $0 ~ /\"/) {
		print "ERROR: Quote in action on line " line;exit}
	if(in_action == 2 && in_words < 1) {in_words = 1}
	if(in_words == 1) {word++}
	if(in_words == 1 && $0 !~ /\"/) {
		print "ERROR: Non-word in noun/verbs on line " line;exit}
	if(word >= words * 2 && (line > 12)) {in_words = 2}
	if(in_rooms == 1 && room % 7 != 6 && $0 ~ /\"/) {
		print "ERROR: Quote in room exit on line " line;exit}
	if(in_rooms == 1 && room % 7 == 6 && $0 !~ /\"/) {
		print "ERROR: Non-quote in room description on line " line
                exit}
	if(in_rooms == 1) {room++}
	if(in_words == 2 && in_rooms == 0) {in_rooms = 1}
	if((room >= rooms * 7) && line > 12) {in_rooms = 2}
	if(in_rooms == 2 && in_messages == 0) {in_messages = 1}
	if(in_messages == 1) {message++
		if($0 !~ /\"/) {
		print "ERROR: Non-quote in message on line " line;exit}
		if($0 ~ /[0-9][ ]*$/) {
		print "ERROR: Message ends in unquoted number on line " line
		exit}
	}
	if((message > messages) && line > 12) {in_messages = 2}
	if(in_items == 1) {item++;
		if($0 !~ /[0-9][ ]*$/) {
		print "ERROR: Item does not end in number on line " line
		exit}
	}	
	if(in_messages == 2 && in_comments == 0) {in_items = 1}
	if((item >= items) && line > 12) {in_items = 2}
	if(in_comments == 1) {comment++;
		if($0 !~ /\"/) {
		print "ERROR: Non-quote in comment on line " line;exit}
	}
	if(in_items == 2 && in_comments == 0) {in_comments = 1}
	if(comment >= actions && line > 12) {in_comments = 2}
}'
