apiVersion: v1
kind: ConfigMap
metadata:
  name: gitea-init
  labels:
    app:  gitea-init
data:
  app.ini: |+
    APP_NAME = Gitea: Git with a cup of tea
    RUN_MODE = prod
    RUN_USER = git

    [repository]
    ROOT = /data/git/repositories

    [repository.local]
    LOCAL_COPY_PATH = /data/gitea/tmp/local-repo

    [repository.upload]
    TEMP_PATH = /data/gitea/uploads

    [server]
    APP_DATA_PATH    = /data/gitea
    SSH_DOMAIN       = gitea.{{ .Values.domain }}
    HTTP_PORT        = 3000
    ROOT_URL         = http://gitea.{{ .Values.domain }}/
    DISABLE_SSH      = false
    SSH_PORT         = 22
    SSH_LISTEN_PORT  = 22
    LFS_START_SERVER = true
    LFS_CONTENT_PATH = /data/git/lfs
    DOMAIN           = gitea.{{ .Values.domain }}
    LFS_JWT_SECRET   = dJd7zxw11VRhqCighcaeLvUbxlDC45IK2VNc4TCsZc8
    OFFLINE_MODE     = false

    [database]
    PATH     = /data/gitea/gitea.db
    DB_TYPE  = postgres
    HOST     = postgres:5432
    NAME     = gitea
    USER     = postgres
    PASSWD   = test
    SSL_MODE = disable
    CHARSET  = utf8

    [indexer]
    ISSUE_INDEXER_PATH = /data/gitea/indexers/issues.bleve

    [session]
    PROVIDER_CONFIG = /data/gitea/sessions
    PROVIDER        = file

    [picture]
    AVATAR_UPLOAD_PATH            = /data/gitea/avatars
    REPOSITORY_AVATAR_UPLOAD_PATH = /data/gitea/repo-avatars
    DISABLE_GRAVATAR              = false
    ENABLE_FEDERATED_AVATAR       = true

    [attachment]
    PATH = /data/gitea/attachments

    [log]
    ROOT_PATH = /data/gitea/log
    MODE      = file
    LEVEL     = info

    [security]
    INSTALL_LOCK   = true
    SECRET_KEY     = F0x0GV8YmJ02wVzDBUKldlCPnnt4xWIDi41Bp3nYc1pJac0gKnZdU3lELku14ebk
    INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE1ODIwMjM5OTN9.hhV2-a4NWxdtq7zRvN-LN166ap9TAKnrHy0h-qHt5Do

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

    [oauth2]
    JWT_SECRET = 26235oZvtjxLx1AKdXiDUQvQGIDU2L7vhTakiY7zKEg

    [mailer]
    ENABLED = false

    [openid]
    ENABLE_OPENID_SIGNIN = false
    ENABLE_OPENID_SIGNUP = false

    [cron.update_mirrors]
    SCHEDULE: @every 2m

    [mirror]
    DEFAULT_INTERVAL: 2m
    MIN_INTERVAL: 2m

    [cron.sync_external_users]
    SCHEDULE: @every 10m
    UPDATE_EXISTING: true
  
  create-admin.sh: |+
    #!/bin/sh
    echo "Create admin for gitea..."
    sleep 5
    times=1
    while [ $times -le 36 ]; do
      API_RESPONSE_CODE=$(curl -s -w %{http_code} -o /dev/null -u {{ .Values.gitea.admin.username }}:{{ .Values.gitea.admin.password }} http://localhost:3000/api/v1/user)
      echo "Response: " $API_RESPONSE_CODE
      if [  "$API_RESPONSE_CODE" == "403" ]; then
        /usr/local/bin/gitea admin user create \
          --email {{ .Values.gitea.admin.email }} \
          --username {{ .Values.gitea.admin.username }} \
          --password {{ .Values.gitea.admin.password }} \
          --admin --must-change-password=false --config /data/gitea/conf/app.ini
        echo "User created"
        curl -v -X POST "http://localhost:3000/api/v1/admin/users/{{ .Values.gitea.admin.username }}/orgs" \
          -u {{ .Values.gitea.admin.username }}:{{ .Values.gitea.admin.password }} \
          -H "Content-Type: application/json" \
          -H  "accept: application/json" \
          --data '{"username": "kdl"}'
        echo "Organization KDL created"
        exit 0
      elif [ "$API_RESPONSE_CODE" == "200"  ]; then
       echo "User exist"
       exit 0
      fi
      echo "Retrying in a five seconds"
      times=$(( times + 1 ))
      sleep 5
    done
    echo "Create admin for gitea failed!"
    exit 1
