#!/usr/bin/env bash
#
# Copyright (c) 2024 YunoHost Contributors
#
# This file is part of YunoHost (see https://yunohost.org)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

readonly MEDIA_GROUP=multimedia
readonly MEDIA_DIRECTORY=/home/yunohost.multimedia

# Initialize the multimedia directory system
#
# usage: ynh_multimedia_build_main_dir
ynh_multimedia_build_main_dir() {

    ## Création du groupe multimedia
    groupadd -f $MEDIA_GROUP

    ## Création des dossiers génériques
    mkdir -p "$MEDIA_DIRECTORY"
    mkdir -p "$MEDIA_DIRECTORY/share"
    mkdir -p "$MEDIA_DIRECTORY/share/Music"
    mkdir -p "$MEDIA_DIRECTORY/share/Picture"
    mkdir -p "$MEDIA_DIRECTORY/share/Video"
    mkdir -p "$MEDIA_DIRECTORY/share/eBook"

    # Disable logging to prevent leaking the user list (and anyway it's long and boring)
    local xtrace_enable=$(set +o | grep xtrace)
    set +o xtrace # set +x

    ## Création des dossiers utilisateurs
    for user in $(yunohost user list --output-as json | jq -r '.users | keys[]'); do
        mkdir -p "$MEDIA_DIRECTORY/$user"
        mkdir -p "$MEDIA_DIRECTORY/$user/Music"
        mkdir -p "$MEDIA_DIRECTORY/$user/Picture"
        mkdir -p "$MEDIA_DIRECTORY/$user/Video"
        mkdir -p "$MEDIA_DIRECTORY/$user/eBook"
        ln -sfn "$MEDIA_DIRECTORY/share" "$MEDIA_DIRECTORY/$user/Share"
        # Création du lien symbolique dans le home de l'utilisateur.
        #link will only be created if the home directory of the user exists and if it's located in '/home' folder
        local user_home="$(getent passwd "$user" | cut -d: -f6 | grep '^/home/')"
        if [[ -d "$user_home" ]]; then
            ln -sfn "$MEDIA_DIRECTORY/$user" "$user_home/Multimedia"
        fi
        # Propriétaires des dossiers utilisateurs.
        chown -R "$user" "$MEDIA_DIRECTORY/$user"
    done

    # Re-enable logging
    eval "$xtrace_enable"

    # Default yunohost hooks for post_user_create,delete will take care
    # of creating/deleting corresponding multimedia folders when users
    # are created/deleted in the future...

    ## Application des droits étendus sur le dossier multimedia.
    # Droit d'écriture pour le groupe et le groupe multimedia en acl et droit de lecture pour other:
    setfacl -RnL -m g:$MEDIA_GROUP:rwX,g::rwX,o:r-X "$MEDIA_DIRECTORY" || true
    # Application de la même règle que précédemment, mais par défaut pour les nouveaux fichiers.
    setfacl -RnL -m d:g:$MEDIA_GROUP:rwX,g::rwX,o:r-X "$MEDIA_DIRECTORY" || true
    # Réglage du masque par défaut. Qui garantie (en principe...) un droit maximal à rwx. Donc pas de restriction de droits par l'acl.
    setfacl -RL -m m::rwx "$MEDIA_DIRECTORY" || true
}

# Add a directory in `yunohost.multimedia`
#
# usage: ynh_multimedia_addfolder --source_dir="source_dir" --dest_dir="dest_dir"
#
# | arg: --source_dir= - Source directory - The real directory which contains your medias.
# | arg: --dest_dir= - Destination directory - The name and the place of the symbolic link, relative to `/home/yunohost.multimedia`
#
# This "directory" will be a symbolic link to a existing directory.
ynh_multimedia_addfolder() {

    # ============ Argument parsing =============
    local -A args_array=([s]=source_dir= [d]=dest_dir=)
    local source_dir
    local dest_dir
    ynh_handle_getopts_args "$@"
    # ===========================================

    # Ajout d'un lien symbolique vers le dossier à partager
    ln -sfn "$source_dir" "$MEDIA_DIRECTORY/$dest_dir"

    ## Application des droits étendus sur le dossier ajouté
    # Droit d'écriture pour le groupe et le groupe multimedia en acl et droit de lecture pour other:
    setfacl -RnL -m g:$MEDIA_GROUP:rwX,g::rwX,o:r-X "$source_dir"
    # Application de la même règle que précédemment, mais par défaut pour les nouveaux fichiers.
    setfacl -RnL -m d:g:$MEDIA_GROUP:rwX,g::rwX,o:r-X "$source_dir"
    # Réglage du masque par défaut. Qui garantie (en principe...) un droit maximal à rwx. Donc pas de restriction de droits par l'acl.
    setfacl -RL -m m::rwx "$source_dir"
}

# Add an user to the multimedia group, in turn having write permission in multimedia directories
#
# usage: ynh_multimedia_addaccess user_name
#
# | arg: user_name   - The name of the user which gain this access.
ynh_multimedia_addaccess() {
    groupadd -f $MEDIA_GROUP
    usermod -a -G $MEDIA_GROUP "$1"
}
