#!/bin/sh
##
##  squid-passwd -- Password Management for Squid
##

passwdfile="@l_prefix@/etc/squid/squid.passwd"
htpasswd="@l_prefix@/sbin/apache-htpasswd"

usage () {
    echo "squid-passwd: USAGE: squid-passwd list" 1>&2
    echo "squid-passwd: USAGE: squid-passwd add <username> [<password>]" 1>&2
    echo "squid-passwd: USAGE: squid-passwd verify <username> [<password>]" 1>&2
    echo "squid-passwd: USAGE: squid-passwd delete <username>" 1>&2
}

if [ $# -eq 0 ]; then
    echo "squid-passwd: ERROR: invalid number of arguments" 1>&2
    usage
    exit 1
fi

cmd="$1"
shift
case "$cmd" in
    list )
        if [ $# -ne 0 ]; then
            echo "squid-passwd: ERROR: invalid number of arguments" 1>&2
            exit 1
        fi
        cat $passwdfile
        ;;
    add )
        if [ $# -lt 1 -o $# -gt 2 ]; then
            echo "squid-passwd: ERROR: invalid number of arguments" 1>&2
            exit 1
        fi
        username="$1"
        shift
        if [ $# -eq 1 ]; then
            password="$1"
            $htpasswd -b $passwdfile $username $password
        else
            $htpasswd $passwdfile $username
        fi
        ;;
    verify )
        if [ $# -lt 1 -o $# -gt 2 ]; then
            echo "squid-passwd: ERROR: invalid number of arguments" 1>&2
            exit 1
        fi
        username="$1"
        shift
        if [ $# -eq 1 ]; then
            password="$1"
            $htpasswd -b -v $passwdfile $username $password
        else
            $htpasswd -v $passwdfile $username
        fi
        ;;
    delete )
        if [ $# -ne 1 ]; then
            echo "squid-passwd: ERROR: invalid number of arguments" 1>&2
            exit 1
        fi
        username="$1"
        shift
        $htpasswd -D $passwdfile $username
        ;;
    * )
        echo "squid-passwd: ERROR: invalid number of arguments" 1>&2
        usage
        exit 1
        ;;
esac

