#!/bin/bash

#############################################
#
#                  PlexUA
#
#                                  v.0.1
#############################################

#########################################################################
#	PlexUA - Software designed to create additional FTP
#	users for a domain within the Parallels Plesk software.
#
#     Copyleft (C) 2009  Ricardo Canto - Union Dev
#     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 3 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.
#
#########################################################################


clear


##############################################
#
#     add user / exit functions
#
##############################################



function add_user {

if cat /etc/passwd |awk -F: '{print $1}'| grep $username > /dev/null
then
echo -e "\n\nuser exists : Abort!"
exit

else


if useradd -u $U_ID -o -d $DEFAULT_PATH$path -g psacln -s /bin/false $username
then
    echo -e "User Added!\n\n"

    echo -e "Assign Password\n\n"

        passwd $username

else

    echo -e "\n\n\t User could not be added !\n\n\t Action Aborted !!"

exit
fi


fi
}


function fail_add {


echo -e "\n\nPC LOAD LETTER"

exit


}


##############################################################
#
# Generate List of Domains and Put into DOMAINS array
#
##############################################################


DOMAIN_LIST=`grep '/var/www/*/vhost' /etc/passwd| grep -v 'web_users' |awk -F: '{print $6}'|awk -F/ '{print $5}'| sort|uniq`
i=0
for domain in `echo $DOMAIN_LIST`
do
DOMAINS[$i]="$domain"
let i+=1
done


##############################################################
#
#    Display menu header, reiterate through array and 
#          list options of available domains
#
##############################################################



echo -e "\n\n##############################################\n"
echo -e "Add FTP user to which of the following domains\n"
echo -e  "##############################################\n\n"

for ((count=0;$count<$i;count++)); do
   echo -e "\n $count) ${DOMAINS[${count}]}"
done


echo -e "\n\n"

dom_num=$i

##############################
#  Test that choice is valid
##############################

while [ $dom_num -ge $i ]
do
echo -n "Choice: "
read dom_num
done

##############################################################
#
#   Start working on ftp for chosen domain
#
##############################################################

##############
# Variables 
##############

U_ID=`grep ${DOMAINS[${dom_num}]} /etc/passwd| grep -v 'web_users'|grep -v 'subdomain' |awk -F: '{print $3}'|sort|uniq | head -1`
DEFAULT_PATH="/var/www/vhosts/${DOMAINS[${dom_num}]}/"

clear 

echo -e "\n\n##############################################\n"
echo -e "\tAdding FTP user for : \n\tDomain - ${DOMAINS[${dom_num}]}\n\tUID - $U_ID\n"
echo -e  "##############################################\n\n"


echo -n "Username: "
read username 

echo -e "\n"

echo -n "Path: $DEFAULT_PATH"
read path

echo -e "\n\n"

echo -e "\n\n##############################################\n"
echo -e "              Is this Correct?"
echo -e "\n##############################################\n\n"



echo -e "Domain : ${DOMAINS[${dom_num}]}\n" 

echo -e "Username : $username\n"

echo -e "Path : $DEFAULT_PATH$path\n"


valid=0

while [ $valid -eq 0 ]
do
valid=1
echo -en "\t\t(y/n): "
read correct

case "$correct" in
        y)
            add_user
            ;;
         
        n)
	    fail_add
            ;;
       *)
           valid=0 

esac
done



