#!/bin/bash

function install_sshpass() {
    log_info "install package sshpass."
    dpkg -i "${install_path}"/Install_Packages/ubuntu18.04_deb/sshpass_1.06-1_amd64.deb
}

function install_expect() {
    log_info "install package expect."
    dpkg -i "${install_path}"/Install_Packages/ubuntu18.04_deb/libtcl8.6_8.6.8+dfsg-3_amd64.deb
    dpkg -i "${install_path}"/Install_Packages/ubuntu18.04_deb/tcl-expect_5.45.4-1_amd64.deb
    dpkg -i "${install_path}"/Install_Packages/ubuntu18.04_deb/expect_5.45.4-1_amd64.deb
}

function install_fio() {
    # install fio
    log_info "install package fio"
    for _ip in ${IP_LIST[@]}; do
        $SCP "${install_path}"/Install_Packages/ubuntu18.04_deb/fio-3.30.deb root@${_ip}:/tmp/
        $SSH root@${_ip} "dpkg -i /tmp/fio-3.30.deb &> /dev/null"
    done
}

function install_mc() {
    # install mc
    log_info "install s3 tool mc"
    \cp "${install_path}"/Install_Packages/mc /usr/local/sbin/
    chmod +x /usr/local/sbin/mc 
}

function install_nfs_server() {
    # install install_ipcalc
    log_info "install nfs"
    if ! $(which nfs &> /dev/null);then
	apt autoremove -y nfs*
	sleep 1
	rm -rf /lib/systemd/system/nfs-*
	sleep 1
	rm -rf /etc/systemd/system/nfs-*
	sleep 3
        dpkg -i "${install_path}"/Install_Packages/ubuntu18.04_deb/nfs/*
        sleep 3
        mkdir -p /data/nas/{esback01,esback02,esback03,esback}

        service rpcbind status
        cat << EOF > /etc/exports
/data/nas/esback01 *(rw,sync,no_root_squash)
/data/nas/esback02 *(rw,sync,no_root_squash)
/data/nas/esback03 *(rw,sync,no_root_squash)
/data/nas/esback *(rw,sync,no_root_squash)
EOF
        #exportfs -rv
        systemctl restart rpcbind
        systemctl restart nfs-common.service
        systemctl restart nfs-utils.service
	systemctl restart nfs-kernel-server.service
	sleep 3
	service nfs-common status | grep ""
	service nfs-utils status | grep ""
	service nfs-kernel-server status | grep ""
    fi
}

function install_requirements() {
    install_sshpass
    install_expect
    echo "================================================="
    source "${install_path}"/scripts/ssh_key_config.sh
    echo "================================================="
    if [ "$TYPE" == "allinone" ]; then
        install_nfs_server
    fi
    if [ "$TYPE" == "cluster" ]; then
        install_nfs_server
        install_fio
    fi
    install_mc
}

install_requirements
