#!/bin/sh

. /etc/PG.conf

RAMDISK="/usr/ramdisk"
FLOWEYE="${RAMDISK}/bin/floweye"
ESCTOOL="${RAMDISK}/bin/esctool"
ROOT="${RAMDISK}/etc/webuser"
DROOT="${PGPATH}/etc/webuser"


echo_error()
{
	echo $1
	exit 1
}


echo_help()
{
	printf "$0 [list|get|add|set|rmv|init] \n"
	printf "\t -l|list level=N nohead=0/1 \n"
	printf "\t -g|get name=X showip=0/1 showpass=0/1 \n"
	printf "\t -a|add name=X password=X level=X desc=X \n"
	printf "\t -e|edit name=X password=X level=X desc=X \n"
	printf "\t -r|rmv name=X \n"

	exit 1
}


echo_time()
{
	if [ "`uname`" = "Linux" ]; then
		date -d @${1} +"%Y-%m-%d/%H:%M:%S"
	else
		date -j -f "%Y-%m-%d %H:%M:%S" "${1}"
	fi
}


get_iptab_name()
{
	${FLOWEYE} table list | awk -v id=$1 \
	'{
		if($1 == id) printf $2;
	}'
}


list_user()
{
	if [ "${API_nohead}" != "1" ]; then
		echo "name password level iptab_id iptab_name desc"
	fi
	
	for f in `ls ${ROOT}`
	do
		iptab_name="NULL"

		. ${ROOT}/${f}

		[ "${desc}" = "" ] && desc="NULL"
		[ "${iptab_id}" = "" ] && iptab_id="NULL"
		[ "${iptab_name}" = "" ] && iptab_name="NULL"

		[ "${API_level}" != "" -a "${API_level}" != "${level}" ] && continue

		password=`${ESCTOOL} -d ${password}`
		echo "${name} ${password} ${level} ${iptab_id} ${iptab_name} ${desc}"
	done
}


get_user()
{
	[ "${API_name}" = "" ] && echo_error "NO_name"

	user_file=`${ESCTOOL} -e ${API_name}`
	file_path="${ROOT}/${user_file}"

	[ ! -f ${file_path} ] && echo_error "NO_THIS_USER:${API_user}"

	. ${file_path}

	[ "${anylogin}" = "" ] && anylogin="allow"

	echo "name=${name}"

	if [ "${API_showpass}" = "1" ]; then
		echo "password=`${ESCTOOL} -d ${password}`"
	else
		echo "password=${password}"
	fi

	echo "desc=${desc}"
	echo "iptab_id=${iptab_id}"
	echo "iptab_name=${iptab_name}"
	echo "anylogin=${anylogin}"
	echo "lastedit=`echo_time ${lastedit}`"

	if [ "${API_showip}" = "1" ]; then
		echo ""
		echo "== allow login ip =="
		if [ "${iptab_id}" = "" ]; then
			echo "any"
		else
			${FLOWEYE} table get id=${iptab_id}
		fi
	fi
}


add_user()
{
	[ "${API_name}" = "" ] && echo_error "NO_name"
	[ "${API_password}" = "" ] && echo_error "NO_password"

	user_file=`${ESCTOOL} -e ${API_name}`
	file_path="${ROOT}/${user_file}"
	[ -f ${file_path} ] && echo_error "USER_EXIST"

	[ "${API_level}"    = "" ] && API_level=1 
	[ "${API_anylogin}" = "" ] && API_anylogin="allow"

	if [ "${API_iptab_id}" != "" ]; then
		API_iptab_name=`get_iptabe_name ${API_iptab_id}`
	fi

	password=`${ESCTOOL} -e ${API_password}`
	
	echo "name=${API_name}" > ${file_path}
	echo "password=${password}" >> ${file_path}
	echo "level=${API_level}" >> ${file_path}
	echo "desc=${API_desc}" >> ${file_path}
	echo "anylogin=${API_anylogin}" >> ${file_path}
	echo "iptab_id=${API_iptab_id}" >> ${file_path}
	echo "iptab_name=${API_iptab_name}" >> ${file_path}
	echo "lastedit=`date +%s`" >> ${file_path}

	cp ${file_path} ${DROOT}/${user_file}

	echo "${API_name}:${API_password}" >> ${RAMDISK}/admin/.htpasswd
	echo "${API_name}:${API_password}" >> ${PGPATH}/admin/.htpasswd
}


edit_user()
{
	[ "${IPE_username}" != "" ] && API_name="${IPE_username}"
	[ "${API_name}" = "" ] && echo_error "NO_NAME"

	user_file=`${ESCTOOL} -e ${API_name}`
	file_path="${ROOT}/${user_file}"

	[ ! -f ${file_path} ] && echo_error "NO_THIS_USER"
	[ "`dirname ${file_path}`" != "${ROOT}" ] && echo_error "NO_THIS_USER"
	
	. ${file_path}

	[ "${API_password}" != "" ] && password=`${ESCTOOL} -e ${API_password}`
	[ "${API_level}"    != "" ] && level="${API_level}"
	[ "${API_desc}"     != "" ] && desc="${API_desc}"
	[ "${API_anylogin}" != "" ] && anylogin="${API_anylogin}"

	if [ "${API_auth}" = "1" ]; then
		[ "${IPE_dec_newpass}" != "" ] && password=${IPE_dec_newpass}
		[ "${IPE_enc_curpass}" != "" ] && API_auth_password="${IPE_enc_curpass}"
		[ "${password}" = "" ] && echo_error ""
		[ "${API_auth_password}" = "" ] && echo_error "ԭʼ(1)"
		[ "${API_auth_password}" != "${password}" ] && echo_error "ԭʼ(2)"
	fi

	if [ "${API_iptab_id}" != "" ]; then
		if [ "${API_iptab_id}" = "any" ]; then
			iptab_id=""
			iptab_name=""
		else
			iptab_id=${API_iptab_id}
			iptab_name=`get_iptab_name ${API_iptab_id}`
		fi
	fi

	echo "name=${API_name}" > ${file_path}
	echo "password=${password}" >> ${file_path}
	echo "level=${level}" >> ${file_path}
	echo "desc=${desc}" >> ${file_path}
	echo "anylogin=${anylogin}" >> ${file_path}
	echo "iptab_id=${iptab_id}" >> ${file_path}
	echo "iptab_name=${iptab_name}" >> ${file_path}
	echo "lastedit=`date +%s`" >> ${file_path}

	cp ${file_path} ${DROOT}
}


rmv_user()
{
	[ "${API_name}" = "" ] && echo_error "NO_NAME"

	user_file=`${ESCTOOL} -e ${API_name}`
	file_path="${ROOT}/${user_file}"
	
	[ ! -f ${file_path} ] && echo_error "NO_THIS_USER"

	rm -rf ${file_path}
	rm -rf ${DROOT}/${user_file}

	grep -v "${API_name}:" ${RAMDISK}/admin/.htpasswd > ${PGPATH}/admin/.htpasswd
	cp ${PGPATH}/admin/.htpasswd ${RAMDISK}/admin/.htpasswd
}


init_user()
{
	return 0;
}


API_action=""

for arg in $*
do
	if [ "${API_action}" = "" ]; then
		API_action=${arg}
		continue
	fi

	args_ok=`echo ${arg} | grep "="`
	[ "${args_ok}" = "" ] && echo_error "INV_ARGS:${arg}"
	
	eval export API_\${arg}
done


case "${API_action}" in
	"-l"|"list")
		list_user
		;;

	"-g"|"get")
		get_user
		;;

	"-a"|"add")
		add_user
		;;

	"-e"|"edit")
		edit_user
		;;

	"-r"|"rmv")
		rmv_user
		;;

	"-i"|"init")
		init_user
		;;

	"-h"|"help")
		echo_help
		;;

	*)
		echo_help
		;;
esac

