#!/usr/bin/env bash
WINE_MENU(){
if [[ $(id -u) != 0 ]];then echo -e "\e[33m请使用root用户运行本脚本\e[0m"; sleep 2; exit 0; fi
WINE_INSTALL=XB6868
cd
URL="https://mirrors.tuna.tsinghua.edu.cn"
export NEWT_COLORS='
root=,blue'
if [ ! $(command -v kali-undercover) ]; then
KALI="安装仿windows桌面"
KALI_UNDERCOVER="bash ${HOME}/undercover"
else
KALI="启动仿windows桌面"
KALI_UNDERCOVER="startvnc"
fi
if [ ! $(command -v wine) ]; then
WINE="你尚未安装wine，请选执行选项9"
else
if [[ $(echo "$(box64 wine64 --version)"|tail -1|cut -b 6) == [4-9] ]]; then
WINE="安装wine3.9"
else
WINE="安装wine-8.5"
fi
fi
if grep ^onboard /usr/local/bin/startwine; then
KB="关闭桌面键盘"
else
KB="开启桌面键盘"
fi
list=$(whiptail --title "运行菜单" --menu "请上下滑动选择\n\n" 0 0 0 \
"1" "开启vnc wine" \
"2" "开启xsdl wine" \
"3" "使用虚拟桌面" \
"4" "关闭虚拟桌面" \
"5" "修改分辨率(非仿windows桌面)" \
"6" "提高程序优先级" \
"7" "${KALI}" \
"8" "${KB}" \
"9" "重新进行首次安装firstrun" \
"10" "安装gecko，mono" \
"11" "${WINE}" \
"12" "安装linux版firefox浏览器、qq和vlc播放器" \
"13" "32位wine4.0.3(该版本仅配置于仿windows桌面)" \
"14" "检测更新box86 box64版本" \
"0" "退出" \
3>&1 1>&2 2>&3)
if [ -n "$list" ]; then
case $list in
1) startwine ;;
2) startvsdl ;;
3) kk ;;
4) gg ;;
5) fbl ;;
6) yy ;;
7) ${KALI_UNDERCOVER}
exit 0 ;;
8) kb ;;
9) bash ${HOME}/firstrun ;;
10)
rm *.msi 2>/dev/null
#gecko ie相关
#mono .net程序相关
case $WINE in
*3.9*)
#最新版本
wget ${URL}/winehq/wine/wine-mono/$(curl ${URL}/winehq/wine/wine-mono/ | grep href | tail -1 | awk -F 'href="' '{print $2}' | cut -d '/' -f 1)/wine-mono-$(curl ${URL}/winehq/wine/wine-mono/ | grep href | tail -1 | awk -F 'href="' '{print $2}' | cut -d '/' -f 1)-x86.msi
wget ${URL}/winehq/wine/wine-gecko/$(curl ${URL}/winehq/wine/wine-gecko/ | grep href | tail -1 | awk -F 'href="' '{print $2}' | cut -d '/' -f 1)/wine-gecko-$(curl ${URL}/winehq/wine/wine-gecko/ | grep href | tail -1 | awk -F 'href="' '{print $2}' | cut -d '/' -f 1)-x86.msi
wget ${URL}/winehq/wine/wine-gecko/$(curl ${URL}/winehq/wine/wine-gecko/ | grep href | tail -1 | awk -F 'href="' '{print $2}' | cut -d '/' -f 1)/wine-gecko-$(curl ${URL}/winehq/wine/wine-gecko/ | grep href | tail -1 | awk -F 'href="' '{print $2}' | cut -d '/' -f 1)-x86_64.msi
;;
*)
#wine3.9
wget https://mirrors.tuna.tsinghua.edu.cn/winehq/wine/wine-mono/4.7.2/wine-mono-4.7.2.msi
wget https://mirrors.tuna.tsinghua.edu.cn/winehq/wine/wine-gecko/2.47/wine_gecko-2.47-x86.msi
wget https://mirrors.tuna.tsinghua.edu.cn/winehq/wine/wine-gecko/2.47/wine_gecko-2.47-x86_64.msi
esac
echo -e "\n\e[33m即将安装，请确认已关闭wine\n如长时间没反应，请按回车并通过control确认是否安装成功\n如不成功可自行安装已下载的msi包\e[0m\n"
read -p "确认请回车" input
unset input
for i in ./wine*.msi;do box64 wine64 start /i $i; done
pstree | grep -q "services"
while [ $? == 0 ]
do
sleep 1
pstree | grep -q "services"
done
echo -e "\e[33m处理完成\e[0m"
exit 0
;;
11)
echo -e "\e[33m正在检测已安装wine版本并进行清除(仅对本脚本安装的wine文件有效)\e[0m"
sleep 3
rm PlayOnLinux* wine-8.5.tar.gz* 2>/dev/null
case $WINE_INSTALL in
PLAYONLINUX)
for i in $(sed 's@\./@/@g' /usr/share/doc/wine/postrm | sed ':a;N;s/\n/ /g;ta'); do if [ -f "$i" ]; then rm -v $i; fi done ;;
*)
for i in $(sed 's@\./@/usr/@g' /usr/share/doc/wine/postrm | sed ':a;N;s/\n/ /g;ta'); do if [ -f "$i" ]; then rm -v $i; fi done
esac
#for i in $(sed ':a;N;s/\n/ /g;ta' /usr/share/doc/wine/postrm|sed 's@\./@/usr/@g'); do if [ -f "$i" ]; then rm -v $i; fi done
case $WINE in
*8.5*)

case $WINE_INSTALL in
PLAYONLINUX)
axel https://www.playonlinux.com/wine/binaries/phoenicis/upstream-linux-amd64/PlayOnLinux-wine-6.17-upstream-linux-amd64.tar.gz
tar zxvf sdcard/xinhao/PlayOnLinux-wine-6.17-upstream-linux-amd64.tar.gz -C /usr/ ./lib >/usr/share/doc/wine/postrm 2>&1
LXC=debian ROOTFS=bullseye WINE_URL="https://dl.winehq.org"; axel -o wine-devel-i386.deb ${WINE_URL}/wine-builds/${LXC}/dists/${ROOTFS}/main/binary-i386/$(curl ${WINE_URL}/wine-builds/${LXC}/dists/${ROOTFS}/main/binary-i386/|grep wine-devel-i386_8|awk -F 'href="' '{print $2}'|cut -d '"' -f 1|tail -n 1); axel -o wine-devel-amd64.deb ${WINE_URL}/wine-builds/${LXC}/dists/${ROOTFS}/main/binary-amd64/$(curl ${WINE_URL}/wine-builds/${LXC}/dists/${ROOTFS}/main/binary-amd64/|grep wine-devel-amd64_8|awk -F 'href="' '{print $2}'|cut -d '"' -f 1|tail -n 1); axel -o wine-devel.deb  ${WINE_URL}/wine-builds/${LXC}/dists/${ROOTFS}/main/binary-amd64/$(curl ${WINE_URL}/wine-builds/${LXC}/dists/${ROOTFS}/main/binary-amd64/|grep wine-devel_8|awk -F 'href="' '{print $2}'|cut -d '"' -f 1|tail -n 1); axel -o winehq-devel.deb ${WINE_URL}/wine-builds/${LXC}/dists/${ROOTFS}/main/binary-amd64/$(curl ${WINE_URL}/wine-builds/${LXC}/dists/${ROOTFS}/main/binary-amd64/|grep winehq-devel_8|awk -F 'href="' '{print $2}'|cut -d '"' -f 1|tail -n 1); for i in wine-devel-amd64.deb  winehq-devel.deb wine-devel.deb wine-devel-i386.deb; do dpkg -X $i / >>/usr/share/doc/wine/postrm 2>&1; done
sed -i 's@^./lib@./usr/lib@' /usr/share/doc/wine/postrm
;;
*)
wget https://shell.xb6868.com/wine/wine-8.5.tar.gz
tar zxvf wine-8.5.tar.gz -C / >/usr/share/doc/wine/postrm 2>&1
rm wine-8.5.tar.gz 2>/dev/null
esac
;;
*)
for i in $(sed 's@^\.@@g' /usr/share/doc/wine/postrm | sed ':a;N;s/\n/ /g;ta'); do if [ -f "$i" ]; then rm -v $i; fi done
wget https://shell.xb6868.com/wine/PlayOnLinux-wine-3.9-upstream-linux-amd64.tar.gz
tar zxvf PlayOnLinux-wine-*-upstream-linux-amd64.tar.gz -C /usr >/usr/share/doc/wine/postrm 2>&1
rm PlayOnLinux-wine-*-upstream-linux-amd64.tar.gz box86.tar.gz box64.tar.gz
esac

rm ${HOME}/桌面/explorer.desktop ${HOME}/Desktop/explorer.desktop 2>/dev/null
sed -i 's/^Icon.*$/Icon=utilities-system-monitor/' /usr/share/applications/wine.desktop
cp /usr/share/applications/wine.desktop ${HOME}/Desktop/wine.desktop 2>/dev/null
sed -i 's/^Exec.*$/Exec=boxwinede %f/' ${HOME}/Desktop/wine.desktop 2>/dev/null
cp /usr/share/applications/wine.desktop ${HOME}/桌面/wine.desktop 2>/dev/null
sed -i 's/^Exec.*$/Exec=boxwinede %f/' ${HOME}/桌面/wine.desktop 2>/dev/null
sed -i 's/^Exec=wine/Exec=box64 wine64/' /usr/share/applications/wine.desktop 2>/dev/null
chmod a+x ${HOME}/桌面 ${HOME}/Desktop -R 2>/dev/null

bash firstrun
exit 0
;;
12)
if [ ! $(command -v kali-undercover) ]; then
echo -e "\e[33m请先安装仿windows桌面\e[0m"
sleep 3
WINE_MENU
fi
if grep -q ID=ubuntu /etc/os-release ; then
DEPENDS="firefox firefox-locale-zh-hans"
else
DEPENDS="firefox-esr firefox-esr-l10n-zh-cn"
fi
apt update
cd
apt install --no-install-recommends vlc $DEPENDS -y
i=0
while [ ! $(command -v vlc) ] || [ ! $(command -v firefox) ] && [[ $i -ne 3 ]]
do
echo -e "\e[31m似乎安装出错,重新执行安装\e[0m"
i=$(( $i+1 ))
sleep 1
apt --fix-broken install -y && apt install $DEPENDS vlc --no-install-recommends -y
done
sed -i 's/geteuid/getppid/' /usr/bin/vlc
cp /usr/share/applications/firefox*.desktop ${HOME}/Desktop
cp /usr/share/applications/vlc.desktop ${HOME}/Desktop
cp /usr/share/applications/firefox*.desktop ${HOME}/桌面
cp /usr/share/applications/vlc.desktop ${HOME}/桌面
rm linuxqq_*_arm64.deb 2>/dev/null
if [ ! $(command -v qq) ]; then
echo -e "\e[33m检测最新版本qq\e[0m"
sleep 1
wget $(curl https://aur.archlinux.org/packages/linuxqq|grep arm64|cut -d '"' -f2)
apt install ./linuxqq_*_arm64.deb -y
if [ $(command -v qq) ]; then
echo 'export GDK_NATIVE_WINDOWS=true' >>/etc/profile
cp /usr/share/applications/qq.desktop ${HOME}/Desktop
cp /usr/share/applications/qq.desktop ${HOME}/桌面
fi
fi
chmod a+x ${HOME}/桌面 ${HOME}/Desktop -R
;;
13) 
if [ ! $(command -v kali-undercover) ] || [ ! $(command -v box86) ]; then
echo -e "你尚未安装box86或仿windows桌面"
sleep 3
WINE_MENU
fi
read -p "本wine4.0将集中在主目录下一个wine文件夹内，且仅对仿windows桌面进行配置并创建桌面快捷，确认请回车"
cd && mkdir wine4 2>/dev/null
rm PlayOnLinux-wine-4.0.3-upstream-linux-x86.tar.gz 2>/dev/null

case $WINE_INSTALL in
PLAYONLINUX)
axel https://www.playonlinux.com/wine/binaries/phoenicis/upstream-linux-x86/PlayOnLinux-wine-4.0.3-upstream-linux-x86.tar.gz
;;
*)
wget https://shell.xb6868.com/wine/PlayOnLinux-wine-4.0.3-upstream-linux-x86.tar.gz
esac
if [ ! -f PlayOnLinux-wine-4.0.3-upstream-linux-x86.tar.gz ]; then
echo -e "\e[31m下载失败，请重试\e[0m"
sleep 3
WINE_MENU
fi
tar zxvf PlayOnLinux-wine-4.0.3-upstream-linux-x86.tar.gz -C wine4
rm -rf wine4/.wine 2>/dev/null
WINEDEBUG=fixme-all WINEPREFIX="/root/wine4/.wine" box86 /root/wine4/bin/wine wineboot
pstree | grep -q "services"
while [ $? == 0 ]
do
sleep 1
pstree | grep -q "services"
done
cp ${HOME}/Desktop/wine.desktop ${HOME}/Desktop/wine4.desktop
sed -i 's@Exec.*@Exec=env BOX86_NOPULSE=1 WINEDEBUG=fixme-all WINEPREFIX="/root/wine4/.wine" box86 /root/wine4/bin/wine explorer /desktop,640x480 taskmgr %f@' ${HOME}/Desktop/wine4.desktop
sed -i 's/=wine/=wine4/' ${HOME}/Desktop/wine4.desktop
chmod a+x ${HOME}/Desktop/wine4.desktop
rm PlayOnLinux-wine-4.0.3-upstream-linux-x86.tar.gz*
;;
14)
if [ ! $(command -v box86) ]||[ ! $(command -v box64) ]; then
echo -e "你尚未安装box86或box64"
sleep 2
else
echo -e "检验中…"
curl -s https://shell.xb6868.com/wine/boxwine.sh|grep '^#box_update'|awk '{print $2}'
echo -e "\n你目前版本的时间是\nbox86 $(date -d "$(LANG=c.UTF_8 box86 --version|awk -F ' on ' '{print $2}'|awk '{print $1,$2,$3}')" +%Y%m%d)\nbox64 $(date -d "$(LANG=c.UTF_8 box64 --version|awk -F ' on ' '{print $2}'|awk '{print $1,$2,$3}')" +%Y%m%d)\n\n是否更新安装"
read -r -p '1) 是 2) 返回: ' input
case $input in
1)
rm box86.tar.gz* box64.tar.gz* 2>/dev/null
wget https://shell.xb6868.com/wine/box86.tar.gz
wget https://shell.xb6868.com/wine/box64.tar.gz
tar zxvf box86.tar.gz -C /
tar zxvf box64.tar.gz -C /
box86 --version
box64 --version
sleep 3
esac
fi
WINE_MENU
;;
0) exit 0
esac
sleep 1.5
WINE_MENU
else
echo  -e "\e[33m你已取消选择\e[0m"
sleep 1
exit 0
fi
}
WINE_MENU "$@"
