#!/bin/bash
#cookie.data=
WEB_URL="192.168.11.200:30179"
ECM_LOGIN_USER="account=admin&password=edoc2edoc2"
ECM_LOGIN_URL="http://${WEB_URL}/api/auth/login"
PACKAGE_FORMS_DIR="content/forms"
EFORM_PUBLISH_ID="ef8c4c74-0133-4027-ba6d-6c9c645e41b1"
EFORM_IMPORT_URL="http://${WEB_URL}/inbiz/eform/admin/export/ImportSystemData"
EFORM_AUDIT_GROUP_ID="d2c5fd5e-1f92-46e6-be41-d45dec192d55"
EFORM_DMS_GROUP_ID="d323a882-6c65-40cf-bb35-5e02cf0827c6"
EFORM_RMS_GROUP_ID="432b66b1-af52-4ccb-a139-059aa004ec40"
EFORM_TMS_GROUP_ID="ed816d72-8f7c-481b-965c-9ac1b4b11f75"
EFORM_AMS_GROUP_ID="82d890be-994d-41e9-b9be-f10f6e6375aa"
EFORM_SYS_GROUP_ID="c1b17e05-5891-4c40-aa6c-5c893e9205b9"
EFORM_RES_IMPORT_URL="http://${WEB_URL}/inbiz/eform/FileResourceManager/CreateFile"
PACKAGE_DATA_DIR="content/data"
TYPE="allinone"
NAMESPACE="edoc2"
MYSQL_URL="192.168.11.200"
MYSQL_PORT="30036"
#MYSQL_USER="repl"
MYSQL_PASSWORD="1qaz2WSX1qaz2WSX"
MYSQL_USER="eform"
#MYSQL_PASSWORD="Xjb0^DDy"
MYSQL_DATABASE="edoc2v5"

rpm -ivh libs/centos/oniguruma-5.9.5-3.el7.art.x86_64.rpm 
rpm -ivh libs/centos/jq-1.5-1.el7.art.x86_64.rpm
yum remove mariadb-libs -y
rpm -ivh libs/centos/mysql-community-common-8.0.17-1.el7.x86_64.rpm
rpm -ivh libs/centos/mysql-community-libs-8.0.17-1.el7.x86_64.rpm 
rpm -ivh libs/centos/mysql-community-client-8.0.17-1.el7.x86_64.rpm 

urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }

showmsg(){ 
  echo -e "\e[1;${Colors[$1]}m$2 \e[0m"
}

login_admin(){
  showmsg info "Gets the credentials of the admin user."
  curl -k -c cookie.data -d ${ECM_LOGIN_USER} -v ${ECM_LOGIN_URL}
  showmsg info ""
}

# result.json  通过curl请求获取
# import eform form.
initialize_forms(){
    if [ ! -d ${PACKAGE_FORMS_DIR} ];then return;fi

    for file in `ls ${PACKAGE_FORMS_DIR} | grep .edoc2sysdata`
    do
        echo "11111111111111111111111111111111111111111111111111111111111111"
        showmsg info "Importing eform form ${file}"
        curl -k -b cookie.data -o result.json -F "SystemFile=@${PACKAGE_FORMS_DIR}/${file}" -F "isReplace=true" -F "PublishId=${EFORM_PUBLISH_ID}" -X POST ${EFORM_IMPORT_URL}
        echo "11111111111111111111111111111111111111111111111111111111111111"
        #showmsg info ""
        IMPORT_KEY=`jq .OtherData result.json | sed s/\"//g | tr -d '\n' | od -An -tx1 | tr ' ' % | xargs echo | sed s/[[:space:]]//g`
        IMPORT_KEY=`urldecode ${IMPORT_KEY}`
        echo "===================="
        echo $IMPORT_KEY
        echo "===================="
        Status=Begin
        while [[ -z `echo $Status | grep End` ]]; do
            #http://localhost/inbiz/eform/admin/export/GetProcessInfo?key=4859a20754e94194aa9f048c3787efc7
            echo ${IMPORT_KEY}
            curl -k -b cookie.data -o result.json "http://"${WEB_URL}"/inbiz/eform/admin/export/GetProcessInfo?key="${IMPORT_KEY} 2>/dev/null 
            Status=`jq .Status result.json | sed s/\"//g | tr -d '\n' | od -An -tx1 | tr ' ' % | xargs echo | sed s/[[:space:]]//g`
            Percent=`jq .Percent result.json | sed s/\"//g | tr -d '\n' | od -An -tx1 | tr ' ' % | xargs echo | sed s/[[:space:]]//g`
            Status=`urldecode ${Status}`
            Percent=`urldecode ${Percent}`
            #echo $Status
            showmsg info "Importing eform form Percent ${Percent}%"
            sleep 2s
        done
    done
}


# Import file resources for the eform form.
initialize_resources(){
    if [ ! -d ${PACKAGE_FORMS_DIR} ];then return;fi
    
    clear_resoureces ${EFORM_AUDIT_GROUP_ID}
    clear_resoureces ${EFORM_DMS_GROUP_ID}
    clear_resoureces ${EFORM_RMS_GROUP_ID}
    clear_resoureces ${EFORM_TMS_GROUP_ID}
    clear_resoureces ${EFORM_AMS_GROUP_ID}
    clear_resoureces ${EFORM_SYS_GROUP_ID}
 
    import_resoureces "audit" ${EFORM_AUDIT_GROUP_ID}
    import_resoureces "dms" ${EFORM_DMS_GROUP_ID}
    import_resoureces "rms" ${EFORM_RMS_GROUP_ID}
    import_resoureces "tms" ${EFORM_TMS_GROUP_ID}
    import_resoureces "ams" ${EFORM_AMS_GROUP_ID}
    import_resoureces "sys" ${EFORM_SYS_GROUP_ID}
}

# Import eform form resources.
import_resoureces(){
    for file in `ls ${PACKAGE_FORMS_DIR} | grep -v .edoc2sysdata | grep $1.`
    do
        showmsg info "Importing eform resource ${file}"
        showmsg info "PACKAGE_FORMS_DIR ${PACKAGE_FORMS_DIR}"
        showmsg info "filename ${file}"
        showmsg info "rootGroupId $2"
        showmsg info "parentFolderId $2-system"
        curl -k -b cookie.data -F "file=@${PACKAGE_FORMS_DIR}/${file};type=text/javascript" -F "name=${file}" -F "rootGroupId=$2" -F "parentFolderId=$2-system" -X POST ${EFORM_RES_IMPORT_URL}
        showmsg info ""
    done
}

clear_resoureces(){ 
    execute_sql "DELETE FROM eform_fileresources WHERE FormGroupId='$1'"
    execute_sql "DELETE FROM eform_fileresourcesdetail WHERE RootGruopId='$1'"
}

##################################################
### 第4步操作
# Execute the specified SQL script to import data.
initialize_data(){
    if [ ! -d ${PACKAGE_DATA_DIR} ];then return;fi
    for file in `ls ${PACKAGE_DATA_DIR} | grep .sql`
    do
       showmsg info "Executing sql script ${file}"
       execute_sql_file "${PACKAGE_DATA_DIR}/${file}"
    done
    unset file
}


execute_sql(){
    #mysql -h${MYSQL_HOST} -P${MYSQL_PORT} -u${MYSQL_USER}  -p${MYSQL_PASSWORD} -D${MYSQL_DATABASE} -e "source $1"
    echo "### execute_sql ###"
    echo $MYSQL_DATABASE
    echo $1
    #HOME=. mysql -D${MYSQL_DATABASE} -e "$1";
    echo "============== all in one sql =============================="
    echo "mysql -h${MYSQL_URL} -u${MYSQL_USER} -p${MYSQL_PASSWORD} -P${MYSQL_PORT} -D${MYSQL_DATABASE} -e \"$1\";"
    mysql -h${MYSQL_URL} -u${MYSQL_USER} -p${MYSQL_PASSWORD} -P${MYSQL_PORT} -D${MYSQL_DATABASE} -e "$1";
    echo "============== all in one sql =============================="
}

execute_sql_file(){
    #mysql -h${MYSQL_HOST} -P${MYSQL_PORT} -u${MYSQL_USER}  -p${MYSQL_PASSWORD} -D${MYSQL_DATABASE} -e "source $1"
    echo $MYSQL_DATABASE
    echo "### execute_sql_file ###"
    echo $1
    mysql -h${MYSQL_URL} -u${MYSQL_USER} -p${MYSQL_PASSWORD} -P${MYSQL_PORT} -D${MYSQL_DATABASE} -e "source $1"
    #HOME=. mysql -D${MYSQL_DATABASE} -e "source $1";
}

### 第五步操作
# Clear temporary files generated after scripts are executed.
#登录redis flushall 清理redis缓存

login_admin
initialize_forms
initialize_resources
#import_resoureces
#clear_resoureces
initialize_data

#kubectl delete -f app/.
#kubectl delete -f inbiz/.
#kubectl delete -f iso/.
#kubectl exec -ti redis-7db7f6486c-ttbkm -n edoc2 -- redis-cli -a 'ZgDSvOoY5Q' -h redis flushall
#kubectl apply -f app/.
#kubectl apply -f app/.
#kubectl apply -f app/.
