
#!/bin/bash

DIRECTORY=/opt
LOG_FILE=$DIRECTORY/sensor-data_install.log
CONTAINER_NAME=sensor-data
VERSION=max1.0-1.0.1
PACKAGE_PATH=http://dl.fogworks.io/facmgr_max/sensor-data/${CONTAINER_NAME}_${VERSION}.tar.bz2
MD5_PATH=http://dl.fogworks.io/facmgr_max/sensor-data/${CONTAINER_NAME}_${VERSION}_md5
INSTALL_DIRECTORY=$DIRECTORY/${CONTAINER_NAME}_${VERSION}
MD5_VERSION=$INSTALL_DIRECTORY/md5_version

preCheck() {

    if [ ! -d $DIRECTORY ]; then
        # 如果目录不存在，创建目录
        mkdir -p $DIRECTORY
        echo "`date` mkdir $DIRECTORY success." >> $LOG_FILE
    else
        echo "`date` $DIRECTORY is exists." >> $LOG_FILE
    fi

    cd $DIRECTORY

    if ! command -v tee > /dev/null; then
        echo "`date` tee command not found. Please install tee." >> $LOG_FILE
        exit 1
    fi

    if ! command -v curl > /dev/null; then
        echo "`date` curl command not found. Please install curl." | tee -a $LOG_FILE
        exit 1
    fi

    if ! command -v wget > /dev/null; then
        echo "`date` wget command not found. Please install wget." | tee -a $LOG_FILE
        exit 1
    fi

    if ! command -v docker > /dev/null; then
        echo "`date` docker command not found. Please install docker." | tee -a $LOG_FILE
        exit 1
    fi

    return 0
}
    

install() {
    # judge install or upgrade
    if docker ps -a | grep $CONTAINER_NAME > /dev/null; then
        # 存在sensor-data 容器
        echo "`date` $CONTAINER_NAME container is exist." | tee -a $LOG_FILE
        if [ ! -d $INSTALL_DIRECTORY ]; then
            # 不存在解压后的安装目录
            wget $PACKAGE_PATH
            downloadResult=$?
            if [ $downloadResult -ne 0 ]; then
                echo "`date` download $CONTAINER_NAME package failed" | tee -a $LOG_FILE
                sleep 1
                wget $PACKAGE_PATH
                downloadRetryOneResult=$?
                if [ $downloadRetryOneResult -ne 0 ]; then
                    echo "`date` download $CONTAINER_NAME package failed retry 1" | tee -a $LOG_FILE
                    sleep 1
                    wget $PACKAGE_PATH
                    downloadRetryTwoResult=$?
                    if [ $downloadRetryTwoResult -ne 0 ]; then
                        echo "`date` download $CONTAINER_NAME package failed retry 2" | tee -a $LOG_FILE
                        return 1
                    fi
                fi
            fi
            tar -jxvf ${CONTAINER_NAME}_${VERSION}.tar.bz2
            decompressResult=$?
            if [ $decompressResult -ne 0 ]; then
                echo "`date` decompress $CONTAINER_NAME package failed" | tee -a $LOG_FILE
                return 1
            fi

            cd $INSTALL_DIRECTORY
            sh upgrade.sh
            upgradeResult=$?
            if [ $upgradeResult -ne 0 ]; then
                echo "`date` upgrade $CONTAINER_NAME failed" | tee -a $LOG_FILE
                return 1
            fi

            return 0
        else
            # 存在解压后的安装目录
            if [ -f $MD5_VERSION ]; then
                local_md5=$(cat $MD5_VERSION)
                online_md5=$(curl $MD5_PATH | tr -d '\n')
                if [ "$online_md5" = "$local_md5" ]; then
                    echo "`date` $CONTAINER_NAME version is already up to date." | tee -a $LOG_FILE
                    return 0
                fi
            fi

            rm -rf $INSTALL_DIRECTORY
            rm -rf ${CONTAINER_NAME}_${VERSION}.tar.bz2

            wget $PACKAGE_PATH
            downloadResult=$?
            if [ $downloadResult -ne 0 ]; then
                echo "`date` download $CONTAINER_NAME package failed" | tee -a $LOG_FILE
                sleep 1
                wget $PACKAGE_PATH
                downloadRetryOneResult=$?
                if [ $downloadRetryOneResult -ne 0 ]; then
                    echo "`date` download $CONTAINER_NAME package failed retry 1" | tee -a $LOG_FILE
                    sleep 1
                    wget $PACKAGE_PATH
                    downloadRetryTwoResult=$?
                    if [ $downloadRetryTwoResult -ne 0 ]; then
                        echo "`date` download $CONTAINER_NAME package failed retry 2" | tee -a $LOG_FILE
                        return 1
                    fi
                fi
            fi
            tar -jxvf ${CONTAINER_NAME}_${VERSION}.tar.bz2
            decompressResult=$?
            if [ $decompressResult -ne 0 ]; then
                echo "`date` decompress $CONTAINER_NAME package failed" | tee -a $LOG_FILE
                return 1
            fi

            cd $INSTALL_DIRECTORY
            sh upgrade.sh
            upgradeResult=$?
            if [ $upgradeResult -ne 0 ]; then
                echo "`date` upgrade $CONTAINER_NAME failed" | tee -a $LOG_FILE
                return 1
            fi

            return 0
        fi
    else
        # 不存在sensor-data 容器
        echo "`date` $CONTAINER_NAME container not exist." | tee -a $LOG_FILE
        if [ ! -d $INSTALL_DIRECTORY ]; then
            echo "`date` start install $CONTAINER_NAME" | tee -a $LOG_FILE
            wget $PACKAGE_PATH
            downloadResult=$?
            if [ $downloadResult -ne 0 ]; then
                echo "`date` download $CONTAINER_NAME package failed" | tee -a $LOG_FILE
                sleep 1
                wget $PACKAGE_PATH
                downloadRetryOneResult=$?
                if [ $downloadRetryOneResult -ne 0 ]; then
                    echo "`date` download $CONTAINER_NAME package failed retry 1" | tee -a $LOG_FILE
                    sleep 1
                    wget $PACKAGE_PATH
                    downloadRetryTwoResult=$?
                    if [ $downloadRetryTwoResult -ne 0 ]; then
                        echo "`date` download $CONTAINER_NAME package failed retry 2" | tee -a $LOG_FILE
                        return 1
                    fi
                fi
            fi
            tar -jxvf ${CONTAINER_NAME}_${VERSION}.tar.bz2
            decompressResult=$?
            if [ $decompressResult -ne 0 ]; then
                echo "`date` decompress $CONTAINER_NAME package failed" | tee -a $LOG_FILE
                return 1
            fi

            cd $INSTALL_DIRECTORY
            sh install.sh
            installResult=$?
            if [ $installResult -ne 0 ]; then
                echo "`date` install $CONTAINER_NAME failed" | tee -a $LOG_FILE
                return 1
            fi

            return 0
        else
            echo "`date` start install $CONTAINER_NAME" | tee -a $LOG_FILE
            cd $INSTALL_DIRECTORY
            sh install.sh
            installResult=$?
            if [ $installResult -ne 0 ]; then
                echo "`date` install $CONTAINER_NAME failed" | tee -a $LOG_FILE
                return 1
            fi

            return 0
        fi
    fi 
}

postCheck() {
    echo "`date` start post check for $CONTAINER_NAME installation" | tee -a $LOG_FILE
    # Check Sensor-data status
    if ! docker ps | grep $CONTAINER_NAME | grep Up > /dev/null; then
        echo "`date` Sensor-data is not running." | tee -a $LOG_FILE
        return 1
    fi
    curl $MD5_PATH > $MD5_VERSION
    return 0
}

# step 1: pre-Check
preCheck
preCheckResult=$?
echo "`date` pre check result is $preCheckResult" | tee -a $LOG_FILE
if [ $preCheckResult -eq 0 ]
then
    echo "`date` Pre-check successful." | tee -a $LOG_FILE
    # Step 2: Install Sensor-data
    install
    installResult=$?
    if [ $installResult -eq 0 ]
    then
        echo "`date` Install Sensor-data successful." | tee -a $LOG_FILE
        # Step 3: Post-check
        postCheck
        postCheckResult=$?
        if [ $postCheckResult -eq 0 ]
        then
            echo "`date` Post-check successful." | tee -a $LOG_FILE
            echo "`date` $CONTAINER_NAME installation process completed." | tee -a $LOG_FILE
            exit 0
        else
            echo "`date` Post-check failed." | tee -a $LOG_FILE
            exit 3
        fi
    else
        echo "`date` Install Sensor-data failed." | tee -a $LOG_FILE
        exit 2
    fi
else
    echo "`date` Pre-check failed." | tee -a $LOG_FILE
    exit 1
fi
