#!/bin/sh
# SPDX-License-Identifier: MIT
# Copyright (C) 2026 pake <pake@fedora.tw> - https://pomelo-os.org
# desk-accounts: called by desk-session (root) after the data partition is mounted, before the login service starts (docs/LOGIN.md).
#   - the account store (passwd, group, shadow, machine.conf, hints/) is /persist/accounts when the data partition is mounted,
#     otherwise a directory on the RAM root (accounts then vanish at power off; the setup wizard / greeter say so)
#   - /etc/pomelo/accounts -> store, /etc/passwd, /etc/group, /etc/shadow -> store files (glibc reads /etc/passwd by itself)
#   - /home -> /persist/home (user homes on the data partition), /run/user, /run/pomelo-dm, /var/lib/pomelo-dm
#   - kernel command line  autologin=NAME : the account NAME is created without a password (administrator) when it does not exist
#     (live mode / test images); the host name is applied
# usage: desk-accounts [autologin-name]
PATH=/usr/bin:/bin:/sbin:/usr/sbin
AL=$1
[ -x /usr/bin/desk-prep ] && exec /usr/bin/desk-prep accounts "$AL"      # native version (docs/BOOT-BUDGET.md); the shell code below is the fallback
mkdir -p /etc/pomelo /run/user /run/pomelo-dm /var/lib/pomelo-dm /home /root
chmod 755 /run/pomelo-dm /run/user
if mountpoint -q /persist 2>/dev/null || [ -d /persist/config ]; then
    # a data disk made by mke2fs / mkimage as an ordinary user has that user's uid on its root directory: never let an account own it
    chown 0:0 /persist 2>/dev/null; chmod 755 /persist 2>/dev/null
    STORE=/persist/accounts; mkdir -p /persist/accounts /persist/home; PERSISTENT=1
    [ -L /home ] && rm -f /home || rm -rf /home
    ln -s /persist/home /home
else
    STORE=/var/lib/pomelo-accounts; mkdir -p $STORE; PERSISTENT=0
fi
rm -rf /etc/pomelo/accounts; ln -s $STORE /etc/pomelo/accounts
export POMELO_ACCOUNTS=$STORE
pomelo-accounts init
grep -q '^_apt:' $STORE/passwd 2>/dev/null || echo '_apt:x:42:65534::/nonexistent:/usr/bin/false' >> $STORE/passwd
grep -q '^nogroup:' $STORE/group 2>/dev/null || echo 'nogroup:x:65534:' >> $STORE/group
for f in passwd group shadow; do rm -f /etc/$f; ln -s $STORE/$f /etc/$f; done
[ -f /etc/shells ] || printf '/bin/sh\n/bin/ash\n' > /etc/shells
echo "$PERSISTENT" > /run/pomelo-dm/persistent
# C:\ (the pomelo-c partition seeded as an ordinary user: its files would belong to uid 1000 = the first account): system trees are root's
for d in "/c" "/c/Windows" "/c/Program Files" "/c/Program Files (x86)" "/c/ProgramData" "/c/Users"; do
    [ -d "$d" ] || continue
    [ "$(stat -c %u "$d" 2>/dev/null)" = 0 ] || { chown 0:0 "$d"; chmod 755 "$d"; [ "$d" = /c/Users ] || chown -R 0:0 "$d" 2>/dev/null; }
done
mkdir -p /etc/skel
if [ -n "$AL" ] && [ -z "$(pomelo-accounts list | awk -v u="$AL" '$1==u')" ]; then
    pomelo-accounts add "$AL" -f "$AL" -a -n >/dev/null && echo "desk-accounts: account $AL created without a password (autologin)"
fi
H=$(POMELO_ACCOUNTS=$STORE pomelo-accounts get hostname 2>/dev/null)
[ -n "$H" ] && { hostname "$H" 2>/dev/null; echo "$H" > /etc/hostname; }
echo "desk-accounts: store $STORE (persistent=$PERSISTENT), $(pomelo-accounts count) account(s)"
exit 0
