#!/bin/bash

co_d="\e[0m"
co_r="\e[31m"

cmd_pre=python3
which $cmd_pre >/dev/null 2>&1
if [ $? -eq 0 ]; then
	#echo "use python3"
	:
else
	cmd_pre=python2
	#echo "use python2"
fi

working_dir=/usr/share/extuner/
install_dir=/usr/share/extuner/
output_dir=`pwd`
script_path=`pwd`
help_main(){
echo "--------------------------------------------------------------------" 
echo "extuner is a data collecting and expert tuning tool for Kylin system"
echo -e 
echo "Usage: " 
echo "    extuner COMMAND [options] "
echo "    extuner {-h|-v}"
echo -e 
echo "Examples:"
echo "    extuner collection --perf --pid -1"
echo "    extuner -v "
echo -e
echo "Commands:"
echo "    collection            collect system data, such as CPU/MEM/NET/IO"
echo "    analysis              collect system data, analysis performance and" 
echo "                          provide tuning advice"
echo "    profile               activate the preset scene profile"
echo "    apply                 apply tuned script"
echo "    tuning                use bayes to get optimal configuration parameters"
echo "    confuse               obfuscated tuned script"
echo -e
echo "Options:"
echo "    -h, --help            show this help message and exit"
echo "    -v, --version         version"
exit
}

help_collection(){
echo "-------------------------------------------" 
echo "collect system data, such as CPU/MEM/NET/IO"
echo -e 
echo "Usage: " 
echo "    extuner collection [options] "
echo -e 
echo "Example:"
echo "    extuner collection --perf --pid -1"
echo -e
echo "Options:"
echo "    -h, --help                      show this help message and exit"
echo "    -o <path>, --out_path=<path>    Generate extunerData/, include log and reports"
echo "                                    default in /usr/share/extuner/extunerData/"
echo "    --perf [PERF_ENABLE_TYPE]       collect perf data,"
echo "                                    default is disabled if not specified"
echo "    --offcpu                        collect offcpu data,"
echo "                                    default is disabled if not specified"
echo -e
echo "Perf required parameters:"
echo "    --pid PERF_PID                  process id to profile,"
echo "                                    -1 to profile the whole system"
echo "    --duration VALUE                duration of perf trace, in seconds"
echo -e
echo "OffCPU required parameters:"
echo "    --offcpu_pid OFFCPU_PID         process id to profile"
echo "    --offcpu_duration VALUE         duration of perf trace, in seconds"
exit
}

help_analysis(){
echo "-------------------------------------------------------------------" 
echo "collect system data, analysis performance and provide tuning advice"
echo -e 
echo "Usage: " 
echo "    extuner analysis [options] "
echo -e 
echo "Example:"
echo "    extuner analysis --perf --pid -1"
echo -e
echo "Options:"
echo "    -h, --help                      show this help message and exit"
echo "    -o <path>, --out_path=<path>    Generate extunerData/, include log and reports"
echo "                                    default in /usr/share/extuner/extunerData/"
echo "    --test                          test mode. Default is False. Only developer use"
echo "    --perf [PERF_ENABLE_TYPE]       collect perf data,"
echo "                                    default is disabled if not specified"
echo "    --offcpu                        collect offcpu data,"
echo "                                    default is disabled if not specified"
echo -e
echo "Perf required parameters:"
echo "    --pid PERF_PID                  process id to profile,"
echo "                                    -1 to profile the whole system"
echo "    --duration VALUE                duration of perf trace, in seconds"
echo -e
echo "OffCPU required parameters:"
echo "    --offcpu_pid OFFCPU_PID         process id to profile"
echo "    --offcpu_duration VALUE         duration of perf trace, in seconds"
exit
}

help_profile(){
echo "---------------------------------" 
echo "activate the preset scene profile"
echo -e 
echo "Usage: " 
echo "    extuner profile COMMAND [options] "
echo -e 
echo "Examples:"
echo "    extuner profile mysql_config /etc/"
echo "    extuner profile recover"
echo "    extuner profile list"
echo -e
echo "Commands:"
echo "    mysql_config CONF_DIR         MySQL scene set, CONF_DIR is MySQL conf path"
echo "    kafka_config CONF_DIR         Kafka scene set, CONF_DIR is Kafka conf path"
echo "    ats_config CONF_DIR           ATS scene set, CONF_DIR is ATS conf path"
echo "    specweb_config CONF_DIR       SPECweb scene set, CONF_DIR is apache conf path"
echo "    dm_config CONF_DIR            DM scene set, CONF_DIR is DM conf path"
echo "    recover                       recover"
echo "    list                          list all profile"
echo -e
echo "Options:"
echo "    -h, --help                    show this help message and exit"
echo "    -o <path>, --out_path=<path>  Generate extunerData/, include log and reports"
echo "                                  default in /usr/share/extuner/extunerData/"

exit
}

help_apply(){
echo "---------------------------------" 
echo "apply the encrypted script"
echo -e 
echo "Usage: " 
echo "    extuner apply SCRIPT"
echo "    extuner apply --enc SCRIPT"
echo -e 
echo "Examples:"
echo "    extuner apply my.extu"
echo "    extuner apply --enc my.sh"
echo -e
echo "Options:"
echo "    SCRIPT                        execute the encrypted script"
echo "    --enc SCRIPT                  encrypt the tuning file"
echo "    -h, --help                    show this help message and exit"
exit
}

help_confuse(){
echo "---------------------------------" 
echo "obfuscated the tuning script"
echo -e 
echo "Usage: " 
echo "    extuner confuse SCRIPT"
echo "    extuner confuse --cnf SCRIPT"
echo -e 
echo "Examples:"
echo "    extuner confuse my.ky"
echo "    extuner confuse --cnf my.sh"
echo -e
echo "Options:"
echo "    SCRIPT                        execute the confused script"
echo "    --cnf SCRIPT                  obfuscated tuning file contents"
echo "    -h, --help                    show this help message and exit"
exit
}

help_tuning(){
echo "---------------------------------"
echo "use bayes to get optimal configuration parameters"
echo -e
echo "Usage: "
echo "    extuner tuning PROJECT_YAML"
echo -e
echo "Examples:"
echo "    extuner tuning gcc_compile.yaml"
echo -e
echo "Options:"
echo "    PROJECT_YAML                  Tuning configuration files"
echo "    -h, --help                    show this help message and exit"
exit
}

#func help
if [ $# -eq 0 ] ;then
	help_main
elif [ $# -eq 1 ] && [ 'profile' = $1 ];then
	help_profile
elif [ $# -eq 1 ] && [ 'apply' = $1 ];then
	help_apply
elif [ $# -eq 1 ] && [ 'tuning' = $1 ];then
       help_tuning
elif [ $# -eq 1 ] && [ 'confuse' = $1 ];then
	help_confuse
fi

args=$*
if [[ $args =~ '-h' ]] || [[ $args =~ '--help' ]] 
then
	if [ $1 = '-h' ] || [ $1 = '--help' ]; then
		help_main
	elif [ $1 = 'collection' ]; then
		help_collection
	elif [ $1 = 'analysis' ]; then
		help_analysis
	elif [ $1 = 'profile' ]; then
		help_profile
	elif [ $1 = 'apply' ]; then
		help_apply
    elif [ $1 = 'tuning' ]; then
        help_tuning

	elif [ $1 = 'confuse' ]; then
		help_confuse
	else 
		args1=$1
		if [ ${args1:0:1} != '-' ]; then
			echo 'No such command:' $1'. Please use /usr/bin/extuner  --help'
			exit
		else 
			help_main
		fi
	fi
fi
# extuner profile
if [ $# -eq 2 ] && [ 'profile' = $1 ] && [ 'kafka_config' = $2 ];then
       echo -e "Too few arguments."
elif [ $# -eq 2 ] && [ 'profile' = $1 ] && [ 'mysql_config' = $2 ];then
       echo -e "Too few arguments."
elif [ $# -eq 2 ] && [ 'profile' = $1 ] && [ 'ats_config' = $2 ];then
       echo -e "Too few arguments."
elif [ $# -eq 2 ] && [ 'profile' = $1 ] && [ 'specweb_config' = $2 ];then
       echo -e "Too few arguments."
elif [ $# -eq 2 ] && [ 'profile' = $1 ] && [ 'dm_config' = $2 ];then
       echo -e "Too few arguments."
elif [ $# -eq 2 ] && [ 'profile' = $1 ] && [ 'list' = $2 ];then
echo "Profile:"
echo "      * kafka_config"
echo "      * mysql_config"
echo "      * ats_config"
echo "      * specweb_config"
echo "      * dm_config"
elif [ $# -ge 2 ] && [ 'profile' = $1 ] && [ '-o' = $2 ];then
echo "extuner profile: error: argument : invalid choice: '-o' (choose from 'kafka_config', 'mysql_config', 'ats_config', 'specweb_config', 'dm_config', 'recover', 'list')"
else
	# if [ `pwd` == '/usr/share/extuner' ]; then
		cmd_exec=$cmd_pre' -B main.py '$@
	# else
	# 	cmd_exec=$cmd_pre' main.py '$@
	# fi
fi

# extuner apply
if [ $# -ge 2 ] && [ 'apply' = $1 ] ;then
        cmd_exec=$cmd_pre' -B main.py '$@"  --PATH=${script_path}"
fi

# extuner confuse
if [ $# -ge 2 ] && [ 'confuse' = $1 ] ;then
        cmd_exec=$cmd_pre' -B main.py '$@"  --PATH=${script_path}"
fi

if [ ! -e $install_dir ] ; then
    echo -e "${co_r}Fatal${co_d}: install directory not exist: \"$install_dir\""
    exit 1
fi

# extuner tuning
if [ $# -eq 2 ] && [ 'tuning' = $1 ] ;then
        # 判断路径类型并转换相对路径为绝对路径
        input_path=$2
        if [[ "$input_path" = /* ]]; then
                abs_path="$input_path"
        else
                abs_path="$(pwd)/$input_path"
        fi
        cmd_exec=$cmd_pre' -B main.py tuning '$abs_path' --tuning_outpath='$output_dir
fi

# if [ `pwd` == '/usr/share/extuner' ]; then
	pushd $install_dir >/dev/null 2>&1 && $cmd_exec && popd >/dev/null 2>&1
# else
# 	$cmd_exec
# fi

exit $?


