#!/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`
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 -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 "TBD"
exit
}

#func help
if [ $# -eq 0 ] ;then
	help_main
elif [ $# -eq 1 ] && [ 'profile' = $1 ];then
	help_profile
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
	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 ] && [ '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 "      * 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', '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

if [ ! -e $install_dir ] ; then
    echo -e "${co_r}Fatal${co_d}: install directory not exist: \"$install_dir\""
    exit 1
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 $?


