#!/bin/bash -ex

NAME=roundup
USER=roundup
ADMIN_PASS=turnkey
DB_PASS=$(mcookie)

RT_TRACKER=/var/lib/roundup/tracker
RT_TEMPLATE=classic
RT_BACKEND=mysql
RT_VER=2.6.0
RT_URL=https://files.pythonhosted.org/packages/64/93/17eca3bc4e9bda03c45a9b3bf4fd972d96865efa7b0c09100a55cd58d4e7/roundup-2.6.0.tar.gz
RT_SHA256=12fd8fb806047415f22131f965c6ab4026a28dc63f7054e055c8f891950591d4
RT_CONF=/etc/roundup/tracker-config.ini
RT_HOME="/home/$USER"
RT_VENV="$RT_HOME/venv"

as_user() { su - "$USER" -c "$*"; }

adduser --disabled-login --gecos 'roundup user' --shell /bin/bash $USER

as_user python3 -m venv --system-site-packages "$RT_VENV"

as_user echo ". $RT_VENV/bin/activate" '>' .bashrc.d/roundup-venv
as_user chmod +x .bashrc.d/roundup-venv

as_user "$RT_VENV/bin/python -m pip install --no-deps \
    'roundup @ $RT_URL#sha256=$RT_SHA256'"
version_output=$(as_user "$RT_VENV/bin/roundup-admin -v")
[[ $version_output == "$RT_VER (python 3.13."*")" ]]

# allow Apache access to docs - requires search perms (x on directory)
DOCS="$RT_VENV/share/doc/roundup/html"
chmod o+x $DOCS{,/_{images,sources,static}}

# allow Apache access to venv dir listing
chmod o+x "$RT_HOME" "$RT_VENV"

a2enmod wsgi
a2enmod rewrite

# start mysql server
service mysql start

# create roundup and user and grant privs to yet to be created db
mysql --batch --execute "\
    GRANT ALL PRIVILEGES ON $NAME.* TO $NAME@localhost IDENTIFIED BY '$DB_PASS'; \
    FLUSH PRIVILEGES;"

mkdir -p $RT_TRACKER
mkdir /etc/roundup
chown -R roundup:roundup $RT_TRACKER
OPTIONS="admin_email=admin,dispatcher_email=admin,tracker_web=https://www.example.com/,mail_domain=example.com,mail_host=localhost,rdbms_password=$DB_PASS"
as_user "$RT_VENV/bin/roundup-admin -i $RT_TRACKER install $RT_TEMPLATE $RT_BACKEND $OPTIONS"
as_user "$RT_VENV/bin/roundup-admin -i $RT_TRACKER initialise $ADMIN_PASS"
echo "tracker = $RT_TRACKER" >> /etc/roundup/roundup-server.ini
chown -R www-data:www-data $RT_TRACKER/db

# configure mod_wsgi
cat > $RT_TRACKER/wsgi.py <<EOF
from roundup.cgi.wsgi_handler import RequestDispatcher
tracker_home = "$RT_TRACKER"
application = RequestDispatcher(tracker_home)
EOF

# move tracker configuration to etc
mv $RT_TRACKER/config.ini $RT_CONF
ln -s $RT_CONF $RT_TRACKER/config.ini

# disable registration confirmation via email
sed -i "\|^email_registration_confirmation =| s|=.*|= no|" $RT_CONF

# create link to local documentation
sed -i "s;Roundup docs</a>;Roundup docs</a>\n    (<a href=\"/docs\" i18n:translate=\"\">local</a>);" $RT_TRACKER/html/page.html

# don't display turnkey-credit in popups
for file in *.help*.html _generic.calendar.html; do
    sed -i "s|</head>|<style type='text/css'> #turnkey-credit { display: none; }</style>\n  </head>|" $RT_TRACKER/html/$file
done

# apache configuration
a2dissite 000-default
a2ensite roundup
rm -rf /var/www/html

mkdir -p /var/www/static/
cat > /var/www/static/README <<EOF
Place robots.txt & favicon.ico in this dir.

Ensure that they are readable by the Apache user (www-data).
EOF

# stop mysql server
service mysql stop
