#!/bin/bash 
#
# Copyright (c) 2004 by Catalin Dumitrescu and The University of Chicago.
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose, without fee, and without written agreement is
# hereby granted, provided that the above copyright notice and the following
# two paragraphs appear in all copies of this software.
#
# IN NO EVENT SHALL THE UNIVERSITY OF CHICAGO BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
# OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
# CHICAGO HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# THE UNIVERSITY OF CHICAGO SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
# ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CHICAGO HAS NO OBLIGATION TO
# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#
############################################################################### 
# 
# Author: Catalin Dumitrescu <catalind@cs.uchicago.edu> 
# Date: March 2005 
# 
# Short description 
#   Check for the site monitor components and creates various required directories and links
# 


# Initialization of some elements 
export CRTDIR=`pwd` ; 
export SPOP="$CRTDIR/s-pop.0" ; 
export VPOP="$CRTDIR/v-pop.0" ; 


# Checkings 
if [ -d "$1" ] ; then 
    export SPOP="$1" ; 
elif [ -d "$CRTDIR/siteMonitor" ] ; then 
    export SPOP="$CRTDIR/siteMonitor" ; 
elif [ -d "$CRTDIR/s-pop.0" ] ; then 
    export SPOP="$CRTDIR/s-pop.0" ; 
else 
    echo "No siteMonitor found (please specify as arg 1)." ; 
    exit 1 ; 
fi ; 

if [ -d "$2" ] ; then 
    export VPOP="$2" ; 
elif [ -d "$CRTDIR/submitterMonitor" ] ; then 
    export VPOP="$CRTDIR/submitterMonitor" ; 
elif [ -d "$CRTDIR/v-pop.0" ] ; then 
    export VPOP="$CRTDIR/v-pop.0" ; 
else 
    echo "No submitterMonitor found (please specify as arg2)." ; 
    exit 1 ; 
fi ; 


# Crontab info local save 
crontab -l > /tmp/cront.tmp.gruber ; 


# Setting up the S-POP environment 
echo -n "Setting up S-POP ... (self-running or crontab) " ; 
mkdir -p "$SPOP/data" ; mkdir -p "$SPOP/data_tmp" ; mkdir -p "$SPOP/data_saved" ; 
export tmpa=`cat /tmp/cront.tmp.gruber | grep "$SPOP/automated.sh"` ;
if [ "a$tmpa" == "a" ] ; then 
    echo "0-59/20 * * * * $SPOP/automated.sh" >> /tmp/cront.tmp.gruber ; 
fi ; 
echo "done." ; 


# Setting up the V-POP environment 
echo -n "Setting up V-POP ... (self-running or crontab) " ; 
mkdir -p "$VPOP/data" ; mkdir -p "$VPOP/data_tmp" ; mkdir -p "$VPOP/data_saved" ; 
export tmpb=`cat /tmp/cront.tmp.gruber | grep "$VPOP/automated.sh"` ;
if [ "a$tmpb" == "a" ] ; then 
    echo "0-59/20 * * * * $VPOP/automated.sh" >> /tmp/cront.tmp.gruber ; 
fi ; 
echo "done." ; 


# Crontab update 
cat /tmp/cront.tmp.gruber | crontab ; 
rm -f /tmp/cront.tmp.gruber ; 
echo "Please check your crontab for correct updates" ; 


# Setting up the engine environment 
echo "Setting up S-POP/V-POP links to Engine ... " ; 
mkdir -p $GLOBUS_LOCATION/schema/up_siteRecommenderService ; 
ln -s $SPOP/data $GLOBUS_LOCATION/schema/up_siteRecommenderService/data ; 
ln -s $VPOP/data $GLOBUS_LOCATION/schema/up_siteRecommenderService/dataVO ; 

echo "done." ; 


