#!/usr/bin/env bash

export TERM="xterm-256color"

WWW_USER="www-data"
WWW_DIR="/var/www/motionui"
DATA_DIR="/var/lib/motionui"
MOTION_CAPTURES_DIR="/var/lib/motion"
MOTION_CONFIG_DIR="/usr/var/lib/motion"
CONFIRM=""
PWD=$(pwd)

# Colors
GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
YELLOW=$(tput setaf 3)
RESET=$(tput sgr0)

# Detecting user
if [ "$(id -u)" -ne "0" ];then
    echo -e "\n${YELLOW}Must be executed with root${RESET}\n"
    exit
fi


# Print help
function help
{
    echo -e "\n   Available parameters:"
    echo -e "   -p | --set-permissions  ➤  Set necessary permissions on Motion-UI directories and files."
}

# Set correct permissions on all directories and files used by motionui
function permissions
{
    # Check if directories and files exist
    if [ ! -d "$WWW_DIR" ];then
        echo -e "[$YELLOW ERROR $RESET] '$WWW_DIR' web directory not found."
        exit 1
    fi
    if [ ! -d "$DATA_DIR" ];then
        echo -e "[$YELLOW ERROR $RESET] '$DATA_DIR' data directory not found."
        exit 1
    fi

    echo "["$(date +"%a %b %-d %H:%M:%S")"][INF] Setting permissions... "

    # Make sure motion captures directory exists
    mkdir -p /var/lib/motion

    # Make sure log directory exists and is writable for services to start
    mkdir -p /var/lib/motionui/logs

    # Make sur /run/go2rtc exists
    mkdir -p /run/go2rtc
    chown -R www-data:www-data /run/go2rtc
    chmod 775 /run/go2rtc

    # Make sure owner and group and permissions are correct on parent directories
    chown www-data:www-data "$WWW_DIR" "$DATA_DIR"
    chmod 770 "$WWW_DIR" "$DATA_DIR" "$MOTION_CONFIG_DIR" "$MOTION_CAPTURES_DIR"   

    # Now go deeper and set permissions recursively
    chown -R www-data:www-data "$WWW_DIR" > /dev/null 2>&1
    chown -R www-data:www-data "$DATA_DIR" > /dev/null 2>&1
    chown -R www-data:www-data "$MOTION_CONFIG_DIR" > /dev/null 2>&1
    chown -R www-data:www-data "$MOTION_CAPTURES_DIR" > /dev/null 2>&1
    chown -R www-data:www-data /usr/lib/motion /usr/var/lib/motion /var/run/motion

    # Set permissions for video devices, if any
    if ls /dev/video* > /dev/null 2>&1; then
        chown root:www-data /dev/video*
    fi

    # Permissions on web directory
    find "$WWW_DIR" -type d -exec chmod 0750 {} \; > /dev/null 2>&1 &
    find "$WWW_DIR" -type f -exec chmod 0640 {} \; > /dev/null 2>&1 &

    # Permissions on data directory (except /.gnupg directory)
    find "$DATA_DIR" -type d -exec chmod 0770 {} \; > /dev/null 2>&1 &
    find "$DATA_DIR" -type f -exec chmod 0660 {} \; > /dev/null 2>&1 &

    # Set permissions on motion config dir
    find "$MOTION_CONFIG_DIR" -type f -exec chmod 0660 {} \; > /dev/null 2>&1 &
    find "$MOTION_CONFIG_DIR" -type d -exec chmod 0770 {} \; > /dev/null 2>&1 &

    # Set permissions on motion captures dir
    find "$MOTION_CAPTURES_DIR" -type f -exec chmod 0660 {} \; > /dev/null 2>&1 &
    find "$MOTION_CAPTURES_DIR" -type d -exec chmod 0770 {} \; > /dev/null 2>&1 &
}

echo '
                 __  .__                              .__ 
   _____   _____/  |_|__| ____   ____            __ __|__|
  /     \ /  _ \   __\  |/  _ \ /    \   ______ |  |  \  |
 |  Y Y  (  <_> )  | |  (  <_> )   |  \ /_____/ |  |  /  |
 |__|_|  /\____/|__| |__|\____/|___|  /         |____/|__|
       \/                           \/                    

'

if [ $# -eq 0 ];then
    help
    exit
fi

while [ $# -ge 1 ];do
    case "$1" in
        --help|-help|-h)
            help
            exit
        ;;
        --set-permissions|--permissions|-p)
            permissions
            exit
        ;;
        *)
    esac
    shift
done

exit