#!/bin/sh
#-----------------------------------------------------------------------------
# /var/install/bin/eisportal-tools-userdrop [username]
#
# Creation:     28.06.2006 ys
# Last Update:  $Id$
#
# Copyright (c) 2005-2006 Yves Schumann <eisportal(at)hiluxumbau(dot)de>
#
# This program is free software; you can redistribute it and/or modify
# it under the 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.
#-----------------------------------------------------------------------------

#include eislib
. /var/install/include/eislib
. /etc/config.d/eisportal

mysql_data_dir=/var/lib/mysql
mysql_base_dir=/usr/local/mysql

required_mysql_version_for_cui="1.2.14"		# ab dieser Version sind die
                                            # benötigten Funktionen verfügbar


### -------------------------------------------------------------------------
### use list-mysql.cui tool
### -------------------------------------------------------------------------
# check if use cui version 
. /etc/config.d/setup


### -------------------------------------------------------------------------
### Check for MySQL-Version to use Curses-Funktions
### -------------------------------------------------------------------------

check=`/var/install/bin/check-version mysql ${required_mysql_version_for_cui}`
case ${check} in
    not-installed)
        useCui="no"		# dieser Fall sollte eigentlich nie eintreten
    ;;
    new)
        useCui="no"
    ;;
    old)
        useCui="yes"
    ;;
    installed)
        useCui="yes"
    ;;
esac


if [ "$MENU" = "/var/install/bin/show-menu.cui" -a $# -lt 1 -a "${useCui}" = "yes" ]
then
    ${mysql_base_dir}/bin/list-mysql.cui --title="Show EisPortal users:" \
        --database=${EISPORTAL_DB_NAME} \
        --server=${EISPORTAL_DB_HOST} \
        --user=${EISPORTAL_DB_ADMIN_USER} \
        --pass=${EISPORTAL_DB_ADMIN_PASS} \
        --column="Login,Firstname,Lastname,Role" \
        --query="SELECT name, fname, lname, ELT(role+1,'No rights','User','-','Portaladmin')  FROM eisportal_user" \
        --quest="Delete user: %s from Eisportal?" \
        --script=/var/install/bin/eisportal-tools-userdrop \
        --helpfile=/var/install/help/eisportal \
        --helpname=EISPORTAL_MENU_USERDROP
    exit 0     
fi


#-----------------------------------------------------------------------------
# check for mysql
#-----------------------------------------------------------------------------
if [ ! -f /var/run/mysql.pid ]
then 
    mecho "" 
    mecho -error "MySQL Server not running" 
    mecho "" 
    exit 1 
fi

# clear screen after mysql check
clrhome

#-----------------------------------------------------------------------------
# if username was given, use this given name
# else ask for username to remove this user
#-----------------------------------------------------------------------------
if [ $# -lt 1 ]
then
    userToRemove=`/var/install/bin/ask "Enter user to remove: " "" "*"`
    mecho ""
else
    userToRemove=$1
    yesRemoveNow="yes"
fi

if [ -z "$userToRemove" ]
then
    mecho -info "Empty username, exiting!"
    mecho ""
    exit 1
else
    mecho ""
    #-----------------------------------------------------------------------------
    # is user existing? extract role too...
    #-----------------------------------------------------------------------------
    result=`${mysql_base_dir}/bin/mysql --skip-column-names -h ${EISPORTAL_DB_HOST} -D${EISPORTAL_DB_NAME} -u${EISPORTAL_DB_ADMIN_USER} -p${EISPORTAL_DB_ADMIN_PASS} -e"SELECT ID, role FROM eisportal_user WHERE ( name = '${userToRemove}' );"`
    # mecho "result: $result"
    user_ID=`echo "${result}" | cut -s -f1`
    user_role=`echo "${result}" | cut -s -f2`
    # mecho "user_ID: $user_ID user_role: $user_role"
        
    if [ -z "${user_ID}" ]
    then
        mecho -warn "User ${userToRemove} not existing, nothing to remove."
    else
        if [ -z "${user_ID}" ]
        then
           mecho "No user_ID found, nothing to remove, exiting!"
              mecho ""
          exit 1
       else
           if [ -z "$yesRemoveNow" ]
           then
                if [ ${user_role} -eq 3 ]
              then
                 yesRemoveNow=`/var/install/bin/ask "User is a EisPortal-Administrator! Really remove?" "n"`
              else
                   yesRemoveNow=`/var/install/bin/ask "Really remove user?" "n"`
              fi
            fi
            if [ "$yesRemoveNow" == "yes" ]
          then
              mecho -info -n "Removing user ${userToRemove} from rights table... "
             ${mysql_base_dir}/bin/mysql -h ${EISPORTAL_DB_HOST} -D${EISPORTAL_DB_NAME} -u${EISPORTAL_DB_ADMIN_USER} -p${EISPORTAL_DB_ADMIN_PASS} -e"DELETE FROM eisportal_rights WHERE ( user_ID = '${user_ID}' );"
             mecho -info "Done."
             mecho -info -n "Removing user ${userToRemove} from user table... "
             ${mysql_base_dir}/bin/mysql -h ${EISPORTAL_DB_HOST} -D${EISPORTAL_DB_NAME} -u${EISPORTAL_DB_ADMIN_USER} -p${EISPORTAL_DB_ADMIN_PASS} -e"DELETE FROM eisportal_user WHERE ( ID = '${user_ID}' );"
             mecho -info "Done."
          else
             mecho -info "User not removed."
          fi
        fi
    fi
    mecho ""
fi

anykey
exit 0

### ---------------------------------------------------------------------------
### End
###----------------------------------------------------------------------------