#!/bin/sh -f

alterator_api_version=1
po_domain="alterator-root"

. alterator-sh-functions

root_chpasswd()
{
    echo "root:$1"|/usr/sbin/chpasswd
}

on_message()
{
	case "$in_action" in
		read)
		    write_string_param passwd_auto "$(pwqgen)"
		    ! test_bool "$in_auto"
		    write_bool_param auto "$?"
		    ;;
		write)
		    [ -n "$in_commit" ] || return

		    if test_bool "$in_auto" && [ -z "$in_passwd_auto" ]; then
			write_error "`_ "You should define a password for system administrator"`"
		    elif test_bool "$in_auto" && [ -n "$in_passwd_auto" ]; then
			root_chpasswd "$in_passwd_auto"
		    elif [ -z "$in_passwd_1" -a -z "$in_passwd_2" ]; then
			write_error "`_ "You should define a password for system administrator"`"
		    elif [ "$in_passwd_1" != "$in_passwd_2" ]; then
			write_error "`_ "Passwords mismatch"`"
		    else
			root_chpasswd "$in_passwd_1"
		    fi
		    ;;
	esac
}

message_loop
