#!/bin/bash

FTP_USERS_LIMIT=500

_PFX="ftp"
NUMBER="${1}"
if [ -z "${NUMBER}" ] ; then
    echo "Usage: ${0} number_of_ftp_users"
    exit 1
fi

if [ -n "$(printf '%s\n' "${NUMBER}" | sed 's/[0-9]//g')" ]; then
    echo "${NUMBER}: is not numeric"
    exit 1
fi

N_EXISTING_USERS=`pure-pw list 2>/dev/null | wc -l`

i=1
j=1
while [ ${i} -le ${NUMBER} ] ; do

    if [ $((${N_EXISTING_USERS}+${i})) -gt ${FTP_USERS_LIMIT} ] ; then
        echo "FTP users limit reached: ${FTP_USERS_LIMIT}"
        break
    fi

    USERNAME="${_PFX}${j}"
    pure-pw show ${USERNAME} > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
        passwd=`apg -M NCL -m 8 -x 8 -n 1`
        echo -e "${passwd}\n${passwd}" | addftpuser "${USERNAME}" > /dev/null
        if [ $? -eq 0 ] ; then
            echo "${i}: ${USERNAME} ${passwd}"
            i=$((i+1))
        fi
    fi

    j=$((j+1))

done

systemctl restart pure-ftpd

