#!/bin/bash

##################################################
# Mount dir                                      #
# - /opt/gitea                                   #
##################################################

set -euo pipefail
export LANG=en_US.UTF-8
trap Quit EXIT

PIDS=
GOT_SIGTERM=

function Print {
    local file=/dev/null
    [ '-f' = "$1" ] && file=$2 && shift && shift
    date +"[%F %T] $*" | tee -a $file
}

function Quit {
    while :; do
        pkill -f gitea && Print killing gitea ... || break
        sleep 1
    done
    Print Container stopped.
    test -n "$GOT_SIGTERM"
}

function Usage {
    Print 'This container should run with
    **root user**
    **/opt/gitea mounted from host**
    '
}

function GenerateConf {
    Print Generating app.ini ...
    mkdir -p custom/conf
    cat > custom/conf/app.ini <<-EOF
APP_NAME = Gitea
RUN_USER = gitea
RUN_MODE = prod

[repository]
ROOT        = /opt/gitea/repos
SCRIPT_TYPE = bash

[security]
PASSWORD_COMPLEXITY = off
DISABLE_GIT_HOOKS   = false

[indexer]
ISSUE_INDEXER_TYPE       = bleve
ISSUE_INDEXER_PATH       = /opt/gitea/indexers/issues.bleve
REPO_INDEXER_ENABLED     = true
REPO_INDEXER_PATH        = /opt/gitea/indexers/repos.bleve
MAX_FILE_SIZE            = 1048576
REPO_INDEXER_INCLUDE     = **.go,**.yml,**.toml,**.c,**.h,**makefile,**.py,**.txt,**.ini,**.rs,**.sh,**.md,**Dockerfile*,**docker-entrypoint*,**.cnf,**.conf,**.json,**.sql,**.xml,**.js,**.jsx,**.vue,**.ts,**.tsx,**.html,**.css,**.scss,**.less

[queue.issue_indexer]
ISSUE_INDEXER_QUEUE_TYPE = levelqueue
ISSUE_INDEXER_QUEUE_DIR  = /opt/gitea/indexers/issues.queue
UPDATE_BUFFER_LEN        = 20

[server]
APP_DATA_PATH          = /opt/gitea/data
PROTOCOL               = http
HTTP_ADDR              = 0.0.0.0
HTTP_PORT              = 3000
#PROTOCOL               = unix
#HTTP_ADDR              = /sock/gitea
#UNIX_SOCKET_PERMISSION = 666
#DOMAIN                 = x.x.x
#ROOT_URL               = http://x.x.x
DISABLE_SSH            = true
START_SSH_SERVER       = false
SSH_DOMAIN             = x.x.x
SSH_PORT               = 3622
LFS_START_SERVER       = true
OFFLINE_MODE           = false
ENABLE_GZIP            = true

[database]
DB_TYPE  = sqlite3
PATH     = /opt/gitea/data/gitea.db
SSL_MODE = disable

[mailer]
ENABLED = false

[service]
REGISTER_EMAIL_CONFIRM            = false
ENABLE_NOTIFY_MAIL                = false
DISABLE_REGISTRATION              = true
ALLOW_ONLY_EXTERNAL_REGISTRATION  = false
ENABLE_CAPTCHA                    = true
REQUIRE_SIGNIN_VIEW               = true
DEFAULT_KEEP_EMAIL_PRIVATE        = true
DEFAULT_ALLOW_CREATE_ORGANIZATION = false
DEFAULT_ENABLE_TIMETRACKING       = true
NO_REPLY_ADDRESS                  =

[picture]
DISABLE_GRAVATAR        = true
ENABLE_FEDERATED_AVATAR = false

[openid]
ENABLE_OPENID_SIGNIN = false
ENABLE_OPENID_SIGNUP = false

[attachment]
ENABLED       = true
ALLOWED_TYPES = */*
MAX_SIZE      = 1024
MAX_FILES     = 5
STORAGE_TYPE  = local
PATH          = /opt/gitea/attachments

[session]
PROVIDER = memory

[time]
FORMAT = RFC3339

[log]
ROOT_PATH = /opt/gitea/log
MODE      = file
LEVEL     = warn
ROUTER    = file

[git]
PATH      =
HOME_PATH = /opt/gitea/data/git-home

[lfs]
PATH = /opt/gitea/lfs

[webhook]
ALLOWED_HOST_LIST = *
EOF
}

function ChangeOwner {
    Print Changing file owner ...
    chown -R gitea:gitea /opt/gitea
}

function StartProc {
    Print Starting gitea ...
    su - gitea -c '
        gitea \
            --work-path /opt/gitea \
            --custom-path /opt/gitea/custom \
            --config /opt/gitea/custom/conf/app.ini \
            web
    ' &>> /opt/gitea/log/gitea.out &
    PIDS="$PIDS $!"
    Print Gitea started.
}

function Main {
    local pid=
    cd /opt/gitea
    Usage
    [ -e custom/conf ] || GenerateConf
    ChangeOwner
    StartProc
    trap "GOT_SIGTERM=1; Print Got SIGTERM ..." SIGTERM
    while [ -z "$GOT_SIGTERM" ] && sleep 2; do
        for pid in $PIDS; do
            [ ! -e /proc/$pid ] && Print Unexpected error! && exit
        done
    done
}

# Start here
Main

