name: ossec-hids
version: latest
version-script: cat $SNAPCRAFT_STAGE/version
summary: Security tool.
description: An Open Source Host-based Intrusion Detection System that performs log analysis, file integrity checking, policy monitoring, rootkit detection, real-time alerting and active response. 
  

grade: devmode
base: core18

architectures:
  - build-on: amd64

parts:
  gnome:
    plugin: nil
    build-packages:
      - software-properties-common
    override-pull: |
      add-apt-repository -y ppa:ubuntu-desktop/gnome-3-28
      apt -y update
      apt -y upgrade

  ossec-hids:
    after:
      - gnome
    plugin: dump
    source:
      # ossec-hids is only available for i386 (x86) and amd64 (x86_64),
      # so fail the build on other architectures.
      - on amd64: https://updates.atomicorp.com/ossec-hids-agent_3.3.0-7681bionic_amd64.deb
      
    source-type: deb
    override-build: |
      ARCHITECTURE=$(dpkg --print-architecture)
      if [ "${ARCHITECTURE}" = "amd64" ]; then
        DEB_API="https://updates.atomicorp.com/ossec-hids-agent_3.3.0-7681bionic_amd64.deb"
      else
        echo "ERROR! ossec-hids agent produces debs for amd64. Failing the build here."
        exit 1
      fi

      DEB_URL=$(curl -w "%{url_effective}\n" -I -L -s -S "${DEB_API}" -o /dev/null)
      VERSION=$(echo "${DEB_URL}" | cut -d'_' -f2)
      echo $VERSION > $SNAPCRAFT_STAGE/version

     - on amd64: https://updates.atomicorp.com/ossec-hids-server_3.3.0-6515bionic_amd64.deb
      
    source-type: deb
    override-build: |
      ARCHITECTURE=$(dpkg --print-architecture)
      if [ "${ARCHITECTURE}" = "amd64" ]; then
        DEB_API="https://updates.atomicorp.com/ossec-hids-server_3.3.0-6515bionic_amd64.deb"
      else
        echo "ERROR! ossec-hids server produces debs for amd64. Failing the build here."
        exit 1
      fi

      DEB_URL=$(curl -w "%{url_effective}\n" -I -L -s -S "${DEB_API}" -o /dev/null)
      VERSION=$(echo "${DEB_URL}" | cut -d'_' -f2)
      echo $VERSION > $SNAPCRAFT_STAGE/version
    
      snapcraftctl build
    build-packages:
      - curl
      - dpkg
      - sed
    stage-packages:
      - fcitx-frontend-gtk3
      - gvfs-libs
      - libasound2
      - libgconf-2-4
      - libglib2.0-bin
      - libgnome-keyring0
      - libgtk-3-0
      - libnotify4
      - libnspr4
      - libnss3
      - libpcre3
      - libpulse0
      - libsecret-1-0
      - libxss1
      - libxtst6
      - yad
      - zlib1g
    prime:
      - -usr/share/doc
      - -usr/share/fonts
      - -usr/share/icons
      - -usr/share/lintian
      - -usr/share/man

  wrapper:
    after:
      - ossec-hids
    plugin: dump
    source: files/

apps:
  ossec-hids:
    command: bin/wrapper $SNAP/usr/share/ossec/bin/ossec
    desktop: usr/share/applications/ossec.desktop
    environment:
      # Fallback to XWayland if running in a Wayland session.
      DISABLE_WAYLAND: 1
      GSETTINGS_SCHEMA_DIR: $SNAP/usr/share/glib-2.0/schemas

  url-handler:
    command: bin/wrapper $SNAP/usr/share/ossec/bin/ossec --open-url
    desktop: usr/share/applications/ossec-url-handler.desktop
    environment:
      # Fallback to XWayland if running in a Wayland session.
      DISABLE_WAYLAND: 1
      GSETTINGS_SCHEMA_DIR: $SNAP/usr/share/glib-2.0/schemas
