#!/bin/bash
# info: list directory
# options: USER DIRECTORY
#
# The function lists directory on the file system

user=$1
path=$2

# Checking arguments
if [ -z "$user" ]; then
    echo "Usage: USER [PATH]"
    exit 1
fi

# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
    echo "Error: vesta user $user doesn't exist"
    exit 3
fi

# Checking user homedir
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
    echo "Error: user home directory doesn't exist"
    exit 12
fi

# Checking path
if [ ! -z "$path" ]; then
    rpath=$(readlink -f "$path")
    if [ -z "$(echo $rpath |grep $homedir)" ]; then
    echo "Error: invalid path $dst_dir"
        exit 2
    fi
else
    path=$homedir
fi

# Listing directory
sudo -u $user find "$path" -maxdepth 1 \
    -printf "%y|%m|%TY-%Tm-%Td|%TH:%TM|%u|%g|%s|%P\n" 2>/dev/null

# Exiting
exit $?
