#!/bin/bash

usage() {
	echo "Usage: vprocunhide < /path/to/vprocunhide.conf"
	echo
	echo "Exit codes:"
	echo "  0    no errors"
	echo "  1    some files were changed but errors occured on other ones"
	echo "  2    operation failed on every file"
	exit 0
}

[[ "$1" == "--help" ]] && usage

o=1
p=0

while read path; do
	case "$path" in
		(\#*) continue;;
		(\~*) flags="ADMIN,WATCH,HIDE"; path=${path#\~};;
		(-*)  flags="ADMIN,HIDE";       path=${path#-};;
		(:*)  flags="WATCH,HIDE";       path=${path#:};;
		(!*)  flags="HIDE";             path=${path#!};;
		(+*)  flags="~HIDE";            path=${path#+};;
		(*)   flags="~HIDE";;
	esac

	test -e "${path}" || continue

	recurse=
	[ "${path}" != "${path%/}" ] && recurse=-R

	setattr ${recurse} -f ${flags} "${path}" && p=1 || o=0
done

test $o -ne 0 && exit 0 || \
test $p -ne 0 && exit 1 || \
exit 2
