#!/bin/sh
#define constants
OSFlag_SUSE=1
OSFlag_REDHAT=0
OSFlag=0
function install()
{
    #define dest path
    echo "iSpaceServer is begining to install ....."
    echo "please input the path of iSpaceServer installing:"
    read answer
    until [ -d "$answer" ]   
    do
        tput clear
        echo " $answer is not exists,please input it again:"
        read answer
    done
    path="$answer""/iSpaceServer"
    #check os  and clean before install
    cleanEnvironment $path
    mkdir $path
    #copy files to dest path and define the environment path in apahce & tomcat
    copyFiles $path   
    createInitFile $path   
    apachepath=$path/apache
    sed -i "s#/usr/local/apache#$apachepath#" $apachepath/conf/httpd.conf
    sed -i "s#/usr/local/apache/bin#$apachepath/bin#" $apachepath/bin/apachectl
    mkdir -p /usr/local/apache/conf/
    ln -s $apachepath/conf/httpd.conf /usr/local/apache/conf/httpd.conf
    #init service for redhat or suse
    if [ $OSFlag = $OSFlag_REDHAT ]; then
        cp iSpaceServer /etc/rc.d/init.d
        cd /etc/rc.d/init.d
    fi
    if [ $OSFlag = $OSFlag_SUSE ]; then
        cp iSpaceServer /etc/init.d
        cd /etc/init.d
    fi
    #add the service to daemon  service
    chmod 755 iSpaceServer
    chkconfig --add iSpaceServer
    chkconfig --list iSpaceServer
    #create the uninstall shell script
    createUninstall $path $OSFlag
    cp uninstall "$path"
    rm -f uninstall
    chmod -R 777 "$path"
    echo  "iSpaceServer install end! iSpaceServer service will be starting..."
}
function cleanEnvironment()
{
    tempPath=$1
    echo "del iSpaceServer service..."
    serverstatus=`ps aux|grep java|grep -v grep|grep iSpaceServer |awk '{print $2}'`     
    if [ -n "$serverstatus" ] ;then   
        service iSpaceServer stop
    fi
    OSFlag=$OSFlag_REDHAT
    versionStr=`eval lsb_release -i`
    echo "$versionStr"
    case $versionStr in
    *SUSE*) OSFlag=$OSFlag_SUSE;;
    esac
    if [ $OSFlag = $OSFlag_REDHAT ]; then
        if [ -f /etc/rc.d/init.d/iSpaceServer ] ; then
            chkconfig --del iSpaceServer
            rm -f /etc/rc.d/init.d/iSpaceServer
        fi
    fi
    if [ $OSFlag = $OSFlag_SUSE ]; then
        if [ -f /etc/init.d/iSpaceServer ] ; then
            chkconfig --del iSpaceServer
            rm -f /etc/init.d/iSpaceServer
        fi
    fi
    if [ -d "/usr/local/apache" ]; then
        rm -rf /usr/local/apache/
    fi
    if [ -d "$tempPath" ]; then
    echo "$tempPath already exists,will del $path..."
    rm -rf "$tempPath"
    fi
}
function copyFiles()
{
    tempPath=$1
    cp -r  jre    $tempPath
    cp -r  tomcat $tempPath
    cp -r  apache $tempPath
}
function createInitFile()
{
    path=$1
    echo "copy tomcat and jre to destfiles over..."
    echo "#!/bin/sh"                                     > iSpaceServer
    echo "# chkconfig: 235 64 36"                         >> iSpaceServer
    echo "# description: Qian-Gao iSpace service"         >> iSpaceServer
    echo "# source function library "                     >> iSpaceServer
    echo ""                                             >> iSpaceServer
    echo "case \"\$1\" in"                              >> iSpaceServer
    echo "start)"                                         >> iSpaceServer
    echo "     $path"/apache/bin/apachectl  start        >> iSpaceServer
    echo "     $path"/tomcat/bin/startup.sh             >> iSpaceServer
    echo "     ;;"                                         >> iSpaceServer
    echo "stop)"                                         >> iSpaceServer
    echo "     $path"/apache/bin/apachectl  stop        >> iSpaceServer
    echo "     $path"/tomcat/bin/shutdown.sh             >> iSpaceServer
    echo "     ;;"                                        >> iSpaceServer
    echo "restart)"                                     >> iSpaceServer
    echo "     $path"/apache/bin/apachectl  stop        >> iSpaceServer
    echo "     $path"/tomcat/bin/shutdown.sh             >> iSpaceServer
    echo "     sleep 6"                                    >> iSpaceServer
    echo "     $path"/apache/bin/apachectl  start        >> iSpaceServer
    echo "     $path"/tomcat/bin/startup.sh             >> iSpaceServer
    echo "     ;;"                                         >> iSpaceServer
    echo "*)"                                             >> iSpaceServer
    echo "     echo \"\$0 {start|stop|restart}\""        >> iSpaceServer
    echo "     ;;"                                         >> iSpaceServer
    echo "esac"                                            >> iSpaceServer
}
function createUninstall()
{
    path=$1
    OSFlag=$2
    echo "#!/bin/sh"                                                > uninstall
    echo "echo 'iSpaceServer uninstall begin....'"                  >>uninstall
    if [ $OSFlag = $OSFlag_SUSE ]; then
    echo "if [ -f /etc/init.d/iSpaceServer ] ; then"                     >>uninstall
    echo "  service iSpaceServer stop"                              >>uninstall
    echo "    rm -f /etc/init.d/iSpaceServer"                        >>uninstall
    echo "fi"                                                       >>uninstall
    fi
    if [ $OSFlag = $OSFlag_REDHAT ]; then
    echo "if [ -f /etc/rc.d/init.d/iSpaceServer ] ; then"           >>uninstall
    echo "  service iSpaceServer stop"                            >>uninstall
    echo "    rm -f /etc/rc.d/init.d/iSpaceServer"                   >>uninstall
    echo "fi"                                                      >>uninstall
    fi
    echo "if [ -d \"$path\" ];then"                                 >>uninstall
    echo "  rm -rf \"$path\""                                     >>uninstall
    echo "fi"                                                    >>uninstall   
    echo "if [ -d \"/usr/local/apache/\" ];then"             >>uninstall
    echo "  rm -rf \"/usr/local/apache/\""                                     >>uninstall
    echo "fi"                                                    >>uninstall
}
case "$1" in
install)
    install
;;
*)
    install
;;
esac