#!/bin/sh
# @(#) $Revision: 1.8 $
# @(#) RCS control in //prime.corp/usr/local/src/cmd/prime/src/do_init.sh
#
# do_init - run init_sieve progs
#
# usage:
#	do_init topdir setname startk n
#
#	topdir		top level directory (often: /usr/local/lib/map)
#	setnmae		name of set (dir under topdir/set) to initialize
#	startk		starting k value
#	n		n value (as in k*2^n+/-1)
#
# 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
#
if [ $# -ne 4 ]; then
    echo "usage: $0 topdir setname startk n" 1>&2
    exit 1
fi
TOPDIR="$1"
setname="$2"
startk="$3"
n="$4"
if [ ! -d "$TOPDIR" ]; then
    echo "$0: $TOPDIR is not a top level directory" 1>&2
    exit 1
fi
cd "$TOPDIR"
if [ ! -f src/init_sieve ]; then
    echo "$0: src/init_sieve not found 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
if [ -d "$setname" ]; then
    echo "$0: directory set/$setname already exists under $TOPDIR" 1>&2
    exit 4
fi

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

# check for the proper files first
#
mkdir "$setname"

# initialize sieve
#
echo "running init_sieve for $setname"
time ../src/init_sieve "$startk" 8388608 "$n" $setname >$setname/init_sieve.$DATE 2>&1
cat $setname/init_sieve.$DATE

# form map programs
#
echo "forming map programs for $setname"
(cd "$setname"; 
 make -f "../../src/build.mk" setup all SETNAME="${setname}" TOPDIR="${TOPDIR}")

# all done
#
exit 0
