#!/bin/bash

########################################################################
# This script some defaults for gitea to run in a FHS compliant manner #
########################################################################

# It assumes that you place this script as gitea in /usr/bin
#
# And place the original in /usr/lib/gitea with working files in /var/lib/gitea
# and main configuration in /etc/gitea/gitea.ini
GITEA=/usr/lib/gitea/gitea
WORK_DIR=/var/lib/gitea
APP_INI=/etc/gitea/gitea.ini

APP_INI_SET=
for i in "$@"; do
	case "$i" in
	"-c")
		APP_INI_SET=1
		;;
	"-c="*)
		APP_INI_SET=1
		;;
	"--config")
		APP_INI_SET=1
		;;
	"--config="*)
		APP_INI_SET=1
		;;
	*)
	;;
	esac
done

if [ -z "$APP_INI_SET" ]; then
	CONF_ARG="-c $APP_INI"
fi


WKP_INI_SET=
for i in "$@"; do
	case "$i" in
	"-w")
		WKP_INI_SET=1
		;;
	"-w="*)
		WKP_INI_SET=1
		;;
	"--work-path")
		WKP_INI_SET=1
		;;
	"--work-path="*)
		WKP_INI_SET=1
		;;
	*)
	;;
	esac
done

if [ -z "$WKP_INI_SET" ]; then
	WORKP_ARG="-w $WORK_DIR"
fi

# Provide FHS compliant defaults to
exec $GITEA $CONF_ARG $WORKP_ARG "$@"
