#!/bin/sh

# TODO: Incorporate capb backup.

set -e
. /usr/share/debconf/confmodule
. /usr/share/migration-assistant/ma-script-utils


if [ -z "$1" ]; then
    mapath="/usr/bin"
else
    mapath="$1"
fi

#FIXME: need a better solution.
OLDIFS="$IFS"
IFS='
'
tmp=""
for line in `os-prober`;
do
    dist=$(expr match "$line" '.*:\(.*\):.*:.*')
    location=$(expr match "$line" '\(.*\):.*:.*:.*')
    tmp="$tmp$dist ($location), "
done
tmp=${tmp%,*}

IFS="$OLDIFS"

db_subst migration-assistant/partitions choices "$tmp"
db_input high migration-assistant/partitions || true
db_go || exit 10

db_get migration-assistant/partitions
IFS='
'
selection=`echo "$RET" | sed -e 's/, /\n/g'`
if [ -z "$selection" ]; then
    exit
fi
for choice in $selection; # Microsoft Windows XP Professional (/dev/hda1)
do
    location=`expr "$choice" : '.*(\(.*\))'` # /dev/hda1
    for line in `os-prober`;
    do
	loc=$(expr match "$line" '\(.*\):.*:.*:.*')
	# make this not if continue, so we can save an indentation
	if [ $location = $loc ]
	then
	    set_os_type $line

	    path=${loc#*/*/} # hda1 or discs/disc0/part1
	    mount_os "$ostype" "$loc"
	    IFS="$OLDIFS"
	    db_register migration-assistant/users migration-assistant/$path/users
	    db_subst migration-assistant/$path/users user-choices "$($mapath/ma-search-users "$ostype" "$mountpoint")"
	    db_subst migration-assistant/$path/users os-type "$choice"
	    

	    db_input high migration-assistant/$path/users || true
	    db_go || exit 10

	    db_get migration-assistant/$path/users

	    user_selection=`echo "$RET" | sed -e 's/, /\n/g'`
	    for usr in $user_selection;
	    do
		itemsret=$($mapath/ma-search-items --path="$mountpoint" --ostype="$ostype" --user="$usr")
		db_register migration-assistant/items migration-assistant/$path/$usr/items
		db_subst migration-assistant/$path/$usr/items choices "$itemsret"
		db_subst migration-assistant/$path/$usr/items user "$usr"


		# migration-assistant/discs/disc0/part1/Evan/items
		db_input high migration-assistant/$path/$usr/items || true
		db_go || exit 10
		

		db_register migration-assistant/user migration-assistant/$path/$usr/user
		db_set migration-assistant/$path/$usr/user "$(echo "$usr" | tr '[A-Z]' '[a-z]')"
		db_subst migration-assistant/$path/$usr/user old-user "$usr"

		db_input high migration-assistant/$path/$usr/user || true
		db_go || exit 10
		
		db_get migration-assistant/$path/$usr/user
		new_user=$RET

		# Only ask if the user doesn't match the one seeded for
		# user-setup.
		db_get passwd/username || true
		if [ "$new_user" = "$RET" ]; then
		    continue
		fi

		# There's no sense in asking about this new user more than once
		db_get migration-assistant/new-user/$new_user/password && continue
		

		# Password
		db_register migration-assistant/password migration-assistant/new-user/$new_user/password
		db_subst migration-assistant/new-user/$new_user/password user "$new_user"

		db_register migration-assistant/password-again migration-assistant/new-user/$new_user/password-again
		db_subst migration-assistant/new-user/$new_user/password-again user "$new_user"

		while [ true ]; do

		    db_input high migration-assistant/new-user/$new_user/password || true
		    db_go || exit 10
		    db_get migration-assistant/new-user/$new_user/password || true
		    pass=$RET


		    db_input high migration-assistant/new-user/$new_user/password-again || true
		    db_go || exit 10
		    db_get migration-assistant/new-user/$new_user/password-again || true
		    pass2=$RET

		    if [ "$pass2" = "$pass" ]; then
			break
		    fi

		    db_set migration-assistant/new-user/$new_user/password ""
		    db_set migration-assistant/new-user/$new_user/password-again ""
		    db_fset migration-assistant/password-mismatch seen "false"
		    db_input high migration-assistant/password-mismatch || true
		    db_fset migration-assistant/new-user/$new_user/password seen "false"
		    db_fset migration-assistant/new-user/$new_user/password-again seen "false"
		    db_go || true

		done

		
		# Full Name
		db_register migration-assistant/fullname migration-assistant/new-user/$new_user/fullname
		db_subst migration-assistant/new-user/$new_user/fullname user "$new_user"

		db_input high migration-assistant/new-user/$new_user/fullname || true
		db_go || exit 10
		
		# Administrator
		db_register migration-assistant/administrator migration-assistant/new-user/$new_user/administrator
		db_subst migration-assistant/new-user/$new_user/administrator user "$new_user"

		db_input high migration-assistant/new-user/$new_user/administrator || true
		db_go || exit 10

		# TODO: Here we need to verify the username and password.  See
		# user-setup-ask, states 6 and 7.

	    done

	    unmount_os
	fi
    done
done
