#!/usr/bin/env bash

log_file=create-users.log

function output_error {
    echo "$1" >&2
    echo "$1" >> $log_file
}

# Create the log file, erasing an existing log.
# Should we ask for confirmation if there is an existing log?
cp /dev/null $log_file

while IFS=':' read -r username fullname password shell; do
    echo "Got: $fullname ($username) with password $password using shell ${shell:=/bin/bash}"

    # TODO: Check if the proposed new user already exists.

    username_length=${#username}
    if [ $username_length -gt 8 ]; then
        message="$username: USERNAME TOO LONG"
        output_error "$message"
    fi

    # TODO: Deal with password length.

    if ! grep -q "^$shell\$" < /etc/shells; then
        message="$username: SHELL $shell NOT RECOGNIZED"
        output_error "$message"
    fi

    # TODO: Invoke `adduser` to actually add the user

done
