captainVersion: 4
caproverOneClickApp:
    instructions:
        start: |-
            You will need your own "Invoice App Key", you can get new one with
            `docker run --rm invoiceninja/invoiceninja php artisan key:generate --show`
            Detailed instructions in [Invoice Ninja Repo](https://github.com/invoiceninja/dockerfiles#alternatively-get-started-with-docker-compose)
            Some variables have default values and can be set in App Config of AppName-app. Check https://github.com/invoiceninja/invoiceninja/blob/master/.env.example
        end: >
            Invoice Ninja is deployed, it might take few moments before it's fully started.
            Initial user is: $$cap_invoiceninja_admin_user_email with password $$cap_invoiceninja_admin_pass.
            Some variables have default values and can be set in App Config of $$cap_appname-app

    displayName: Invoice Ninja
    isOfficial: false
    description: Small Business Invoicing & Payments.
    documentation: |-
        Based on https://github.com/invoiceninja/dockerfiles
        - Changed the db backup system to use automysqlbackup

    variables:
        - id: $$cap_invoiceninja_VERSION
          label: Invoice Ninja Version
          defaultValue: '5'
          description: It's a good idea to check for breaking changes between versions
          validRegex: /^([^\s^\/])+$/

        - id: $$cap_invoiceninja_APP_KEY
          label: Invoice App Key
          description: Your own App Key by Invoice Ninja. See description above.
          validRegex: /^base64\:[A-Za-z0-9+\/]+={0,2}$/
        - id: $$cap_invoiceninja_admin_user_email
          label: ADMIN_USER_EMAIL
          defaultValue: admin@example.com
          description: 'Admin user for Invoice Ninja'
          validRegex: /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/

        - id: $$cap_invoiceninja_admin_pass
          label: ADMIN_USER_PASS
          defaultValue: $$cap_gen_random_hex(10)
          description: 'Admin pass for Invoice Ninja'
          validRegex: /.{1,}/

        - id: $$cap_invoiceninja_DB_USERNAME
          label: DB_USERNAME
          defaultValue: ninja
          validRegex: /^([a-zA-Z0-9\-\.\_])+$/

        - id: $$cap_invoiceninja_DB_PASSWORD
          label: DB_PASSWORD
          defaultValue: $$cap_gen_random_hex(10)
          description: 'The password of Invoice Ninjas database user.'
          validRegex: /.{1,}/

        - id: $$cap_invoiceninja_DB_DATABASE
          label: DB_DATABASE
          defaultValue: ninja
          validRegex: /^([a-zA-Z0-9\-\.\_])+$/

        - id: $$cap_invoiceninja_DB_ROOT_PASSWORD
          label: DB_ROOT_PASSWORD
          defaultValue: $$cap_gen_random_hex(10)
          description: 'The password of DB superuser.'
          validRegex: /.{1,}/

        - id: $$cap_invoiceninja_debugmode
          label: Debug Mode
          defaultValue: 'false'
          description: 'Useful for debugging, not suitable for production'
          validRegex: /^(true|false)$/

services:
    $$cap_appname-app:
        restart: always
        depends_on:
            - $$cap_appname-db
        user:
        environment:
            APP_URL: http://$$cap_appname.$$cap_root_domain
            APP_KEY: $$cap_invoiceninja_APP_KEY
            APP_DEBUG: $$cap_invoiceninja_debugmode
            REQUIRE_HTTPS: false
            PHANTOMJS_PDF_GENERATION: false
            PDF_GENERATOR: snappdf
            QUEUE_CONNECTION: database
            DB_HOST: srv-captain--$$cap_appname-db
            DB_PORT: 3306
            DB_DATABASE: $$cap_invoiceninja_DB_DATABASE
            DB_USERNAME: $$cap_invoiceninja_DB_USERNAME
            DB_PASSWORD: $$cap_invoiceninja_DB_PASSWORD
            # TODO defined as variables
            IN_USER_EMAIL: $$cap_invoiceninja_admin_user_email
            IN_PASSWORD: $$cap_invoiceninja_admin_pass
            TRUSTED_PROXIES: 10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
        volumes:
            - $$cap_appname-app-public:/var/www/app/public
            - $$cap_appname-app-storage:/var/www/app/storage
        caproverExtra:
            notExposeAsWebApp: true
            dockerfileLines:
                - FROM invoiceninja/invoiceninja:$$cap_invoiceninja_VERSION
                - USER root
                - RUN mkdir -p /var/www/app/public /var/www/app/storage
                - RUN chown 1500:1500 /var/www/app/*
                - USER 1500

    $$cap_appname-db:
        restart: always
        image: mariadb:10.4
        environment:
            MYSQL_ROOT_PASSWORD: $$cap_invoiceninja_DB_ROOT_PASSWORD
            MYSQL_USER: $$cap_invoiceninja_DB_USERNAME
            MYSQL_PASSWORD: $$cap_invoiceninja_DB_PASSWORD
            MYSQL_DATABASE: $$cap_invoiceninja_DB_DATABASE
        volumes:
            - $$cap_appname-db:/var/lib/mysql
        caproverExtra:
            notExposeAsWebApp: true

    $$cap_appname-server:
        restart: always
        depends_on:
            - $$cap_appname-app
        environment:
            APP_CONTAINER: srv-captain--$$cap_appname-app:9000
        volumes:
            - $$cap_appname-app-public:/var/www/app/public
        caproverExtra:
            # Workaround for hard-coded container name in nginx config
            dockerfileLines:
                - FROM nginx:1.21
                - ADD https://raw.githubusercontent.com/invoiceninja/dockerfiles/5.3.34/config/nginx/in-vhost.conf /etc/nginx/templates/in-vhost.conf.template
                - RUN sed -i 's/app:9000/${APP_CONTAINER}/g' /etc/nginx/templates/in-vhost.conf.template

    $$cap_appname-db-backup:
        restart: always
        image: selim13/automysqlbackup:2.6-9
        depends_on:
            - $$cap_appname-db
        environment:
            USERNAME: root
            PASSWORD: $$cap_invoiceninja_DB_ROOT_PASSWORD
            DBHOST: srv-captain--$$cap_appname-db
            DBEXCLUDE: 'performance_schema information_schema'
            CRON_SCHEDULE: '0 0 * * *'
            EXTRA_OPTS: '--single-transaction'
        volumes:
            - $$cap_appname-db-backup:/backup
        caproverExtra:
            notExposeAsWebApp: true
