#!/bin/sh
# This shell script makes use of the htpasswd utility normally
# installed by the apache web server rpm.
DEST=/etc

if [ `grep "User " /etc/httpd/conf/httpd.conf | grep -c -v "#"` = 1 ]; then
	USER=`grep "User " /etc/httpd/conf/httpd.conf | grep -v "#" | cut -f 2 -d' '`
	GROUP=`grep "Group " /etc/httpd/conf/httpd.conf | grep -v "#" | cut -f 2 -d' '`
else
	echo
	echo "Tried to work out what ownserships the password file"
	echo "should be saved with but encountered multiple 'User'"
	echo "definitions in /etc/httpd/conf/httpd.conf"
	echo "Defaulting to nobody.nobody. You may wish to check"
	echo "that $DEST/piranha.passwd has the correct ownership"
	echo "for use with your web server configuration"
	echo
	USER=nobody
	GROUP=nobody
fi


if [ -z "$1" ]; then
	echo "Sorry, you should supply a single password as the argument to this command"
	echo "eg piranha-passwd password"
	echo
	exit 1;
fi

# Two possibilities,.. the file exists and the file doesn't exist

if [ -f $DEST/piranha.passwd ]; then
	/usr/bin/htpasswd -b $DEST/piranha.passwd piranha $1
fi

if [ ! -f $DEST/piranha.passwd ]; then
	/usr/bin/htpasswd -c -b $DEST/piranha.passwd piranha $1
fi

# Paranoia, unfortunately the web server itself must be able to read the file

chmod 600 $DEST/piranha.passwd
chown $USER.$GROUP $DEST/piranha.passwd

