# Maintainer: Maurizio Porrato <maurizio.porrato@gmail.com>
# Contributor: Martchus <martchus@gmx.net>

pkgname=janus-gateway
pkgver=0.6.2
pkgrel=2
pkgdesc="WebRTC gateway"
arch=('i686' 'x86_64')
url="http://janus.conf.meetecho.com/"
license=('GPL3')
depends=('libmicrohttpd' 'jansson' 'libnice' 'openssl' 'libsrtp'
         'sofia-sip' 'libusrsctp-git' 'libwebsockets' 'libevent' 
         'librabbitmq-c' 'opus' 'libogg' 'glib2' 'ding-libs' 'lua>=5.3'
         'ffmpeg' 'libconfig')
makedepends=('gengetopt' 'paho-mqtt-c-git')
provides=(janus-gateway)
backup=(
  etc/janus/janus.eventhandler.mqttevh.jcfg
  etc/janus/janus.eventhandler.rabbitmqevh.jcfg
  etc/janus/janus.eventhandler.sampleevh.jcfg
  etc/janus/janus.jcfg
  etc/janus/janus.plugin.audiobridge.jcfg
  etc/janus/janus.plugin.echotest.jcfg
  etc/janus/janus.plugin.nosip.jcfg
  etc/janus/janus.plugin.recordplay.jcfg
  etc/janus/janus.plugin.sip.jcfg
  etc/janus/janus.plugin.streaming.jcfg
  etc/janus/janus.plugin.textroom.jcfg
  etc/janus/janus.plugin.videocall.jcfg
  etc/janus/janus.plugin.videoroom.jcfg
  etc/janus/janus.plugin.voicemail.jcfg
  etc/janus/janus.transport.http.jcfg
  etc/janus/janus.transport.mqtt.jcfg
  etc/janus/janus.transport.pfunix.jcfg
  etc/janus/janus.transport.rabbitmq.jcfg
  etc/janus/janus.transport.websockets.jcfg
)
install=janus-gateway.install

source=("https://github.com/meetecho/janus-gateway/archive/v${pkgver}.tar.gz"
        janus.service)
sha256sums=('d605c6482d170b760cf917d190a2681362422993070d45c3e368eb973560b17d'
            'cf2b6c8fdcd60ccfa179c4cd207a23ee7edfee90588c2c3847aa6cda418bed7f')

_userid=531

build() {
  cd "$srcdir/$pkgname-$pkgver"
  ./autogen.sh
  ./configure --prefix=/usr --sysconfdir=/etc --disable-docs \
--enable-javascript-commonjs-module --enable-post-processing \
--enable-mqtt --enable-rabbit-mq
  DESTDIR="$pkgdir/" make

  # make sysusers file to run as user janus
  echo "u janus $_userid \"Janus WebRTC gateway user\" /var/lib/janus /bin/bash" > "$srcdir/janus.conf"
}

package() { 
  cd "$srcdir/$pkgname-$pkgver"
  DESTDIR="$pkgdir/" make install configs
  rm -f "$pkgdir/usr/share/janus/certs/mycert.{key,pem}"

  # install systemd unit file to run as user janus
  install -dm0700 -o "$_userid" -g "$_userid" "${pkgdir}/var/lib/janus"
  install -Dm644 "$srcdir/janus.service" -t "${pkgdir}"/usr/lib/systemd/system/
  install -Dm644 "$srcdir/janus.conf" -t "${pkgdir}"/usr/lib/sysusers.d/
} 
