#!/bin/bash -e
#
# Copyright (C) 2025       FUSS Project <info@fuss.bz.it>
# Author:                  Simone Piccardi <piccardi@truelite.it>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

#
# Function usage, print help
#
usage () {
    echo "Usage: ./create-public-html [-h] [ [-n] [-u USERNAME] [-g GROUP] ]"
    echo
    echo " Script to create directories for the personal HTML pages"
    echo " of users (served by the fuss-server)"
    echo
    echo "   -h  print help and exit"
    echo "   -u USERNAME "
    echo "      create /home/userdirs/USERNAME directory and the public_html"
    echo "      simbolic link to it in the user home, give an error"
    echo "      when the directory is already existing"
    echo "   -g GROUP "
    echo "      create HTML page directories for all user of GROUP group"
    echo "   -n  dry run, only print commands"
}

#
# Option and argument scan
#
dry=""
while getopts "nhg:u:" option
do
    case $option in
      n) dry="echo"
	 echo "Dry run requested, it will just print the commands"
	 ;;
      g) GROUP=$OPTARG
         ;;
      u) USERNAME=$OPTARG
         ;;
      h) usage
         exit 0
         ;;
      *) echo "Wrong option"
	 usage
	 exit 1
         ;;
    esac
done
shift $(($OPTIND - 1))

#
# Syntax checks
#
if [ "$#" -ne 0 ]; then 
    echo "This script do not need any argument, use just the right option"
    echo
    usage
    exit 1
fi
if [ -n "$USERNAME" -a -n "$GROUP" ]; then 
    echo "You must specify or a user or a group"
    echo
    usage
    exit 1
fi
if [ -z "$USERNAME" -a -z "$GROUP" ]; then 
    echo "You must specify at least a user or a group"
    echo
    usage
    exit 1
fi

# check if members command is available
if ! which members > /dev/null; then
    echo "This script need members command, install it with:"
    echo "  apt install members"
    exit 1
fi

# check if userdirs 
if grep -q UserDir /etc/apache2/mods-enabled/userdir.conf 2>/dev/null; then
    userdirs=$(grep UserDir /etc/apache2/mods-enabled/userdir.conf|
                   head -n 1|awk '{print $2}')
else
    echo "Support for personl HTML page is not enabled,"
    echo "To enable it add this ansible variable definition:"
    echo "  enable_personal_html: yes"
    echo "inside /etc/fuss-server/fuss-server-defaults.yaml and run:"
    echo "  fuss-server upgrade"
    exit 1
fi

# create HTML pages directory for a single username
if [ -n "$USERNAME" ]; then 
    if [ -d $userdirs/$USERNAME ]; then
	echo "ERROR: HTML pages directory $userdirs/$USERNAME already exists"
	echo "exiting"
	exit 1
    fi
    if id $USERNAME &>/dev/null; then
	echo "Creating HTML pages directory for user $USERNAME"
	HOMEDIR=$(eval echo "~$USERNAME")
	if ! [ -d $HOMEDIR ]; then
            echo "ERROR: home directory $HOMEDIR do not exists"
            echo "something really wrong with this user"
            echo "exiting"
            exit 1
        fi
	$dry mkdir $userdirs/$USERNAME
	$dry chmod -R u=rwX,g=rX,o=rX $userdirs/$USERNAME
	$dry chown -R $USERNAME $userdirs/$USERNAME
	$dry ln -sf $userdirs/$USERNAME $HOMEDIR/public_html
	exit 0
    else
	echo "ERROR: user $USERNAME do not exists!"
	exit 1
    fi
fi

# function to create HTML pages directory for a single user
createdir () {
    user=$1
    echo "Creating HTML pages directory for user $user"
    [ -d  $userdirs/$user ] && return
    homedir=$(eval echo "~$user")
    if ! [ -d $homedir ]; then
	echo "ERROR: $user home $homedir do not exists, skipping"
	return
    fi
    $dry mkdir $userdirs/$user
    $dry chmod -R u=rwX,g=rX,o=rX $userdirs/$user
    $dry chown -R $user $userdirs/$user
    $dry ln -sf $userdirs/$user $homedir/public_html    
}

# create HTML pages directory for the users of a group
if [ -n "$GROUP" ]; then
    for i in $(members $GROUP); do
	createdir $i
    done
    exit 0
fi
