# Description: The analytics platform for all your metrics
# URL: https://grafana.com/
# Maintainer: Tim Biermann, tbier at posteo dot de
# Depends on: fontconfig go yarn

name=grafana
version=10.0.3
release=1
source=(https://github.com/grafana/grafana/archive/v$version/$name-$version.tar.gz
  #https://dl.grafana.com/oss/release/grafana-$version.linux-amd64.tar.gz
  grafana.confd grafana.service)

build() {
  export CGO_LDFLAGS="${LDFLAGS}"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOFLAGS="-buildmode=pie -trimpath -modcacherw"

  mkdir "$PKGMK_SOURCE_DIR/gopath" || true
  export GOPATH="$PKGMK_SOURCE_DIR/gopath"

  export PATH="/usr/bin:$PATH"

  cd $SRC/$name-$version

  sed -ri 's,^(\s*data\s*=).*,\1 /var/lib/grafana,' conf/defaults.ini
  sed -ri 's,^(\s*plugins\s*=).*,\1 /var/lib/grafana/plugins,' conf/defaults.ini
  sed -ri 's,^(\s*provisioning\s*=).*,\1 /var/lib/grafana/conf/provisioning,' conf/defaults.ini
  sed -ri 's,^(\s*logs\s*=).*,\1 /var/log/grafana,' conf/defaults.ini

  # backend
  make gen-go
  go run build.go setup
  go run build.go build

  # frontend
  export NPM_CONFIG_PREFIX="$SRC/npm"
  export PATH="$NPM_CONFIG_PREFIX/bin:$PATH"
  export NODE_OPTIONS="--max-old-space-size=16000"

  /usr/bin/yarn install
  NODE_ENV=production /usr/bin/yarn run build

  # install
  install -Dm644 $SRC/$name.confd $PKG/etc/conf.d/$name
  install -Dsm755 bin/linux-amd64/grafana-server $PKG/usr/bin/grafana-server
  install -Dsm755 bin/linux-amd64/grafana-cli $PKG/usr/bin/grafana-cli
  install -Dm644 conf/sample.ini $PKG/etc/grafana.ini
  install -Dm644 conf/defaults.ini $PKG/usr/share/$name/conf/defaults.ini
  install -dm755 $PKG/usr/share/$name
  cp -r public $PKG/usr/share/$name/

  ## install rc file
  install -Dm 755 $SRC/grafana.service $PKG/etc/rc.d/grafana
}
