#! /bin/bash

#*******************************************************************************
# ALMA Project
#
# "@(#) $Id: accdbConfig,v 1.3 2005/11/03 09:31:06 gchiozzi Exp $"
#
# who       when      what
# --------  --------  ----------------------------------------------
# gchiozzi  2005-03-21 created
#

#************************************************************************
#   NAME 
#   accdbConfig - Configures the acc database
#
#   SYNOPSIS
#   accdbConfig [OPTIONS] 
# 
#   DESCRIPTION
#
#   Configures the acc database for ACS.
#   This is necessary for tat tests on the LCU.
#
#   Highly incomplete!!!!!
#   Just a first step1
#
#   - Copies the msql standard files from (VLTTOP)/config/msql in ACSDATA/msql
#   FILES
#
#   ENVIRONMENT
#
#   RETURN VALUES
#   0 - if everything OK
#   2 - in case of errors
# 
#   CAUTIONS
#
#   EXAMPLES
#
#   SEE ALSO
#
#   BUGS     
#
#------------------------------------------------------------------------
#

#
#  Get some basic utilities, in particular to get my host IP
#
. acsstartupAcsPorts

#
# Usage info. Be nice and keep this up-to-date!
#
function printUsage {
   echo "=======> Implemensation incomplete!!!!!"
   echo "Configures the acc database for ACS"
   echo "Usage: $THIS [OPTIONS] ocnfig_file?"
   echo "Options: "
   echo "   -h | --help                   prints this help and exits"
   echo ""
}

#
# These options can be recognized (longopts comma-separated. colon means argument is required)
# LONGOPTS=help,managerReference:,DALReference:
# SHORTOPTS=hm:d:b:e:
#
LONGOPTS=help
SHORTOPTS=h

THIS=`basename $0`
CL_HELP=
CL_ACTION="update"

#
# Run getopt (posixly_correct needed). We run twice:
# First run is simply to check the commandline for correctness
# Second run is the real deal which replaces the command line args with getopt's output

# export POSIXLY_CORRECT=1   # Original code had export, but this does not work
#                            # when calling accLoadData
POSIXLY_CORRECT=1

getopt -n $THIS -Q -u -a -l $LONGOPTS $SHORTOPTS "$@" || {
   printUsage
   exit 43;
}

set -- `getopt -u -a -l $LONGOPTS $SHORTOPTS "$@"`


#
# Iterate over getopt's output and set CL_XXX variables accordingly
#
while : 
do
	case "$1" in
	--help)             CL_HELP=true ;; 
	-h)                 CL_HELP=true ;; 
	--) break ;;
	esac
	shift
done
shift

if [ "$CL_HELP" ] ; then
   printUsage
   exit 0
fi

## end cmd line parsing ###############################

########################################

## TODO: checks if $ACSDATA/msql exist

## copy files from where they are (normally ACSROOT)
## into ACSDATA/msql
## TODO: uses searchFile utility to locate files in search tree

echo "##### Install acs database files"
cp -rf $ACSROOT/config/msql $ACSDATA

## GCH 2005-11-02: added basic editing of the file
## to put proper host name and IP.
## The file in config is now a template.
## but we can improve it.

# Get my host IP and check it is not localhosts
# In case, issues a warning.
MY_IP=`getIP`
if [ `$MY_IP` = "127.0.0.1" ]
then
    echo "WARNING: this script has detected that your PC is incorrectly configured"
    echo "         for CORBA and VxWorks communication. The IP address (i.e., $HOST)"
    echo "         found is what is normally used with DHCP. In other words, forget"
    echo "         about connecting any CORBA client/servant residing on this machine" 
    echo "         to a CORBA object residing on another host. Also, do not use this"
    echo "         PC to generate TAT reference files involving Python as the reference"
    echo "         files will be different from those generated under a 'normal'"
    echo "         PC. For information on resolving this issue, please see:"
    echo "             http://almasw.hq.eso.org/almasw/bin/view/ACS/FAQGeneralDevelopmentEnvironmentBadEtcHosts"
    echo ""
    sleep 5
fi

acsReplace %%INSTALL_HOST%% $HOSTNAME  $ACSDATA/msql/accData.sql 
acsReplace %%INSTALL_HOST_IP%% $MY_IP  $ACSDATA/msql/accData.sql 


## checks if msqld running
MSQLD_PID=` ps -ef | grep msqld | grep -v grep | awk '/[^awk]/ {print $2}'`

## if not, start masqld
if [ "$MSQLD_PID" = "" ]
then
   echo "##### msqld not running. Starting it!"
   msqld &
   sleep 2
else
   echo "##### msqld already running with pid $MSQLD_PID"
fi

## load the database
echo "##### Loading new data in acc database"
accLoadData

echo "##### Display acc database contents"
vccShow

echo "==== ACS $CL_ACTION done! ===="

#
# ___oOo___
