#!/bin/sh
# postinst script for liquidsoap
set -e

case "$1" in
  configure) ;;

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

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

if ! getent group liquidsoap > /dev/null; then
  addgroup --system liquidsoap
fi

# Create the new system account
id liquidsoap > dev/null 2>&1 || (
  adduser --system --disabled-password --disabled-login \
    --home /var/cache/liquidsoap --ingroup liquidsoap liquidsoap &&
    usermod --append --groups audio liquidsoap
)

# Add again /usr/share/liquidsoap if user was already created
if ! test -d /usr/share/liquidsoap; then
  mkdir /usr/share/liquidsoap
fi

if ! test -d /var/cache/liquidsoap; then
  mkdir -p /var/cache/liquidsoap
fi

echo "Generating cache for the standard library.."
LIQ_CACHE_SYSTEM_DIR=/var/cache/liquidsoap liquidsoap --cache-only '()'

# Fix directories ownership
chown -R liquidsoap:liquidsoap /var/log/liquidsoap
chmod -R +r /var/log/liquidsoap
chown -R liquidsoap:liquidsoap /var/cache/liquidsoap
chown -R root:root /usr/share/liquidsoap

#DEBHELPER#

exit 0
