version:                2.0

jobs:
  archlinux:
    docker:
      - image:          archlinux/base:latest
    environment:
      LANG:             C
      DISPLAY:          :10
      G_MESSAGES_DEBUG: pulseeffects
    steps:
      - run:            date +%F > /tmp/date
      - run:            |
                        mkdir -p pacman_cache /root/.ccache
                        pacman -Sy --noconfirm
                        # circleci cache needs tar
                        pacman -S tar --noconfirm
      - restore_cache:
          keys:
            - pacman-cache-{{ checksum "/tmp/date" }}
      - run:            |
          pacman -Su --cachedir pacman_cache --noconfirm
          pacman -S --cachedir pacman_cache --noconfirm pkg-config git gcc meson itstool boost appstream-glib gettext gtk3 gtkmm3 glibmm pulseaudio gstreamer gst-plugins-good gst-plugins-bad boost-libs libsigc++ libsndfile libsamplerate zita-convolver libebur128 lilv calf zam-plugins rubberband mda.lv2 lsp-plugins xorg-server-xvfb gawk ccache
          pacman -Sc --cachedir pacman_cache --noconfirm
      - save_cache:
          key:          pacman-cache-{{ checksum "/tmp/date" }}
          paths:
            - pacman_cache
      - run:            rm -rf pacman_cache
      - run:
          name:         Starting Xvfb
          command:      Xvfb $DISPLAY -screen 0 1280x1024x24
          background:   true
      - run:
          name:         Starting dbus
          command:      |
                DBUS=$(dbus-launch | awk -F, '{print $1;exit}') && echo $DBUS
                echo "export $DBUS" >> $BASH_ENV
      - run:
          name:         Starting Pulseaudio
          command:      |
                pulseaudio -D --exit-idle-time=-1
                pactl load-module module-null-sink sink_name=DummyOutput
                pactl load-module module-null-source source_name=DummyInput
                pacmd set-default-source DummyInput
                pacmd set-default-sink DummyOutput
                pactl list sinks && pactl list sources
      - restore_cache:
          keys:
            - ccache-{{ checksum "/tmp/date" }}
      - checkout
      - run:
          name:         Compile and install PulseEffects
          command:      |
                meson build && cd build && ninja -j 8 && ninja -j 8 install
      - save_cache:
          key:          ccache-{{ checksum "/tmp/date" }}
          paths:
            - /root/.ccache
      - run:
          name:         Starting PE in background
          command:      |
                pulseeffects
          background:   true
      - run:
          name:         Waiting PulseEffects initialization
          command:      .circleci/wait_pe.sh
      - run:
          name:         Testing play/pause
          command:      |
                .circleci/enable_all_plugins.sh
                # sink inputs
                gst-launch-1.0 audiotestsrc num-buffers=100 ! queue ! pulsesink
                gst-launch-1.0 audiotestsrc num-buffers=100 ! queue ! pulsesink
                # source outputs
                gst-launch-1.0 pulsesrc num-buffers=100 ! queue ! pulsesink
                gst-launch-1.0 pulsesrc num-buffers=100 ! queue ! pulsesink
      - run:
          name:         Simulating audio applications
          command:      |
                gst-launch-1.0 audiotestsrc ! queue ! pulsesink &
                gst-launch-1.0 pulsesrc ! queue ! pulsesink
          background:   true
      - run:
          name:         Testing plugin enable/disable on the fly
          command:      |
                sleep 5
                .circleci/enable_all_plugins.sh
                sleep 1
                .circleci/disable_all_plugins.sh
                sleep 1
                .circleci/enable_all_plugins.sh
      - run:
          name:         Testing presets
          command:      |
                sleep 5
                cp -v .circleci/presets/soe_preset_disable_all.json /root/.config/PulseEffects/input
                cp -v .circleci/presets/soe_preset_enable_all.json /root/.config/PulseEffects/input
                cp -v .circleci/presets/sie_preset_disable_all.json /root/.config/PulseEffects/output
                cp -v .circleci/presets/sie_preset_enable_all.json /root/.config/PulseEffects/output
                pulseeffects -l soe_preset_disable_all || true
                sleep 1
                pulseeffects -l soe_preset_enable_all || true
                sleep 1
                pulseeffects -l sie_preset_disable_all || true
                sleep 1
                pulseeffects -l sie_preset_enalbe_all || true
      - run:
          name:         Reset Configuration
          command:      |
                sleep 5
                pulseeffects -r || true
      - run:
          name:         Finish PE process
          command:      |
                sleep 5
                pulseeffects -q || true

workflows:
  version:              2
  build:
    jobs:
      - archlinux
