#!/bin/bash
# info: search file or directory
# options: USER OBJECT [PATH]
#
# The function search files and directories on the file system

user=$1
object=$2
path=$3

# Checking arguments
if [ -z "$object" ]; then
    echo "Usage: USER OBJECT [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" -name "$object" \
    -printf "%y|%m|%TY-%Tm-%Td|%TH:%TM|%u|%g|%s|%P\n" 2>/dev/null
#    -printf "%y|%m|%TY-%Tm-%Td|%TH:%TM:%TS|%u|%g|%s|%P\n" 2>/dev/null

# Exiting
exit $?