#!/bin/bash
#
# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# @@@ END COPYRIGHT @@@
#

SQCONFIG_FILE=sqconfig

function Usage {
    script_name=`/bin/basename $0`
    echo
    echo $script_name regenerates the Trafodion configuration file in the $TRAF_HOME/sql/scripts directory.
    echo
    echo "Usage: $script_name [-? | -h ] | { -config } [ <sqconfig_filename> ]"
    echo "  -?       Help"
    echo "  -h       Help"
    echo "  -config  Regenerate the Trafodion configuration file ($SQCONFIG_FILE)"
    echo "  <sqconfig_filename> Name of the Trafodion configuration file"
    echo "                      in $TRAF_HOME/sql/scripts directory (defaults to 'sqconfig')"
    echo
    exit 1;
}

function RegenConfig {
    echo "Regenerating Trafodion configuration: $SQCONFIG_FILE"
    cd $TRAF_HOME/sql/scripts
    ./regensq.pl $SQCONFIG_FILE
    if [ $? != 0 ]; then
        echo "Regensq failed."
        exit $?
    fi
    echo "Copying Trafodion configuration file $SQCONFIG_FILE to all nodes"
    pdcp `trafconf -wname` $SQCONFIG_FILE $TRAF_HOME/sql/scripts/
    exit $?
}

while [ $# != 0 ]
  do
    flag="$1"
    if [ ! -z $2 ]; then
        SQCONFIG_FILE=$2
    fi 
    case "$flag" in
        -h)        Usage ;;
        -?)        Usage ;;
        -config)   RegenConfig ;;
    esac
    shift
  done

Usage;
