#!/bin/sh
# @(#) $Revision: 1.11 $
# @(#) RCS control in //prime.corp/usr/local/src/cmd/prime/src/do_map1.sh
#
# do_map1 - restart map1 progs
#
# usage:
#	do_map1 [topdir]
#
#	optional top level directory to work from
#
# We wait 60 seconds between sets so that they don't all hit the
# disk at the same time.
#
# Copyright (C) 1997 Landon Curt Noll, all rights reserved.
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose is hereby granted, provided that
# the above copyright, this permission notice, and the disclaimer
# below appear in all of the following:
#
#         * supporting documentation
#         * source copies
#         * source works derived from this source
#
# THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
# EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
# USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#
# Landon Curt Noll <chongo was here> /\oo/\
#
# chongo@{toad,sgi}.com		Share and Enjoy!

# parse args
#
TOPDIR=/usr/local/lib/map
if [ $# -eq 1 ]; then
    TOPDIR="$1"
fi
if [ ! -d "$TOPDIR" ]; then
    echo "$0: $TOPDIR is not a top level directory" 1>&2
    exit 1
fi
cd "$TOPDIR"
if [ ! -x "src/testlock" ]; then
    echo "$0: src/testlock is not an executable under $TOPDIR" 1>&2
    exit 2
fi
if [ ! -d set ]; then
    echo "$0: set is not a directory under $TOPDIR" 1>&2
    exit 3
fi
cd set

# Note start time
#
DATE=`date '+%d%h%y.%H%M%S'`

# check for the proper files first
#
ls -1F . | grep / | grep -v '[A-Z]' | sed -e 's;/$;;' | while read setname; do
    if [ ! -d "$setname" ]; then
	echo "$0: directory $setname not found" 1>&2
	exit 3
    fi
    if [ ! -x "$setname/map1" ]; then
	echo "$0: execitable $setname/map1 not found" 1>&2
	exit 4
    fi
    for j in ckpt neg parm pos; do
	if [ ! -f "$setname/$setname.$j" ]; then
	    echo "$0: $setname/$setname.$j missing" 1>&2
	    exit 5
	fi
	if [ ! -s "$setname/$setname.$j" ]; then
	    echo "$0: $setname/$setname.$j empty" 1>&2
	    exit 6
	fi
	if [ ! -r "$setname/$setname.$j" ]; then
	    echo "$0: $setname/$setname.$j not readable" 1>&2
	    exit 7
	fi
    done
done

# launch
#
cpu=0
ls -1F . | grep / | grep -v '[A-Z]' | sed -e 's;/$;;' | while read setname; do
    if ../src/testlock "$setname/$setname.ckpt"; then
	echo "launching map1 for $setname"
	(cd $setname; 
	 runon $cpu nohup npri -h 199 time ./map1 $setname >map1.$DATE 2>&1 &)
    else
	echo "$setname is already running"
    fi
    cpu="`expr $cpu + 1`"
done
