#!/bin/bash
PA=abc.com
JA=/JavaWeb
TT=/usr/local/tomcat

#Gets the parameter

[ $# -ne 1 ] && echo "Please enter an instance name (For example: ./tomcatadd test.wltest.com)" && exit 1
ITN=`echo $1`
#Add new instance information

ADD () {
if [ -d $JA/$ITN ];then
    echo "Warning:There are already exists $JA/$ITN,Please check"
    ANII
elif [ -d $TT/$ITN ];then
    echo "Warning:There are already exists $TT/$ITN,Please check"
    ANII
else
    TBCI
fi
}

ANII () {
read -p "Enter the instance name you want to create:" ITN1
if [ -d $JA/$ITN1 ];then
    echo "Warning:There are already exists $JA/$ITN1,Please check"
    ANII
elif [ -d $TT/$ITN1 ];then
    echo "Warning:There are already exists $TT/$ITN1,Please check"
    ANII
else
    echo "The instance name is $ITN1"
    read -p "Is ok,please enter y or n:" READA
        if [ $READA == y ];then
            ITN=`echo $ITN1`
            TBCI
        else
            ANII
        fi
fi
}

#Testing basic configuration information
TBCI () {
/bin/echo "" > /tmp/tomcatadd.txt
F=`find /usr/local/tomcat/ -name "server.xml"`
for i in $F
    do
    N=`sed -n  "/port=/p" $i | awk '{print $2}'| grep -v "protocol" | grep -v "8443"| awk -F "=" '{print $2}'|awk -F '"' '{print $2}'`
    echo $N >> /tmp/tomcatadd.txt
done
/bin/netstat -lnpt | grep java |awk '{print $4}'|awk -F: '{print $NF}' >> /tmp/tomcatadd.txt


[ ! -d $JA ] && echo "Warning:Not found $JA,Please check " && exit 1
[ ! -d $TT ] && echo "Warning:Not found $TT,Please check " && exit 1
[ ! -d $TT/conf/ ] && echo "Warning:Not found $TT/conf/,Please check " && exit 1
[ ! -d $TT/webapps/ ] && echo "Warning:Not found $TT/webapps/,Please check " && exit 1
[ ! -d $TT/temp/ ] && echo "Warning:Not found $TT/temp/,Please check " && exit 1
[ ! -d $TT/work/ ] && echo "Warning:Not found $TT/work/,Please check " && exit 1
[ ! -d $TT/logs/ ] && echo "Warning:Not found $TT/logs/,Please check " && exit 1

ANIIUN
}

#Select the administrator account

ANIIUN () {
UNX=`echo $ITN | awk -F "." '{print $1}'`
if [ "$(/bin/grep "^$UNX" /etc/passwd)" == "" ];then
   UN=`echo $UNX`
   RAN1
else
   ANIIUNF
fi
}

ANIIUNF () {
UNF=`echo $(( RANDOM % 9+1 ))`
UNX1=`echo $UNX$UNF`
if [ "$(/bin/grep "^$UNX1" /etc/passwd)" == "" ];then
   UN=`echo $UNX1`
   RAN1
else
   ANIIUNF
fi
}

#Randomly generated port numbers

RAN1 () {
RANU1=`echo $(( RANDOM % 918+8081 ))`
if [ "$(/bin/find /tmp/tomcatadd.txt | /usr/bin/xargs /bin/grep $RANU1)" == "" ];then
    echo $RANU1 >> /tmp/tomcatadd.txt
    NUM1=`echo $RANU1`
    RAN2
else
    RAN1
fi
}

RAN2 () {
RANU2=`echo $(( RANDOM % 918+8081 ))`
if [ "$(/bin/find /tmp/tomcatadd.txt | /usr/bin/xargs /bin/grep $RANU2)" == "" ];then
    echo $RANU2 >> /tmp/tomcatadd.txt
    NUM2=`echo $RANU2`
    RAN3
else
    RAN2
fi
}

RAN3 () {
RANU3=`echo $(( RANDOM % 918+8081 ))`
if [ "$(/bin/find /tmp/tomcatadd.txt | /usr/bin/xargs /bin/grep $RANU3)" == "" ];then
    NUM3=`echo $RANU3`
    AIAS
else
    RAN3
fi
}

 

#Add an instance, and make the appropriate settings
AIAS () {
mkdir -p $JA/$ITN/ROOT
echo -e "#!/bin/bash\nexport  CATALINA_BASE=$TT/$ITN\nsh "\$CATALINA_HOME"/bin/startup.sh" >$JA/$ITN/start.sh
echo -e "#!/bin/bash\nexport  CATALINA_BASE=$TT/$ITN\nsh "\$CATALINA_HOME"/bin/shutdown.sh" >$JA/$ITN/stop.sh
chmod 755 $JA/$ITN/start.sh
chmod 755 $JA/$ITN/stop.sh
mkdir $TT/$ITN
cp -r $TT/conf/ $TT/$ITN
cp -r $TT/webapps/ $TT/$ITN
cp -r $TT/temp/ $TT/$ITN
cp -r $TT/work/ $TT/$ITN
cp -r $TT/logs/ $TT/$ITN
sed -i -e s/webapps/"\/JavaWeb\/$ITN"/ $TT/$ITN/conf/server.xml

#Modify the port number
sed -i s/8005/$NUM1/ $TT/$ITN/conf/server.xml
sed -i s/8080/$NUM2/ $TT/$ITN/conf/server.xml
sed -i s/8009/$NUM3/ $TT/$ITN/conf/server.xml

#Add an account
useradd $UN
echo $PA | passwd --stdin $UN &>/dev/null
chown -R $UN $JA/$ITN
chown -R $UN $TT/$ITN

#Information confirmation
echo -e "\n"
echo "Make sure the information is correct"
echo -e "\n"
sed -n '22p' $TT/$ITN/conf/server.xml
sed -n '79p' $TT/$ITN/conf/server.xml
sed -n '105p' $TT/$ITN/conf/server.xml
sed -n '137,138p' $TT/$ITN/conf/server.xml
echo -e "\n"

#Information output
echo -e "你好,\n$ITN服务器如下:\nIP:192.168.1.1\n管理员账号:$UN\n密码:$PA\n\n账号权限仅限于以下目录:\nTomcat实例路径:$TT/$ITN \n程序存放目录:$JA/$ITN\n程序本地直接测试端口号:$NUM2(不允许私自更改)\n域名生效后,程序外网访问:$ITN  \n\n另外提示下: \n$JA/$ITN目录下的 \nstart.sh为启动应用程序 \nstop.sh为关闭应用程序 \n谢谢!"
}

#The main program
ADD