#!/bin/sh
# desk-mounts: make C:\ (/c) writable and persistent.  Called once from desk-session, before anything uses /c.
# The root file system is read-only; Windows installers (MSI/NSIS/Inno) write C:\Program Files, C:\ProgramData, C:\Users and the
# registry file C:\Windows\System32\config\registry.txt, so /c must be a writable disk.
#   cdisk=LABEL=pomelo-c | cdisk=PARTUUID=.. | cdisk=/dev/sdaN   (kernel command line; default LABEL=pomelo-c)
# 1. find the partition (looked up in /proc/fsprobe), mount it on /persistc
# 2. refresh the system files that belong to the image (C:\Windows\System32|SysWOW64 *.dll *.exe, from the initramfs / root that
#    are visible under /c now) and add anything missing from the image's /c tree (cp -n: never overwrite user data, registry, installs)
# 3. unmount it and mount the partition on /c (the kernel has no bind mounts).
# No partition / mount failure: a RAM overlay (tmpfs seeded from the image's /c) is used and a warning is shown on the console, in
# /tmp/desk-mounts.log and as a notification: installed programs are then lost at power off.
[ -x /usr/bin/desk-prep ] && exec /usr/bin/desk-prep mounts      # native version (docs/BOOT-BUDGET.md); the shell code below is the fallback
LOG=/tmp/desk-mounts.log
say() { echo "desk-mounts: $*" | tee -a $LOG; echo "desk-mounts: $*" > /dev/console 2>/dev/null; }
CD=LABEL=pomelo-c
for w in $(cat /proc/cmdline); do case "$w" in cdisk=*) CD=${w#cdisk=};; esac; done
[ "$CD" = none ] && exit 0
mkdir -p /persistc /c
dev=
case "$CD" in
LABEL=*|PARTUUID=*|UUID=*)
    key=${CD%%=*}; val=${CD#*=}; val=${val#\"}; val=${val%\"}
    for t in 1 2 3 4 5 6 7 8 9 10; do
        dev=$(sed -n "s|^\(/dev/[^:]*\):.* $key=\"$val\".*|\1|p" /proc/fsprobe 2>/dev/null | head -n1)
        [ -n "$dev" ] && break; sleep 1
    done;;
*) dev=$CD;;
esac
seed() {   # $1 = mounted target dir (empty or populated), copy the image's /c into it
    cp -a -n /c/. "$1/" 2>/dev/null
    for d in Windows/System32 Windows/SysWOW64; do
        [ -d "/c/$d" ] || continue
        mkdir -p "$1/$d"
        cp -u -p /c/$d/*.dll /c/$d/*.exe "$1/$d/" 2>/dev/null
    done
}
if [ -n "$dev" ] && [ -b "$dev" ] && { mount "$dev" /persistc 2>/dev/null || mount -t ext4 "$dev" /persistc 2>/dev/null; }; then
    seed /persistc
    mkdir -p /persistc/Users/pomelo/Desktop /persistc/Users/pomelo/Documents /persistc/Users/pomelo/Downloads "/persistc/Program Files" "/persistc/Program Files (x86)" /persistc/ProgramData
    sync; umount /persistc 2>/dev/null   # the kernel has no bind mounts: re-mount the partition itself on /c
    if mount "$dev" /c 2>/dev/null || mount -t ext4 "$dev" /c 2>/dev/null; then
        say "C: is the persistent partition $dev (programs, registry and C:\\Users survive power off)"
        # pomelo-c has no journal and this kernel writes dirty ext4 pages back only on sync: flush every 3 s so that an install is on disk
        # even when the VM is powered off abruptly (a clean desk-power poweroff syncs again)
        ( while :; do sleep 3; sync; done ) >/dev/null 2>&1 &
        exit 0
    fi
    say "cannot attach $dev to /c"
else
    say "ERROR: C: partition ($CD) not found or not mountable"
fi
# fallback: RAM overlay
mkdir -p /ramc && mount -t tmpfs tmpfs /ramc 2>/dev/null && {
    ( cd /c && find . -mindepth 1 -maxdepth 1 | while read -r e; do cp -a "$e" /ramc/ 2>/dev/null; done )
    mount -t tmpfs tmpfs /c 2>/dev/null || true
}
if [ -z "$(ls /c 2>/dev/null | head -1)" ] && [ -d /ramc ]; then cp -a /ramc/. /c/ 2>/dev/null; fi
say "WARNING: C: is in RAM: installed programs and registry changes are LOST at power off (image has no usable pomelo-c partition)"
( sleep 25; notify-send -a "C:" -u critical -t 0 "C: is not persistent" "No pomelo-c partition found: installed Windows programs and the registry are lost at power off." 2>/dev/null ) &
exit 0
