#!/bin/sh

po_domain="alterator-livecd"
alterator_api_version=1

image_dir="${image_dir:-/image}"
target_dir="${target_dir:-/mnt/destination}"

. alterator-sh-functions

on_message() {
  case "$in_action" in
    write)
	local dev mpoint rest
	sort -r -k2,2 /proc/mounts|
	    while read dev mpoint rest; do
		[ "$mpoint" != "/" -a "$mpoint" != "$image_dir" ] || continue
		[ -b "$dev" ] || [ "${mpoint##$target_dir}" != "$mpoint" ] || continue
		if ! umount "$mpoint";then
		    local msg="$(printf "`_ "Unable to unmount %s\n"`" "$mpoint")"
		    write_error "$msg"
		    return
		fi
	    done

	if ! swapoff -a; then
	    write_error "`_ "Unable to turn off swap partitions"`"
	    return
	fi

	if ! mkdir -p -- "$target_dir"; then
	    write_error "`_ "Unable to create directory for new system"`"
	    return
	fi
	;;
  esac
}

message_loop
