#!/bin/bash



clear



echo ==================================================================
echo "                     UserAdd Process Start                      "
echo ==================================================================



echo -n "User Name : "
read username

useradd $username

echo ------------------------

echo PassWord Setting

echo ------------------------
passwd $username

echo "User Add Success!!!"

echo ==================================================================
echo "  Tomcat User Add OR PHP CONFIG  & VirtualHost config making    "
echo ==================================================================

existCheck=$(grep -n "#SERVICE $username CONFIG START" /etc/httpd/conf/VirtualHost.conf | cut -d: -f1 | head -1)

function fn_tomcatConfigration(){
echo "Tomcat User Add.."
echo "****************************************************************"
cat /etc/httpd/conf/port_list
echo "****************************************************************"
echo -n "add New AJP Port : "
read ajpport
echo -n "add New Shutdown Port : "
read shutdownport
echo -n "add New Redirect Port : "
read redirectport
echo "SERVICE : $username / AJP_PORT : $ajpport / SHUTDOWN_PORT : $shutdownport / REDIRECT_PORT : $redirectport" >> /etc/httpd/conf/port_list

echo "#SERVICE $username CONFIG START" >> /etc/httpd/conf/workers.properties
echo "#AJP port : $ajpport" >> /etc/httpd/conf/workers.properties
echo "#Shutdown port : $shutdownport" >> /etc/httpd/conf/workers.properties
echo "#Redirect port : $redirectport" >> /etc/httpd/conf/workers.properties
echo "worker.list = $username" >> /etc/httpd/conf/workers.properties
echo "worker.$username.type=ajp13" >> /etc/httpd/conf/workers.properties
echo "worker.$username.host=localhost" >> /etc/httpd/conf/workers.properties
echo "worker.$username.port=$ajpport" >> /etc/httpd/conf/workers.properties
echo "worker.$username.lbfactor=1" >> /etc/httpd/conf/workers.properties
echo "#SERVICE $username CONFIG END" >> /etc/httpd/conf/workers.properties

echo "#SERVICE $username CONFIG START" >> /etc/httpd/conf/VirtualHost.conf
echo "#AJP port : $ajpport" >> /etc/httpd/conf/VirtualHost.conf
echo "#Shutdown port : $shutdownport" >> /etc/httpd/conf/VirtualHost.conf
echo "#Redirect port : $redirectport" >> /etc/httpd/conf/VirtualHost.conf
echo "<VirtualHost *:80>" >> /etc/httpd/conf/VirtualHost.conf
echo "DocumentRoot /home/$username/webapps/ROOT" >> /etc/httpd/conf/VirtualHost.conf
echo "ServerName $username.ubncorp.co.kr" >> /etc/httpd/conf/VirtualHost.conf
echo "ServerAlias $username.ubncorp.co.kr" >> /etc/httpd/conf/VirtualHost.conf
echo "ErrorLog /var/log/httpd/$username-error_log" >> /etc/httpd/conf/VirtualHost.conf
echo "CustomLog /var/log/httpd/$username-access_log combiend" >> /etc/httpd/conf/VirtualHost.conf
echo "" >> /etc/httpd/conf/VirtualHost.conf
echo "JkMount /* $username" >> /etc/httpd/conf/VirtualHost.conf
echo "JkUnMount /*.gif ajp13" >> /etc/httpd/conf/VirtualHost.conf
echo "JkUnMount /*.jpg ajp13" >> /etc/httpd/conf/VirtualHost.conf
echo "JkUnMount /*.jpeg ajp13" >> /etc/httpd/conf/VirtualHost.conf
echo "JkUnMount /*.bmp ajp13" >> /etc/httpd/conf/VirtualHost.conf
echo "JkUnMount /*.png ajp13" >> /etc/httpd/conf/VirtualHost.conf
echo "JkUnMount /*.htm ajp13" >> /etc/httpd/conf/VirtualHost.conf
echo "JkUnMount /*.html ajp13" >> /etc/httpd/conf/VirtualHost.conf
echo "</VirtualHost>" >> /etc/httpd/conf/VirtualHost.conf
echo "#SERVICE $username CONFIG END" >> /etc/httpd/conf/VirtualHost.conf

echo "<?xml version='1.0' encoding='utf-8'?>" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "<Server port='$shutdownport' shutdown='SHUTDOWN'>" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "  <Listener className='org.apache.catalina.core.AprLifecycleListener' SSLEngine='on' />" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "  <Listener className='org.apache.catalina.core.JasperListener' />" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "  <Listener className='org.apache.catalina.core.JreMemoryLeakPreventionListener' />" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "  <Listener className='org.apache.catalina.mbeans.GlobalResourcesLifecycleListener' />" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "  <Listener className='org.apache.catalina.core.ThreadLocalLeakPreventionListener' />" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "  <Service name='Catalina'>" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "    <Connector port='$ajpport' protocol='AJP/1.3' redirectPort='$redirectport' URIEncoding='UTF-8' />" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "      <Engine name='Catalina' defaultHost='$username.ubncorp.co.kr'>" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "        <Host name='$username.ubncorp.co.kr' appBase='/home/$username/webapps' unpackWARs='true' autoDeploy='true' xmlValidation='false' xmlNamespaceAware='false'>" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "          <Alias>$username.ubncorp.co.kr</Alias>" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "          <Valve className='org.apache.catalina.valves.AccessLogValve' directory='/home/$username/logs'" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "               prefix='$username.ubncorp.co.kr_access_log.' suffix='.txt'" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "               pattern='%h %l %u %t &quot;%r&quot; %s %b' />" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "        </Host>" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "      </Engine>" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "  </Service>" >> /usr/share/tomcat7/virtualhost/$username-server.xml
echo "</Server>" >> /usr/share/tomcat7/virtualhost/$username-server.xml

SERVERS_HOME=/home
SERVER_NAME=$username
SERVER_PATH="$SERVERS_HOME/$username"
TOMCAT_HOME=/usr/share/tomcat7
echo "new tomcat will create in "$SERVER_PATH"....."
mkdir $SERVER_PATH/logs
cp -Rf $TOMCAT_HOME/webapps $SERVER_PATH/webapps
cp -Rf $TOMCAT_HOME/conf $SERVER_PATH/conf
#ln -s $TOMCAT_HOME/bin $SERVER_PATH/bin
#ln -s $TOMCAT_HOME/lib $SERVER_PATH/lib
chown -R $username:$username $SERVER_PATH
chmod -R 755 $SERVER_PATH
echo "webapps subdirectory deleted..."
rm -rf /home/$username/webapps/manager
rm -rf /home/$username/webapps/host-manager
rm -rf /home/$username/webapps/examples
rm -rf /home/$username/webapps/docs
rm -rf /home/$username/webapps/ROOT
echo "Tomcat User Add Success!!!"
}

function fn_removeTomcatConfig(){
rm -f /usr/share/tomcat7/virtualhost/$username-server.xml
rm -rf /home/$username/*
mv /etc/httpd/conf/port_list /etc/httpd/conf/port_list.tmp

while read line1;
do
if [[ $line1 != *"$username"*  ]]; then
echo "$line1" >> /etc/httpd/conf/port_list
fi
done < /etc/httpd/conf/port_list.tmp
rm -f /etc/httpd/conf/port_list.tmp

sed -i "/#SERVICE $username CONFIG START/,/#SERVICE $username CONFIG END/d" /etc/httpd/conf/VirtualHost.conf
cat /etc/httpd/conf/VirtualHost.conf
sed -i "/#SERVICE $username CONFIG START/,/#SERVICE $username CONFIG END/d" /etc/httpd/conf/workers.properties
cat /etc/httpd/conf/workers.properties
echo "delete user from config is complete..."

echo "**************************************************************"
echo "  workers.properties AND VirtualHost.conf REMOVE COMPLETE!!!  "
echo "**************************************************************"
}

function fn_overwirteTomcatConfig(){
if [ $existCheck ]; then
echo -n "user exist! overwrite user? (Y/N) : "
read overwrite_yn
if [ "$overwrite_yn" == "Y" ]; then
fn_removeTomcatConfig
fn_tomcatConfigration
elif [ "$overwrite_yn" == "N" ]; then
echo "tomcat configration pass!!!"
else
echo "Please check the input value.. (overwrite Y/N)"
fn_overwirteTomcatConfig
fi
else
fn_tomcatConfigration
fi
}

function fn_phpConfig(){

echo "PHP VirtualHost setting..."
echo -n "PHP root Directory path : "

echo "/home/$username/public_html"

mkdir -p /home/$username/public_html

echo "#SERVICE $username CONFIG START" >> /etc/httpd/conf/VirtualHost.conf
echo "#support PHP config.." >> /etc/httpd/conf/VirtualHost.conf
echo "<VirtualHost *:80>" >> /etc/httpd/conf/VirtualHost.conf
echo "   DocumentRoot /home/$username/public_html" >> /etc/httpd/conf/VirtualHost.conf
echo "   ServerName $username.ubncorp.co.kr" >> /etc/httpd/conf/VirtualHost.conf
echo "   ServerAlias $username.ubncorp.co.kr" >> /etc/httpd/conf/VirtualHost.conf
echo "   ErrorLog /home/$username/log/$username-error_log" >> /etc/httpd/conf/VirtualHost.conf
echo "   CustomLog /home/$username/log/$username-access_log combiend" >> /etc/httpd/conf/VirtualHost.conf
echo "</VirtualHost>" >> /etc/httpd/conf/VirtualHost.conf
echo "#SERVICE $username CONFIG END" >> /etc/httpd/conf/VirtualHost.conf

echo -n "PHP log path : "
echo "/home/$username/public_html"
mkdir -p /home/$username/log

chmod 775 /home/$username
chmod 775 /home/$username/public_html
chmod 775 /home/$username/log

chown $username.$username /home/$username/public_html
chown $username.$username /home/$username/log

echo " " >>/home/$username/public_html/index.php
echo "<? phpinfo(); ?>" >> /home/$username/public_html/index.php

chown $username.$username /home/$username/public_html/index.php
chmod 775 /home/$username/public_html/index.php

echo checking -----------------------------------------------------------
ls -al /home/$username
echo checking -----------------------------------------------------------
ls -al /home/$username/public_html

}

function fn_overwirtePHPConfig(){
if [ $existCheck ]; then
echo -n "user exist! overwrite user? (Y/N) : "
read overwrite_yn1
if [ "$overwrite_yn1" == "Y" ]; then
fn_removeTomcatConfig
fn_phpConfig
elif [ "$overwrite_yn1" == "N" ]; then
echo "PHP configration pass!!!"
else
echo "Please check the input value.. (overwrite Y/N)"
fn_overwirtePHPConfig
fi
else
fn_phpConfig
fi
}

function fn_selectLanguage(){
echo -n "select Development Language....... (PHP/JAVA) : "
read language
if [ "$language" == "JAVA" ]; then
fn_overwirteTomcatConfig
elif [ "$language" == "PHP" ]; then
fn_overwirtePHPConfig
else
echo "Please check the input value.. (Supported language : PHP / JAVA)"
fn_selectLanguage
fi
}

fn_selectLanguage

echo ==================================================================
echo "                            SVN Create                          "
echo ==================================================================

function fn_createSvn(){
echo -n "create SVN repo? (Y/N) : "
read create_svn_yn
if [ "$create_svn_yn" == "Y" ]; then
svnadmin create --fs-type fsfs /repos/$username
ls -l /repos
mv /repos/$username/conf/svnserve.conf /repos/$username/conf/svnserve.conf.old
echo "[general]" >> /repos/$username/conf/svnserve.conf
echo "anon-access = none" >> /repos/$username/conf/svnserve.conf
echo "auth-access = write" >> /repos/$username/conf/svnserve.conf
echo "password-db = passwd" >> /repos/$username/conf/svnserve.conf
echo "authz-db = authz" >> /repos/$username/conf/svnserve.conf
cat /repos/$username/conf/svnserve.conf

echo "SVN Create Success!!!"
elif [ "$create_svn_yn" == "N" ]; then
echo "SVN Create PASS!!!"
else
echo "Please check the input value.. (create SVN repo : Y/N)"
fn_createSvn
fi
}

fn_createSvn

echo ==================================================================
echo "                MariaDB Create Database & useradd               "
echo ==================================================================

function fn_createMariaDB(){
echo -n "create MariaDB user and database? (Y/N) : "
read create_db_yn
if [ "$create_db_yn" == "Y" ]; then
echo "Username : $username"
echo "dbname : $username"
echo -n "User p/w : "
read userpw
echo " " >> mysql_useradd.sql
echo "use mysql" >> mysql_useradd.sql
echo "create database $username default character set utf8 COLLATE utf8_general_ci;" >> mysql_useradd.sql
echo "insert into user (host, user, password) values ('localhost', '$username', password('$userpw'));" >> mysql_useradd.sql
echo "GRANT ALL PRIVILEGES ON $username.*TO '$username'@'%' IDENTIFIED BY '$userpw' with GRANT OPTION;" >> mysql_useradd.sql
echo "FLUSH PRIVILEGES;" >> mysql_useradd.sql
mysql -u root -ptft105 < ./mysql_useradd.sql
cat ./mysql_useradd.sql
rm -rf ./mysql_useradd.sql
echo "MariaDB Create Success!!!"
elif [ "$create_db_yn" == "N" ]; then
echo "MariaDB Create PASS!!!"
else
echo "Please check the input value.. (create MariaDB user and database : Y/N)"
fn_createMariaDB
fi
}

fn_createMariaDB

echo ==================================================================
echo "                               END                              "
echo ==================================================================
service httpd restart
cd /home/$username
su $username