#!/bin/sh

BACKUPDB="/var/captiveportal/captiveportal.sqlite"
BACKUPFILE="/conf/captiveportal.sqlite"
BACKUPOFF="<captiveportalbackup>-1</captiveportalbackup>"

if [ "$(grep -c "${BACKUPOFF}" /conf/config.xml)" != "0" ]; then
	rm -f ${BACKUPFILE}
elif [ "${1}" = "restore" ]; then
	if [ -f "${BACKUPFILE}" ]; then
		mkdir -p "$(dirname ${BACKUPDB})"
		cp "${BACKUPFILE}" "${BACKUPDB}"
	fi
else
	if [ -f "${BACKUPDB}" ]; then
		sqlite3 "${BACKUPDB}" ".backup ${BACKUPFILE}"
	fi
fi
