#!/bin/sh
# deploy-boot - push the shared boot config from /etc to a pmOS phone.
#
# The phone shares /etc with pc/laptop through Syncthing, but a fresh install
# has no sync folder yet and no machine layer. This script installs the
# portable boot files so the phone boots from /etc/inittab -> /etc/rc.d/rc.boot
# -> openrc, exactly like pc/laptop use rc.boot.
#
# usage: ./deploy-boot [user@host]
#
# Files intentionally not copied here: passwd/shadow/group, fstab, hostname,
# hosts, resolv.conf, wireguard, ssh, pam.d, apk/openrc/conf.d/runlevels.
# Those stay machine-local; /etc/.stignore keeps them out of the sync.

set -e
H=${1:-root@10.66.0.3}
P=${PHONE_PASS:-password}
SSH="sshpass -p $P ssh -o StrictHostKeyChecking=no"
SCP="sshpass -p $P scp -o StrictHostKeyChecking=no"

for f in inittab machine.sh .stignore issue motd profile \
	rc.d/rc.boot rc.d/rc.local rc.d/rc.svc rc.d/rc.getty rc.d/rc.shutdown; do
	$SSH "$H" "mkdir -p /etc/rc.d"
	$SCP "/etc/$f" "$H:/etc/$f"
done

$SSH "$H" '
	chmod 755 /etc/machine.sh /etc/rc.d/rc.boot /etc/rc.d/rc.local \
		/etc/rc.d/rc.svc /etc/rc.d/rc.getty /etc/rc.d/rc.shutdown
	for f in /etc/rc.d/rc.boot /etc/rc.d/rc.local /etc/rc.d/rc.svc \
		/etc/rc.d/rc.getty /etc/rc.d/rc.shutdown /etc/machine.sh /etc/profile; do
		sh -n "$f" || { echo "syntax error: $f"; exit 1; }
	done
	. /etc/machine.sh
	echo "machine=$MACHINE boot config installed"
'
