#!/bin/sh

. "$(dirname "$0")/common"
HTPASSWD=/usr/local/bin/htpasswd

[ "$SYNOPKG_PKG_STATUS" = "UPGRADE" ] && exit 0

if [ ! -L "/var/services/web" ]; then
	date >> $WebalizerLog
	echo "web shared folder does not exist." >> $WebalizerLog
	echo "Please enable Web Station before enable the Webalizer." >> $WebalizerLog
	exit 1
fi

if [ ! -d "$WebalizerDir" ]; then
	mkdir "$WebalizerDir"

	if [ $? != 0 ]; then
		date >> $WebalizerLog
		echo "Failed to create webalizer directory in web shared folder." >> $WebalizerLog
		exit 1
	else
		date >> $WebalizerLog
		echo "Create the web/webalizer to store web statistic." >> $WebalizerLog
		chown -R http:http "$WebalizerDir"
	fi
fi

cp -rf $SynoHtAccessFile $WebalizerDir/.htaccess
$HTPASSWD -cb $WebalizerDir/.htpasswd "$pkgwizard_webalizer_user" "$pkgwizard_webalizer_password"

exit 0

