#!/bin/bash

################################################################################
# Title:        build.sh
# Description:  Script to build Peppermint ISO image
# Author:       manuel rosa <manuelsilvarosa@gmail.com>
# Date:         Outubro 29, 2023
# License:      GPL-3.0-or-later
################################################################################

PATH="/sbin:/usr/sbin:/usr/local/sbin:$PATH"

# Set the working folder variable
fusato="$(pwd)"


# Create the fusato folder, move into it removing stale mountpoints and files there.
[ -e fusato ] && [ ! -d fusato ] && rm -f fusato || [ ! -e fusato ] && mkdir fusato
cd fusato
umount $(mount | grep "${PWD}/chroot" | tac | cut -f3 -d" ") 2>/dev/null
for i in * .build ; do [ $i = cache ] && continue || rm -rf $i ; done

lb config noauto \
	--binary-images iso-hybrid \
	--mode debian \
	--architectures i386 \
	--linux-flavours 686-pae \
	--distribution bookworm \
	--archive-areas "main contrib non-free non-free-firmware" \
	--mirror-bootstrap https://deb.debian.org/debian \
	--parent-mirror-bootstrap https://deb.debian.org/debian \
	--parent-mirror-chroot https://deb.debian.org/debian \
	--parent-mirror-chroot-security https://security.debian.org/debian-security  \
	--parent-mirror-binary https://deb.debian.org/debian \
	--parent-mirror-binary-security https://security.debian.org/debian-security  \
	--mirror-chroot https://deb.debian.org/debian \
	--mirror-chroot-security https://security.debian.org/debian-security \
	--uefi-secure-boot enable \
	--updates true \
	--security true \
	--backports false \
	--cache true \
	--apt-recommends true \
	--firmware-binary true \
	--firmware-chroot true \
	--iso-application "PeppermintOS" \
	--win32-loader false \
	--iso-preparer "PeppermintOS-https://peppermintos.com/" \
	--iso-volume "PeppermintOS" \
	--iso-publisher "Peppermint OS Team" \
	--win32-loader false \
	--checksums sha512 \
	--zsync false \
     "${@}"



# Install the Openbox window manager
mkdir -p $fusato/fusato/config/package-lists
echo openbox > $fusato/fusato/config/package-lists/desktop.list.chroot 

# Install software
echo "# Install software to the squashfs for calamares to unpack to the OS.
fonts-liberation
gir1.2-webkit2-4.0
inputattach
inxi
locales
nala
pulseaudio-module-bluetooth
python3-pip
python3-tk
python3-pil.imagetk
python3.11-venv
luakit
simple-scan
smartmontools
smbclient
sqlite3
wireless-tools
linux-headers-686-pae
tint2
thunar
thunar-archive-plugin
#rofi
desktop-base
mousepad
lxpolkit
lxsession-logout
pnmixer
#pasystray
alsa-utils
gmrun
git
curl
fonts-cantarell
nitrogen
compton
compton-conf
compton-conf-l10n
xarchiver
xdg-user-dirs-gtk
cbatticon
dunst
xfce4-clipman
xfce4-screenshooter
xfce4-power-manager
xfce4-settings
xfce4-terminal
plank
gnome-screensaver
ristretto
jgmenu
tela-icon-theme
marwaita-peppermint-gtk-theme
locales
dkms
dbus-x11
ntp
deb-multimedia-keyring
xorg 
xserver-xorg 
xserver-xorg-input-synaptics 
xserver-xorg-input-all 
xserver-xorg-video-vmware 
xserver-xorg-video-all
w32codecs
ffmpeg 
sox 
twolame 
lame 
faad 
gstreamer1.0-plugins-good 
gstreamer1.0-plugins-ugly 
gstreamer1.0-plugins-bad 
gstreamer1.0-pulseaudio
unrar 
rar 
p7zip-full 
p7zip-rar 
zip 
unzip
pulseaudio 
pavucontrol 
alsa-utils 
synaptic 
gparted 
libelf-dev 
htop 
gvfs-backends 
network-manager 
network-manager-gnome
bluez 
blueman
gtk2-engines
plymouth 
plymouth-themes
cups 
system-config-printer 
lightdm 
lightdm-gtk-greeter
lightdm-gtk-greeter-settings
gnome-system-tools 
gnome-disk-utility 
neofetch 
accountsservice 
catfish
fuseiso 
fwupd 
dconf-editor
printer-driver-cups-pdf
gnome-screensaver
calamares
calamares-settings-debian 
firmware-linux 
firmware-linux-free
firmware-linux-nonfree 
firmware-misc-nonfree 
firmware-realtek 
firmware-atheros 
firmware-bnx2 
firmware-bnx2x 
firmware-brcm80211 
firmware-iwlwifi 
firmware-libertas 
firmware-netxen 
firmware-zd1211  
gdebi 
f2fs-tools
xfsprogs
xfsdump
ffmpegthumbnailer
grub-pc
spice-vdagent
console-setup
plank
obmenu-generator
qt5-style-plugins 
qt5ct
openbox-theme-collection

" > $fusato/fusato/config/package-lists/packages.list.chroot 

# Packages to be stored in /pool but not installed in the OS .
echo "# These packages are available to the installer, for offline use. 
efibootmgr
grub2-common
grub-efi-ia32
grub-efi-ia32-bin
grub-efi-ia32-signed
libefiboot1
libefivar1
mokutil
os-prober
shim-helpers-i386-signed
shim-signed
shim-signed-common
shim-unsigned


" > $fusato/fusato/config/package-lists/installer.list.binary 


# Setup the chroot structure
mkdir -p $fusato/fusato/config/archives
mkdir -p $fusato/fusato/config/includes.binary
mkdir -p $fusato/fusato/config/hooks/live
mkdir -p $fusato/fusato/config/hooks/normal
mkdir -p $fusato/fusato/config/bootloaders
mkdir -p $fusato/fusato/config/packages.chroot
mkdir -p $fusato/fusato/config/includes.chroot/etc
mkdir -p $fusato/fusato/config/includes.chroot/usr/bin
mkdir -p $fusato/fusato/config/includes.chroot/usr/share/applications
mkdir -p $fusato/fusato/config/includes.chroot/etc/calamares
mkdir -p $fusato/fusato/config/includes.chroot/etc/live/config.conf.d
mkdir -p $fusato/fusato/config/includes.chroot/usr/sbin
mkdir -p $fusato/fusato/config/includes.chroot/etc/lightdm
mkdir -p $fusato/fusato/config/includes.chroot/usr/share/pixmaps
mkdir -p $fusato/fusato/config/includes.chroot/etc/skel/Desktop
mkdir -p $fusato/fusato/config/includes.chroot/etc/default
mkdir -p $fusato/fusato/config/includes.chroot/boot/grub/themes
mkdir -p $fusato/fusato/config/includes.chroot/etc/skel/.local/share/pmostools
mkdir -p $fusato/fusato/config/includes.chroot/opt/pypep/dbpep
mkdir -p $fusato/fusato/config/includes.chroot/opt/pepconf
mkdir -p $fusato/fusato/config/includes.chroot/usr/share/polkit-1/actions
mkdir -p $fusato/fusato/config/includes.chroot/usr/lib/python3/dist-packages
mkdir -p $fusato/fusato/config/includes.chroot/usr/share/fonts/pepconf
mkdir -p $fusato/fusato/config/includes.chroot/usr/lib/live/config
mkdir -p $fusato/fusato/config/includes.chroot/usr/local/bin
mkdir -p $fusato/fusato/config/includes.chroot/etc/skel/.local/share/pmostools

# Copy Configs to the chroot
cp $fusato/userconfig/* $fusato/fusato/config/includes.chroot/etc/live/config.conf.d
cp $fusato/applications/* $fusato/fusato/config/includes.chroot/usr/share/applications
cp $fusato/hooks/live/* $fusato/fusato/config/includes.chroot/usr/lib/live/config
cp $fusato/hooks/normal/* $fusato/fusato/config/hooks/normal
cp $fusato/grub/grub $fusato/fusato/config/includes.chroot/etc/default
cp $fusato/calamares/sources-debian/* $fusato/fusato/config/includes.chroot/usr/sbin
cp $fusato/calamares/bootloader-config-32/* $fusato/fusato/config/includes.chroot/usr/sbin
cp $fusato/calamares/install-peppermint $fusato/fusato/config/includes.chroot/usr/bin
cp $fusato/database/* $fusato/fusato/config/includes.chroot/opt/pypep/dbpep
cp $fusato/PepProPixMaps/* $fusato/fusato/config/includes.chroot/usr/share/pixmaps
cp $fusato/font/* $fusato/fusato/config/includes.chroot/usr/share/fonts/pepconf
cp $fusato/PepProTools/* $fusato/fusato/config/includes.chroot/usr/local/bin
cp $fusato/polkit/* $fusato/fusato/config/includes.chroot/usr/share/polkit-1/actions
cp $fusato/lightdm/* $fusato/fusato/config/includes.chroot/etc/lightdm
cp $fusato/pepconf/* $fusato/fusato/config/includes.chroot/opt/pepconf


cp -r $fusato/bootloaders/* $fusato/fusato/config/includes.binary
cp -r $fusato/repos/* $fusato/fusato/config/archives
cp -r $fusato/calamares/debian/* $fusato/fusato/config/includes.chroot/etc/calamares
cp -r $fusato/configs/* $fusato/fusato/config/includes.chroot/etc/
#cp -r $fusato/packages/* $fusato/fusato/config/packages.chroot
cp -r $fusato/grub/themes/* $fusato/fusato/config/includes.chroot/boot/grub/themes
cp -r $fusato/pmostools/* $fusato/fusato/config/includes.chroot/etc/skel/.local/share/pmostools
cp -r $fusato/pylibraries/* $fusato/fusato/config/includes.chroot/usr/lib/python3/dist-packages

# fusato the ISO #
lb build  #--debug --verbose 


