#!/usr/bin/env bash
BFSU="https://mirrors.bfsu.edu.cn/"
DEB="focal main restricted universe multiverse"
LXC="lxc-images/images/ubuntu/focal/arm64/default/"
ROOTFS="rootfs.tar.xz"
echo -e "\e[33m即将下载系统,本脚本是进行全新安装,非恢复包\e[0m"
sleep 1
rm ${ROOTFS} 2>/dev/null
VERSION=`curl ${BFSU}${LXC} | grep href | tail -n 2 | cut -d '"' -f 4 | head -n 1`
curl -O ${BFSU}${LXC}${VERSION}${ROOTFS}
mkdir focal
tar xvf ${ROOTFS} -C focal
rm ${ROOTFS}
echo -e "\e[33m系统已下载,文件夹名为focal\e[0m"
sleep 2
sed -i "3i\rm -rf \/tmp\/.X\*" focal/etc/profile
sed -i "/zh_CN.UTF/s/#//" focal/etc/locale.gen
rm focal/etc/resolv.conf 2>/dev/null
echo "nameserver 223.5.5.5
nameserver 223.6.6.6" >focal/etc/resolv.conf
echo "deb ${BFSU}ubuntu-ports/ focal ${DEB}
deb ${BFSU}ubuntu-ports/ focal-updates ${DEB}
deb ${BFSU}ubuntu-ports/ focal-backports ${DEB}
deb ${BFSU}ubuntu-ports/ focal-security ${DEB}" >focal/etc/apt/sources.list
touch "focal/root/.hushlogin"
echo $(uname -a) | sed 's/Android/GNU\/Linux/' >focal/proc/version
if [ ! -f "focal/usr/bin/perl" ]; then

        cp focal/usr/bin/perl* focal/usr/bin/perl
fi
echo ". firstrun" >>focal/etc/profile
cat >focal/root/firstrun<<-'eof'
echo -e "\e[33m正在配置首次运行\n安装常用应用\e[0m"
sleep 1
apt update
apt install -y && apt install --no-install-recommends curl wget vim fonts-wqy-zenhei tar firefox firefox-locale-zh-hans ffmpeg mpv xfce4 xfce4-terminal ristretto dbus-x11 lxtask pavucontrol libxtst6 libnss3 -y
apt install tigervnc-standalone-server tigervnc-viewer -y
if [ ! $(command -v dbus-launch) ] || [ ! $(command -v tigervncserver) ] || [ ! $(command -v xfce4-session) ]; then
echo -e "\e[31m似乎安装出错,重新执行安装\e[0m"
sleep 2
apt --fix-broken install -y && apt install --no-install-recommends curl wget vim fonts-wqy-zenhei tar firefox firefox-locale-zh-hans ffmpeg mpv xfce4 xfce4-terminal ristretto dbus-x11 lxtask tigervnc-standalone-server tigervnc-viewer pavucontrol libxtst6 libnss3 -y
fi
if [ $(command -v firefox) ]; then
if grep -q '^ex.*MOZ_FAKE_NO_SANDBOX=1' /etc/environment; then
printf "%s\n" "MOZ_FAKE_NO_SANDBOX=1" /etc/environment
else
echo 'export MOZ_FAKE_NO_SANDBOX=1' >>/etc/environment
fi
if ! grep -q 'PATH' /etc/environment; then
sed -i '1i\PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"' /etc/environment
fi
if ! grep -q 'environment' /etc/profile; then
echo 'source /etc/environment' >>/etc/profile
fi
fi
ln -s /usr/lib/firefox/libsmime3.so /usr/lib/aarch64-linux-gnu/
ln -s /usr/lib/firefox/libnssutil3.so /usr/lib/aarch64-linux-gnu/
curl -O https://shell.xb6868.com/ut/files/PSTREE.tar.gz
tar zxvf PSTREE.tar.gz && bash bash_me
rm -rf PSTREE.tar.gz bash_me
echo '#!/usr/bin/env bash
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
Xvnc -ZlibLevel=1 -verbose -ImprovedHextile -CompareFB 1 -br -retro -a 5 -wm -alwaysshared -geometry 1089x2320 -once -depth 16 -localhost -securitytypes None &
export DISPLAY=:0
. /etc/X11/xinit/Xsession 2>/dev/null &
exit 0' >/usr/local/bin/easyvnc && chmod +x /usr/local/bin/easyvnc
mkdir -p /etc/X11/xinit 2>/dev/null
echo '#!/usr/bin/env bash
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
if [ $(command -v xfce4-session) ]; then
xfce4-session
else
startxfce4
fi' >/etc/X11/xinit/Xsession && chmod +x /etc/X11/xinit/Xsession
locale-gen
sed -i "/firstrun/d" /etc/profile
echo -e "打开vnc请输\e[33measyvnc\e[0m\nvnc viewer地址输127.0.0.1:0\nvnc的退出,在系统输exit即可
如果启动失败,请输\e[33mbash firstrun\e[0m重新安装"
read -r -p "按回车键继续" input
case $input in
*) ;; esac
eof

echo "pkill -9 pulseaudio 2>/dev/null
pulseaudio --start &
unset LD_PRELOAD
proot --kill-on-exit -S focal --link2symlink -b /sdcard:/root/sdcard -b /sdcard -b focal/proc/version:/proc/version -b focal/root:/dev/shm -w /root /usr/bin/env -i HOME=/root TERM=$TERM USER=root PATH=/usr/local/sbin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/games:/usr/local/games TZ=Asia/Shanghai LANG=zh_CN.UTF-8 /bin/bash --login" >start-focal.sh && chmod +x start-focal.sh
echo -e "已创建root用户系统登录脚本,登录方式为\e[33m./start-focal.sh\e[0m"
