# a community maintained IB API container!
#
# https://github.com/gnzsnz/ib-gateway-docker
#
# For piker we (currently) include some minor deviations
# for some config files in the `volumes` section.
#
# See full configuration settings @
# - https://github.com/gnzsnz/ib-gateway-docker?tab=readme-ov-file#configuration
# - https://github.com/gnzsnz/ib-gateway-docker/discussions/103

services:
  ib_gw_paper:

    # apparently java is a mega cukc:
    # https://stackoverflow.com/a/56895801
    # https://bugs.openjdk.org/browse/JDK-8150460
    ulimits:
      # nproc: 65535
      nproc: 6000
      nofile:
        soft: 2000
        hard: 3000

    # other image tags available:
    # https://github.com/waytrade/ib-gateway-docker#supported-tags
    # image: waytrade/ib-gateway:1012.2i
    image: ghcr.io/gnzsnz/ib-gateway:latest

    restart: 'no'  # restart on boot whenev there's a crash or user clicsk
    network_mode: 'host'

    volumes:
      - type: bind
        source: ./jts.ini
        target: /root/Jts/jts.ini
        # don't let IBC clobber this file for
        # the main reason of not having a stupid
        # timezone set..
        read_only: true

      # force our own IBC config
      - type: bind
        source: ./ibc.ini
        target: /root/ibc/config.ini

      # force our noop script - socat isn't needed in host mode.
      - type: bind
        source: ./fork_ports_delayed.sh
        target: /root/scripts/fork_ports_delayed.sh

      # force our noop script - socat isn't needed in host mode.
      - type: bind
        source: ./run_x11_vnc.sh
        target: /root/scripts/run_x11_vnc.sh
        read_only: true

    # NOTE: an alt method to fill these out is to
    # define an `.env` file in the same dir as
    # this compose file.
    environment:
      TWS_USERID: ${TWS_USERID}
      # TWS_USERID: 'myuser'
      TWS_PASSWORD: ${TWS_PASSWORD}
      # TWS_PASSWORD: 'guest'
      TRADING_MODE: ${TRADING_MODE}
      # TRADING_MODE: 'paper'
      VNC_SERVER_PASSWORD: ${VNC_SERVER_PASSWORD}
      # VNC_SERVER_PASSWORD: 'doggy'

      # TODO, see if we can get this supported like it
      # was on the old `waytrade` image?
      # VNC_SERVER_PORT: '3003'

    # ports:
    #   - target: 4002
    #     host_ip: 127.0.0.1
    #     published: 4002
    #     protocol: tcp

      # original mappings for use in non-host-mode
      # which we won't really need going forward since
      # ideally we just pick the port to have ib-gw listen
      # on **when** we spawn the container - i.e. everything
      # will be driven by a ``brokers.toml`` def.
      # - "127.0.0.1:4001:4001"
      # - "127.0.0.1:4002:4002"
      # - "127.0.0.1:5900:5900"

  # TODO, a masked but working example of dual paper + live
  # ib-gw instances running in a single app run!
  #
  # ib_gw_live:
  #   image: waytrade/ib-gateway:1012.2i
  #   restart: no
  #   network_mode: 'host'

  #   volumes:
  #     - type: bind
  #       source: ./jts_live.ini
  #       target: /root/jts/jts.ini
  #       # don't let ibc clobber this file for
  #       # the main reason of not having a stupid
  #       # timezone set..
  #       read_only: true

  #     # force our own ibc config
  #     - type: bind
  #       source: ./ibc.ini
  #       target: /root/ibc/config.ini

  #     # force our noop script - socat isn't needed in host mode.
  #     - type: bind
  #       source: ./fork_ports_delayed.sh
  #       target: /root/scripts/fork_ports_delayed.sh

  #     # force our noop script - socat isn't needed in host mode.
  #     - type: bind
  #       source: ./run_x11_vnc.sh
  #       target: /root/scripts/run_x11_vnc.sh
  #       read_only: true

  #   # NOTE: to fill these out, define an `.env` file in the same dir as
  #   # this compose file which looks something like:
  #   environment:
  #     TRADING_MODE: 'live'
  #     VNC_SERVER_PASSWORD: 'doggy'
  #     VNC_SERVER_PORT: '3004'
