#!/bin/sh
export THIS_DIR="$(cd "$( dirname "$0" )" && pwd )"
now_time=$(date +%Y%m%d%H%M%S)  # 執行當下的日期時間(ex. 20191018191432)

mysqlVer=`mysql -V | cut -d ' ' -f 6 | cut -c 1-3`
if [ -n "$mysqlVer" ];then
    apiCustomTagLine=`grep -n '##HgigaAPICustomMyCnf' /etc/my.cnf | head -1 | cut -d ':' -f 1`
    if [ -z "$apiCustomTagLine" ];then
        #備份舊的mysql設定檔
        mycnf_backup_filename=/etc/my.cnf.${now_time}
        /bin/cp -af /etc/my.cnf $mycnf_backup_filename

        if [ $mysqlVer == '5.7' ] ;then
           /bin/cp -af $THIS_DIR/conf/my.cnf.5.7.API /etc/my.cnf
        else
           /bin/cp -af $THIS_DIR/conf/my.cnf.API /etc/my.cnf
        fi

        if [ -f /bin/systemctl ];then
            /bin/systemctl restart mysqld.service
        elif [ -f /etc/init.d/mysqld ] ;then
            /etc/init.d/mysqld restart
        else
            service mysqld restart
        fi
    fi
fi

reStartApache=0
if [ ! -f /etc/httpd/conf.d/hgiga_api.conf ];then
    if [ -f $THIS_DIR/conf/hgiga_api.conf.apache ] || [ -f $THIS_DIR/conf/hgiga_api.conf.apache.2.4 ];then
        reStartApache=1
        if [ -e /etc/httpd/conf.d/ssl.conf ];then
            mv -f /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/ssl.conf.remove
            touch /etc/httpd/conf.d/ssl.conf
        fi
        apacheVer=`apachectl -v |head -n 1 | cut -d '/' -f 2-2 | cut -c 1-3`
        checkApacheVer=`echo $apacheVer 2.3 | awk '{if($1>$2) {print"1"} else {print"0"}}'`
        if [ $checkApacheVer == '1' ] ;then
            /bin/cp -af $THIS_DIR/conf/hgiga_api.conf.apache.2.4 /etc/httpd/conf.d/hgiga_api.conf
        else
            /bin/cp -af $THIS_DIR/conf/hgiga_api.conf.apache /etc/httpd/conf.d/hgiga_api.conf
        fi
    fi
fi

if [ ! -e /etc/httpd/conf.d/ssl.conf ] && [ -e /etc/httpd/conf.d/ssl.conf.remove ];then
    touch /etc/httpd/conf.d/ssl.conf
fi

#如果hgiga_api_user_msg.conf有異動，且與上次內容不同，則設定標記reStartApache
if [ -f /etc/httpd/conf.d/hgiga_api_user_msg.conf.rpmsave ];then
    diff /etc/httpd/conf.d/hgiga_api_user_msg.conf /etc/httpd/conf.d/hgiga_api_user_msg.conf.rpmsave > /dev/null
    compare_value=$?

    if [ $compare_value -eq 1 ];then
        reStartApache=1
        backup_filename=/etc/httpd/conf.d/hgiga_api_user_msg.conf.rpmsave.${now_time}
        mv -f /etc/httpd/conf.d/hgiga_api_user_msg.conf.rpmsave $backup_filename
    fi
fi

if [ "$reStartApache" == '1' ];then
    if [ -f /bin/systemctl ];then
        /bin/systemctl restart httpd.service
    elif [ -f /etc/init.d/httpd ] ;then
        /etc/init.d/httpd restart
    else
        service httpd restart
    fi
fi

if [ ! -d /var/lock/hgiga-api ];then
    mkdir -p /var/lock/hgiga-api
    chmod 0777 /var/lock/hgiga-api
fi

if [ ! -d /etc/cron.min ];then
    mkdir -p /etc/cron.min
    chmod 0755 /etc/cron.min
fi

if [ ! -d /etc/cron.per5min ];then
    mkdir -p /etc/cron.per5min
    chmod 0755 /etc/cron.per5min
fi

if [ ! -d /etc/cron.per10min ];then
    mkdir -p /etc/cron.per10min
    chmod 0755 /etc/cron.per10min
fi

if [ ! -d /etc/cron.per15min ];then
    mkdir -p /etc/cron.per15min
    chmod 0755 /etc/cron.per15min
fi

if [ ! -d /etc/cron.per30min ];then
    mkdir -p /etc/cron.per30min
    chmod 0755 /etc/cron.per30min
fi

#自動校時
chmod +x /etc/cron.hourly/update_system_time.sh

#產生空的favicon圖標檔案，避免apache產生error log
touch /var/www/html/favicon.ico

## 移除apache 預設目錄
if [ -d /var/www/manual ];then
    rm -rf /var/www/manual
    rm -f /etc/httpd/conf.d/manual.conf
fi

if [ -d /var/www/icons ];then
    rm -rf /var/www/icons
fi

## 關閉SELINUX
selinuxCnf=`cat /etc/selinux/config | grep SELINUX= | grep -v '#' | cut -d '=' -f 2`
if [ "$selinuxCnf" != 'disabled' ];then
    selinuxLineNum=`grep -n 'SELINUX=' /etc/selinux/config | grep  -v '#' | head -1 | cut -d ':' -f 1`
    sed -i $selinuxLineNum"c SELINUX=disabled" /etc/selinux/config
    setenforce 0
else
    echo "SELINUX already disabled"
fi

## 關閉PrivateTmp
if [ -f /usr/lib/systemd/system/httpd.service ];then
    httpPrivateTmpCnf=`cat /usr/lib/systemd/system/httpd.service | grep PrivateTmp= | grep -v '#' | cut -d '=' -f 2`
    if [ "$httpPrivateTmpCnf" == 'true' ];then
        privateTmpLineNum=`grep -n 'PrivateTmp=' /usr/lib/systemd/system/httpd.service | grep -v '#' | head -1 | cut -d ':' -f 1`
        sed -i $privateTmpLineNum"c PrivateTmp=false" /usr/lib/systemd/system/httpd.service
        systemctl daemon-reload
        /bin/systemctl restart httpd.service
    fi
fi

