#!/bin/bash
SHOWDOC_SCRIPT_VERSION='2.0'
if [[  -n "$1" ]] ; then 
	action=$1
else
	action='install'
fi
if [ "$action" == "start" ] ;then
	sudo -s  service docker start
	sudo -s docker start showdoc
	exit 1 
fi
if [ "$action" == "restart" ] ;then
	sudo -s docker restart showdoc 
	exit 1
fi
if [ "$action" == "stop" ] ;then
	sudo -s docker stop showdoc 
	exit 1
fi
if [ "$action" == "update" ] ;then
	DATE=$(date +%Y%m%d_%H%M%S_%N) 
	if [ ! -d "/showdoc_data/html" ]; then 
		echo  "Directory /showdoc_data/html does not exist"
		echo "/showdoc_data/html 目录不存在"
		exit 1 ;
	fi
	# 判断语言
	result=$(tail -n 1000 /showdoc_data/html/web/index.html | grep zh)
	if [ ! -z "$result" ] ; then 
		sudo -s docker pull registry.cn-shenzhen.aliyuncs.com/star7th/showdoc
		sudo -s docker tag registry.cn-shenzhen.aliyuncs.com/star7th/showdoc:latest star7th/showdoc:latest  
	else
		sudo -s docker pull star7th/showdoc
	fi

	sudo -s mv /showdoc_data/html /showdoc_data/html_bak_${DATE}

	sudo -s mkdir -p /showdoc_data/html
	sudo -s chmod  -R 777 /showdoc_data/html
	sudo -s docker stop showdoc && sudo -s docker rm showdoc 
	sudo -s docker run -d --name showdoc --env SHOWDOC_SCRIPT_VERSION=${SHOWDOC_SCRIPT_VERSION}  --user=root  --restart=always --privileged=true -p 4999:80 -v /showdoc_data/html:/var/www/html/ star7th/showdoc

	seconds_left=10
	while [ $seconds_left -gt 0 ];do
  		echo -n $seconds_left
  		sleep 1
  		seconds_left=$(($seconds_left - 1))
  		echo -ne "\r     \r" #清除本行文字
	done


	result=$(tail -n 1000 /showdoc_data/html_bak_${DATE}/web/index.html | grep zh)
	if [ ! -z "$result" ] ; then 
		curl http://localhost:4999/install/non_interactive.php?lang=zh
	else
		curl http://localhost:4999/install/non_interactive.php?lang=en
	fi

	\cp  -f  /showdoc_data/html_bak_${DATE}/Sqlite/showdoc.db.php /showdoc_data/html/Sqlite/showdoc.db.php
	\cp -r -f /showdoc_data/html_bak_${DATE}/Public/Uploads /showdoc_data/html/Public/

	sudo -s curl https://www.showdoc.com.cn/script/showdoc > ./showdoc

	exit 1
fi
if [ "$action" == "uninstall" ] ;then
	echo -e " Showdoc is about to be uninstalled. Are you sure to delete all showdoc data? [Y/n] \n 即将卸载showdoc，你是否确认删除showdoc所有数据? [Y/n]  "
	read -r -p '' input
	case $input in
	    [yY][eE][sS]|[yY])
				echo "uninstalling..."
				echo "正在卸载..."
				# 停止所有含有showdoc关键字的容器
				for container in $(sudo -s docker ps -a | grep showdoc | awk '{print $1}'); do
					sudo -s docker stop $container 2>/dev/null
				done
				# 删除所有含有showdoc关键字的容器
				for container in $(sudo -s docker ps -a | grep showdoc | awk '{print $1}'); do
					sudo -s docker rm $container 2>/dev/null
				done
				# 删除所有含有showdoc关键字的镜像
				sudo -s docker rmi  -f `docker images | grep showdoc| awk '{print $3}'` >/dev/null 2>&1
				# 删除数据目录
				sudo -s rm -rf /showdoc_data
				echo "Done"
				echo "完成"

			;;

	    [nN][oO]|[nN])
	       		;;
	    *)
		exit 1
		;;
	esac
	exit 1
fi
if ! [ -x "$(command -v docker)" ]; then
  echo 'It was detected that Docker was not installed. Attempting to install from the network... The time required is related to your network environment.'
  echo '检测到Docker尚未安装。正在试图从网络安装...所需时间与你的网络环境有关'
  sudo -s curl -sSL https://www.showdoc.com.cn/script/install_docker.sh | bash
  sudo -s  chkconfig docker on 
fi
if ! [ -x "$(command -v docker)" ]; then
  echo 'It was detected that Docker was not installed. Attempting to install from the network... The time required is related to your network environment.'
  echo '检测到Docker尚未安装。正在试图从网络安装...所需时间与你的网络环境有关'
  sudo -s curl -sSL https://get.daocloud.io/docker | sh
  sudo -s  chkconfig docker on 
fi
if ! [ -x "$(command -v docker)" ]; then
  echo 'It was detected that Docker was not installed. Attempting to install from the network... The time required is related to your network environment.' 
  echo '检测到Docker尚未安装。正在试图从网络安装...所需时间与你的网络环境有关'
  sudo -s curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh -
  sudo -s  chkconfig docker on 
fi
if ! [ -x "$(command -v docker)" ]; then
  echo 'Docker failed to install automatically. It is recommended that you install the docker environment manually before starting this script.' 
  echo 'Docker自动安装失败,建议你手动安装好docker环境后再启动本脚本' 
  exit 1 
fi

if  [  "$(docker images  |grep showdoc)" ]; then
  echo ""
  echo "You have installed showdoc image"
  echo "If you want to update showdoc, please execute  ./showdoc update "
  echo "If you want to restart showdoc, please execute    ./showdoc restart "
  echo "If you want to uninstall showdoc, please execute    ./showdoc uninstall "
  echo ""
  echo "你已经安装过showdoc镜像"
  echo "如果你想更新showdoc，请执行  ./showdoc update "
  echo "如果你想重启showdoc，请执行  ./showdoc restart "
  echo "如果你想卸载showdoc，请执行  ./showdoc uninstall "
  exit 1 
fi
sudo -s  service docker start
echo 'The showdoc image is being retrieved, please wait a moment... The time required is related to your network environment.'
echo '正在拉取showdoc镜像，请稍后...所需时间与你的网络环境有关'

if [ "$action" == "en" ] ;then
 	sudo -s docker pull star7th/showdoc
else
	sudo -s docker pull registry.cn-shenzhen.aliyuncs.com/star7th/showdoc
	sudo -s docker tag registry.cn-shenzhen.aliyuncs.com/star7th/showdoc:latest star7th/showdoc:latest 
fi



sudo -s mkdir /showdoc_data
if  [ ! -d "/showdoc_data" ]; then 
	echo "/showdoc_data directory does not exist. Make sure you have create permissions"
	echo "/showdoc_data 目录不存在，请确保有创建权限"
	exit 1 ;
fi
sudo -s mkdir /showdoc_data/html
sudo -s chmod  -R 777 /showdoc_data
sudo -s docker run -d --name showdoc --env SHOWDOC_SCRIPT_VERSION=${SHOWDOC_SCRIPT_VERSION}   --user=root  --restart=always --privileged=true -p 4999:80 -v /showdoc_data/html:/var/www/html/ star7th/showdoc

seconds_left=15
while [ $seconds_left -gt 0 ];do
  echo -n $seconds_left
  sleep 1
  seconds_left=$(($seconds_left - 1))
  echo -ne "\r     \r" #清除本行文字
done

sudo -s chmod  -R 777 /showdoc_data/html/server/Application
sudo -s chmod  -R 777 /showdoc_data/html/web/
sudo -s chmod  -R 777 /showdoc_data/html/web_src/
sudo -s chmod  -R 777 /showdoc_data/html/install/

if [ "$action" == "en" ] ;then
	echo -e "\n \033[32m Successful installation , The address is：http://localhost:4999 (You can also use LAN or public IP / domain name access)  \033[0m \n"
	echo -e " \033[32m The account password is showdoc/123456 , After you log in, you can see the admin buttun at the top right.Suggested password modification after login   \033[0m \n"
	echo -e " \033[32m For questions or suggestions on showdoc, please go to  https://github.com/star7th/showdoc   \033[0m \n"

else
	echo -e "\n \033[32m 安装成功，访问地址：http://localhost:4999 (你也可以用局域网或者公网IP/域名访问)  \033[0m \n"
	echo -e " \033[32m 如打算用局域网或者公网IP/域名访问，请注意防火墙/安全组等要对4999端口放行  \033[0m \n"
	echo -e " \033[32m 账户密码是showdoc/123456，登录后你便可以看到右上方的管理后台入口。建议登录后修改密码。   \033[0m \n"
	echo -e " \033[32m 对showdoc的问题或建议请至https://github.com/star7th/showdoc 提issue   \033[0m \n"
	echo -e " \033[32m 若觉得showdoc好用，不妨到github点个star。良好的关注度和参与度有助于开源项目的长远发展   \033[0m \n"
fi
