#!/bin/sh
#
# Install the live Antler root tree onto the MFM ext2 partition.
#
# This script is intentionally explicit because it is destructive.  It is
# normally disabled by /etc/default/install and is enabled only for a prepared
# installer boot image.  The copy uses the installed recursive cp command; when
# source is / and the destination is under /mnt, that cp implementation skips
# the /mnt mount tree so it will not copy the target into itself.

if test -f /etc/default/install; then
	. /etc/default/install
fi

case "$MFM_INSTALL_ON_BOOT" in
yes|YES|true|TRUE|1|once|ONCE)
	;;
*)
	exit 0
	;;
esac

target=${MFM_INSTALL_TARGET:-/mnt/mfm}
device=${MFM_INSTALL_DEVICE:-/dev/mfm0p1}
fallback=${MFM_INSTALL_FALLBACK_DEVICE:-/dev/mfm0}
whole=${MFM_INSTALL_WHOLE_DEVICE:-/dev/mfm0}
mbr_image=${MFM_INSTALL_MBR_IMAGE:-}
boot_source=${MFM_INSTALL_BOOT_SOURCE:-}
boot_sectors=${MFM_INSTALL_BOOT_SECTORS:-19}
clone_source=${MFM_INSTALL_CLONE_SOURCE:-}
clone_target=${MFM_INSTALL_CLONE_TARGET:-$device}
clone_bs=${MFM_INSTALL_CLONE_BS:-512}
clone_skip=${MFM_INSTALL_CLONE_SKIP:-}
clone_seek=${MFM_INSTALL_CLONE_SEEK:-}
format=${MFM_INSTALL_FORMAT:-no}
blocks=${MFM_INSTALL_BLOCKS:-4087}
clone_count=${MFM_INSTALL_CLONE_COUNT:-$blocks}
label=${MFM_INSTALL_LABEL:-AntlerMFM}
check=${MFM_INSTALL_CHECK:-no}
log=${MFM_INSTALL_LOG:-}
tag=${MFM_INSTALL_TAG:-manual}
marker=$target/etc/antler-mfm-install.tag

if test -n "$log"; then
	exec > "$log" 2>&1
fi

echo "MFM install: target $target"
mkdir -p "$target" || exit 1

echo "MFM install: starting mfm_st506"
service start mfm_st506 || true
echo "MFM install: starting ext2fs"
service start ext2fs || true

# A blank MFM disk needs an MBR before a partition device can be opened.  The
# preferred path writes the generated MBR image from /boot because it avoids
# depending on sector-zero reads from the running controller.  The older
# boot_source path is kept for hand-built rescue images that need to copy a
# small pre-partition range from a known-good disk.
if test -n "$mbr_image"; then
	echo "MFM install: writing MBR image $mbr_image to $whole"
	if ! dd if="$mbr_image" of="$whole" bs=512 count=1; then
		echo "MFM install: MBR image write failed"
		exit 1
	fi
	sync
elif test -n "$boot_source"; then
	echo "MFM install: writing boot sectors from $boot_source to $whole"
	if ! dd if="$boot_source" of="$whole" bs=512 count="$boot_sectors"; then
		echo "MFM install: boot-sector copy failed"
		exit 1
	fi
	sync
fi

if test -n "$clone_source"; then
	echo "MFM install: cloning $clone_source to $clone_target"
	clone_args="if=$clone_source of=$clone_target bs=$clone_bs count=$clone_count"
	if test -n "$clone_skip"; then
		clone_args="$clone_args skip=$clone_skip"
	fi
	if test -n "$clone_seek"; then
		clone_args="$clone_args seek=$clone_seek"
	fi
	if ! dd $clone_args; then
		echo "MFM install: clone failed"
		exit 1
	fi
	sync

	case "$check" in
	yes|YES|true|TRUE|1)
		echo "MFM install: checking cloned filesystem $device"
		if ! e2fsck -y "$device"; then
			echo "MFM install: cloned filesystem check failed"
			exit 1
		fi
		;;
	esac

	umount "$target" >/dev/null 2>&1 || true
	if mount -t ext2 "$device" "$target" >/dev/null 2>&1; then
		echo "MFM install: mounted cloned $device"
	else
		echo "MFM install: cannot mount cloned $device"
		exit 1
	fi

	mkdir -p "$target/dev" "$target/mnt/mfm" "$target/tmp" "$target/var/log"
	cat > "$target/etc/default/install" <<EOF
MFM_INSTALL_ON_BOOT=no
MFM_INSTALL_DEVICE=$device
MFM_INSTALL_FALLBACK_DEVICE=$fallback
MFM_INSTALL_WHOLE_DEVICE=$whole
MFM_INSTALL_MBR_IMAGE=$mbr_image
MFM_INSTALL_TARGET=$target
MFM_INSTALL_TAG=$tag
EOF
	echo "$tag" > "$marker"
	sync

	case "$check" in
	yes|YES|true|TRUE|1)
		echo "MFM install: unmounting cloned target for final e2fsck"
		if umount "$target"; then
			if ! e2fsck -y "$device"; then
				echo "MFM install: final cloned filesystem check failed"
				exit 1
			fi
		else
			echo "MFM install: could not unmount cloned target"
			exit 1
		fi
		;;
	esac
	echo "MFM install: complete"
	exit 0
fi

case "$format" in
yes|YES|true|TRUE|1|force|FORCE)
	echo "MFM install: formatting $device as ext2 blocks=$blocks"
	if ! mkfs.ext2 -F -b 1024 -m 0 -L "$label" "$device" "$blocks"; then
		echo "MFM install: cannot format $device"
		exit 1
	fi
	sync
	case "$check" in
	yes|YES|true|TRUE|1)
		echo "MFM install: checking fresh filesystem $device"
		if ! e2fsck -y "$device"; then
			echo "MFM install: fresh filesystem check failed"
			exit 1
		fi
		;;
	esac
	;;
esac

# Make the installer own the mount state.  If rc.sys mounted the partition
# through /etc/mount.cfg already, drop that mount and re-open it here.
umount "$target" >/dev/null 2>&1 || true
if mount -t ext2 "$device" "$target" >/dev/null 2>&1; then
	echo "MFM install: mounted $device"
else
	if mount -t ext2 "$fallback" "$target" >/dev/null 2>&1; then
		echo "MFM install: mounted $fallback"
	else
		echo "MFM install: cannot mount $device or $fallback"
		exit 1
	fi
fi

if test -f "$marker"; then
	read oldtag < "$marker"
	if test "$oldtag" = "$tag"; then
		echo "MFM install: tag $tag is already installed"
		exit 0
	fi
fi

echo "MFM install: deleting old OS tree"
for name in bin boot config dev etc home netboot root sbin system tmp user usr var
do
	rm -rf "$target/$name"
done
rm -f "$target/kernel"
rm -rf "$target/lost+found"

echo "MFM install: copying configured OS"
for name in kernel bin sbin system config etc boot root home user netboot var
do
	if ! cp -Rf "/$name" "$target"; then
		echo "MFM install: copy failed at /$name"
		exit 1
	fi
done

mkdir -p "$target/dev" "$target/mnt/mfm" "$target/tmp" "$target/var/log"

# The installed MFM root must never wipe itself on boot.  The marker records
# the image tag so the SerDrive installer image also skips repeated installs.
cat > "$target/etc/default/install" <<EOF
MFM_INSTALL_ON_BOOT=no
MFM_INSTALL_DEVICE=$device
MFM_INSTALL_FALLBACK_DEVICE=$fallback
MFM_INSTALL_WHOLE_DEVICE=$whole
MFM_INSTALL_MBR_IMAGE=$mbr_image
MFM_INSTALL_TARGET=$target
MFM_INSTALL_TAG=$tag
EOF
echo "$tag" > "$marker"

sync
case "$check" in
yes|YES|true|TRUE|1)
	echo "MFM install: unmounting target for final e2fsck"
	if umount "$target"; then
		if ! e2fsck -y "$device"; then
			echo "MFM install: final filesystem check failed"
			exit 1
		fi
	else
		echo "MFM install: could not unmount target for final check"
		exit 1
	fi
	;;
esac
echo "MFM install: complete"
