#!/bin/bash

die () {
    echo >&2 "$@"
    exit 1
}

case "$1" in
    configure)

        if ! getent group jitsi > /dev/null ; then
            groupadd jitsi
        fi

        if ! getent passwd jibri > /dev/null ; then
            echo "Creating jibri user and group"
            # Create the Jibri user and group
            useradd --system --create-home jibri
            echo "Done creating jibri user and group"
            usermod -a -G jibri,jitsi,audio,video jibri
            echo "jibri user added to audio, video and jibri groups"
        fi

        # Make the directory for the logs and set the permissions
        mkdir -p /var/log/jitsi/jibri
        chgrp jitsi /var/log/jitsi/jibri
        chmod g+w /var/log/jitsi/jibri

        # Move the asoundrc file to the jibri home directory
        if [ -f /etc/jitsi/jibri/asoundrc ] ; then
            mv /etc/jitsi/jibri/asoundrc /home/jibri/.asoundrc
        fi

        # Make the directory for the icewm preferences and move the preferences file into it
        if [ -f /etc/jitsi/jibri/icewm.preferences ] ; then
            mkdir -p /home/jibri/.icewm
            mv /etc/jitsi/jibri/icewm.preferences /home/jibri/.icewm/preferences
        fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

exit 0
