#! /bin/bash
# create and sanitise files for pushing to repo
# catatrophically forgot to sanitize localsettingsWSL.py - oops.

#Make sure you have the WSL permissions system working, or you will push unsanitized files as this will fail
# Philip Sargent 2022/04/12

HOSTNAME=`hostname`
echo "** This copies file to _deploy/${HOSTNAME}/ !"
cd ..

cd troggle
echo `pwd`
echo deprecations.

PYTHON="uv run"

source .venv/bin/activate
python3 -Wall manage.py check -v 3 2>deprecations.txt >/dev/null
deactivate
echo diffsettings.
rm diffsettings.txt
if test -f "diffsettings.txt"; then
    echo "diffsettings.txt not deleted. You have a serious permissions problem. Aborting.."
    exit
fi
$PYTHON manage.py diffsettings | grep "###" > diffsettings.txt

echo inspectdb.
# this next line requires database setting to be troggle.sqlite:
$PYTHON manage.py inspectdb > troggle-inspectdb.py
#egrep -in "unable|error" troggle-inspectdb.py
echo remove passwords.
cp localsettings.py localsettings-${HOSTNAME}.py
sed -i '/EXPOUSERPASS/ s/^.*$/EXPOUSERPASS = "nnn:gggggg - real-expo-password---imported-from-localsettings.py"/' diffsettings.txt
sed -i '/EXPOUSERPASS/ s/^.*$/EXPOUSERPASS = "nnn:gggggg - real-expo-password---imported-from-localsettings.py"/' localsettings-${HOSTNAME}.py
echo "    reset: EXPOUSERPASS = \"nnn:gggggg\"  - real-expo-password---imported-from-localsettings.py"

sed -i '/EXPOADMINUSERPASS/ s/^.*$/EXPOADMINUSERPASS = "gggggg:nnn - real-expo-password---imported-from-localsettings.py"/' diffsettings.txt
sed -i '/EXPOADMINUSERPASS/ s/^.*$/EXPOADMINUSERPASS = "gggggg:nnn - real-expo-password---imported-from-localsettings.py"/' localsettings-${HOSTNAME}.py
echo "    reset: EXPOUSERPASS = \"gggggg:nnn\"  - real-expo-password---imported-from-localsettings.py"

sed -i '/EMAIL_HOST_PASSWORD/ s/^.*$/EMAIL_HOST_PASSWORD = "real-email-password---imported-from-localsettings.py"/' diffsettings.txt
sed -i '/EMAIL_HOST_PASSWORD/ s/^.*$/EMAIL_HOST_PASSWORD = "real-email-password---imported-from-localsettings.py"/' localsettings-${HOSTNAME}.py
echo "    reset: EMAIL_HOST_PASSWORD = \"real-email-password--imported-from-localsettings.py\""

sed -i '/SECRET_KEY/ s/^.*$/SECRET_KEY = "real-SECRET_KEY--imported-from-localsettings.py"/' diffsettings.txt
sed -i '/SECRET_KEY/ s/^.*$/SECRET_KEY = "real-SECRET_KEY--imported-from-localsettings.py"/' localsettings-${HOSTNAME}.py
echo "    reset: SECRET_KEY = \"real-SECRET_KEY--imported-from-localsettings.py\""

mkdir -p _deploy/${HOSTNAME}
mv _deploy/${HOSTNAME}/localsettings-${HOSTNAME}.py _deploy/${HOSTNAME}/localsettings-${HOSTNAME}.py.bak
mv localsettings-${HOSTNAME}.py _deploy/${HOSTNAME}
cp *.sh _deploy/${HOSTNAME}

#
# Do these before final testing, *not* just before pushing:
# in ./pre-run.sh
# $PYTHON reset-django.py
# $PYTHON manage.py makemigrations
# $PYTHON manage.py test
# $PYTHON manage.py inspectdb > troggle-inspectdb.py
# egrep -i "unable|error" troggle-inspectdb.py
