#!/sbin/openrc-run
# Copyright 2025 CleanStart
# Distributed under the terms of the MIT license

name="GitLab"
description="GitLab Enterprise Edition - Git repository management"

: ${gitlab_user:="git"}
: ${gitlab_group:="git"}
: ${gitlab_home:="/var/lib/gitlab"}
: ${gitlab_path:="/usr/share/webapps/gitlab"}
: ${gitlab_env:="production"}

command="/usr/bin/bundle"
command_args="exec puma -C ${gitlab_path}/config/puma.rb"
command_background="yes"
command_user="${gitlab_user}:${gitlab_group}"
pidfile="/run/gitlab/gitlab.pid"

required_files="${gitlab_path}/config/gitlab.yml"
required_files="${required_files} ${gitlab_path}/config/database.yml"

depend() {
need net redis postgresql
use nginx
after postgresql redis
}

start_pre() {
checkpath -d -m 0755 -o "${gitlab_user}:${gitlab_group}" /run/gitlab
checkpath -d -m 0750 -o "${gitlab_user}:${gitlab_group}" "${gitlab_home}/tmp"
checkpath -d -m 0755 -o "${gitlab_user}:${gitlab_group}" /var/log/gitlab

cd "${gitlab_path}"
export RAILS_ENV="${gitlab_env}"
export NODE_ENV="${gitlab_env}"
}

start() {
ebegin "Starting GitLab"
start-stop-daemon --start \
--user "${gitlab_user}" \
--group "${gitlab_group}" \
--chdir "${gitlab_path}" \
--pidfile "${pidfile}" \
--background \
--make-pidfile \
--env RAILS_ENV="${gitlab_env}" \
--env NODE_ENV="${gitlab_env}" \
--exec ${command} -- ${command_args}
eend $?
}

stop() {
ebegin "Stopping GitLab"
start-stop-daemon --stop \
--pidfile "${pidfile}" \
--retry 30
eend $?
}
