#! /bin/bash
# and autopasswd that checks whether the user is in %customers before
# allowing the update

if [ -z "$2" ] ; then
    echo Usage: $0 username passwd
    exit 1
fi

USERNAME="$1"
PASSWD="$2"

id="/usr/bin/id"
grep="/bin/grep"

if ( $id -Gn $USERNAME | $grep customers ); then
    (expect - $USERNAME $PASSWD || exit 3) <<'EOF-EXPECT'
#    exp_internal 1
    spawn "/usr/bin/passwd" [lindex $argv 0]
    set passwd [lindex $argv 1]
    set send_slow {8 .1}

    proc send {ignore arg} {
	sleep .1
	exp_send -s -- $arg
    }

    set timeout -1
    expect -exact "Enter new UNIX password: "
    send -- "$passwd\r"
    expect -exact "\r\nRetype new UNIX password: "
    send -- "$passwd\r"
    expect -re "^passwd\: password updated successfully$"
EOF-EXPECT
else
    echo User is not in %customers
    exit 2
fi;

