#!/bin/bash
 
#. /etc/bashrc

source /etc/init.d/functions 2> /dev/null

# Import Environment Variables
for f in ~/.env/*
do
    . $f
done

function welcome {
    cat <<EOF

    Welcome to OpenShift shell

    This shell will assist you in managing OpenShift applications.

    !!! IMPORTANT !!! IMPORTANT !!! IMPORTANT !!!
    Shell access is quite powerful and it is possible for you to
    accidentally damage your application.  Proceed with care!
    If worse comes to worst, destroy your application with 'rhc app destroy'
    and recreate it
    !!! IMPORTANT !!! IMPORTANT !!! IMPORTANT !!!

    Type "help" for more info.

EOF
}

function ctl_all {
    for cmd in `awk 'BEGIN { for (a in ENVIRON) if (a ~ /_CTL_SCRIPT$/) print ENVIRON[a] }'`
    do
        echo "+ $cmd $@"
        $cmd $@
    done
}


function mysql() {
   #  Setup default options.
   if [ -n "$OPENSHIFT_DB_GEAR_DNS" ]; then
      hostopt="-h $OPENSHIFT_DB_GEAR_DNS"
   else
      [ -n "$OPENSHIFT_DB_HOST" ]  &&  hostopt="-h $OPENSHIFT_DB_HOST"
   fi
   portopt="-P ${OPENSHIFT_DB_PORT:-3306}"
   useropt="-u ${OPENSHIFT_DB_USERNAME:-'admin'}"
   passopt=--password="$OPENSHIFT_DB_PASSWORD"

   #  Unset default value if it was provided to us.
   for arg in $@; do
      case "$arg" in
         --host=*|-h)      unset hostopt  ;;
         --port=*|-P)      unset portopt  ;;
         --user=*|-u)      unset useropt  ;;
         --password=*|-p)  unset passopt  ;;
         *)  ;;
      esac
   done

   /usr/bin/mysql ${hostopt} ${portopt} ${useropt} ${passopt} "$@"

}  #  End of  mysql  function.


function mongo() {
   if test $# -gt 0; then
      uopt=""
      popt=""
   else
      uopt="--username ${OPENSHIFT_NOSQL_DB_USERNAME:-'admin'}"
      [ -n "$OPENSHIFT_NOSQL_DB_PASSWORD" ]  &&  popt="--password $OPENSHIFT_NOSQL_DB_PASSWORD"
   fi

   if echo "$@" | egrep "\-\-host|$OPENSHIFT_NOSQL_DB_HOST" > /dev/null; then
      hopt=""  #  Do not override if --host is passed.
   else
      hopt="${OPENSHIFT_NOSQL_DB_HOST:-'127.0.0.1'}:${OPENSHIFT_NOSQL_DB_PORT:-27017}/admin"
   fi

   /usr/bin/mongo ${hopt} ${uopt} ${popt} "$@"

}  #  End of  mongo  function.


function help {
# FIXME: Comment out quota for now - till the selinux policy is in place.
# quota           list free space
    cat <<EOF
Help menu: The following commands are available to help control your openshift
application and environment.

ctl_app         control your application (start, stop, restart, etc)
ctl_all         control application and deps like mysql in one command
tail_all        tail all log files
export          list available environment variables
rm              remove files / directories
ls              list files / directories
ps              list running applications
kill            kill running applications
mysql           interactive MySQL shell
mongo           interactive MongoDB shell

EOF
}

alias ctl_app=$OPENSHIFT_GEAR_CTL_SCRIPT
alias tail_all="/usr/bin/tail -f $OPENSHIFT_GEAR_DIR/logs/*"

export PS1="[$OPENSHIFT_GEAR_DNS \W]\> "
export TMOUT=300
welcome

if [ -z $SSH_TTY ]
then
    echo "WARNING: This ssh terminal was started without a tty." 1>&2
    echo "          It is highly recommended to login with: ssh -t" 1>&2
fi
