#!/usr/bin/env bash

case $(dpkg --print-architecture) in
        aarch64|arm64) echo "" ;;
	*) echo -e "\e[31m不支持你的架构\e[0m"
                sleep 2
                exit 0
esac

#proot登录方式
PROOT=PRO

#普通用户 TRUE
NOR_USER=

#镜像站检测
MIRROR=TRUE

#容器
ROOTFS=sid

#box安装方式
BOX_INSTALL=XB6868
#box_update box86最后更新时间:20230628
#box_update box64最后更新时间:20230322

#wine安装方式
WINE_INSTALL=XB6868

#wine版本
WINE_VERSION=3.9

#自定义容器名字
NAME=wine-aarch64

CONTAINER=".${NAME}"

START=start-${NAME}

LXC=debian
DEB="main contrib non-free non-free-firmware"

URL="https://mirrors.tuna.tsinghua.edu.cn"
cd

if [ $(uname -o) != Android ]; then echo -e "\e[33m仅适用于termux环境\e[0m"; sleep 1; exit 0; fi

echo -e "\e[33m检测环境..\e[0m"
sleep 1
clear
unset i
while [ ! $(command -v proot) ] && [[ $i -ne 3 ]]
do
pkg up -y && yes|pkg upgrade && pkg i -y curl proot tar pulseaudio wget 
#xkeyboard-config virglrenderer-android
i=$(( $i+1 ))
done
if [ ! $(command -v proot) ]; then
echo -e "\e[33m检测环境失败，安装中止\e[0m"
sleep 1
exit 0
fi
unset i
if [ -d ${CONTAINER} ]; then echo -e "\n检测已有相关文件夹，是否删除？\n\e[33m1) 删除重装\n2) 删除\n0) 退出\e[0m\n";
read -r -p "请选择: " input
case $input in
1) rm -rf ${CONTAINER} ;;
2) rm -rf ${CONTAINER} ${PREFIX}/bin/start-wine*
echo -e "\e[33m已御载${NAME}\e[0m"
sleep 1
exit 0 ;;
*) exit 0
esac
fi
echo -e "\n\n\e[33m本脚本为支持proot容器运行windows exe的32位与64位程序\n\n即将下载系统,本脚本是进行全新安装,非恢复包\e[0m"
sleep 3
rm rootfs.tar.xz 2>/dev/null
echo -e "\n检测最新更新的容器${LXC}-${ROOTFS}\e[0m\n"
sleep 1

mkdir ${CONTAINER}

case $MIRROR in
TRUE)
echo -e "源地址检测中…"
TUNA=`echo $(ping -w5 -W5 -c5 -q mirrors.tuna.tsinghua.edu.cn)|awk -F '/' '{print $NF}'|cut -d '.' -f 1`; if [ -z $TUNA ]; then echo -e "清华源网络不通"; TUNA=999; fi
BFSU=`echo $(ping -w5 -W5 -c5 -q mirrors.bfsu.edu.cn)|awk -F '/' '{print $NF}'|cut -d '.' -f 1`; if [ -z $BFSU ]; then echo -e "北外源网络不通"; BFSU=999; fi
if (( $TUNA == 999 )) && (( $BFSU == 999 )); then
echo -e "源地址网络不通，中止安装"
sleep 2
exit 0
elif (( $BFSU > $TUNA )); then
echo -e "\e[33m清华源网络比较稳定，将使用清华源\e[0m"
URL="https://mirrors.tuna.tsinghua.edu.cn"
else
URL="https://mirrors.bfsu.edu.cn"
echo -e "\e[33m北外源网络比较稳定，将使用北外源\e[0m"
fi
sleep 2
esac

curl -O ${URL}/lxc-images/images/${LXC}/${ROOTFS}/arm64/default/$(curl ${URL}/lxc-images/images/${LXC}/${ROOTFS}/arm64/default/ | grep href | tail -n 2 | cut -d '"' -f 4 | head -n 1)rootfs.tar.xz
echo -e "\e[33m检测包下载完整性\e[0m"
if [[ $(du -b rootfs.tar.xz|awk '{print $1}') != $(curl -I ${URL}/lxc-images/images/${LXC}/${ROOTFS}/arm64/default/$(curl ${URL}/lxc-images/images/${LXC}/${ROOTFS}/arm64/default/ | grep href | tail -n 2 | cut -d '"' -f 4 | head -n 1)rootfs.tar.xz|grep -i length|awk '{print $2}'|sed "s/\r//") ]]; then echo -e "\e[31m下载的包并不完整，但不会中止此次操作，如果安装失败，建议切换网络重新执行此脚本，\e[0m"; read -p "回车键继续"; fi


tar xvf rootfs.tar.xz -C ${CONTAINER}
rm rootfs.tar.xz
echo 'for i in /var/run/dbus/pid /tmp/.X*-lock /tmp/.X11-unix/X*; do if [ -e "${i}" ]; then rm -vf ${i}; fi done' >>${CONTAINER}/etc/profile
#伪proc文件
mkdir ${CONTAINER}/etc/proc/ -p
printf ' 52 memory_bandwidth! 53 network_throughput! 54 network_latency! 55 cpu_dma_latency! 56 xt_qtaguid! 57 vndbinder! 58 hwbinder! 59 binder! 60 ashmem!239 uhid!236 device-mapper!223 uinput!  1 psaux!200 tun!237 loop-control! 61 lightnvm!228 hpet!229 fuse!242 rfkill! 62 ion! 63 vga_arbiter\n' | sed 's/!/\n/g' >${CONTAINER}/etc/proc/misc
printf "%-1s %-1s %-1s %8s %6s %6s %6s %6s %6s %6s %6s %6s %6s %6s %6s\n" Node 0, zone DMA 3 2 2 4 3 3 2 1 2 2 0 Node 0, zone DMA32 1774 851 511 220 67 3 2 0 0 1 0 >${CONTAINER}/etc/proc/buddyinfo
rm ${CONTAINER}/etc/proc/filesystems 2>/dev/null
for i in sysfs rootfs ramfs bdev proc cpuset cgroup cgroup2 tmpfs configfs debugfs tracefs sockfs dax bpf pipefs devpts fuse fusectl selinuxfs oprofilefs pstore sdcardfs; do echo "nodev	$i" >>${CONTAINER}/etc/proc/filesystems; done
for i in fuseblk v7 sysv iso9660 msdos vfat squashfs ext2 ext4 ext3; do sed -i "/devpts/a\	$i" ${CONTAINER}/etc/proc/filesystems; done

echo "0.03 0.03 0.00 1/116 17521" >${CONTAINER}/etc/proc/loadavg
touch ${CONTAINER}/etc/proc/kmsg
echo 'tty0                 -WU (EC p  )    4:7' >${CONTAINER}/etc/proc/consoles
echo '0-0     Linux                   [kernel]' >${CONTAINER}/etc/proc/execdomains
echo '0 EFI VGA' >${CONTAINER}/etc/proc/fb
echo '    0:     9 8/8 3/1000000 27/25000000' >${CONTAINER}/etc/proc/key-users
echo '285490.46 1021963.95' >${CONTAINER}/etc/proc/uptime
echo $(uname -a) | sed 's/Android/GNU\/Linux/' >${CONTAINER}/etc/proc/version
touch ${CONTAINER}/etc/proc/vmstat
echo 'Character devices:!  1 mem!  4 /dev/vc/0!  4 tty!  4 ttyS!  5 /dev/tty!  5 /dev/console!  5 /dev/ptmx!  7 vcs! 10 misc! 13 input! 21 sg! 29 fb! 81 video4linux!128 ptm!136 pts!180 usb!189 usb_device!202 cpu/msr!203 cpu/cpuid!212 DVB!244 hidraw!245 rpmb!246 usbmon!247 nvme!248 watchdog!249 ptp!250 pps!251 media!252 rtc!253 dax!254 gpiochip!!Block devices:!  1 ramdisk!  7 loop!  8 sd! 11 sr! 65 sd! 66 sd! 67 sd! 68 sd! 69 sd! 70 sd! 71 sd!128 sd!129 sd!130 sd!131 sd!132 sd!133 sd!134 sd!135 sd!179 mmc!253 device-mapper!254 virtblk!259 blkext' | sed 's/!/\n/g' >${CONTAINER}/etc/proc/devices
echo "cpu  0 0 0 0 0 0 0 0 0 0
cpu0 0 0 0 0 0 0 0 0 0 0
intr 1
ctxt 0
btime 0
processes 0
procs_running 1
procs_blocked 0
softirq 0 0 0 0 0 0 0 0 0 0 0" >${CONTAINER}/etc/proc/stat
cpus=`cat -n /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_max_freq | tail -n 1 | awk '{print $1}'`
if [ -n $cpus ]; then
while [[ $cpus -ne 1 ]]
do
cpus=$(( $cpus-1 ))
sed -i "2a cpu${cpus} 0 0 0 0 0 0 0 0 0 0" ${CONTAINER}/etc/proc/stat
done
fi

sed -i '3i export MOZ_FAKE_NO_SANDBOX=1' ${CONTAINER}/etc/profile

sed -i "/#.*zh_CN.UTF-8 UTF-8/s/#//;/#.*zh_TW.UTF-8 UTF-8/s/#//;/#.*ja_JP.UTF-8 UTF-8/s/#//" ${CONTAINER}/etc/locale.gen
rm ${CONTAINER}/etc/resolv.conf 2>/dev/null
echo "nameserver 223.5.5.5
nameserver 223.6.6.6" >${CONTAINER}/etc/resolv.conf
echo "deb ${URL}/debian/ ${ROOTFS} ${DEB}" >${CONTAINER}/etc/apt/sources.list
curl ${URL}/debian/pool/main/c/ca-certificates/$(curl ${URL}/debian/pool/main/c/ca-certificates/|grep all.deb|awk -F 'href="' '{print $2}'|cut -d '"' -f 1|tail -n 1) -o ${CONTAINER}/root/ca.deb
curl ${URL}/debian/pool/main/o/openssl/$(curl ${URL}/debian/pool/main/o/openssl/|grep openssl_1.*arm64.deb|awk -F 'href="' '{print $2}'|cut -d '"' -f 1|tail -n 1) -o ${CONTAINER}/root/openssl.deb
curl ${URL}/kali/pool/main/o/openssl/$(curl ${URL}/kali/pool/main/o/openssl/|grep libssl1.1_1.*arm64.deb|awk -F 'href="' '{print $2}'|cut -d '"' -f 1|tail -n 1) -o ${CONTAINER}/root/libssl.deb

echo -e "\e[33m系统已下载,文件夹名为${CONTAINER}\e[0m"
echo -e "登录命令为\e[33m${START}\e[0m"
sleep 5

cat >${CONTAINER}/root/firstrun<<-'eof'
#!/usr/bin/env bash

URL="https://mirrors.tuna.tsinghua.edu.cn"
echo -e "\n\e[33m正在配置首次运行\n安装常用应用\e[0m"
sleep 1
export LANG=C.UTF-8
cd
DEPENDS="apt-utils python3 git busybox curl wget tar vim fonts-wqy-microhei gnupg2 dbus-x11 libxinerama1 libxrandr2 libxcomposite1 libxcursor1 libncurses5 libgtk2.0-0 tigervnc-standalone-server tigervnc-viewer pulseaudio axel x11vnc xvfb psmisc procps onboard xfwm4 whiptail libtcmalloc-minimal4 xserver-xephyr mesa-utils cabextract p7zip-full winbind mesa-vulkan-drivers"

DEPENDS0="zenity:armhf libegl-mesa0:armhf libgl1-mesa-dri:armhf libglapi-mesa:armhf libglx-mesa0:armhf libasound*:armhf libstdc++6:armhf libtcmalloc-minimal4:armhf gcc-arm-linux-gnueabihf libncurses5:armhf mesa-vulkan-drivers:armhf sl:armhf -y"


dpkg -l ca-certificates | grep ii
if [ $? == 1 ]; then
dpkg -i libssl.deb
dpkg -i openssl.deb
dpkg -i ca.deb
fi

dpkg --add-architecture armhf
apt update

i=0
apt install ${DEPENDS0} --no-install-recommends -y
while [ ! -f /usr/games/sl  ] && [[ $i -ne 3 ]]
do
echo -e "\e[31m似乎安装出错,重新执行安装\e[0m"
i=$(( $i+1 ))
sleep 1
apt --fix-broken install -y && apt install ${DEPENDS0} --no-install-recommends -y
done
if [ ! -f /usr/games/sl  ]; then
echo -e "\e[31m似乎安装出错,退出安装\e[0m"
return 0
fi

i=0
apt install -y && apt install ${DEPENDS} --no-install-recommends -y
while [ ! $(command -v dbus-launch) ] && [[ $i -ne 3 ]]
do
echo -e "\e[31m似乎安装出错,重新执行安装\e[0m"
i=$(( $i+1 ))
sleep 2
apt --fix-broken install -y && apt install ${DEPENDS}  --no-install-recommends -y
done
if [ ! $(command -v dbus-launch) ]; then
echo -e "\e[31m似乎安装出错,退出安装\e[0m"
return 0
fi


if [ ! -f /var/lib/dbus/machine-id ]; then
dbus-uuidgen > /var/lib/dbus/machine-id
fi

if [ $(command -v busybox) ]; then
for i in ps uptime killall egrep top; do if [ $(command -v $i) ]; then ln -svf $(command -v busybox) $(command -v $i); else ln -svf $(command -v busybox) /usr/bin/$i; fi done
fi
chown root:root /usr/bin/sudo
chmod 4755 /usr/bin/sudo /usr/bin/sudo
chmod 4711 /usr/bin/su

cd && rm -rf temp

export LANG=zh_CN.UTF-8

cat >/usr/local/bin/boxwine<<-'BOXWINE'
#!/usr/bin/env bash
cd
if [[ $(id -u) = 0 ]];then
if [ ! -d /tmp/runtime-$(id -u) ]; then
#mkdir -pv "/var/run/user/$(id -u)"
mkdir -pv /tmp/runtime-$(id -u)
fi
chmod -R 1777 "/tmp/runtime-$(id -u)"
service dbus start
else
if [ ! -d /tmp/runtime-$(id -u) ]; then
sudo mkdir -pv "/tmp/runtime-$(id -u)"
fi
sudo chmod -R 1777 "/tmp/runtime-$(id -u)"
sudo service dbus start
fi
export XDG_RUNTIME_DIR="/tmp/runtime-$(id -u)"
if ! grep '"SimSun"="wqy-microhei.ttc"' ${HOME}/.wine/system.reg; then
sed -i '/\[Environment/i \[Software\\\\Wine\\\\Explorer\]\n"Desktop"="Default"\n\n\[Software\\\\Wine\\\\Explorer\\\\Desktops\]\n"Default"="800x600"\n\n\[Software\\\\Wine\\\\X11 Driver\]\n"Decorated"="N"\n"Managed"="Y"\n\n\[Software\\\\Wine\\\\Drivers\]\n"Audio"="alsa"\n\n' ${HOME}/.wine/user.reg
#"GrabFullscreen"="y"
sed -i '/FontMapper/i \[Software\\\\Microsoft\\\\Windows NT\\\\CurrentVersion\\\\FontLink\\\\SystemLink\]\n"Arial"="wqy-microhei.ttc"\n"Arial Black"="wqy-microhei.ttc"\n"Lucida Sans Unicode"="wqy-microhei.ttc"\n"MS Sans Serif"="wqy-microhei.ttc"\n"SimSun"="wqy-microhei.ttc"\n"Tahoma"="wqy-microhei.ttc"\n"Tahoma Bold"="wqy-microhei.ttc"\n\n' ${HOME}/.wine/system.reg
sed -i '/X11/i [Software\\Wine\\WineDbg]\n"ShowCrashDialog"=dword:00000000\n' ${HOME}/.wine/user.reg
fi
trap "killall Xvnc 2>/dev/null; killall x11vnc 2>/dev/null; killall Xvfb 2>/dev/null; exit" SIGINT EXIT
wine64 taskmgr
exit 0
BOXWINE

echo '#!/usr/bin/env bash
vncserver -kill $DISPLAY 2>/dev/null
for i in Xtightvnc Xtigertvnc Xvnc vncsession; do pkill -9 $i 2>/dev/null; done
case $1 in
	*nodxvk*) export GALLIUM_DRIVER=llvmpipe
		;;
	*) export MESA_VK_WSI_DEBUG=sw LD_LIBRARY_PATH=/opt/turnip-zink/arm64/lib/aarch64-linux-gnu/:/opt/turnip-zink/armhf/lib/arm-linux-gnueabihf/:/opt/turnip-zink/arm64/lib/aarch64-linux-gnu:/opt/turnip-zink/armhf/lib/arm-linux-gnueabihf VK_ICD_FILENAMES=/opt/turnip-zink/arm64/share/vulkan/icd.d/freedreno_icd.aarch64.json:/opt/turnip-zink/armhf/share/vulkan/icd.d/freedreno_icd.armv8.2l.json MESA_LOADER_DRIVER_OVERRIDE=zink LIBGL_DRIVERS_PATH=/opt/turnip-zink/arm64/lib/aarch64-linux-gnu/dri:/opt/turnip-zink/armhf/lib/arm-linux-gnueabihf/dri/
esac
DEPTH=24
Xvnc -ZlibLevel=1 -quiet -ImprovedHextile -CompareFB 1 -br -retro -a 5 -alwaysshared -geometry 800x600 -depth $DEPTH -once -localhost -securitytypes None :0 &
export DISPLAY=:0
echo -e "\n\e[33m请打开vncviewer输127.0.0.1:0\e[0m\n"
#dbus-launch xfwm4 &
#onboard &
boxwine >/dev/null 2>wine_log &
am start -n com.realvnc.viewer.android/com.realvnc.viewer.android.app.ConnectionChooserActivity 2>/dev/null
exit 0' >/usr/local/bin/startwine
rm /usr/local/bin/startxsdl 2>/dev/null
#vnc转xsdl对于用物理鼠标玩即时战略游戏较vnc更为贴近电脑，但运行效率也会相对降低。
cp /usr/local/bin/startwine /usr/local/bin/startxsdl
sed -i 's/:0/:1/g;/^echo.*vncviewer/d;/am/d;/exit 0/d' /usr/local/bin/startxsdl
sed -i '/Zlib/i echo -e "\\e[33m请先打开xsdl\\e[0m\n"\nread -p "已打开请回车"' /usr/local/bin/startxsdl
echo 'am start -n x.org.server/x.org.server.MainActivity 2>/dev/null
DISPLAY=127.0.0.1:0 xvncviewer -fullscreen 127.0.0.1:1 &
exit 0' >>/usr/local/bin/startxsdl

#修改分辨率
cat >/usr/local/bin/fbl<<-'FBL'
#!/usr/bin/env bash
echo -e "\n\n\e[33m修改分辨率\e[0m\n为适配游戏应用显示，可修改分辨率达最佳显示效果"
echo -e "你现在的分辨率是\e[33m$(sed -n 's/^Xvnc.*geometry \(.*.\) -depth.*/\1/'p /usr/local/bin/startwine)\e[0m\n"
read -r -p "请输长度，例如1080 : " LENGTH
read -r -p "请输宽度，例如768 : " WIDTH
#if [ -z "$LENGTH" ] && [ -z "$WIDTH" ]; then
if [[ "$LENGTH" -gt 0 ]] && [[ "$WIDTH" -gt 0 ]] 2>/dev/null ;then
echo -e "你输入的分辨率为 \e[33m$LENGTH\e[0m x \e[33m$WIDTH\e[0m"
sed -i "s/^\"Default.*$/\"Default\"=\"${LENGTH}x${WIDTH}\"/" ${HOME}/.wine/user.reg
sed -i "s/-geometry .* -depth/-geometry ${LENGTH}x${WIDTH} -depth/" /usr/local/bin/startwine /usr/local/bin/startxsdl
else
echo -e "输入无效，请重新输用本命令"
fi
sleep 1
FBL
#关虚拟桌面
cat >/usr/local/bin/gg<<-'DEG'
#!/usr/bin/env bash
sed -i '/Desktop/s/^/#/' /usr/local/bin/boxwine
i=0
pstree | grep -q "services"
while [ $? == 0 ] && [[ $i -ne 6 ]]
do
echo -ne ".\r"
i=$(( $i+1 ))
sleep 1
pstree | grep -q "services"
done
if [[ $i -eq 6 ]]; then
echo -e "操作失败，请确认已关闭wine"
sleep 1
fi
if grep '^"Desktop"="Default' ${HOME}/.wine/user.reg; then
sed -E -i 's/("Desktop"="Default")/#\1/' ${HOME}/.wine/user.reg
sed -E -i 's/(\[Software\\\\Wine\\\\Explorer\])/#\1/' ${HOME}/.wine/user.reg
fi
echo -e "\e[33m已关闭虚拟桌面\e[0m"
DEG

#开虚拟桌面
cat >/usr/local/bin/kk<<'DEK'
#!/usr/bin/env bash
sed -i '/Desktop/s/#//g' /usr/local/bin/boxwine
pstree | grep -q "services"
while [ $? == 0 ] && [[ $i -ne 6 ]]
do
echo -ne ".\r"
i=$(( $i+1 ))
sleep 1
pstree | grep -q "services"
done
if [[ $i -eq 6 ]]; then
echo -e "操作失败，请确认已关闭wine"
sleep 1
fi
if grep '#"Desktop"="Default' ${HOME}/.wine/user.reg; then
sed -E -i 's/^.*("Desktop"="Default")/\1/' ${HOME}/.wine/user.reg
sed -E -i 's/^.*(\[Software\\\\Wine\\\\Explorer\])/\1/' ${HOME}/.wine/user.reg
elif
grep '^"Desktop"="Default' ${HOME}/.wine/user.reg; then
echo ""
else
if ! grep Desktops .wine/user.reg; then
sed -i '/\[Environment/i \[Software\\\\Wine\\\\Explorer\\\\Desktops\]\n"Default"="800x600"' ${HOME}/.wine/user.reg
fi
if ! grep '^"Desktop"="Default' ${HOME}/.wine/user.reg; then
sed -i '/\[Software\\\\Wine\\\\Explorer\\\\Desktops\]/i \[Software\\\\Wine\\\\Explorer\]\n"Desktop"="Default"\n\n' ${HOME}/.wine/user.reg
fi
fi
echo -e "\e[33m已开启虚拟桌面\e[0m"
DEK

#创建winetricks启动脚本
cat >/usr/local/bin/startricks<<-'tricks'
#!/usr/bin/env bash
vncserver -kill $DISPLAY 2>/dev/null
for i in Xtightvnc Xtigertvnc Xvnc vncsession; do pkill -9 $i 2>/dev/null; done
gg
Xvnc -ZlibLevel=1 -quiet -ImprovedHextile -CompareFB 1 -background -retro -a 5 -alwaysshared -geometry 1024x768 -depth 16 -once -localhost -securitytypes None :0 &
export DISPLAY=:0
echo -e "\n\e[33m请打开vncviewer输127.0.0.1:0\e[0m\n"
dbus-launch xfwm4 &
winetricks -q --gui &
wine64 explorer &
am start -n com.realvnc.viewer.android/com.realvnc.viewer.android.app.ConnectionChooserActivity 2>/dev/null
exit 0
tricks
chmod a+x /usr/local/bin/startricks

cat >/usr/local/bin/kb<<-'jp_'
#!/usr/bin/env bash
if grep ^onboard /usr/local/bin/startwine; then
echo -e "\e[33m关闭桌面键盘\e[0m"
sleep 1
sed -i '/onboard/s/^/#/;/xfwm4/s/^/#/' /usr/local/bin/startwine /usr/local/bin/startxwine /usr/local/bin/startxsdl /usr/local/bin/startvsdl
else
echo -e "\e[33m启动桌面键盘\e[0m"
sleep 1
sed -i '/onboard/s/#//g;/xfwm4/s/#//g' /usr/local/bin/startwine /usr/local/bin/startxsdl
fi
jp_


chmod +x /usr/local/bin/ -R
apt purge --allow-change-held-packages gvfs udisks2 -y 2>/dev/null
ln -sf /usr/share/zoneinfo/Etc/GMT-8 /etc/localtime
sed -i "/zh_CN.UTF/s/#//" /etc/locale.gen
sed -i '/^SUPPORTED/s/^/#/;/^ALIASES/s/^/#/' /usr/sbin/locale-gen
locale-gen
#sed -i -e '/GBK/,/^}/s/^/#/' /usr/share/X11/locale/zh_CN.UTF-8/XLC_LOCALE
sed -i '/LANG=zh_CN.UTF-8/d' /etc/profile && sed -i '2i export LANG=zh_CN.UTF-8' /etc/profile
sed -i "/firstrun/d" /etc/profile
sed -i "/return/d" ${HOME}/firstrun
if ! grep -q termux /etc/bash.bashrc; then
if [ -f /data/data/com.termux/files/usr/etc/motd.sh ]; then
cat /data/data/com.termux/files/usr/etc/motd.sh|sed '/com.termux/d;s/pkg/apt/' >>/etc/bash.bashrc
sed -i '/TERMUX_APP_PACKAGE_MANAGER/i clear\nTERMUX_VERSION=$(cat /etc/os-release|grep PRETTY | cut -d "=" -f 2)\nclear' /etc/bash.bashrc
echo -e 'if [ -d ${HOME}/.wine/drive_c/users/*/Temp/*490 ]; then echo -e "\e[31m注意，有不明文件夹！谨慎中电脑病毒。\e[0m"; fi' >>/etc/bash.bashrc
fi
fi
case $NOR_USER in
TRUE)
chown root:root /usr/bin/sudo
chmod 4755 /usr/bin/sudo
chmod 4711 /usr/bin/su
echo -e "\e[33m请设置root用户密码(输入内容不会反显)\e[0m\n"
passwd
echo -e "\e[33m请设置普通用户密码(输入内容不会反显)，用户名为 $(grep 'Android user' /etc/passwd|cut -d ':' -f 1)\e[0m\n"
passwd $(grep 'Android user' /etc/passwd|cut -d ':' -f 1)
sed -i "/\%sudo/a $(grep 'Android user' /etc/passwd|cut -d ':' -f 1) ALL=(ALL:ALL) NOPASSWD:ALL" /etc/sudoers
cp /etc/skel/.profile /home/*/
cp /etc/skel/.bashrc /home/*/
esac

cd
#安装wine
rm -rf /root/.wine 2>/dev/null
mkdir -v /root/wine 2>/dev/null
if [ ! -f /opt/wine-devel/bin/wine ] && [ ! -f /root/wine/opt/wine-devel/bin/wine ] && [ ! -f /root/wine/bin/wine ]; then
rm wine-8.13-amd64.tar.xz box86*.tar.gz box64*.tar.gz turnip-zink-stable.deb 2>/dev/null
rm -rf wine
wget https://shell.xb6868.com/wine/wine-8.13-amd64.tar.xz
tar Jxvf wine-8.13-amd64.tar.xz
mv wine-8.13-amd64 wine
wget https://shell.xb6868.com/wine/box86_20230802.tar.gz
wget https://shell.xb6868.com/wine/box64_20230802.tar.gz
tar zxvf box86_20230802.tar.gz -C /
tar zxvf box64_20230802.tar.gz -C /
wget https://shell.xb6868.com/wine/turnip-zink-stable.deb
apt install ./turnip-zink-stable.deb -y
fi
if [ -f /opt/wine-devel/bin/wine ] || [ -f /root/wine/opt/wine-devel/bin/wine ] || [ -f /root/wine/bin/wine ] && [ $(command -v box64) ] && [ $(command -v box86) ]; then
if [ ! -f /usr/local/bin/winetricks ]; then
echo -e "\n\e[33m下载 winetricks\e[0m\n"
sleep 1
curl https://ghproxy.com/https://raw.githubusercontent.com/Winetricks/winetricks/master/src/winetricks -o /usr/local/bin/winetricks
if [ -f /usr/local/bin/winetricks ]; then
#sed -i '2a export WINEARCH=win32 BOX64_NOBANNER=1 BOX86_NOBANNER=1 WINEDEBUG=fixme-all' /usr/local/bin/winetricks
sed -E -i 's/GitHub.*似乎不是个有效的版本号(.*)/执行文件检测试中\1/' /usr/local/bin/winetricks
sed -E -i "s/(latest_version=).*winetricks.*/\1$(grep 'WINETRICKS_VERSION=' /usr/local/bin/winetricks|cut -d = -f2)/;/I_run_Wine_as_root.3F/s/w_warn/echo/;/supported upstream/s/w_warn/echo/" /usr/local/bin/winetricks
curl --connect-timeout 5 -m 8 -s https://github.com/Winetricks/ >/dev/null
if [ $? == 1 ]; then
echo -e "\e[33m你的网络无法访问github，将为你添加代理\e[0m"
sleep 2
sed -i 's/github/kgithub/g;s@https.*raw.kgithub@https://ghproxy.com/https://raw.github@' /usr/local/bin/winetricks
fi
#sed -i "/latest_version=.*winetricks.*/s/^/#/" /usr/local/bin/winetricks
#sed -E -i "s/(latest_version=.*winetricks.*)/^/#/" /usr/local/bin/winetricks
chmod a+x /usr/local/bin/winetricks
mkdir -p ${HOME}/.cache/winetricks/ 2>/dev/null
echo 0 >${HOME}/.cache/winetricks/track_usage
fi
fi
fi
BIN_PATH=$(find /root/wine/ -name wineserver -type f)
if [ -n $BIN_PATH ]; then
rm /usr/local/bin/wine /usr/local/bin/wine64 /usr/local/bin/wineserver /usr/local/bin/winecfg /usr/local/bin/wineboot 2>/dev/null
if [ -f "${BIN_PATH%/*}"/wine64 ]; then
echo -e "#!/bin/sh
box64 "${BIN_PATH%/*}"/wineserver \"\$@\"" >/usr/local/bin/wineserver
else
echo -e "#!/bin/sh
box86 "${BIN_PATH%/*}"/wineserver \"\$@\"" >/usr/local/bin/wineserver
sed -i 's/wine64/wine/' /usr/local/bin/boxwine* 2>/dev/null
fi
echo -e "#!/bin/sh
box86 "${BIN_PATH%/*}"/wine \"\$@\"" >/usr/local/bin/wine
echo -e "#!/bin/sh
box64 "${BIN_PATH%/*}"/wine64 \"\$@\"" >/usr/local/bin/wine64
ln -sf "${BIN_PATH%/*}"/winecfg /usr/local/bin
ln -sf "${BIN_PATH%/*}"/wineboot /usr/local/bin
chmod a+x /usr/local/bin/wine /usr/local/bin/wine64 /usr/local/bin/wineserver /usr/local/bin/winecfg /usr/local/bin/wineboot
fi

echo -e "\e[33m进行wine初始化配置，等待时间比较长…\e[0m"
sleep 1
rm -rf .wine 2>/dev/null
sleep 5
if [ -f "${BIN_PATH%/*}"/wine64 ]; then
wineboot 2>/dev/null
fi
pstree | grep -q "services"
while [ $? == 0 ]
do
sleep 1
pstree | grep -q "services"
done

while [ ! -f .wine/user.reg ]
do
sleep 1
done
while ! grep Desktops .wine/user.reg
do
sleep 1

sed -i '/\[Environment/i \[Software\\\\Wine\\\\Explorer\]\n"Desktop"="Default"\n\n\[Software\\\\Wine\\\\Explorer\\\\Desktops\]\n"Default"="800x600"\n\n\[Software\\\\Wine\\\\X11 Driver\]\n"Decorated"="N"\n"Managed"="Y"\n\n\[Software\\\\Wine\\\\Drivers\]\n"Audio"="alsa"\n\n' ${HOME}/.wine/user.reg
#"GrabFullscreen"="y"

#[Software\\Wine\\X11 Driver]
#"ScreenDepth"="16"

done

#Disable the GUI crash dialog
sed -i '/X11/i [Software\\Wine\\WineDbg]\n"ShowCrashDialog"=dword:00000000\n' ${HOME}/.wine/user.reg

sed -i '/FontMapper/i \[Software\\\\Microsoft\\\\Windows NT\\\\CurrentVersion\\\\FontLink\\\\SystemLink\]\n"Arial"="wqy-microhei.ttc"\n"Arial Black"="wqy-microhei.ttc"\n"Lucida Sans Unicode"="wqy-microhei.ttc"\n"MS Sans Serif"="wqy-microhei.ttc"\n"SimSun"="wqy-microhei.ttc"\n"Tahoma"="wqy-microhei.ttc"\n"Tahoma Bold"="wqy-microhei.ttc"\n\n' ${HOME}/.wine/system.reg

rm -rf box86* box64* wqy.reg wine.tar.gz ca.deb openssl.deb libssl.deb wine_tmp 2>/dev/null

#解决错误提示:preloader: Warning: failed to reserve range 00000000-60000000 or winevdm: unable to exec 'application name': DOS memory range unavailable
if [ ! -f /etc/sysctl.conf ] || ! grep -q vm /etc/sysctl.conf; then
echo 'vm.mmap_min_addr=0' >>/etc/sysctl.conf
fi
echo -e "\e[33m配置中，请稍候..\e[0m"
pstree | grep -q "wineboot"
while [ $? == 0 ]
do
sleep 3
du -sh ${HOME}/.wine
pstree | grep -q "wineboot"
done
export BOX86_LOG=0 BOX86_NOBANNER=1 BOX64_LOG=0 BOX64_NOBANNER=1 WINEDEBUG=fixme-all
#WINEDEBUG=fps,fixme-all

#VERSION=`curl https://kgithub.com/doitsujin/dxvk/releases|grep tag.*Version|sed -n 1p|awk -F 'Version ' '{print $2}'|cut -d '<' -f 1`; wget https://kgithub.com/doitsujin/dxvk/releases/download/v$VERSION/dxvk-$VERSION.tar.gz
if [ ! -f ${HOME}/dxvk-2.2/x32/dxgi.dll ]; then
curl -O https://shell.xb6868.com/wine/dxvk-2.2.tar.gz
tar zxvf dxvk-2.2.tar.gz
fi
cp dxvk-*/x64/* ${HOME}/.wine/drive_c/windows/system32/ -v
cp dxvk-*/x32/* ${HOME}/.wine/drive_c/windows/syswow64/ -v
if grep DllOverrides ${HOME}/.wine/user.reg; then
sed -i '/DllOverrides/a"d3d10core"="native"\n"d3d11"="native"\n"d3d9"="native"\n"dxgi"="native"' ${HOME}/.wine/user.reg
else
sed -i '/Debug/i \[Software\\\\Wine\\\\DllOverrides\]\n"d3d10core"="native"\n"d3d11"="native"\n"d3d9"="native"\n"dxgi"="native"' ${HOME}/.wine/user.reg
fi

echo -e "\n\e[33m配置完毕\e[0m\n"
echo -e "\n如果上面的安装失败,请输\e[33mbash firstrun\e[0m重新安装"
case $NOR_USER in
TRUE)
echo -e "普通用户登录容器\e[33mstart-$(grep 'Android user' /etc/passwd|cut -d ':' -f 1)\e[0m"
esac
echo -e "root用户登录容器\e[33m${START}\e[0m\n退出容器,在termux输\e[33mexit\e[0m即可\e[0m\n安装仿windows界面，输\e[33mbash undercover\e[0m"

if [ -f /opt/wine-devel/bin/wine ] || [ -f /root/wine/opt/wine-devel/bin/wine ] || [ -f /root/wine/bin/wine ] && [ $(command -v box64) ] && [ $(command -v box86) ]; then

echo -e "vnc打开wine请输\e[33mstartwine\e[0m，vnc viewer地址输127.0.0.1:0\nxsdl打开wine\e[33m请先打开xsdl\e[0m，再输\e[33mstartxsdl\e[0m\n修改分辨率适配游戏，请输\e[33mfbl\e[0m\n使用图形winetricks输\e[33mstartricks\e[0m\n禁用dxvk，请输\e[33mstartwine nodxvk\e[0m 或 \e[33mstartxsdl nodxvk\e[0m\n"
fi
read -p "确认请回车"
exit 0
eof

#If no 'isolated_environment', the following host directories will be available:


cat >${CONTAINER}/root/undercover<<-'eof'
#!/usr/bin/env bash
cd
#精简安装
#:<<\eof
if [ ! $(command -v xfce4-session) ]; then
apt update && apt install xfce4 xfce4-terminal ristretto lxtask dbus-x11 tigervnc-standalone-server tigervnc-viewer pulseaudio xserver-xorg x11-utils python3 tumbler python-gi-dev --no-install-recommends -y
fi
i=0
while [ ! $(command -v xfce4-session) ] && [[ $i -ne 3 ]]
do
apt --fix-broken install -y
i=$(( $i+1 ))
done

if [ ! $(command -v xfce4-session) ]; then
echo -e "\e[33m安装出错，退出安装\e[0m"
exit 0
fi
#eof

if [ ! $(command -v kali-undercover) ]; then
rm undercover.deb 2>/dev/null
wget -O undercover.deb https://mirrors.tuna.tsinghua.edu.cn/kali/pool/main/k/kali-undercover/$(curl https://mirrors.tuna.tsinghua.edu.cn/kali/pool/main/k/kali-undercover/ | grep all.deb | cut -d '"' -f 4)
apt update
apt install ./undercover.deb xfce4-terminal -y
if [ ! $(command -v kali-undercover) ]; then
apt --fix-broken install -y
fi
sed -i '/Depends/s/xfce4-power-manager-plugins, //' /var/lib/dpkg/status
apt purge xfce4-power* -y
fi

if [ ! -e /etc/X11/xinit/Xsession ]; then
mkdir -p /etc/X11/xinit 2>/dev/null
fi
echo '#!/usr/bin/env bash
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
if [[ $(id -u) = 0 ]];then
if [ ! -d /tmp/runtime-$(id -u) ]; then
#mkdir -pv "/var/run/user/$(id -u)"
mkdir -pv /tmp/runtime-$(id -u)
fi
chmod -R 1777 "/tmp/runtime-$(id -u)"
service dbus start
else
if [ ! -d /tmp/runtime-$(id -u) ]; then
sudo mkdir -pv "/tmp/runtime-$(id -u)"
fi
sudo chmod -R 1777 "/tmp/runtime-$(id -u)"
sudo service dbus start
fi
export XDG_RUNTIME_DIR="/tmp/runtime-$(id -u)"
if [ $(command -v xfce4-session) ]; then
dbus-launch xfce4-session
else
dbus-launch startxfce4
fi' >/etc/X11/xinit/Xsession && chmod +x /etc/X11/xinit/Xsession

cat >/usr/local/bin/startvnc<<-'eom'
#!/usr/bin/env bash

export RUNLEVEL=5
LENGTH=1440
WIDTH=900
DEPTH=24
vncserver -kill $DISPLAY 2>/dev/null
for i in Xtightvnc Xtigertvnc Xvnc vncsession; do pkill -9 $i 2>/dev/null; done
Xvnc -ZlibLevel=1 -quiet -ImprovedHextile -CompareFB 1 -br -retro -a 5 -alwaysshared -geometry ${LENGTH}x${WIDTH} -depth ${DEPTH} -once -localhost -securitytypes None :0 &
export DISPLAY=:0
. /etc/X11/xinit/Xsession >/dev/null 2>&1 &
echo -e "\n\e[33m请打开vncviewer输127.0.0.1:0\e[0m\n"
sleep 1
am start -n com.realvnc.viewer.android/com.realvnc.viewer.android.app.ConnectionChooserActivity 2>/dev/null
eom

rm /usr/share/xfce4/panel/plugins/power-manager-plugin.desktop undercover.deb 2>/dev/null

chmod +x /usr/local/bin/startvnc

for i in 16x16 22x22 24x24 32x32 48x48 256x256; do cp -v /usr/share/icons/Windows-10-Icons/$i/apps/utilities-system-monitor.png /usr/share/icons/hicolor/$i/apps ; done
gtk-update-icon-cache /usr/share/icons/hicolor
curl https://shell.xb6868.com/wine/Windows10.jpg -o /usr/share/kali-undercover/backgrounds/Windows-10.jpg

sed -i '/exit/s/^/#/' $(command -v kali-undercover)
#sed -E -i 's/(\$USER_PROFILE \])/\1 || grep undercover ~\/.bashrc/' $(command -v kali-undercover)
cp $(command -v kali-undercover) /usr/bin/kali-undercover.bak
#sed -i 's/disable_undercover()/disable()/' $(command -v kali-undercover)
#sed -i 's/disable_undercover/enable_undercover/' $(command -v kali-undercover)
mkdir ${HOME}/Desktop 2>/dev/null
mkdir ${HOME}/桌面 2>/dev/null
mkdir ${HOME}/'[Invalid UTF-8]' 2>/dev/null
sed -E -i '/root.*0:0/s/^.*$/root:x:0:0:Administrator:\/root:\/bin\/bash/' /etc/passwd
cp /usr/share/applications/kali-undercover.desktop /etc/xdg/autostart/
if ! grep -q autostart $(command -v kali-undercover) ; then
sed -i '2i if [ -f /etc/xdg/autostart/kali-undercover.desktop ]; then rm /etc/xdg/autostart/kali-undercover.desktop; fi' $(command -v kali-undercover) $(command -v kali-undercover.bak)
fi
bash -c "$(sed '/am start/d' /usr/local/bin/startvnc)" >/dev/null 2>&1 &
sleep 5
vncserver -kill $DISPLAY 2>/dev/null
for i in Xtightvnc Xtigertvnc Xvnc vncsession; do pkill -9 $i 2>/dev/null; done
cp /usr/bin/kali-undercover.bak $(command -v kali-undercover) 

if [ -f /opt/wine-devel/bin/wine ] || [ -f /root/wine/opt/wine-devel/bin/wine ] || [ -f /root/wine/bin/wine ]; then
cp /usr/share/applications/xfce4-terminal.desktop ${HOME}/Desktop/rscreen.desktop
#sed -i 's/xfce4-terminal/xrandr \-s 0/;s/Xfce\ Terminal/恢复屏幕/;/\]=/d;/=T/d;/preferences\]/,$d' ${HOME}/Desktop/rscreen.desktop
sed -i 's/xfce4-terminal/xrandr \-s 0/;s/Xfce\ Terminal/rscreen/;/\]=/d;/=T/d;/preferences\]/,$d;/rscreen/a Name[zh_CN]=恢复屏幕' ${HOME}/Desktop/rscreen.desktop
cp /usr/share/applications/xfce4-file-manager.desktop Desktop/explorer.desktop
#sed -E -i 's/Name=File\ Manager/Name=wine资源管理器/;/\]=/d' ${HOME}/Desktop/explorer.desktop

sed -E -i 's/Name=File\ Manager/Name=wine explorer/;s/(^Exec=).*$/\1wine64 explorer %U/;/\]=/d;/wine explorer/a Name[zh_CN]=wine资源管理器' ${HOME}/Desktop/explorer.desktop
cp /opt/wine-devel/share/applications/wine.desktop /usr/share/applications 2>/dev/null
cp /root/wine/opt/wine-devel/share/applications/wine.desktop /usr/share/applications 2>/dev/null
cp /root/wine/share/applications/wine.desktop /usr/share/applications 2>/dev/null

cat >/usr/share/applications/wine.desktop<<-de
[Desktop Entry]
Type=Application
Name=任务管理器
Exec=wine start /unix %f
MimeType=application/x-ms-dos-executable;application/x-msi;application/x-ms-shortcut;
Icon=utilities-system-monitor
NoDisplay=true
StartupNotify=true
de

chmod a+x /usr/share/applications/wine.desktop

sed -i 's/^Exec=wine/Exec=wine64/' /usr/share/applications/wine.desktop 2>/dev/null
sed -i 's/^Icon.*$/Icon=utilities-system-monitor/' /usr/share/applications/wine.desktop
#sed -i 's/^Name=Wine Windows Program Loader/Name=wine任务管理器/' /usr/share/applications/wine.desktop
sed -i '/^Name=Wine Windows Program Loader/a Name[zh_CN]=wine任务管理器' /usr/share/applications/wine.desktop
#sed -i 's/^MimeType.*$/Categories=GTK;System;Monitor;/' /usr/share/applications/wine.desktop

cp /usr/share/applications/wine.desktop ${HOME}/Desktop/
sed -i 's/^Exec.*$/Exec=wine64 taskmgr %f/' ${HOME}/Desktop/wine.desktop

cat >${HOME}/Desktop/windows.desktop<<-'win'
[Desktop Entry]
Type=Application
Name=此电脑
Exec=thunar /sdcard
Icon=computer
NoDisplay=true
StartupNotify=true
win

cp ${HOME}/Desktop/* ${HOME}/桌面/
chmod a+x ${HOME}/Desktop/ -R
chmod a+x ${HOME}/桌面/ -R
fi
echo -e "\n已安装，启动命令\e[33mstartvnc\e[0m\n\n如果kali-undercover安装崩溃，请重新执行本脚本\e[33mbash undercover\e[0m\n如果需要用回xfce4桌面，请点击：开始--其他--kali-undercover进行切换\e[0m\n"
read -r -p "确定请回车 " input
unset input
echo -e "\n\e[33m正在进行自动首次登录...\e[0m"
xvfb-run /etc/X11/xinit/Xsession >/dev/null 2>&1 &
sleep 3
startvnc >/dev/null 2>&1
echo -e "\n\e[33m请打开vncviewer输127.0.0.1:0\e[0m\n"
eof
sed -i "2i WINE_VERSION=$WINE_VERSION" ${CONTAINER}/root/firstrun
sed -i "2i ROOTFS=$ROOTFS" ${CONTAINER}/root/firstrun
sed -i "2i BOX_INSTALL=$BOX_INSTALL" ${CONTAINER}/root/firstrun
sed -i "2i LXC=$LXC" ${CONTAINER}/root/firstrun
sed -i "2i WINE_INSTALL=$WINE_INSTALL" ${CONTAINER}/root/firstrun
sed -i "2i START=$START" ${CONTAINER}/root/firstrun
sed -i "2i NOR_USER=$NOR_USER" ${CONTAINER}/root/firstrun
if [ -z $ANDROID_RUNTIME_ROOT ]; then
export ANDROID_RUNTIME_ROOT=/apex/com.android.runtime
fi

cat >>${HOME}/${CONTAINER}/etc/profile<<-EOF
export ANDROID_ART_ROOT=${ANDROID_ART_ROOT-}
export ANDROID_DATA=${ANDROID_DATA-}
export ANDROID_I18N_ROOT=${ANDROID_I18N_ROOT-}
export ANDROID_ROOT=${ANDROID_ROOT-}
export ANDROID_RUNTIME_ROOT=${ANDROID_RUNTIME_ROOT-}
export ANDROID_TZDATA_ROOT=${ANDROID_TZDATA_ROOT-}
export BOOTCLASSPATH=${BOOTCLASSPATH-}
export COLORTERM=${COLORTERM-}
export DEX2OATBOOTCLASSPATH=${DEX2OATBOOTCLASSPATH-}
export EXTERNAL_STORAGE=${EXTERNAL_STORAGE-}
export PATH=\${PATH}:/data/data/com.termux/files/usr/bin:/system/bin:/system/xbin
export PREFIX=${PREFIX-/data/data/com.termux/files/usr}
export TERM=${TERM-xterm-256color}
export TMPDIR=/tmp
export PULSE_SERVER=tcp:127.0.0.1:4713
export BOX86_LOG=0 BOX86_NOBANNER=1 BOX86_NOPULSE=1 BOX64_NOPULSE=1 BOX64_LOG=0 BOX64_NOBANNER=1 WINEDEBUG=fixme-all
EOF
sed -i '/=$/d' ${HOME}/${CONTAINER}/etc/profile
cat >${PREFIX}/bin/start-${NAME}<<eof
#!/usr/bin/env bash
cd
pkill -9 pulseaudio 2>/dev/null
rm -rf ${PREFIX}/tmp/.* ${PREFIX}/tmp/*
pulseaudio --start --load="module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1" --exit-idle-time=-1 &
unset LD_PRELOAD
proot --kill-on-exit -b /vendor -b /system -b /sdcard -b /sdcard:/root/sdcard -b /data/data/com.termux/files -b /data/data/com.termux/cache -b /data/data/com.termux/files/usr/tmp:/tmp -b /dev/null:/proc/sys/kernel/cap_last_cap -b /data/dalvik-cache -b ${CONTAINER}/tmp:/dev/shm -b /proc/self/fd/2:/dev/stderr -b /proc/self/fd/1:/dev/stdout -b /proc/self/fd/0:/dev/stdin -b /proc/self/fd:/dev/fd -b /dev/urandom:/dev/random --sysvipc --link2symlink -S ${CONTAINER} -w /root /usr/bin/env -i HOME=/root PATH=/usr/local/sbin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/games:/usr/local/games LANG=zh_CN.UTF-8 TZ=Asia/Shanghai TERM=xterm-256color USER=root /bin/bash --login
eof
for i in version misc buddyinfo kmsg consoles execdomains stat fb filesystems loadavg key-users uptime devices vmstat; do if [ ! -r /proc/"${i}" ]; then sed -E -i "s@(cap_last_cap)@\1 -b ${CONTAINER}/etc/proc/${i}:/proc/${i}@" ${PREFIX}/bin/start-${NAME}; fi done
for i in /system_ext /linkerconfig/ld.config.txt /plat_property_contexts /property_contexts /apex; do if [ -e $i ]; then sed -i "s@shm@shm \-b ${i}@" ${PREFIX}/bin/start-${NAME}; fi done


case $NOR_USER in
TRUE)
cp ${PREFIX}/bin/start-${NAME} ${PREFIX}/bin/start-$(whoami)
sed -i "s@/bin/bash --login@/bin/su -l $(whoami)@" ${PREFIX}/bin/start-$(whoami)
mkdir -p "${HOME}/${CONTAINER}/home/$(whoami)"
echo "$(id -un):x:$(id -u):$(id -g):Android user:/home/$(whoami):/bin/bash" >> "${HOME}/${CONTAINER}/etc/passwd"
echo "$(id -un):*:18446:0:99999:7:::" >> "${HOME}/${CONTAINER}/etc/shadow"


#local group_name group_id
while read -r group_name group_id; do
echo "${group_name}:x:${group_id}:root,$(id -un)" >> ${HOME}/${CONTAINER}/etc/group
if [ -f "${CONTAINER}/etc/gshadow" ]; then
echo "${group_name}:*::root,$(id -un)" >> ${HOME}/${CONTAINER}/etc/gshadow
fi
done < <(paste <(id -Gn | tr ' ' '\n') <(id -G | tr ' ' '\n'))
esac

echo "bash firstrun" >>${HOME}/${CONTAINER}/etc/profile
proot --help | grep -q sysvipc
if [ $? == 1 ]; then
sed -i 's/--sysvipc//' ${PREFIX}/bin/start-${NAME} ${PREFIX}/bin/start-$(whoami) 2>/dev/null
fi

chmod a+x ${PREFIX}/bin/start-${NAME} ${PREFIX}/bin/start-$(whoami) 2>/dev/null
. ${PREFIX}/bin/start-${NAME}
