#!/bin/bash
#
# Copyright 2005 Perforce Software. All rights reserved.
#
#
# Default to current user for sending email address of notifications
#
ADMIN_EMAIL=ejpadmin@perforce.com
ROOT=$PWD
testport=3344
export P4USER=admin
export P4CLIENT=client1
export P4PORT=$MYHOST:$testport
export P4SPECSUFFIX=".p4s"
if [ "$1" = "" ]; then
echo "Usage: $0 <start|stop>"
exit
fi
#
# Make sure this is being run in the right locations
#
if [ -d triggers ]; then
echo "Found triggers directory"
else
echo "Missing trigger directory"
echo "$0 must be run in the directory containing it"
exit
fi
RUBY_BIN=`which ruby`
P4_BIN=`which p4`
P4D_BIN=`which p4d`
fail=0
if [ "$RUBY_BIN" = "" ]; then
echo "Unable to find ruby"
fail=1
fi
if [ "$P4_BIN" = "" ]; then
echo "Unable to find p4"
fail=1
fi
if [ "$P4D_BIN" = "" ]; then
echo "Unable to find p4d"
fail=1
fi
if [ "$fail" = "1" ]; then
echo "Aborting"
exit
fi
if [ "$1" = "start" ]; then
echo "Starting p4d..."
echo " to kill it use the p4 command:"
echo " p4 -usu -Ppw admin stop"
#
# Setup the depot root
#
if [ -d depot.$$ ]; then
echo "depot.$$ already exists. Aborting"
exit
else
mkdir depot.$$
echo "Instantiating p4d into depot.$$"
fi
if [ -d triggers.$$ ]; then
echo "triggers.$$ already exists. Aborting"
exit
else
mkdir triggers.$$
ln -s triggers.$$ cur.triggers
echo "Created trigger directory triggers.$$"
fi
if [ -d logs.$$ ]; then
echo "logs.$$ already exists. Aborting"
exit
else
mkdir logs.$$
echo "Trigger logs in logs.$$"
fi
#
# Run the server
#
echo "Staring server with: $P4D_BIN -d -vserver=1 -L$ROOT/logs.$$/p4d.log -p $testport -r $ROOT/depot.$$"
$P4D_BIN -d -vserver=1 -L$ROOT/logs.$$/p4d.log -p $testport -r $ROOT/depot.$$
sleep 5 # wait to make sure server is running
echo "running"
#
# Turn on the spec depot
#
$P4_BIN depot -i <<FOOBAR
Depot: spec
Owner: admin
Description:
Created by admin.
Type: spec
Address: local
Suffix: $P4SPECSUFFIX
Map: spec/...
FOOBAR
#
# Copy the triggers and substitute in target locations
#
cp triggers/*.rb triggers.$$
sed -e "s=EJP_TRIGGERS_DIR=$ROOT/triggers.$$=g" \
-e "s=EJP_TRIGGERS_LOG_DIR=$ROOT/logs.$$=g" \
-e "s=EJP_TRIGGERS_EMAIL_ADDR=$ADMIN_EMAIL=g" \
-e "s=EJP_P4D_PORT=$P4PORT=g" \
-e "s=EJP_P4USER=$P4USER=g" \
-e "s=EJP_P4_BIN=$P4_BIN=g" \
-e "s=EJP_P4SPEC_SUFFIX=$P4SPECSUFFIX=g" \
triggers/P4Data.rb > triggers.$$/P4Data.rb
#
# Load the triggers table substituting the SCRIPTPATH variable
#
sed -e "s=TRIGGERS_DIR=$ROOT/triggers.$$=g" \
-e "s=RUBY_BIN=$RUBY_BIN=g" \
$ROOT/triggers.txt | $P4_BIN triggers -i
#
# Load the jobspec and create sample users
#
$P4_BIN jobspec -i < $ROOT/jobspec.txt
. $ROOT/p4user.setup one
. $ROOT/p4user.setup two
. $ROOT/p4user.setup three
. $ROOT/p4user.setup four
elif [ "$1" = "stop" ]; then
#
# Stop the server
#
$P4_BIN -usu -Ppw admin stop
sleep 5 # wait for it to stop
echo "stopped"
/bin/rm -rf $ROOT/depot.[0-9]* $ROOT/logs.[0-9]* $ROOT/cur.triggers $ROOT/triggers.[0-9][0-9]* $ROOT/one $ROOT/two $ROOT/three $ROOT/four
else
echo "Usage: $0 <start|stop>"
fi