#!/bin/bash
# SynergyOS disk installer — copies the running live system onto a disk.
# WARNING: erases the chosen disk completely.
set -euo pipefail
[ "$(id -u)" = 0 ] || { echo "run as root (sudo synergyos-install)"; exit 1; }
[ -d /run/live/rootfs ] || { echo "not running from the live ISO"; exit 1; }

echo "== SynergyOS installer =="
echo
lsblk -d -o NAME,SIZE,MODEL,TRAN | grep -v -E 'loop|sr0'
echo
read -rp "Target disk to ERASE and install to (e.g. nvme0n1 or sda): " DISK
DEV="/dev/$DISK"
[ -b "$DEV" ] || { echo "no such block device $DEV"; exit 1; }
LIVE_DEV=$(findmnt -no SOURCE /run/live/medium 2>/dev/null || true)
case "$LIVE_DEV" in "$DEV"*) echo "that is the live USB itself"; exit 1;; esac
read -rp "Hostname [synergyos]: " HOSTNAME_; HOSTNAME_=${HOSTNAME_:-synergyos}
read -rp "Username [synergy]: " USER_; USER_=${USER_:-synergy}
while :; do
  read -rsp "Password for $USER_: " PW1; echo
  read -rsp "Again: " PW2; echo
  [ "$PW1" = "$PW2" ] && [ -n "$PW1" ] && break
  echo "passwords differ or empty"
done
echo
echo "ALL DATA ON $DEV WILL BE DESTROYED."
read -rp "Type YES to continue: " OK; [ "$OK" = YES ] || exit 1

UEFI=0; [ -d /sys/firmware/efi ] && UEFI=1
P=""; [[ "$DISK" == nvme* || "$DISK" == mmcblk* ]] && P="p"

echo "-- partitioning"
wipefs -a "$DEV" >/dev/null
if [ $UEFI = 1 ]; then
  sgdisk -Z "$DEV" >/dev/null
  sgdisk -n1:0:+512M -t1:ef00 -c1:EFI -n2:0:0 -t2:8300 -c2:root "$DEV" >/dev/null
  partprobe "$DEV"; sleep 2
  mkfs.vfat -F32 "${DEV}${P}1" >/dev/null
  mkfs.ext4 -F -L synergyos "${DEV}${P}2" >/dev/null
  ROOT="${DEV}${P}2"; ESP="${DEV}${P}1"
else
  parted -s "$DEV" mklabel msdos mkpart primary ext4 1MiB 100% set 1 boot on
  partprobe "$DEV"; sleep 2
  mkfs.ext4 -F -L synergyos "${DEV}${P}1" >/dev/null
  ROOT="${DEV}${P}1"; ESP=""
fi

echo "-- copying system (this takes a few minutes)"
mkdir -p /mnt/target
mount "$ROOT" /mnt/target
rsync -aHAX --info=progress2 /run/live/rootfs/*/ /mnt/target/ \
  --exclude=/proc/* --exclude=/sys/* --exclude=/dev/* --exclude=/run/* --exclude=/tmp/*
for d in proc sys dev run tmp; do mkdir -p /mnt/target/$d; done
mount --bind /dev /mnt/target/dev; mount --bind /proc /mnt/target/proc
mount --bind /sys /mnt/target/sys;  mount --bind /run /mnt/target/run
if [ $UEFI = 1 ]; then mkdir -p /mnt/target/boot/efi; mount "$ESP" /mnt/target/boot/efi; fi

echo "-- configuring"
ROOT_UUID=$(blkid -s UUID -o value "$ROOT")
{ echo "UUID=$ROOT_UUID / ext4 errors=remount-ro 0 1"
  [ $UEFI = 1 ] && echo "UUID=$(blkid -s UUID -o value "$ESP") /boot/efi vfat umask=0077 0 1"
} > /mnt/target/etc/fstab
echo "$HOSTNAME_" > /mnt/target/etc/hostname
sed -i "s/^127.0.1.1.*/127.0.1.1 $HOSTNAME_/" /mnt/target/etc/hosts
chroot /mnt/target bash -c "
  set -e
  apt-get purge -y -qq live-boot live-boot-initramfs-tools >/dev/null 2>&1 || true
  if [ '$USER_' != synergy ]; then usermod -l '$USER_' -d /home/'$USER_' -m synergy; groupmod -n '$USER_' synergy; fi
  echo '$USER_:$PW1' | chpasswd
  # live-session leftovers: autologin, passwordless sudo, serial console, installer shortcuts
  rm -rf /etc/systemd/system/getty@tty1.service.d /etc/systemd/system/serial-getty@ttyS0.service.d
  rm -f /etc/sddm.conf.d/zz-live-autologin.conf /etc/sudoers.d/zz-live-nopasswd
  printf '[Last]
Session=/usr/share/xsessions/plasma.desktop
User=%s
' '$USER_' > /var/lib/sddm/state.conf
  chown sddm:sddm /var/lib/sddm/state.conf 2>/dev/null || true
  rm -f /home/'$USER_'/Desktop/install-synergyos.desktop /home/'$USER_'/Desktop/install-debian.desktop
  apt-get purge -y -qq calamares calamares-settings-debian >/dev/null 2>&1 || true
  systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target >/dev/null 2>&1 || true
  if [ $UEFI = 1 ]; then
    grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=SynergyOS --recheck >/dev/null
  else
    grub-install --target=i386-pc --recheck $DEV >/dev/null
  fi
  [ -f /etc/default/grub ] || cp /usr/share/grub/default/grub /etc/default/grub
  sed -i 's/^GRUB_DISTRIBUTOR=.*/GRUB_DISTRIBUTOR=SynergyOS/' /etc/default/grub
  update-initramfs -u -k all >/dev/null 2>&1
  update-grub >/dev/null 2>&1
"
sync
umount -R /mnt/target
echo
echo "== SynergyOS installed on $DEV. Remove the USB and reboot. =="
