CFGROOT := img_mount/
-_target: ${CFGROOT}/opt/RetroPie-Setup/ ${CFGROOT}/etc/sudoers.d/nopassword _vc4driver _groups _autologin
+_target: ${CFGROOT}/etc/sudoers.d/nopassword _retropie _vc4driver _groups _autologin | _config
+
+_retropie: ${CFGROOT}/opt/RetroPie-Setup/ # ${CFGROOT}/opt/retropie/
RetroPie-Setup/:
[ ! -d "$@" ] && git clone --depth=1 https://github.com/RetroPie/RetroPie-Setup.git
-${CFGROOT}/opt/RetroPie-Setup/: RetroPie-Setup/ ${CFGROOT} | _config
+${CFGROOT}/opt/RetroPie-Setup/: RetroPie-Setup/ | _config
cp -au "$<" "$@"
-_vc4driver: ${CFGROOT} | _config
+# ${CFGROOT}/opt/retropie/: retropie/ | _config
+# -cp -au "$<" "$@"
+
+_vc4driver: | _config
sed -Ei 's;^ *dtoverlay=vc4-fkms-v3d;# &;' ${CFGROOT}/boot/config.txt
-${CFGROOT}/etc/sudoers.d/nopassword: ${CFGROOT}
+${CFGROOT}/etc/sudoers.d/nopassword: | _config
printf '%%sudo ALL=NOPASSWD: ALL\n' >"$@"
chmod 440 "$@"
-_groups: _users
+_groups: _users | _config
chroot "$${CFGROOT}" adduser rpi sudo
chroot "$${CFGROOT}" adduser rpi input
chroot "$${CFGROOT}" adduser rpi video
-_autologin: ${CFGROOT}/etc/systemd/system/getty@tty1.service.d/override.conf ${CFGROOT}
-${CFGROOT}/etc/systemd/system/getty@tty1.service.d/override.conf:
+_autologin: ${CFGROOT}/etc/systemd/system/getty@tty1.service.d/override.conf ${CFGROOT}/etc/profile.d/retropie_autostart.sh
+${CFGROOT}/etc/systemd/system/getty@tty1.service.d/override.conf: | _config
mkdir -p ${CFGROOT}/etc/systemd/system/getty@tty1.service.d/
printf '[Service]\nExecStart=\nExecStart=-/sbin/agetty --autologin %s --noclear %%I $$TERM' \
rpi >$@
+
+${CFGROOT}/etc/profile.d/retropie_autostart.sh: | _config
+ printf '%s\n' '[ "$$USER" = rpi ] && \\' \
+ 'if which emulationstation; then' ' emulationstation' \
+ 'else' ' sudo /opt/RetroPie-Setup/retropie_setup.sh' 'fi' \
+ >"$@"