#!/bin/bash
# 
# Copyright (c) 2008 - 2014, Hewlett-Packard Development Co., L.P. 

#
### verticad - startup/shutdown for the Vertica Analytic Database daemon
#
#   The Vertica Analytic Database is a distributed column-oriented database.
#
#   Notes:
#       This is rather a thin implementation because starting up a single
#       node of a clustered database is complex.  So this script simply
#       delivers a control command to the Vertica Administration Tools, which
#       are responsible for implementing the command and returning the result.
#
# RedHat metadata format:
#   chkconfig: 345 99 10
#   description: Startup/shutdown script for the Vertica Analytic Database
#
# LSB/SUSE format:
### BEGIN INIT INFO
# Provides:       verticad
# Required-Start: $network ntpd
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start:  3 5
# Default-Stop:   0 1 6
# Description:    Startup/shutdown script for the Vertica Analytic Database
### END INIT INFO


#
# Determine our OS
#

[ -f /etc/redhat-release ] && OS=redhat
[ -f /etc/SuSE-release ] && OS=suse
[ -f /etc/debian_version ] && OS=debian

#
# Source function library.
#
case $OS in
    redhat)
        . /etc/rc.d/init.d/functions
        ;;
    suse)
        . /etc/rc.status
        ;;
    debian)
        . /lib/lsb/init-functions
        ;;
    *)
        echo Unsupported OS
        exit 1
        ;;
esac

verticad_dbg=${verticad_dbg:-''}

function dbg {
    if [ -z "$verticad_dbg" ]; then
        return;
    fi
    echo debug: "$@" >&2
}


#
# OS-specific helper functions
#
# error(msg) prints the error message then exits
#
function error {
    if [ "$mode" == "status" ]; then
        echo $1
    else
        echo $1
        case $OS in
            redhat)
                echo_failure
                ;;
            suse)
                echo $rc_failed
                ;;
            debian)
                log_failure_msg
                ;;
        esac
    fi
    exit 1
}

# success() exits appropriately
#
function success {
    if [ "$mode" == "status" ]; then
        echo $1
    else
        echo $1
        case $OS in
            redhat)
                echo_success
                ;;
            suse)
                echo -e "$rc_running"
                ;;
            debian)
                log_success_msg
                ;;
        esac
    fi
    exit 0
}


# Check argument
case "$1" in
    start)
        ;;
    stop)
        ;;
    status)
        mode=status
        ;;
    restart)
        ;;
    condrestart)
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|condrestart}"
        exit 1
        ;;
esac

# Ready to roll...

echo -n $"Vertica: "

#
# Sanity checks: networking must be up, the vertica admin tools must
# be installed.
#
if [ "$OS" == "redhat" ]; then
    . /etc/sysconfig/network
    if [ ${NETWORKING} = "no" ]; then
        error 
    fi
fi

# Find out where vertica is installed, use that path

if [ "$OS" == "redhat" -o "$OS" == "suse" ]; then
    vertica_prefix=`rpm -q vertica --qf %{INSTPREFIXES}`
    if [ $? -ne 0 ]; then
        # No RPM installed
        error "No RPM: $vertica_prefix"
    fi
elif [ "$OS" ==  "debian" ]; then
    query_out=`dpkg-query -S /opt/vertica/config | awk -F": " '{print $2}'`
    if [ $? -ne 0 -o -z "$query_out" ]; then
        # No package installed
        error "No package"
    fi
    vertica_prefix=`expr "$query_out" : "\(.*\)/config"`
else
    error "Unknown OS = $OS"
fi

dbg "OS = $OS"

eula_acceptance="${vertica_prefix}/config/d5415f948449e9d4c421b568f2411140.dat"

if [ ! -f ${eula_acceptance} ]; then
    error "EULA not  yet accepted."
fi;

atconf="${vertica_prefix}/config/admintools.conf"

if [ ! -f "$atconf" ]; then
    error "Vertica not yet installed. Run ${vertica_prefix}/sbin/install_vertica"
fi

user=""
userid=""
remainder="" # should remain empty

read user userid remainder <<< $(/usr/bin/stat --format "%U %u" "$atconf")
rc="$?"

if [ "$rc" != "0" ]; then
    error "Error running /usr/bin/stat on admintools.conf (rc=$rc)"
fi

if [ -n "$remainder" ]; then
    error "Unexpected output from /usr/bin/stat on admintools.conf"
fi

if ! /usr/bin/id "$user" > /dev/null; then
    error "admintools.conf owner '$user' (id=$userid) gives error for /usr/bin/id."
fi

if [ "$user" = "root" ]; then
    error "admintools.conf owned by root. To resolve, run ${vertica_prefix}/sbin/install_vertica"
fi

dbg "user = $user, id = $userid"

# run command as the DBA user
# output goes to /dev/null for this script, but most stuff is in admin tools log
su -l "$user" -c "${vertica_prefix}/bin/adminTools -t command_host -c$1" > /dev/null
rc="$?"

if [ "$rc" != "0" ]; then
    dbg "adminTools rc = $rc"
    error "$1 not OK"
fi

# For status, we return success iff the command succeeded for some user
success "$1 OK for users: $user"
