#!/bin/bash

# VERSION=2
# CHANGES="block installation if lighttpd-fallback is missing"

/bin/mount -oremount,rw /

## install new lighttpd only if fallback exist
if ! /usr/fallback/lighttpd -v ; then
	/bin/rm -f /pkginfo/VERSION.lighttpd
	/bin/mount -o remount,rw /usr/local
	/bin/rm -f /usr/local/conf/rootfs/VERSION.lighttpd
	/bin/mount -o remount,ro /usr/local
	/bin/mount -o remount,rw /home/admin
	/bin/rm -r /home/admin/conf/rootfs/pkg/lighttpd
	/bin/mount -o remount,ro /home/admin
	if /usr/bin/lighttpd -v | grep '1.4.32' ; then
		/usr/fallback/beroconf set root TLSv1.2-disabled ERROR_TLS_DISABLED
	fi
fi

grep "plain.userfile" /etc/lighttpd.conf > /dev/null
if [ "${?}" == "1" ]; then
	exit 0
fi

pass_plain=$(expr match "$(cat /usr/conf/apiusers.conf)" "admin:\(.*\)")
pass_crypt=$(echo "<?php \$ret=crypt(\"${pass_plain}\", base64_encode(\"${pass_plain}\")); echo \$ret; ?>" | /usr/bin/php -q)

echo -n "admin:${pass_crypt}" > /usr/conf/apiusers.conf

exit 0
