#!/bin/bash

function install_sshpass() {
    log_info "install package sshpass."
    rpm -Uvh "${install_path}"/Install_Packages/sshpass-1.06-8.ky10.aarch64.rpm
}

function install_expect() {
    log_info "install package expect."
    rpm -Uvh "${install_path}"/Install_Packages/tcl-8.5.13-8.el7.x86_64.rpm 
    rpm -Uvh "${install_path}"/Install_Packages/expect-5.45.4-3.ky10.aarch64.rpm
}

function install_jq() {
    log_info "install package jq."
    rpm -Uvh "${install_path}"/Install_Packages/jq-1.5-17.ky10.aarch64.rpm
}

function install_iperf3() {
    # install iperf3
    log_info "install package iperf3"
    for _ip in ${IP_LIST[@]}; do
        $SCP "${install_path}"/Install_Packages/iperf3-3.6-6.ky10.aarch64.rpm root@${_ip}:/tmp/
        $SSH root@${_ip} "rpm -ivh /tmp/iperf3-3.6-6.ky10.aarch64.rpm"
    done
}

function install_fio() {
    # install fio
    log_info "install package fio"
    for _ip in ${IP_LIST[@]}; do
        $SCP "${install_path}"/Install_Packages/fio root@${_ip}:/tmp/
        $SSH root@${_ip} "rpm -ivh /tmp/fio/* &> /dev/null"
    done
}

function install_bc() {
    # install bc
    log_info "install package bc"
    for _ip in ${IP_LIST[@]}; do
        $SCP "${install_path}"/Install_Packages/bc-1.07.1-10.ky10.aarch64.rpm root@${_ip}:/tmp/
        $SSH root@${_ip} "rpm -ivh /tmp/bc-1.07.1-10.ky10.aarch64.rpm &> /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_ipcalc() {
    # install install_ipcalc
    log_info "install ipcalc"
    if ! $(which ipcalc &> /dev/null);then
        rpm -ivh "${install_path}"/Install_Packages/ipcalc*.rpm
    fi
}

function install_sysstat() {
    # install install_sysstat
    log_info "install sysstat"
    for _ip in ${IP_LIST[@]}; do
        $SCP Install_Packages/lm_sensors-3.4.0-21.ky10.aarch64.rpm root@${_ip}:/tmp/
	$SCP Install_Packages/sysstat-12.2.1-6.p01.ky10.aarch64.rpm root@${_ip}:/tmp/
	$SSH root@${_ip} "rpm -ivh /tmp/lm_sensors-3.4.0-21.ky10.aarch64.rpm &> /dev/null"
	$SSH root@${_ip} "rpm -ivh /tmp/sysstat-12.2.1-6.p01.ky10.aarch64.rpm &> /dev/null"
    done
}

function install_nfs_server() {
    # install install_ipcalc
    log_info "install nfs"
    if ! $(which nfs &> /dev/null);then
        #chmod 777 "${install_path}"/Install_Packages/nfs-utils-1.3.0-0.68.el7.x86_64.rpm
        #chmod 777 "${install_path}"/Install_Packages/rpcbind-0.2.0-49.el7.src.rpm
        #rpm -ivh "${install_path}"/Install_Packages/nfs-utils-1.3.0-0.68.el7.x86_64.rpm --force --nodeps
        #rpm -ivh "${install_path}"/Install_Packages/rpcbind-0.2.0-49.el7.src.rpm --force --nodeps
        yum -y remove nfs-utils rpcbind
        sleep 3
        rpm -ivh "${install_path}"/Install_Packages/nfs-arm/* --force --nodeps
        #yum -y install nfs-utils rpcbind
        mkdir -p /data/nas/{esback01,esback02,esback03,esback,data,weboffice}
        service nfs status
        service rpcbind status
        cat << EOF > /etc/exports
/data/nas/esback *(rw,sync,no_root_squash)
/data/nas/data *(rw,sync,no_root_squash)
/data/nas/weboffice *(rw,sync,no_root_squash)
EOF
        exportfs -rv
        systemctl restart rpcbind.service
        systemctl restart nfs.service
        systemctl restart nfs
    fi
}

function install_requirements() {
    install_sshpass
    install_expect
    install_sysstat
    source "${install_path}"/scripts/ssh_key_config.sh
    if [ "$REGION" == "subregion" ] and [ "$TYPE" == "allinone" ]; then
        install_jq
    fi
    if [ "$TYPE" == "allinone" ]; then
        install_nfs_server
    fi
    if [ "$TYPE" == "cluster" ]; then
        install_nfs_server
        install_fio
        install_iperf3
        install_bc
        install_expect
    fi
    install_mc
    #install_ipcalc
}

install_requirements
