#!/bin/bash

# Copyright 2018 Nikos Giotis, Athens, GR
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# rc.vms init script.
# See rc.vms.conf file for configuration

print_usage() {
    cat << EOF

Usage: $0 [command]

Commands

  status    Display the status of all machines in the set
  start     Start all the machines in the set
  stop      Stop all the machines in the set
  restart   Restart all the machines in the set
  cmd <username> <vmname> <vms-option>
            Call 'vms <vname> <vms-option>' as user <username>

  See rc.vms(8)
EOF
}

# Allow normal users to only run rc.vms without arguments
if [ "$1" != "" ] && [ "$(id -u)" != "0" ]; then
    echo "$0 must be run as root"
    exit 2
fi

confs=$( cat /etc/rc.d/rc.vms.conf | grep -o '^[^#]*' )

vms_cmd() {
    if [ "$1" == "" -o "$2" == "" ]; then
        echo "usage: $0 cmd <username> <vmname> <vms-option>"
        exit 2
    fi
    # This hack needs to be reviewed
    secure=$( su -c "vms info $3" $1 | grep 'secure: yes' )
    if [ "${secure: -3}" == "yes" ]; then
        vm_dir=$(su -c 'echo $HOME' $1)/.vms/$3
        if [ "$1" != 'root' ]; then
            echo -n 'S:'
            if [ ! -d $HOME/.vms ]; then
                mkdir -p $HOME/.vms
            fi
            if [ ! -L $HOME/.vms/$3 ]; then
                echo "Creating link for $3 for root"
                ln -s $vm_dir $HOME/.vms/$3
            fi
            vmsconf=$HOME/.vms/vms.conf
            if [ -f $vmsconf ]; then
                mv $vmsconf $vmsconf.bak
            fi
            cp $vm_dir/../vms.conf $vmsconf
            vms $2 $3
            rm $vmsconf
            if [ -f $vmsconf.bak ]; then
                mv $vmsconf.bak $vmsconf
            fi
        fi
    else
        su -c "vms $2 $3" $1
    fi
}

vms_cmd_all() {
    if [ "$1" == "" ]; then
        echo "usage: $0 <command> [rev]"
        exit 2
    fi
    if [ "$2" == "rev" ]; then
        confs=$(tac /etc/rc.d/rc.vms.conf | grep -o '^[^#]*')
    fi
    index=0; user=""
    for conf in $confs; do
        if [[ $(( index % 2 )) -eq 0 ]]; then
            user=$conf
        else
            vms_cmd $user $1 $conf
        fi
        index=$(( index + 1 ))
    done
}

vms_status() {
    vms_cmd_all status
}

vms_start() {
    vms_cmd_all start
}

vms_stop() {
    vms_cmd_all stop rev
}

vms_restart() {
    vms_cmd_all stop rev
    vms_cmd_all start
}

case "$1" in
'status')
    vms_status
    ;;
'start')
    vms_start
    ;;
'stop')
    vms_stop
    ;;
'restart')
    vms_restart
    ;;
'cmd')
    vms_cmd $2 $3 $4
    ;;
*)
    print_usage
    exit 2
esac
