#!/bin/sh

# When this script is called, settings.py.dist should always exist and contain
# intact version of settings.py as it was installed by weblate.

# This could happen if weblate was just upgraded and l10n_opensuse_org-installer is on schedule.
# Skip patching to prevent rejects. Patching will happen just after installation
# of the second package.
if ! grep -q '^Version: \(2\.17\.1\|2\.18\)$' /usr/share/weblate/PKG-INFO ; then
	echo "Incorrect weblate version (expected intermediate upgrade state). Skipping changes."
	exit 0
fi

cd /etc/weblate
if ! test -f settings.py.dist ; then
	echo "l10n_opensuse_org-weblate-settings-py error: No weblate-settings-py.dist" >&2
	exit 2
fi
cp -a settings.py.dist settings.py.l10n_opensuse_org
if sed -f /etc/l10n_opensuse_org-secrets.sed </usr/share/l10n_opensuse_org/weblate-settings-py.diff | patch ; then
	if test -f settings.py.l10n_opensuse_org.orig ; then
		echo "l10n_opensuse_org-weblate-settings-py warning: weblate-settings-py.diff did not apply cleanly (but no rejects)" >&2
		rm settings.py.l10n_opensuse_org.orig
	fi
	mv settings.py.l10n_opensuse_org settings.py
else
	rm settings.py.l10n_opensuse_org*
	echo "l10n_opensuse_org-weblate-settings-py error: Failed to apply weblate-settings-py.diff" >&2
	exit 1
fi
