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

FRPS_DOWNLOAD_SERVER=http://files.80x86.io/router/rom/opt/frp
#FRPS_DOWNLOAD_SERVER=http://router.mirror1.80x86.io:8088/opt/frp

FRPS_INSTALL_DIR=/usr/local/frp
FRPS_BIN=frps_linux_amd64
FRPS_CONF=/etc/frps/frps.ini
ARCH=$(uname -m)
echo "detect arch: $ARCH"
WGET_BIN=$(which wget)
if [ "$?" != "0" ];then
	echo "error: wget is not available."
	exit 1
fi
SYSTEMD_SERVICE_FILE=/etc/systemd/system/frps.service
SYSVINIT_SERVICE_FILE=/etc/init.d/frps

if [ -f $FRPS_CONF ]; then
	echo "error: frps config file $FRPS_CONF found."
	echo "looks like your've already installed frps service, exit ..."
	exit 1
fi

if [ -f $SYSTEMD_SERVICE_FILE ] || [ -f $SYSVINIT_SERVICE_FILE ]; then
	echo "error: frps service file found."
	echo "looks like your've already installed frps service, exit ..."
	exit 0
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)

case "$ARCH" in
  i686)
	FRPS_BIN=frps_linux_386
	;;
  x86_64)
	FRPS_BIN=frps_linux_amd64
	;;
  aarch64)
	FRPS_BIN=frps_linux_arm64
	;;
  mips)
	FRPS_BIN=frps_linux_mipsle
	;;
  *)
	echo "error: arch $ARCH is not supported."
	exit 1
esac


func_download() {
	local save_dir=$1
	local save_name=$2
	local url_filepath=$3
	local url=$FRPS_DOWNLOAD_SERVER/$url_filepath

	echo "starting download $save_name to $save_dir from $url ..."
	test -d $save_dir || mkdir -p $save_dir
	$WGET_BIN --progress=bar -T30 -U "wget-for-frps-deploy/1.0.0" -O$FRPS_INSTALL_DIR/$save_name "$url"
	if [ "$?" != "0" ];then
		echo "error: wget failed to download file $FRPS_BIN."
		exit 1
	fi
}

func_download $FRPS_INSTALL_DIR frps frps/$FRPS_BIN
func_download $FRPS_INSTALL_DIR frps.init frps.init
func_download $FRPS_INSTALL_DIR frps.service frps.service
func_download $FRPS_INSTALL_DIR frps.ini frps.ini
func_download $FRPS_INSTALL_DIR frps-service-start-centos.sh frps-service-start-centos.sh

chmod a+x $FRPS_INSTALL_DIR/frps-service-start-centos.sh

echo "chmod $FRPS_INSTALL_DIR/frps ..."
chmod a+x $FRPS_INSTALL_DIR/frps

echo "install service ..."
if [ "$IS_CENTOS7" == "yes" ]; then
	cp -v $FRPS_INSTALL_DIR/frps.service $SYSTEMD_SERVICE_FILE
else
	cp -v $FRPS_INSTALL_DIR/frps.init $SYSVINIT_SERVICE_FILE
	chmod a+x $SYSVINIT_SERVICE_FILE
fi

echo "install config file to $FRPS_CONF ..."
install -D $FRPS_INSTALL_DIR/frps.ini $FRPS_CONF

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