#!/usr/bin/env bash
# frps auto install script
# author: 荒野无灯
# date: 2018-07-29

SYSTEMD_SERVICE_FILE=/etc/systemd/system/frps.service
SYSVINIT_SERVICE_FILE=/etc/init.d/frps
FRPS_CONF=/etc/frps/frps.ini

if [ ! -f $FRPS_CONF ]; then
	echo "error: frps config file $FRPS_CONF found, exit ..."
	exit 1
fi

if [ ! -f $SYSTEMD_SERVICE_FILE ] && [ ! -f $SYSVINIT_SERVICE_FILE ]; then
	echo "error:no frps service file found, exit ..."
	exit 1
fi

func_is_CentOS() {
	yum --version >/dev/null 2>&1
	if [ "$?" == "0" ];then
		echo "yes"
		return
	fi
	echo "no"
}

func_is_CentOS7() {
	local is_centos=$(func_is_CentOS)
	if [ "$is_centos" != "yes" ];then
		echo "no"
		return
	fi
	systemctl --version >/dev/null 2>&1
	if [ "$?" == "0" ];then
		echo "yes"
		return
	fi
	echo "no"
}

IS_CENTOS=$(func_is_CentOS)
if [ "$IS_CENTOS" != "yes" ]; then
	echo "error: this script only support CentOS Linux."
	exit 1
fi
IS_CENTOS7=$(func_is_CentOS7)

echo "starting the service ..."
if [ "$IS_CENTOS7" == "yes" ]; then
	/usr/bin/systemctl enable frps.service
	/usr/bin/systemctl stop frps.service
	/usr/bin/systemctl start frps.service
	/usr/bin/systemctl status frps.service
else
	/sbin/chkconfig frps on
	$SYSVINIT_SERVICE_FILE stop
	$SYSVINIT_SERVICE_FILE start
	$SYSVINIT_SERVICE_FILE status
fi