#!/bin/bash -e
# Directory contains the target rootfs

TARGET_ROOTFS_DIR="binary"
ARCH=arm64
echo -e "\033[36m Building for $ARCH \033[0m"

if [ -d "$TARGET_ROOTFS_DIR" ]; then
  # Control will enter here if $DIRECTORY exists.
  rm -rf $TARGET_ROOTFS_DIR
  sleep 5
fi

if [ ! -e ubuntu-22.04.1-arm64.tar.gz ]; then
        echo "\033[please put ubuntu-22.04.1-arm64.tar.gz file in this contents \033[0m"
        exit -1
fi

finish() {
        sudo umount $TARGET_ROOTFS_DIR/dev
        exit -1
}
trap finish ERR

echo -e "\033[36m Extract image \033[0m"
sudo tar -xpf ubuntu-22.04.1-arm64.tar.gz
# packages folder
sudo mkdir -p $TARGET_ROOTFS_DIR/packages
sudo cp -rf packages/$ARCH/* $TARGET_ROOTFS_DIR/packages
# overlay folder
sudo cp -rf overlay/* $TARGET_ROOTFS_DIR/

# overlay-firmware folder
sudo cp -rf overlay-firmware/* $TARGET_ROOTFS_DIR/

# overlay-debug folder
# adb, video, camera  test file
sudo cp -rf overlay-debug/* $TARGET_ROOTFS_DIR/

## hack the serial
sudo cp -f overlay/usr/lib/systemd/system/serial-getty@.service $TARGET_ROOTFS_DIR/lib/systemd/system/serial-getty@.service

# adb
sudo cp -f overlay-debug/usr/local/share/adb/adbd-64 $TARGET_ROOTFS_DIR/usr/bin/adbd

# bt/wifi firmware
sudo mkdir -p $TARGET_ROOTFS_DIR/system/lib/modules/
sudo mkdir -p $TARGET_ROOTFS_DIR/vendor/etc
sudo find ../kernel/drivers/net/wireless/rockchip_wlan/*  -name "*.ko" | \
    xargs -n1 -i sudo cp {} $TARGET_ROOTFS_DIR/system/lib/modules/
sudo find ../kernel/drivers/bluetooth/*  -name "*.ko" | \
    xargs -n1 -i sudo cp {} $TARGET_ROOTFS_DIR/system/lib/modules/

echo -e "\033[36m Change root.....................\033[0m"
sudo cp -f /etc/resolv.conf $TARGET_ROOTFS_DIR/etc/
sudo cp /usr/bin/qemu-aarch64-static $TARGET_ROOTFS_DIR/usr/bin/

sudo mount -o bind /dev $TARGET_ROOTFS_DIR/dev
sudo mount -o bind /dev/pts $TARGET_ROOTFS_DIR/dev/pts
cat << EOF | sudo chroot $TARGET_ROOTFS_DIR/
apt-get update
apt-get upgrade -y
chmod o+x /usr/lib/dbus-1.0/dbus-daemon-launch-helper
export APT_INSTALL="apt-get install -fy --allow-downgrades"

apt remove -fy firefox-esr chromium* firefox

#---------------power management --------------
\${APT_INSTALL} pm-utils triggerhappy bsdmainutils
cp /etc/Powermanager/triggerhappy.service  /lib/systemd/system/triggerhappy.service

#rm /etc/Powermanager -rf


#---------------Rga--------------
\${APT_INSTALL} /packages/rga/*.deb
\${APT_INSTALL} /packages/rga2/*.deb

echo -e "\033[36m Setup Video.................... \033[0m"
\${APT_INSTALL} gstreamer1.0-plugins-bad gstreamer1.0-plugins-base gstreamer1.0-plugins-ugly gstreamer1.0-tools gstreamer1.0-alsa \
gstreamer1.0-plugins-base-apps qtmultimedia5-examples gstreamer1.0-opencv 
for package in $(sudo apt-get upgrade 2>&1 | sudo grep "warning: files list file for package '" | sudo grep -Po "[^'\n ]+'" | sudo grep -Po "[^']+"); do sudo apt-get install --reinstall "$package"; done
\${APT_INSTALL} /packages/mpp/*
\${APT_INSTALL} /packages/gst-rkmpp/*.deb
#\${APT_INSTALL} /packages/gstreamer/*.deb
#\${APT_INSTALL} /packages/gst-plugins-base1.0/*.deb
#\${APT_INSTALL} /packages/gst-plugins-bad1.0/*.deb
#\${APT_INSTALL} /packages/gst-plugins-good1.0/*.deb
#\${APT_INSTALL} /packages/gst-plugins-ugly1.0/*.deb
#\${APT_INSTALL} /packages/gst-libav1.0/*.deb

#---------Camera---------
echo -e "\033[36m Install camera.................... \033[0m"
\${APT_INSTALL} cheese 
\${APT_INSTALL} /packages/rkisp/*.deb
\${APT_INSTALL} /packages/rkaiq/*.deb
\${APT_INSTALL} /packages/libv4l/*.deb


#--------------before deb--------------
#\${APT_INSTALL} /packages/topeet/*.deb


#---------------Openbox--------------
#echo -e "\033[36m Install openbox.................... \033[0m"
\${APT_INSTALL} /packages/openbox/*.deb

#---------update chromium-----
\${APT_INSTALL} /packages/chromium/*.deb
#\${APT_INSTALL} firefox 

#------------------libdrm------------
echo -e "\033[36m Install libdrm.................... \033[0m"
\${APT_INSTALL} /packages/libdrm/*.deb

#------------------libdrm-cursor------------
echo -e "\033[36m Install libdrm-cursor.................... \033[0m"
\${APT_INSTALL} /packages/libdrm-cursor/*.deb

# Only preload libdrm-cursor for X
sed -i "/libdrm-cursor.so/d" /etc/ld.so.preload
sed -i "1aexport LD_PRELOAD=libdrm-cursor.so.1" /usr/bin/X

#------------------pcmanfm------------
echo -e "\033[36m Install pcmanfm.................... \033[0m"
\${APT_INSTALL} /packages/pcmanfm/*.deb

#------------------blueman------------
echo -e "\033[36m Install blueman.................... \033[0m"
\${APT_INSTALL} blueman
echo exit 101 > /usr/sbin/policy-rc.d
chmod +x /usr/sbin/policy-rc.d
\${APT_INSTALL} blueman
rm -f /usr/sbin/policy-rc.d

#------------------rkwifibt------------
echo -e "\033[36m Install rkwifibt.................... \033[0m"
rm -rf /usr/lib/firmware
\${APT_INSTALL} /packages/rkwifibt/*.deb
ln -s /system/etc/firmware /vendor/etc/
mkdir -p /lib/firmware/
cp /packages/rkwifibt/8723du/* /lib/firmware/
#------------------glmark2------------
echo -e "\033[36m Install glmark2.................... \033[0m"
\${APT_INSTALL} /packages/glmark2/*.deb

echo -e "\033[36m Install Chinese fonts.................... \033[0m"
# Uncomment zh_CN.UTF-8 for inclusion in generation
sed -i 's/^# *\(zh_CN.UTF-8\)/\1/' /etc/locale.gen
echo "LANG=zh_CN.UTF-8" >> /etc/default/locale

# Generate locale
locale-gen

# Export env vars
echo "export LC_ALL=zh_CN.UTF-8" >> ~/.bashrc
echo "export LANG=zh_CN.UTF-8" >> ~/.bashrc
echo "export LANGUAGE=zh_CN.UTF-8" >> ~/.bashrc

source ~/.bashrc

\${APT_INSTALL} fonts-aenigma
\${APT_INSTALL} xfonts-intl-chinese

# HACK debian11.3 to fix bug
\${APT_INSTALL} fontconfig --reinstall

#\${APT_INSTALL} xfce4
#ln -sf /usr/bin/startxfce4 /etc/alternatives/x-session-manager


# mark package to hold
apt list --installed | grep -v oldstable | cut -d/ -f1 | xargs apt-mark hold

#---------------GPU--------------
cp /packages/libmali/*.deb /
cp -rf /packages/rga/ /
cp -rf /packages/rga2/ /
# mark rga package to unhold
apt-mark unhold librga2 librga-dev librga2-dbgsym
cp /packages/libmali/libmali-valhall-g610-g6p0-x11.so /usr/lib/aarch64-linux-gnu/
ln -s /usr/lib/aarch64-linux-gnu/libmali-valhall-g610-g6p0-x11.so /usr/lib/aarch64-linux-gnu/libmali.so
ln -s /usr/lib/aarch64-linux-gnu/libmali-valhall-g610-g6p0-x11.so /usr/lib/aarch64-linux-gnu/libmali.so.1

#---------------Custom Script--------------
systemctl mask systemd-networkd-wait-online.service
systemctl mask NetworkManager-wait-online.service
rm /lib/systemd/system/wpa_supplicant@.service

#----------------user add-------------------
adduser topeet --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password
echo "topeet:topeet" |  chpasswd
echo "topeet ALL=(ALL:ALL) ALL" >> /etc/sudoers
echo "topeet ALL=(ALL:ALL) ALL" >> /etc/sudoers
echo "password" | passwd root --stdin > /dev/null 2>&1
echo "root:topeet" | chpasswd


#----------------beautiful terminal_________
echo "iTOP-RK3588" > /etc/hostname
echo  "127.0.0.1 localhost" >/etc/hosts
echo  "127.0.0.1 iTOP-RK3588" >> /etc/hosts
echo  "127.0.1.1 iTOP-RK3588.localdomain topeet" >>/etc/hosts
echo " alias ls='ls --color' " >>/root/.bashrc
echo " alias ls='ls --color' " >>/home/topeet/.bashrc
echo " export LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:'" >>/root/.bashrc
echo "export LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:'" >>/home/topeet/.bashrc
echo "PS1='${debian_chroot:+($debian_chroot)}\[\033[01;33;1m\]\u\[\033[00;32;1m\]@\[\033[01;36;1m\]\h\[\033[00;32;1m\]:\[\033[00;34;1m\]\w \[\033[01;32;1m\]\$ \[\033[01;37;1m\]' " >>/root/.bashrc
echo " PS1='${debian_chroot:+($debian_chroot)}\[\033[01;33;1m\]\u\[\033[00;32;1m\]@\[\033[01;36;1m\]\h\[\033[00;32;1m\]:\[\033[00;34;1m\]\w \[\033[01;32;1m\]\$ \[\033[01;37;1m\]'" >>/home/topeet/.bashrc

#-----------beautiful login---------------
\${APT_INSTALL} toilet
rm -rf /usr/share/figlet/*
cp /packages/figlet-fonts/* /usr/share/figlet/
timedatectl set-timezone Asia/Shanghai
cp /packages/libdrm/lightdm.conf /etc/lightdm/
#echo "tty -s && mesg n || true" >> /root/.profile

#---------------background---------------
cp /packages/backdrops/* /usr/share/xfce4/backdrops/

#---------Xserver---------
echo -e "\033[36m Install Xserver.................... \033[0m"
\${APT_INSTALL} libpcre2-dev libxfont-dev libxkbfile-dev libpciaccess-dev mesa-common-dev libdmx1 xdmx
#\${APT_INSTALL} /packages/xserver/libsepol1*.deb
#\${APT_INSTALL} /packages/xserver/libselinux1*.deb
#\${APT_INSTALL} /packages/xserver/xdmx_1.20.11-1_arm64.deb
#\${APT_INSTALL} /packages/xserver/xdmx-tools_1.20.11-1_arm64.deb
#\${APT_INSTALL} /packages/xserver/xdmx-tools-dbgsym_1.20.11-1_arm64.deb
#\${APT_INSTALL} /packages/xserver/xdmx-dbgsym_1.20.11-1_arm64.deb
#dpkg -i  /packages/xserver/xserver-xorg-core_1.20.11-1_arm64.deb

#------remove unused packages------------
apt remove --purge -fy linux-firmware*

#---------------Clean--------------
if [ -e "/usr/lib/arm-linux-gnueabihf/dri" ] ;
then
        cd /usr/lib/arm-linux-gnueabihf/dri/
        cp kms_swrast_dri.so swrast_dri.so rockchip_dri.so /
        rm /usr/lib/arm-linux-gnueabihf/dri/*.so
        mv /*.so /usr/lib/arm-linux-gnueabihf/dri/
elif [ -e "/usr/lib/aarch64-linux-gnu/dri" ];
then
        cd /usr/lib/aarch64-linux-gnu/dri/
        cp kms_swrast_dri.so swrast_dri.so rockchip_dri.so /
        rm /usr/lib/aarch64-linux-gnu/dri/*.so
        mv /*.so /usr/lib/aarch64-linux-gnu/dri/
        rm /etc/profile.d/qt.sh
fi
cd -


rm -rf /var/lib/apt/lists/*
rm -rf /var/cache/
rm -rf /packages/
mkdir -p /var/cache/apt/archives/partial
EOF
sudo umount $TARGET_ROOTFS_DIR/dev/pts
sudo umount $TARGET_ROOTFS_DIR/dev
