#/usr/bin/env bash
cd ${HOME}
if [ $(id -u) != 0 ]; then sudo=sudo; fi
YELLOW="\e[33m"
RES="\e[0m"
confirm() {
read -p "回车继续" input
case $input in
	*) echo "" ;;
esac
}
install_wine() {
echo -e "本脚本仅在bullseye中完成测试\n如果安装失败，请重试"
confirm
rm -rf box64.tar.gz box64 box86.tar.gz box86
$sudo apt install wget tar tigervnc-standalone-server tigervnc-viewer pulseaudio curl python3 -y && $sudo apt install -y
dpkg --add-architecture armhf
apt update
$sudo apt install zenity:armhf libstdc++6:armhf gcc-arm-linux-gnueabihf mesa-utils:armhf mesa-common-dev:armhf mesa-opencl-icd:armhf mesa-vdpau-drivers:armhf mesa-va-drivers:armhf mesa-vulkan-drivers:armhf mesa-utils-extra:armhf libasound2:armhf libasound2-plugins:armhf libasound2-dev:armhf libasound2-plugin-smixer:armhf libasound2-data:armhf libasound-dev:armhf -y && $sudo apt install -y
$sudo apt install zenity libstdc++6 mesa* cmake build-essential libasound2 libasound2-data libasound2-plugins libasound2-dev libasound2-plugin-smixer libasound-dev -y && $sudo apt install -y
#apt install git build-essential cmake wget gcc-arm-linux-gnueabihf zenity:armhf libasound*:armhf libstdc++6:armhf mesa*:armhf
unset version
while [ -z "${version}" ]
do version=$(curl --connect-timeout 5 -m 8 https://github.com/ptitSeb/box86 | grep '/ptitSeb/box86/releases/tag/' | awk -F 'href="' '{print $2}' | cut -d '/' -f 6 | cut -d '"' -f 1)
done
wget -O box86.tar.gz https://codeload.github.com/ptitSeb/box86/tar.gz/refs/tags/${version}
mkdir box86
tar zxvf box86.tar.gz -C box86
VERSION=`ls box86`
mkdir -p box86/$VERSION/build
cd box86/$VERSION/build
cmake .. -DRPI4ARM64=1 -DARM_DYNAREC=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo
make -j$(nproc); make install
cd
unset version
while [ -z "${version}" ]
do version=$(curl --connect-timeout 5 -m 8 https://github.com/ptitSeb/box64 | grep '/ptitSeb/box64/releases/tag/' | awk -F 'href="' '{print $2}' | cut -d '/' -f 6 | cut -d '"' -f 1)
done
wget -O box64.tar.gz https://codeload.github.com/ptitSeb/box64/tar.gz/refs/tags/${version}
mkdir box64
tar zxvf box64.tar.gz -C box64
VERSION=`ls box64`
mkdir -p box64/$VERSION/build
cd box64/$VERSION/build
#cmake .. -DARM_DYNAREC=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=/usr
cmake .. -DARM_DYNAREC=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DRPI4ARM64=1
make -j$(nproc); make install
cd
rm -rf box64.tar.gz box64 box86.tar.gz box86
mkdir wine64
read -r -p "是否获取wine下载地址 1)是 2)否 " input
case $input in
	2) 
	echo -e "已为你在主目录创建wine64文件夹，请把wine包解压至此目录"
		confirm ;;
	*) 
unset version
version=$(curl https://www.playonlinux.com/wine/binaries/phoenicis/upstream-linux-amd64/ | grep "tar.gz'" | awk -F "href='" '{print $2}' | awk -F "'>" '{print $1}' | grep 6.17)
echo -e "下载地址：\n${YELLOW}https://www.playonlinux.com/wine/binaries/phoenicis/upstream-linux-amd64/$version${RES}\n"
read -r -p "1)下载(速度很慢) 0)返回 " input
case $input in
	1)
wget https://www.playonlinux.com/wine/binaries/phoenicis/upstream-linux-amd64/$version
tar zxvf PlayOnLinux-wine-6.17-upstream-linux-amd64.tar.gz -C wine64 ;;
	*) echo -e "\n已为你在主目录创建wine64文件夹，记得把wine包解压到wine64目录哦"
		confirm ;;
esac ;;
esac
#tar zxvf /sdcard/BaiduNetdisk/PlayOnLinux-wine-6.14-upstream-linux-amd64.tar.gz -C wine64

main
}
add_msi() {
#box64 wine64 msiexec -i *.msi
echo -e "
1) 获取mono最新版下载
2) 获取gecko最新版下载
0) 返回\n"
read -r -p "请选择: " input
case $input in
	1)
version=`curl https://mirrors.tuna.tsinghua.edu.cn/winehq/wine/wine-mono/ | grep href | tail -1 | awk -F 'href="' '{print $2}' | cut -d '/' -f 1`
wget https://mirrors.tuna.tsinghua.edu.cn/winehq/wine/wine-mono/$version/wine-mono-$version-x86.msi ;;

	2)
version=`curl https://mirrors.tuna.tsinghua.edu.cn/winehq/wine/wine-gecko/ | grep href | tail -1 | awk -F 'href="' '{print $2}' | cut -d '/' -f 1`
wget https://mirrors.tuna.tsinghua.edu.cn/winehq/wine/wine-gecko/$version/wine-gecko-$version-x86.msi ;;
	*) echo "" ;;
esac
main
}
start_none() {
echo -e "${YELLOW}启动程序时间比较长，请耐心等待，如果长时间不动，请关闭重新启动${RES}"
vncserver -kill $DISPLAY 2>/dev/null
pkill -9 Xtightvnc 2>/dev/null
pkill -9 Xtigertvnc 2>/dev/null
pkill -9 Xvnc 2>/dev/null
pkill -9 vncsession 2>/dev/null
#export USER="$(whoami)"
export PULSE_SERVER=127.0.0.1
trap "pkill Xvnc 2>/dev/null; exit" SIGINT EXIT
Xvnc -ZlibLevel=1 -quiet -ImprovedHextile -CompareFB 1 -br -retro -a 5 -wm -alwaysshared -geometry 800x600 -once -depth 16 -localhost -securitytypes None &
export DISPLAY=:0
export BOX86_PATH=~/wine64/bin/
export BOX86_LD_LIBRARY_PATH=~/wine64/lib/wine/i386-unix/:/lib/i386-linux-gnu:/lib/aarch64-linux-gnu/
export BOX64_PATH=~/wine64/bin/
export BOX64_LD_LIBRARY_PATH=~/wine64/lib/wine/i386-unix/:~/wine64/lib/wine/x86_64-unix/:/lib/i386-linux-gnu/:/lib/x86_64-linux-gnu:/lib/aarch64-linux-gnu/
#export WINEPREFIX="~/.wine"
#export WINEARCH=win32
box64 wine64 taskmgr
}
start_wine() {
echo -e "${YELLOW}启动程序时间比较长，请耐心等待，如果长时间不动，请关闭重新启动${RES}"
export BOX86_PATH=~/wine64/bin/
export BOX86_LD_LIBRARY_PATH=~/wine64/lib/wine/i386-unix/:/lib/i386-linux-gnu:/lib/aarch64-linux-gnu/
export BOX64_PATH=~/wine64/bin/
export BOX64_LD_LIBRARY_PATH=~/wine64/lib/wine/i386-unix/:~/wine64/lib/wine/x86_64-unix/:/lib/i386-linux-gnu/:/lib/x86_64-linux-gnu:/lib/aarch64-linux-gnu/
read -r -p "1)启动任务管理器(运行exe程序) 2)启动winecfg " input
case $input in
	2) box64 wine64 winecfg ;;
	*) box64 wine64 taskmgr ;;
esac
exit 0
}
fix_() {
echo -e "由于proot环境原因，此次操作将在每次登录容器自动删除wine配置文件(主目录.wine)\n"
confirm
echo 'rm -rf ${HOME}/.wine' >>.bashrc
rm -rf ${HOME}/.wine
main
}
down_bullseye() {
echo -e "请下载好容器再使用此脚本配置"
sleep 2
bash -c "$(curl https://shell.xb6868.com/ut/bullseye.sh)"
}
main(){
clear
echo -e "
${YELLOW}欢迎使用BOX+WINE自编译配置脚本 (测试版)${RES}"
echo -e "运行exe的效率会比qemu高很多，特别是3d游戏，同时bug也多"
uname -a | grep 'Android' -q
        if [ $? == 0 ]; then
echo -e "
请在容器中使用本脚本\n
1)	下载容器编译安装box86+box64+wine
0)	退出\n"
read -r -p "请选择: " input
case $input in
	1) down_bullseye ;;
	*) exit 0 ;;
esac
	else
echo -e "
由于涉及两种架构的包，系统极容崩掉，请做好备份，系统崩溃恕不负责\n
1)	编译安装box86+box64+wine
2)	启动wine(桌面运行)
3)	启动wine(非桌面环境，不建议)
4)	退出容器后无法再次启动
5)	下载mono与gecko插件
0)	退出\n"
	read -r -p "请选择: " input
	case $input in
		1) install_wine ;;
		2) start_wine ;;
		3) start_none ;;
		4) fix_ ;;
		5) add_msi ;;
		*) exit 0 ;;
	esac
	fi
}
main "$@"
