#!/bin/bash

BUILD_ARCH=amd64
BUILD_SUITE=yangtze
EXSUITES=()
INCLUDES=(
    ### Base
    bash
    bash-completion
    curl
    wget
    file
    libc-bin
    lsb-release
    grub-efi-amd64
    dialog
    #zenity
    #keyboard-configuration
    #console-setup
    ### Locale
    locales
    gettext-base
    language-pack-zh-hans
    ### Themes
    fonts-droid-fallback
    ### Qt 5
    #libqt5core5a
    #libqt5gui5
    #libqt5widgets5
    #libqt5network5
    #libqt5dbus5
    #libqt5script5
    #libqt5scripttools5
    #qttranslations5-l10n
    #qt5-gtk-platformtheme
    #libqt5waylandclient5
    #libqt5waylandcompositor5
    ### X
    #x11-xserver-utils
    #x11-utils
    #x11-apps
    xinit
    sudo
)
COMPONENTS=(
    main
    cross
    pty
)
MIRRORS=http://archive.build.openkylin.top/openkylin/

KEYRING=
VARIANT=minbase

ROOTFS_DIR=$(pwd)/kare-ok1

# Suite
for SUITE in "${EXSUITES[@]}"; do
    EX_SUITES="${EX_SUITES:+${EX_SUITES},}${SUITE}"
done

# Packages
for PKG in "${INCLUDES[@]}"; do
    EX_PKGS="${EX_PKGS:+${EX_PKGS},}${PKG}"
done

# Components
for COM in "${COMPONENTS[@]}"; do
    EX_COMS="${EX_COMS:+${EX_COMS},}${COM}"
done

BOOTSTRAP_OPTS=
BOOTSTRAP_OPTS="${BOOTSTRAP_OPTS:+${BOOTSTRAP_OPTS} }--verbose"
BOOTSTRAP_OPTS="${BOOTSTRAP_OPTS:+${BOOTSTRAP_OPTS} }--variant=${VARIANT}"
BOOTSTRAP_OPTS="${BOOTSTRAP_OPTS:+${BOOTSTRAP_OPTS} }--arch=${BUILD_ARCH}"


if [ -n "${EX_SUITES}" ]; then
    BOOTSTRAP_OPTS="${BOOTSTRAP_OPTS:+${BOOTSTRAP_OPTS} }--extra-suites=${EX_SUITES}"
fi

if [ -n "${EX_PKGS}" ]; then
    BOOTSTRAP_OPTS="${BOOTSTRAP_OPTS:+${BOOTSTRAP_OPTS} }--include=${EX_PKGS}"
fi

if [ -n "${EX_COMS}" ]; then
    BOOTSTRAP_OPTS="${BOOTSTRAP_OPTS:+${BOOTSTRAP_OPTS} }--components=${EX_COMS}"
fi

if [ -n "${KEYRING}" ]; then
    BOOTSTRAP_OPTS="${BOOTSTRAP_OPTS:+${BOOTSTRAP_OPTS} }--keyring=${KEYRING}"
fi

sudo rm -rf "${ROOTFS_DIR}"
# sudo mkdir -p "${CACHE_DIR}" "${ROOTFS_DIR}"
sudo mkdir -p "${ROOTFS_DIR}"
eval sudo debootstrap "${BOOTSTRAP_OPTS}" "${BUILD_SUITE}" "${ROOTFS_DIR}" "${MIRRORS}"



# 构建目录
sudo mv kare-ok1 /kare-ok1
KARE_PATH=/kare-ok1
SHAREDDIRS=(
    box
    data
    home
    media
    mnt
    run
    sys
    tmp
)

KAREDIRS=(
    bin
    etc
    lib
    lib64
    sbin
    usr
    var
)

BWRRAP_OPTS="--uid 0 --gid 0"

for DIR in "${KAREDIRS[@]}"; do
    if [ -d "/$KARE_PATH/$DIR" ]; then
        BWRRAP_OPTS="$BWRRAP_OPTS --bind $KARE_PATH/$DIR /$DIR"
    fi
done

for DIR in "${SHAREDDIRS[@]}"; do
    if [ -d "/$DIR" ]; then
        BWRRAP_OPTS="$BWRRAP_OPTS --bind /$DIR /$DIR"
    fi
done

BWRRAP_OPTS="$BWRRAP_OPTS --dev-bind /dev /dev --proc /proc"

BWRRAP_OPTS="$BWRRAP_OPTS --setenv DISPLAY :0"

eval sudo /usr/bin/bwrap $BWRRAP_OPTS apt update

eval sudo /usr/bin/bwrap $BWRRAP_OPTS apt install -f -y


# 复制用户配置
USERCONFIG=(
    passwd
    shadow
    group
    gshadow
    subgid
    subuid
    passwd-
    shadow-
    group-
    gshadow-
    subgid-
    subuid-
    uid_list
)

for CONFIG in "${USERCONFIG[@]}"; do
    sudo cp /etc/$CONFIG $KARE_PATH/etc/
done

#获取宿主机可登录用户
usernames=$(getent passwd |grep -E "/bin/(bash|sh)$" | grep -vE "nologin|false|sync|shutdown|root" | cut -d: -f1)

for user in $usernames; do
    echo "$user:qwer1234" | sudo /usr/bin/bwrap $BWRRAP_OPTS chpasswd
done


/usr/bin/buildCompatibilityEnv.sh non