#!/usr/bin/env bash
YELLOW="\e[33m"
RES="\e[0m"
pkill filebrowser 2>/dev/null
if [ $(uname -o) == Android ]; then echo -e "\e[33
m仅适用于容器内\e[0m"; sleep 1; exit 0; fi
CONFIG() {
cd
if [ ! $(command -v filebrowser) ]; then
rm linux-arm64-filebrowser.tar.gz 2>/dev/null
#wget https://shell.xb6868.com/ut/linux-arm64-filebrowser.tar.gz
while [ -z "${version}" ]
do version=$(curl --connect-timeout 5 -m 8 https://kgithub.com/filebrowser/filebrowser|grep 'href="/filebrowser/filebrowser/releases/tag/' | awk -F 'tag/' '{print $2}' | cut -d '"' -f 1)
done
wget https://kgithub.com/filebrowser/filebrowser/releases/download/$version/linux-arm64-filebrowser.tar.gz
tar zxvf linux-arm64-filebrowser.tar.gz filebrowser
mv filebrowser /usr/local/bin/
cd /usr/local/bin/
chmod a+rx filebrowser
fi
cd /etc/
rm filebrowser.db 2>/dev/null
echo -e "${YELLOW}初始化数据库文件${RES}"
sleep 1
filebrowser -d filebrowser.db config init
echo -e "${YELLOW}设定监听地址${RES}"
sleep 1
filebrowser config set --address 0.0.0.0
if [ -e /sdcard ]; then
echo -e "${YELLOW}设定手机存储为当前主目录${RES}"
sleep 1
filebrowser config set --root /sdcard
else
echo -e "${YELLOW}设定主目录为当前主目录${RES}"
sleep 1
filebrowser config set --root ${HOME}
fi
echo -e "${YELLOW}设定端口${RES}"
sleep 1
filebrowser config set --port 38080
echo -e "${YELLOW}修改日志文件路径${RES}"
sleep 1
filebrowser config set --log /var/log/filebrowser.log
echo -e "${YELLOW}设定中文语言${RES}"
sleep 1
filebrowser config set --locale zh-cn
filebrowser config set --singleClick
echo -e "${YELLOW}设定管理员用户名与密码${RES}"
read -r -p "请输管理员用户名: " NAME
read -r -p "请输密码: " CODE
filebrowser users add $NAME $CODE --perm.admin
echo -e "你的用户名是 ${YELLOW}$NAME${RES} 登录密码 ${YELLOW}$CODE${RES}"
read -r -p "确认请回车" input
unset input
MAIN
}

GUEST() {
cd /etc
echo -e "\n1) 添加访问用户
2) 查询所有用户
3) 删除用户\n"

read -r -p "请选择: " input
case $input in
	1)
echo -e "${YELLOW}添加访问用户名与密码${RES}"
read -r -p "请输用户名: " NAME
read -r -p "请输密码: " CODE
filebrowser users add $NAME $CODE
;;
2)
filebrowser users ls
;;
3)
read -r -p "请输用户名: " NAME
filebrowser users rm $NAME
;;
esac
read -p "继续请回车"
MAIN
}

ETC_() {
#配置systemd，可忽略
cat >/etc/systemd/system/filebrowser.service <<-'EndOFsystemd'
[Unit]
Description=FileBrowser
After=network.target
Wants=network.target

[Service]
Type=simple
PIDFile=/var/run/filebrowser.pid
ExecStart=/usr/local/bin/filebrowser -d /etc/filebrowser.db
Restart=on-failure

[Install]
WantedBy=multi-user.target
EndOFsystemd
chmod a+rx /etc/systemd/system/filebrowser.service
#配置开机服务，可忽略
mkdir -pv /etc/init.d
cd /etc/init.d
cat >filebrowser <<-'EndOFinit'
#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides:          filebrowser
# Required-Start:    \$network \$local_fs \$remote_fs
# Required-Stop:     \$remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: filebrowser
### END INIT INFO
PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/local/bin/filebrowser
NAME="filebrowser"
DESC="filebrowser"
PIDFILE=/var/run/filebrowser.pid


[ -x "\$DAEMON" ] || exit 0

. /lib/lsb/init-functions

DAEMON_OPTS=""

case "\$1" in
  start)
    printf "%s\n" "Starting filebrowser... "
   /usr/local/bin/filebrowser -d /etc/filebrowser.db &
    ;;
restart)
printf "%s\n" "Restarting filebrowser... "
 pkill filebrowser
 /usr/local/bin/filebrowser -d /etc/filebrowser.db &
 ;;
  stop)
    printf "%s\n" "Stopping filebrowser... "
    pkill filebrowser
     log_daemon_msg "Stopping \$DESC" "\$NAME"
     start-stop-daemon --stop --quiet --oknodo --pidfile \$PIDFILE --remove-pidfile --exec \$DAEMON
     log_end_msg \$?
    ;;
  status)
  status_of_proc -p \$PIDFILE "\$DAEMON" "\$NAME" && exit 0 || exit \$?
    ;;
  *)
    printf "%s\n" "Usage: /etc/init.d/filebrowser {start|stop|status}"
    exit 1
    ;;
esac
exit 0
EndOFinit
chmod a+xr filebrowser
}
START() {
cd /etc
IP=`ip -br -4 a | egrep '192|172' | awk '{print $3}' | cut -d '/' -f 1| awk '{printf("%d) %s\n" ,NR,$0)}'`
if [ -z "$IP" ]; then
	filebrowser config set --address 0.0.0.0
	echo -e "本机访问请打开浏览器输地址 ${YELLOW}127.0.0.1:38080${RES}"
	filebrowser -d /etc/filebrowser.db &
	exit
else
echo "$IP" | grep '2)'
if [ $? == 1 ]; then
IP=`ip -br -4 a | egrep '192|172' | awk '{print $3}' | cut -d '/' -f 1`
echo -e "${YELLOW}检测到你的局域网ip地址是$IP${RES}"
sleep 2
filebrowser config set --address $IP
echo -e "局域网请打开浏览器输地址 ${YELLOW}$IP:38080${RES}"
filebrowser -d /etc/filebrowser.db &
exit
fi
fi
echo -e "\n${YELLOW}检测到你有多个局域网ip地址${RES}"
echo -e "$IP${RES}\n"
read -r -p "请选择本设备ip: " input
IP=`echo "$IP" | grep -w "${input})" | awk '{print $2}'`
if [ -n "$IP" ]; then
filebrowser config set --address $IP
echo -e "局域网请打开浏览器输地址 ${YELLOW}$IP:38080${RES}"
else
filebrowser config set --address 0.0.0.0
echo -e "本机访问请打开浏览器输地址 ${YELLOW}127.0.0.1:38080${RES}"
fi
filebrowser -d /etc/filebrowser.db &
sleep 2
}
MAIN() {
clear
unset NAME CODE input IP
echo -e "
1) 安装配置
2) 运行
3) 用户管理
0) 退出\n"
	read -r -p "请选择 " input
	case $input in
		1) CONFIG ;;
		2) START ;;
		3) GUEST ;;
		*) exit 0 ;;
	esac
}
MAIN "$@"
