#!/bin/bash set -eu -o pipefail setup_db() { set -eu if [ -f "/data/gitea/conf/app.ini" ]; then echo "-----> Running database migration" gitea migrate fi } setup_root_user() { set -eu if [ ! -f "/data/gitea/conf/app.ini" ]; then echo "-----> Missing app.ini, not setting up root user" return fi user_count=$(mysql -u"${DB_USER}" -p"${DB_PASSWD}" -h mysql --database="${DB_NAME}" -N -B -e "SELECT count(*) FROM user;") if [[ "${user_count}" == "0" ]]; then echo "-----> Setting up root user for initial deployment" if gitea admin create-user --username "$ADMIN_USER" --password "$ADMIN_PASS" --email "$ADMIN_MAIL" --admin; then echo "-----> root user added" else echo "-----> Failed to add root user" exit 1 fi fi } setup_app_ini() { set -eu if [ ! -f "/data/gitea/conf/app.ini" ]; then echo "-----> Missing app.ini, not setting up root user" return fi declare app_ini="/data/gitea/conf/app.ini" echo "-----> Using crudini to setup configuration" crudini --del "$app_ini" database PATH crudini --set "$app_ini" database DB_TYPE mysql crudini --set "$app_ini" database HOST "${DB_HOST}" crudini --set "$app_ini" database NAME "${DB_NAME}" crudini --set "$app_ini" database USER "${DB_USER}" crudini --set "$app_ini" database PASSWD "${DB_PASSWD}" crudini --set "$app_ini" database SSL_MODE "disable" crudini --set "$app_ini" server PROTOCOL "http" crudini --set "$app_ini" server DOMAIN "${DOMAIN}" crudini --set "$app_ini" server ROOT_URL "https://%(DOMAIN)s/" crudini --set "$app_ini" sserver HTTP_ADDR "" crudini --set "$app_ini" sserver HTTP_PORT "3000" } main() { set -eu setup_db setup_root_user setup_app_ini } main /usr/bin/entrypoint "$@"