#!/bin/bash
set -e

# === Variabel dasar ===
ISO_ORI="ubuntu-25.04-desktop-amd64.iso"
NEWISO="/root/ZahraOS-25.04-amd64.iso"
WORKDIR="/root/ubuntu-distro"
MNT="$WORKDIR/mnt"
EXTRACT="$WORKDIR/iso"
EDIT="$WORKDIR/edit"

# === Cek ISO, auto download kalau belum ada ===
if [ ! -f "$ISO_ORI" ]; then
    echo "[*] Downloading $ISO_ORI dari mirror UDX..."
    wget -O "$ISO_ORI" "https://ftp.udx.icscoe.jp/Linux/ubuntu-releases/25.04/$ISO_ORI"
fi

echo "[*] Unmount ISO lama (jika ada)..."
umount "$MNT" 2>/dev/null || true

echo "[*] Buat folder kerja..."
mkdir -p "$MNT" "$EXTRACT" "$EDIT"
rm -rf "$EXTRACT"/* "$EDIT"/*

echo "[*] Mount ISO asli (read-only)..."
mount -o loop,ro "$ISO_ORI" "$MNT"

echo "[*] Menyalin isi ISO..."
rsync -a --exclude=/casper/minimal.squashfs "$MNT/" "$EXTRACT/"

echo "[*] Extract minimal.squashfs..."
unsquashfs -d "$EDIT" "$MNT/casper/minimal.squashfs"

echo "[*] Siapkan mount chroot..."
mount --bind /dev "$EDIT/dev"
mount --bind /run "$EDIT/run"
mount -t proc /proc "$EDIT/proc"
mount -t sysfs /sys "$EDIT/sys"

echo "[*] Fix resolv.conf untuk DNS..."
rm -f "$EDIT/etc/resolv.conf"
cp /etc/resolv.conf "$EDIT/etc/resolv.conf"

echo "[*] Masuk ke dalam chroot untuk modifikasi sistem..."
chroot "$EDIT" /bin/bash <<'EOF'
export DEBIAN_FRONTEND=noninteractive

echo "=== Update system & install paket umum ==="
apt-get update
apt-get upgrade -y
apt-get install -y htop gparted gimp vlc libreoffice btop wget curl gnupg software-properties-common apt-transport-https unzip
echo "=== Install zahraos-desktop ==="
#rm -rf /usr/share/pixmaps/ubuntu-logo-text.png
#rm -rf /usr/share/pixmaps/ubuntu-logo-text-dark.png
rm -f "$EDIT/share/pixmaps/ubuntu-logo-*.png"
wget -O /tmp/zahraos-desktop.deb https://mirror.unilak.ac.id/zahraos-desktop.deb
dpkg -i /tmp/zahraos-desktop.deb
echo "=== Install Brave ==="
curl -fsS https://dl.brave.com/install.sh | sh
### Install Google Chrome ###
echo "=== Install Google Chrome ==="
if ! command -v google-chrome &> /dev/null; then
    wget -O /tmp/google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
    dpkg -i /tmp/google-chrome.deb
    rm /tmp/google-chrome.deb
else
    echo "Google Chrome sudah terpasang."
fi

### Install Visual Studio Code ###
echo "=== Install VS Code ==="
if ! command -v code &> /dev/null; then
    wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
    install -o root -g root -m 644 packages.microsoft.gpg /usr/share/keyrings/
    echo "deb [arch=amd64 signed-by=/usr/share/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" > /etc/apt/sources.list.d/vscode.list
    rm -f packages.microsoft.gpg
    apt-get update
    apt-get install -y code
else
    echo "VS Code sudah terpasang."
fi
EOF

# Logo distributor
mkdir -p "$EDIT/usr/share/icons/hicolor/48x48/apps"
mkdir -p "$EDIT/usr/share/icons/hicolor/256x256/apps"
mkdir -p "$EDIT/usr/share/pixmaps"
if [ -f "logo.png" ]; then
    cp logo.png "$EDIT/usr/share/icons/hicolor/48x48/apps/distributor-logo.png"
    cp logo.png "$EDIT/usr/share/icons/hicolor/256x256/apps/distributor-logo.png"
fi

echo "[*] Tambahkan Plymouth (boot logo)..."
mkdir -p "$EDIT/usr/share/plymouth/themes/ZahraOS"
cat > "$EDIT/usr/share/plymouth/themes/ZahraOS/ZahraOS.plymouth" <<PLY
[Plymouth Theme]
Name=ZahraOS
Description=Boot theme for ZahraOS
ModuleName=script

[script]
ImageDir=/usr/share/plymouth/themes/ZahraOS
ScriptFile=/usr/share/plymouth/themes/ZahraOS/ZahraOS.script
PLY

cat > "$EDIT/usr/share/plymouth/themes/ZahraOS/ZahraOS.script" <<'PLY'
wallpaper_image = Image("logo.png");
screen_width = Window.GetWidth();
screen_height = Window.GetHeight();
sprite = Sprite(wallpaper_image);
sprite.SetX(screen_width/2 - wallpaper_image.GetWidth()/2);
sprite.SetY(screen_height/2 - wallpaper_image.GetHeight()/2);
PLY

if [ -f "logo.png" ]; then
    cp logo.png "$EDIT/usr/share/plymouth/themes/ZahraOS/"
fi

chroot "$EDIT" update-alternatives --install /usr/share/plymouth/themes/default.plymouth default.plymouth /usr/share/plymouth/themes/ZahraOS/ZahraOS.plymouth 100
chroot "$EDIT" update-alternatives --set default.plymouth /usr/share/plymouth/themes/ZahraOS/ZahraOS.plymouth

echo "[*] Tambahkan wallpaper login & default..."
mkdir -p "$EDIT/usr/share/backgrounds/ZahraOS"

if [ -f "login.jpg" ]; then
    cp login.jpg "$EDIT/usr/share/backgrounds/ZahraOS/login.jpg"
    cat > "$EDIT/usr/share/glib-2.0/schemas/90_zahraos-login.gschema.override" <<CONF
[org/gnome/desktop/background]
picture-uri='file:///usr/share/backgrounds/ZahraOS/login.jpg'
CONF
fi

if [ -f "default.jpg" ]; then
    cp default.jpg "$EDIT/usr/share/backgrounds/ZahraOS/default.jpg"
    cat > "$EDIT/usr/share/glib-2.0/schemas/91_zahraos-default.gschema.override" <<CONF
[org/gnome/desktop/background]
picture-uri='file:///usr/share/backgrounds/ZahraOS/default.jpg'
CONF
fi

chroot "$EDIT" glib-compile-schemas /usr/share/glib-2.0/schemas

echo "[*] Unmount semua mountpoint dari chroot..."
umount -lf "$EDIT/dev" || true
umount -lf "$EDIT/run" || true
umount -lf "$EDIT/proc" || true
umount -lf "$EDIT/sys" || true

echo "[*] Membuat ulang minimal.squashfs..."
mksquashfs "$EDIT" "$EXTRACT/casper/minimal.squashfs" -b 1048576 -comp xz -Xbcj x86 -noappend

echo "[*] Membuat ISO baru (UEFI-only, pakai GRUB EFI)..."
xorriso -as mkisofs \
  -r -V "ZahraOS_25_04" \
  -o "$NEWISO" \
  -J -l -iso-level 3 -full-iso9660-filenames \
  -isohybrid-gpt-basdat \
  -eltorito-alt-boot \
  -e EFI/boot/bootx64.efi \
  -no-emul-boot -isohybrid-apm-hfsplus \
  "$EXTRACT"

echo "[*] Build selesai!"
ls -lh "$NEWISO"
