#!/bin/bash
#
#***************************************************************************
# Author: liwanggui
# Email: liwanggui@163.com
# Date: 2023-08-29
# FileName: install-go-fastdfs.sh
# Description: 用于快速安装 go-fastdfs 
# Copyright (C): 2023 All rights reserved
#***************************************************************************
# Usage: 
#    安装：/bin/bash install-go-fastdfs.sh 
#    升级: /bin/bash install-go-fastdfs.sh upgrade
#

set -ue

PROXY="https://gh.wglee.org"
GITHUB="${PROXY}/https://github.com"
RELEASE_URL="https://api.github.com/repos/sjqzhang/go-fastdfs/releases/latest"

PROJ_NAME="fileserver"
INSTALL_DIR="/data/fileserver"

_curl() {
    $(type -P curl) -L -q --retry 5 --retry-delay 10 --retry-max-time 60 $@
}

download_go_fastdfs() {
    local VERSION=$1
    local DOWNLOAD_LINK="${GITHUB}/sjqzhang/go-fastdfs/releases/download/${VERSION}/fileserver"
    local SAVE_PATH="${INSTALL_DIR}/${PROJ_NAME}"
    echo "Downloading go-fastdfs binary file: $DOWNLOAD_LINK"
    echo "Save to: ${SAVE_PATH}"
    _curl $DOWNLOAD_LINK --create-dirs -o ${SAVE_PATH}
}

install_go_fastdfs() {
    local LATEST_VERSION=$(curl -s $RELEASE_URL | grep tag_name | cut -f4 -d "\"") 
    [ -f "${INSTALL_DIR}/${PROJ_NAME}" ] && {
        echo "Warning: ${PROJ_NAME} is already installed"
        exit 1
    }
    echo "Install ${PROJ_NAME}"
    download_go_fastdfs $LATEST_VERSION
    chmod +x ${INSTALL_DIR}/${PROJ_NAME}
}

_upgrade_go_fastdfs() {
	local LATEST_VERSION=$1
    echo "Upgrade ${PROJ_NAME} to ${LATEST_VERSION}"
    echo "Stopped ${PROJ_NAME}"
    systemctl stop ${PROJ_NAME}.service
    sleep 3

    download_go_fastdfs $LATEST_VERSION
    chmod +x ${INSTALL_DIR}/${PROJ_NAME}
}

upgrade_go_fastdfs() {
    [ ! -f ${INSTALL_DIR}/${PROJ_NAME} ] && {
		echo "Warning: ${PROJ_NAME} is not installed"
		install_go_fastdfs
		return
	}

    local LATEST_VERSION=$(curl -s $RELEASE_URL | grep tag_name | cut -f4 -d "\"")
    local CURRENT_VERSION

	if ${INSTALL_DIR}/${PROJ_NAME} -h | grep -q '^Available'; then
    	CURRENT_VERSION=$(${INSTALL_DIR}/${PROJ_NAME} version | grep ^Version | awk -F: '{print $NF}')
    	[ $LATEST_VERSION == $CURRENT_VERSION ] && {
    	    echo "Warning: ${PROJ_NAME} is already the latest version($LATEST_VERSION)"
    	    exit 0
    	}
	else
		CURRENT_VERSION=$(${INSTALL_DIR}/${PROJ_NAME} -v | head -n1 | awk -F: '{print $NF}')		
	fi

    echo "The current version is ${CURRENT_VERSION}, the latest version is: ${LATEST_VERSION}"
    _upgrade_go_fastdfs "${LATEST_VERSION}"
}

systemd_config() {
    local SYSTEMD_CONFIG="/usr/lib/systemd/system/${PROJ_NAME}.service"
    echo "Generate ${PROJ_NAME} systemd configuration: ${SYSTEMD_CONFIG}"
    cat <<EOF>${SYSTEMD_CONFIG}
[Unit]
Description=go-fastdfs
Documentation=https://sjqzhang.gitee.io/go-fastdfs/
Wants=network-online.target
After=network-online.target

[Service]
#User=root
#Group=root
#EnvironmentFile=-/etc/sysconfig/${PROJ_NAME}
WorkingDirectory=${INSTALL_DIR}
ExecStart=${INSTALL_DIR}/${PROJ_NAME} server
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s QUIT \$MAINPID
Restart=always
LimitNOFILE=100000
TasksMax=infinity
TimeoutStopSec=infinity
SendSIGKILL=no

[Install]
WantedBy=multi-user.target
EOF
}

start_go_fastdfs() {
    echo "Start ${PROJ_NAME}"
    systemctl daemon-reload
    systemctl enable ${PROJ_NAME}.service 
    systemctl start ${PROJ_NAME}.service 
}

restart_go_fastdfs() {
    echo "Restart ${PROJ_NAME}"
    systemctl daemon-reload
    systemctl enable ${PROJ_NAME}.service
    systemctl restart ${PROJ_NAME}.service
}

main() {
    if [[ $# -ge 1 ]] && [[ $1 == "upgrade" ]]; then
        upgrade_go_fastdfs 
        systemd_config
        restart_go_fastdfs
    else
        install_go_fastdfs
        systemd_config
        start_go_fastdfs
    fi
}

main "$@"
