#!/bin/bash

REMOTE_URL="http://softlib.service.ucloud.cn/usa/ucloud-basic-init/ucloud-basic-init-grey.tgz"
INIT_TMP_DIR="/tmp/os_init"
INIT_LOG="$INIT_TMP_DIR/init.log"
BASEDIR="$INIT_TMP_DIR/ucloud-basic-init"

ACTION=$1
ENVFLAG=$2
PANGU=$3

# 判断初始化机器来源：install,reinstall,upan
CHANNEL=$4

PREPARE(){
    sed -i '/softlib.service.ucloud.cn/d' /etc/hosts
    sed -i '/yum.service.ucloud.cn/d' /etc/hosts
    HOSTIP=`ip -o r get 192.168.168.106|sed -n 's/.*src \([0-9.]*\) .*/\1/p'`
    if [[ `echo $HOSTIP|grep -c '^100.'` -eq 1  ]];then
        local softlib='172.18.181.49'
        echo "$softlib softlib.service.ucloud.cn" >> /etc/hosts
        echo "$softlib yum.service.ucloud.cn" >> /etc/hosts
    fi
    yum install -y wget mysql python-requests dmidecode > /dev/null 2>&1

    # Physical machine install
    if [[ $(dmidecode -t1 | grep -c 'Red Hat') -eq 0 || $(dmidecode -t1 | grep -c KVM) -eq 0 ]];then
        yum -y -q install ipmitool OpenIPMI hwdata MegaCli storcli perccli ssacli ssaducli Arcconf nvme-cli sg3_utils > /dev/null 2>&1
	ln -sf /usr/Arcconf/arcconf /usr/sbin/arcconf
        service ipmi restart > /dev/null 2>&1
    fi
}

Usage(){
    echo -e "\033[31mUsage:\t
	1. bash $0 install
	2. bash $0 check
	Debug: cat /tmp/os_init/init.log
	 \033[0m"
}

Wget(){
    rm -rf $INIT_TMP_DIR
    mkdir $INIT_TMP_DIR
    touch $INIT_LOG

    # Wget
    wget $REMOTE_URL -O $INIT_TMP_DIR/ucloud-basic-init.tgz > /dev/null 2>&1
    if [ $? -ne 0 ]; then
	echo -e "\033[31m[ERROR] Package Download Failed.\033[0m"
        exit 1
    fi

    # Untar
    tar -zxvf $INIT_TMP_DIR/ucloud-basic-init.tgz -C $INIT_TMP_DIR > /dev/null 2>&1
    if [ $? -ne 0 ]; then
	echo -e "\033[31m[ERROR] Package Unzip Failed.\033[0m"
        exit 1
    fi
}


if [[ $ACTION = install ]];then
  if [[ -f "/ucloud/exports.ini" ]];then
    if  [[ -z $PANGU ]] && [[ `grep -c "UCLOUD_INIT=1" /ucloud/exports.ini` -ne 0 ]];then
      echo "System Init Already Done! "
      echo "If you want do it again, Delete the line 'UCLOUD_INIT=' in /ucloud/exports.ini"
      exit 1
    else
      sed -i '/UCLOUD_INIT/d' /ucloud/exports.ini
    fi
  fi

    # prepare
    PREPARE

    begin_time=$(date -u +"%Y-%m-%d %H:%M:%S")
    echo -e "\033[32m[INIT BEGIN] 开始时间: $begin_time UTC\033[0m"

    Wget
elif [[ $ACTION = check ]];then
    # 保存install日志，避免check环节被删除
    \cp -f $INIT_LOG /tmp/INIT_LOG    
    Wget
    \cp -f /tmp/INIT_LOG $INIT_LOG   
    echo -e "\033[32m[CHECK BEGIN] \033[0m"
else
    echo -e "\033[31m[ERROR] Argument Input Failed, As Follows: \033[0m"
    Usage
    exit 1
fi

# Load libs.sh
. $BASEDIR/lib.sh

# Run
if [[ $(grep -c 'Tip' /tmp/os_init/init.log) -eq 0 ]];then
    PRINT "<font color="green">Tips: Step1: 系统初始化, Step2: 交付前检测</font>"
    PRINT "--------------------------------------------------------"
fi

# Get Running Environment Info
Get_Env_Info
if [[ $? -ne 0 ]];then
    PRINT_ERR "[ERROR] Init Environment Check Fail."
    echo -e "\033[31m[ERROR] Init Environment Check Fail. \033[0m"
    if [[ -n $PANGU ]];then
        UPLOAD_LOG fail
    fi
    exit 1
fi
UPLOAD_LOG ing

cd $INIT_TMP_DIR/ucloud-basic-init
source ./start.sh
