#!/usr/bin/env sh
# Gerbera - https://gerbera.io/
#
# docker-entrypoint.sh - this file is part of Gerbera.
#
# Copyright (C) 2021-2025 Gerbera Contributors
#
# Gerbera is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# Gerbera is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# NU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gerbera.  If not, see <http://www.gnu.org/licenses/>.
#
# $Id$

_UID=${UID-$IMAGE_UID}
_GID=${GID-$IMAGE_GID}

if [ "${_UID}" -ne "$IMAGE_UID" -a "${_UID}" -gt 0 ]; then
  sudo usermod -u "${_UID}" "$IMAGE_USER"
fi

if [ "${_GID}" -ne "$IMAGE_GID" -a "${_GID}" -gt 0 ]; then
  sudo groupmod -g "${_GID}" "$IMAGE_GROUP" || usermod -a -G "${_GID}" "$IMAGE_USER"
fi

if [ ! -d /var/run/gerbera/.config/gerbera ]; then
  # Make default config dir (only applies of config was generated by older version)
  sudo mkdir -p /var/run/gerbera/.config/gerbera
fi

if [ ! -f /var/run/gerbera/config.xml ]; then
  if [ ! -d /var/run/gerbera/ -o "$(id -u)" -ne '0'  ]; then
    sudo mkdir -p /var/run/gerbera
    sudo chown "${_UID}:${_GID}" /var/run/gerbera
  fi

  # Generate a config file with home set
  gerbera --create-config --home /var/run/gerbera > /var/run/gerbera/config.xml

  # Automatically scan /content with inotify (for a volume mount)
  sed 's|<import hidden-files="no">|<import hidden-files="no">\n\
    <autoscan use-inotify="yes">\n\
    <directory location="/mnt/content" mode="inotify" \
               recursive="yes" hidden-files="no"/>\n\
    </autoscan>|' -i /var/run/gerbera/config.xml

  # Add directory for custom JavaScript scripts
  sed 's|</common>|</common>\
        <custom>/mnt/customization/js</custom>|' -i /var/run/gerbera/config.xml

  # Allow customization of Gerbera configuration file
  if [ -x /mnt/customization/shell/gerbera_config.sh ]; then
    . /mnt/customization/shell/gerbera_config.sh
  fi
fi

if [ -e /dev/video10 ]; then
  # Add permission to all users on /dev/video10 device
  sudo chown root:video /dev/video10
fi

if [ -e /dev/video11 ]; then
  # Add permission to all users on /dev/video11 device
  sudo chown root:video /dev/video11
fi

if [ -e /dev/video12 ]; then
  # Add permission to all users on /dev/video12 device
  sudo chown root:video /dev/video12
fi

if [ -e /dev/dri ]; then
  # Add permission to all users on /dev/dri device
  sudo chown root:video /dev/dri
fi

GRB_CONFIG_USER=$IMAGE_USER
if [ -f  /var/run/gerbera/config.xml ]; then
    GRB_CONFIG_USER=$(sudo stat -c "%u" /var/run/gerbera/config.xml)
fi
sudo chown -R $IMAGE_USER:$IMAGE_GROUP /var/run/gerbera
if [ -f  /var/run/gerbera/config.xml ]; then
    sudo chown $GRB_CONFIG_USER /var/run/gerbera/config.xml
    sudo chmod g+r /var/run/gerbera/config.xml
fi

# If we are root, chown home and drop privs
if [[ "$3" =~ "^gerbera " || "$1" == "gerbera" ]]; then
  if [ "$(id -u)" -eq '0' ]; then
    echo "Root DEF"
    if ! (command su-exec 2>&1) > /dev/null
    then
        sudo touch /var/run/gerbera/run.sh
        sudo chown $IMAGE_USER:$IMAGE_GROUP /var/run/gerbera/run.sh
        sudo chmod 777 /var/run/gerbera/run.sh
        sudo echo $@ > /var/run/gerbera/run.sh
        exec su - $IMAGE_USER -c "/var/run/gerbera/run.sh"
    else
        exec su-exec $IMAGE_USER "$@"
    fi
    exit
  fi
fi

# Otherwise run as the user provided
if [ "$1" = "--" ]; then
    shift
fi
exec "$@"
