#!/bin/sh

. /usr/share/debconf/confmodule

newns () {
	[ "$OS_PROBER_NEWNS" ] || exec /usr/lib/os-prober/newns "$0" "$@"
}

error () {
    logger -t migration-assistant "error: $@"
}

log () {
    logger -t migration-assistant "info: $@"
}

ostype=""
set_os_type () {
# Rather than test for every distro possible in the shortname, we test
# the bootloader type for 'linux.'  This *should* be fine as we're only
# working with user's home directories.

    if [ ${1##*:} = "linux" ]; then
	ostype="linux"
	return 0
    fi
    
    case `LC_ALL=C expr match "$1" '.*:.*:\(.*\):.*'` in
	"Windows" )
	ostype="windowsxp"
        return 0
	;;

        Windows[0-9] )
        ostype="windowsxp"
        return 0
        ;;

	"Windows9xMe" )
	ostype="windows9x"
        return 0
	;;

	"MacOSX" )
	ostype="macosx"
        return 0
	;;

    *)
    echo "Unknown ostype from $1" 1>&2
    return 1
    ;;
    esac
}
mountpoint="/mnt/migrationassistant"

unmount_os() {
    # If we didn't mount the device ourselves, don't unmount it.
    [ "$mountpoint" = "/mnt/migrationassistant" ] || return 0
    unmount_previously_run=
    device="$1"

    if [ -f /etc/mtab ]; then
        MTAB=/etc/mtab
    else
        MTAB=/proc/mounts
    fi

    while :; do
        failed=
        
	ISMOUNTED=
	if [ "$device" ]; then
		ISMOUNTED=$(grep "^$device " $MTAB) || ISMOUNTED=
	else
		ISMOUNTED=$(grep " $mountpoint " $MTAB) || ISMOUNTED=
	fi
        if [ -z "$ISMOUNTED" ]; then
	        break
        fi
        HOME=$(grep " $mountpoint/home " $MTAB) || HOME=
        if [ "$HOME" ]; then
            umount "$mountpoint/home" || failed="$mountpoint/home"
        fi
        if [ -z "$failed" ]; then
            if [ -z "$device" ]; then
                umount $mountpoint || failed="$mountpoint"
            else
                umount $device || failed="$device"
            fi
        fi

        if [ -z "$failed" ]; then
            break
        fi
        # lets try waiting briefly once before we completely give up on
        # unmounting the partition and dump the problem on the user.
        if [ -z "$unmount_previously_run" ]; then
            unmount_previously_run=1
            sleep 15
            continue
        fi

        db_reset migration-assistant/failed-unmount
        db_subst migration-assistant/failed-unmount MOUNTED "$failed"
        db_input critical migration-assistant/failed-unmount || true
        db_go || exit 10
        db_get migration-assistant/failed-unmount
        [ "$RET" = true ] || exit 10
    done
}

mount_os () {
    ostype="$1"
    device="$2"

    if [ "$1" = "linux" ]; then
        mkdir -p $mountpoint
        unmount_os $device
        if ! mount $device $mountpoint; then
            error "Failed to mount $device"
            exit 1
        fi
        if [ -f "$mountpoint/etc/fstab" ]; then
            while read uuid mp rest; do
                echo "$uuid" | grep -q '^#' && continue
                if [ "$mp" != "/home" ]; then
                    continue
                fi
                devname=
                if [ -n "${uuid#*=}" ]; then
                    if [ "${uuid%=*}" = "UUID" ]; then
                        devname="/dev/disk/by-uuid/${uuid#*=}"
                    elif [ "${uuid%=*}" = "LABEL" ]; then
                        devname="/dev/disk/by-label/${uuid#*=}"
                    fi
                fi
                if [ -n "$devname" ]; then
                    uuid=$(readlink -e "$devname") || uuid=
                    if [ -z "$uuid"]; then
                        error "$devname does not exist."
                        return 1
                    fi
                else
                    error "couldn't understand $uuid."
                    return 1
                fi
                
                if [ ! -e "$uuid" ]; then
                    # This happens when the IDE driver in the old OS
                    # doesn't match the driver in the installer.  The old
                    # /home might be mounted on /dev/hda3 which could now
                    # be /dev/sda3 or something entirely different.  Since
                    # there's no way to determine what the device is
                    # without the old kernel loaded, we fail gracefully so
                    # that we can continue to the next OS.
                    error "$uuid does not exist."
                    return 1
                fi
                if ! mount $uuid "$mountpoint/home"; then
                    unmount_os "$uuid"
                    if ! mount $uuid "$mountpoint/home"; then
                        error "failed to mount $uuid"
                        return 1
                    fi
                fi
                break
            done < "$mountpoint/etc/fstab"
        fi
    elif [ "$1" = "windowsxp" ]; then
        # Since we don't have to worry about separate partitions making up the
        # whole system in Windows (yet), we can allow already mounted
        # partitions.  This may fix some corner cases.  At any rate, it's
        # required for Wubi to function properly.
        mnt=$(grep "$device" /proc/mounts | head -n1 | cut -d " " -f 2)
        [ -z "$mnt" ] && [ -f /etc/mtab ] && mnt=$(grep "$device" /etc/mtab | head -n1 | cut -d " " -f 2)
        [ -n "$mnt" ] && mountpoint=$mnt && return 0

        mkdir -p $mountpoint
        unmount_os $device
        if ! mount -t ntfs $device $mountpoint -o umask=0022,nls=utf8 3>&-; then
            log "Mounting $device to $mountpoint with NTFS failed."
            if ! mount -t vfat $device $mountpoint -o umask=0022,utf8; then
                log "Mounting $device to $mountpoint with VFAT failed."
                return 1
            fi
        fi
    fi
}

ROOT=/target
add_user() {
    local username
    local fullname
    local password

    username="$1"
    fullname="$2"
    password="$3"

    chroot=chroot

# Taken from user-setup/user-setup-apply

	# Add the user
	if [ -x $ROOT/usr/sbin/adduser ]; then
		$chroot $ROOT adduser --disabled-password --gecos \
		"$fullname" "$username" >/dev/null || true
	else
		$chroot $ROOT useradd -c "$fullname" -m "$username" >/dev/null || true
	fi

	# Set the password
	$chroot $ROOT chpasswd <<EOF
$username:$password
EOF

	# Add the user to groups
	if [ -n "$username" ]; then
		db_get passwd/user-default-groups
		for group in $RET; do
			log $chroot $ROOT adduser "$USER" $group >/dev/null 2>&1 || true
		done
	fi
}
# vim:ai:et:sts=4:tw=80:sw=4:
