name: tautulli
adopt-info: tautulli
summary: A Python based monitoring and tracking tool for Plex Media Server.
description: >
  Tautulli is a 3rd party application that you can run alongside your Plex Media Server to monitor activity and track various statistics. 
  Most importantly, these statistics include what has been watched, who watched it, when and where they watched it, and how it was watched. 
  The only thing missing is "why they watched it", but who am I to question your 42 plays of Frozen. 
  All statistics are presented in a nice and clean interface with many tables and graphs, which makes it easy to brag about your server to everyone else.

base: core22
confinement: strict

architectures:
  - build-on: amd64
  - build-on: arm64
  - build-on: armhf

plugs:
  shared-memory:
    private: true

parts:
  tautulli:
    plugin: dump
    source: .
    stage-packages:
      - python3
      - python3-openssl
      - python3-cryptography
      - python3-setuptools
      - python3-pkg-resources
    build-packages:
      - git
      - python3
      - python3-openssl
      - python3-cryptography
      - python3-setuptools
      - python3-pkg-resources
    override-pull: |
      craftctl default
      git config --global --add safe.directory /data/parts/tautulli/src
      TAG_FULL=$(git describe --tag)
      TAG=$(echo $TAG_FULL | grep -oP '(v\d+\.\d+\.\d+(?>-beta)?)')
      BRANCH=$(git rev-parse --abbrev-ref HEAD)
      COMMIT=$(git rev-parse HEAD)
      if [ "$TAG" = "$TAG_FULL" ]; then
        VERSION=$TAG
      else
        VERSION=$(echo $COMMIT | head -c 7)
      fi
      if [ ! "$VERSION" = "$TAG" ] || [ ! "$VERSION" = "${VERSION%-beta}" ]; then
        GRADE=devel
      else
        GRADE=stable
      fi
      if [ "$VERSION" = "$TAG" ] && [ ! "$VERSION" = "${VERSION%-beta}" ]; then
        BRANCH=beta
      elif [ "$VERSION" = "$TAG" ]; then
        BRANCH=master
      fi
      echo $BRANCH > branch.txt
      echo $COMMIT > version.txt
      craftctl set version="$VERSION"
      craftctl set grade="$GRADE"
    override-prime: |
      craftctl default
      mkdir -p usr/bin
      ln -s /usr/bin/python3 usr/bin/python3

apps:
  tautulli:
    command: usr/bin/python3 $SNAP/Tautulli.py --datadir $SNAP_USER_COMMON/Tautulli --config $SNAP_USER_COMMON/Tautulli/config.ini --quiet --nolaunch
    daemon: simple
    restart-condition: on-abnormal
    restart-delay: 5s
    plugs:
      - home
      - network
      - network-bind
      - removable-media
    environment:
      PYTHONPATH: $SNAP/usr/lib/python3/dist-packages:$PYTHONPATH
      TAUTULLI_SNAP: "True"
