#!/bin/bash
#******************************************************************************
# @(#) STOP_UML: stop the running UML instance for a particular user
# @(#) Copyright (C) 2006 by KUDOS BVBA <info@kudos.be>.  All rights reserved.
# @(#) $Id: stop_uml,v 1.1 2006/07/22 20:54:38 patrick Exp $
# @(#) vim: set et ts=4 sw=4 ffs=unix:
#******************************************************************************
# This program is a free software; you can redistribute it and/or modify
# it under the same terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
#
#******************************************************************************
# Description: this script will stop the running UML instance for a particular 
# system using the 'uml_mconsole' utility
#
#******************************************************************************

#******************************************************************************
# DATA structures (global)
#******************************************************************************

# -------------------- configuration starts here ------------------------------
# path setting
PATH="/usr/bin:/usr/sbin:/usr/local/bin:/sbin:/bin"
# --------------------- configuration ends here -------------------------------
SCRIPT_NAME="$(basename $0)"


#******************************************************************************
# FUNCTION routines
#******************************************************************************

# display help text
function display_usage ()
{
    cat << EOT
**** ${SCRIPT_NAME} **** 
(c) 2006 - KUDOS BVBA (Patrick Van der Veken) ****

Stop running UML instance for a particular system user

Syntax: ./${SCRIPT_NAME} [-h] [<user name>]

-h            : show this help text
<user name>   : name of the system user owning the UML instanced
EOT
    exit 0
}

# check that we run as root
function check_runas ()
{
    PROC_OWNER=$(IFS='()'; set -- $(id); echo $2)
    [[ "${PROC_OWNER}" != "root" ]] && { 
        echo "ERROR: script must be run as root"
        exit 1
    } 
    return 0
}

# check that the UML system user exists
function check_user ()
{
    CHECK_USER=$(grep -c -E -e "^${UML_OWNER}" /etc/passwd)
    [[ "${CHECK_USER}" -eq 0 ]] && { 
        echo "ERROR: UML owner account does not exist in /etc/passwd: ${UML_OWNER}"
        exit 1
    } 
    return 0
}

# check for helpers applications
function check_helpers
{
    UML_MCONSOLE=$(which uml_mconsole)
    [[ ! -x ${UML_MCONSOLE} ]] && {
        echo "ERROR: cannot find UML helper 'uml_mconsole' in PATH: ${PATH}"
        exit 1
    }
    return 0
}

# check for running UML instance
function check_uml 
{
    CHECK_UML=$(screen -ls | grep -c "${UML_OWNER}")
    [[ ${CHECK_UML} -eq 0 ]] && {
        echo "ERROR: no running UML instance for user: ${UML_OWNER}"
        exit 1
    }
    return 0
}


#******************************************************************************
# MAIN routine
#******************************************************************************

# check for cmd line option
if [[ $# -eq 1 ]] 
then
    if [[ "$1" = "-h" ]]
    then
        display_usage
    else
        UML_OWNER="$1"
    fi
else
    display_usage
fi

# do  checks
echo "Checking script user ..."
check_runas
echo "Checking UML user ..."
check_user
echo "Checking UML helper applications ..."
check_helpers
echo "Checking for running UML instance ..."
check_uml

# stop UML instance
echo "Stopping UML ..."
UML_CMD="uml_mconsole ${UML_OWNER} sysrq s; uml_mconsole ${UML_OWNER} sysrq u; uml_mconsole  ${UML_OWNER} sysrq e; uml_mconsole ${UML_OWNER} halt"
su ${UML_OWNER} -c "${UML_CMD}"
echo "UML instance stopped ..."

exit 0

#******************************************************************************
# END of script 
#******************************************************************************
