#!/usr/bin/sh

### BEGIN INIT INFO
# Provides:          gitea
# Required-Start:    $local_fs $network $remote_fs
# Required-Stop:     $local_fs $network $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Private, Fast, Reliable DevOps Platform
# Description:       A lightweight, self-hosted Git service that provides a
#                    web-based interface for managing Git repositories. It is
#                    designed to be easy to install and configure, making it
#                    accessible for individuals and organizations looking to
#                    host their own version control system. Gitea offers
#                    features such as issue tracking, pull requests, code
#                    reviews, and a built-in wiki, all while maintaining a
#                    user-friendly experience. Its open-source nature allows
#                    for customization and community contributions, making it
#                    a popular choice for developers seeking an alternative to
#                    larger platforms like GitHub or GitLab.

NAME='gitea'
DESC='Gitea DevOps Platform'
USER='git'
GROUP='git'
PIDFOLDER="/run/${NAME}"
PIDFILE="${PIDFOLDER}/${NAME}.pid"
DAEMON='/usr/sbin/gitea'
DAEMON_CONFIG='/etc/gitea/app.ini'
DAEMON_OPTS="web --config ${DAEMON_CONFIG}"

set -e

secrets() {
  if [ ! -f '/etc/gitea/internal_token' ]; then
    /usr/sbin/gitea generate secret INTERNAL_TOKEN > '/etc/gitea/internal_token'
    /usr/bin/chown "${USER}":"${GROUP}" '/etc/gitea/internal_token'
    /usr/bin/chmod 0440 '/etc/gitea/internal_token'
  fi
  if [ ! -f '/etc/gitea/jwt_secret' ]; then
    /usr/sbin/gitea generate secret JWT_SECRET > '/etc/gitea/jwt_secret'
    /usr/bin/chown "${USER}":"${GROUP}" '/etc/gitea/jwt_secret'
    /usr/bin/chmod 0440 '/etc/gitea/jwt_secret'
  fi
  if [ ! -f '/etc/gitea/lfs_jwt_secret' ]; then
    /usr/sbin/gitea generate secret LFS_JWT_SECRET > '/etc/gitea/lfs_jwt_secret'
    /usr/bin/chown "${USER}":"${GROUP}" '/etc/gitea/lfs_jwt_secret'
    /usr/bin/chmod 0440 '/etc/gitea/lfs_jwt_secret'
  fi
  if [ ! -f '/etc/gitea/secret_key' ]; then
    /usr/sbin/gitea generate secret SECRET_KEY > '/etc/gitea/secret_key'
    /usr/bin/chown "${USER}":"${GROUP}" '/etc/gitea/secret_key'
    /usr/bin/chmod 0440 '/etc/gitea/secret_key'
  fi
}

[ -f "${DAEMON_CONFIG}" ]

. /lib/lsb/init-functions

[ -x "${DAEMON}" ]

case "${1}" in
  start)
    secrets
    /usr/bin/install --directory --group="${GROUP}" ---mode='0755' --owner="${USER}" "${PIDFOLDER}"
    log_daemon_msg "Starting ${DESC}" "${NAME}"
    if /usr/sbin/start-stop-daemon --quiet \
                                   --start \
                                   --oknodo \
                                   --make-pidfile \
                                   --pidfile "${PIDFILE}" \
                                   --user "${USER}" \
                                   --group "${GROUP}" \
                                   --exec "${DAEMON}" -- "${DAEMON_OPTS}"; then
      log_end_msg 0
    else
      log_end_msg 1
      /usr/bin/test -f "${PIDFILE}" && \
        /usr/bin/rm --force "${PIDFILE}"
    fi
  ;;
  stop)
    log_daemon_msg "Stopping ${DESC}" "${NAME}"
    if /usr/sbin/start-stop-daemon --quiet \
                                   --stop \
                                   --oknodo \
                                   --retry 30 \
                                   --remove-pidfile \
                                   --pidfile "${PIDFILE}" \
                                   --user "${USER}" \
                                   --group "${GROUP}" \
                                   --exec "${DAEMON}"; then
      /usr/bin/test -f "${PIDFILE}" && \
        /usr/bin/rm --force "${PIDFILE}"
      log_end_msg 0
    else
      log_end_msg 1
    fi
  ;;
  restart)
    "${0}" stop
    "${0}" start
  ;;
  status)
    status_of_proc -p "${PIDFILE}" "${DAEMON}" "${NAME}" && \
      exit 0 || \
      exit "${?}"
  ;;
  *)
    echo "Usage: /etc/init.d/${NAME} {start|stop|restart|status}" >&2
    exit 1
  ;;
esac
