#!/bin/bash

# picaper
#
# Permissions converter.
#
# Copyright (c) 2013-2026: Alexis Puente Montiel   < pica (a) picalibre.org >
#
# Licensed according to GNU AGPL version 3.0.
#
# It is libre/free software; you can use, redistribute and/or modify it according to the terms of GNU AGPL as published by GNU, version 3.0, 19 November 2007.
#
# It is distributed in the hope that it will be useful, but without any warranty. Read GNU AGPL version 3.0 for additional details.
#
# A copy of GNU AGPL version 3.0 is available at /usr/share/doc/<software-package-name>/agpl-3.0.txt (additionally on Internet as text at https://www.gnu.org/licenses/agpl-3.0.txt and as HTML at https://www.gnu.org/licenses/agpl-3.0-standalone.html ).
#
# Note: Additionally to the official e-mails, picalibre.org is strictly the only official site for this software project, please consider using it to download, report bugs and contribute.
#
# Depends: bash, coreutils, gawk | mawk | original-awk, grep, sed


### SCRIPT VARIABLES ########################################

CNAME="picaper"
VERSION="1.3.1"
TITLE="'picaper'"

# Translations
if [ "$LANG" = "" ] ; then export $(cat /etc/default/locale | grep -a 'LANG=') ; fi
TEXTDOMAIN=picainfo
TEXTDOMAINDIR=/usr/share/locale/

## Write errors to log
ERRORLOG="$HOME/.${CNAME}.log"
#if [ -e "$ERRORLOG" ] ; then
#	mv -f $ERRORLOG ${ERRORLOG}.ant
#fi
#if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi

for i in /etc/pica-global.dist /etc/pica-global.orig /etc/pica-global /etc/pica-global.local ~/.pica-global ~/pica-global ; do
	if [ -f "$i" ] ; then
	cat "$i"
	source "$i"
	source <(cat $i | sed -e "s/=\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)/=Y/g" -e "s/=\(No\|no\|n\)/=N/g" -e "s/=\"\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)\"/=Y/g" -e "s/=\"\(No\|no\|n\)\"/=N/g")
	fi
done
if [ "$DEBUG" = "Y" ] ; then
	set -xv
	DEBUG="Y"
else
	ERRORLOG="/tmp/.${CNAME}_$(id -nu).log"
	if [ -e "$ERRORLOG" ] ; then mv -f $ERRORLOG ${ERRORLOG}.ant ; fi
	if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi
fi
if [ "$DEBUG" = "" ] ; then DEBUG="N" ; fi
if [ "$DEBUG" != "N" ] ; then
exec > >(tee -a "$ERRORLOG") 2>&1
echo "$0" "$*" >> "$ERRORLOG"
echo "env:" >> "$ERRORLOG"
env >> "$ERRORLOG"
echo "set:" >> "$ERRORLOG"
set >> "$ERRORLOG"
#else
#exec 2>>"$ERRORLOG"
fi

# Description:
BDESCRIP=$"Permissions converter."
LDESCRIP=$"$TITLE is a permissions converter."

# Documentation:
docu_info () {
echo "$CNAME ($VERSION) - $BDESCRIP"
echo 
echo $"Usage:" $CNAME [$"OPTIONS"] [$"FILE"]
echo 
echo $"Options:"
echo -e "$ODESCRIP"
echo 
echo $"'man $CNAME' for more information."
echo 
}
ODESCRIP=" -x""\t"$"Show help documentation."

if [ "$1" = "-x" ] ; then
while getopts x OPTION ; do
	case $OPTION in
		x )   docu_info ; exit 0 ;;
	esac
done
fi


### SCRIPT ########################################

if [ "$(echo $*)" = "" ] ; then
	echo $"ERROR: No value or file provided."
	exit 1
elif [ -f "$*" ] ; then
	LIST=$(echo $(cat "$*" | awk '{print $1}'))
else
	LIST=$(echo "$*")
fi

TESTN=$(echo "$LIST" | grep -aEi "[0-9]")
TESTL=$(echo "$LIST" | grep -aEi "[a-z-]")

if [ "$TESTN" = "" ] && [ "$TESTL" != "" ] ; then
	for j in $LIST ; do i=$(echo "$j" | sed "s|+$||g") ; printf '%b' "$i" ; unset V0 V1 V2 V3 ; N=0 ; for t in $(echo $i | tr "[A-Z]" "[a-z]" | sed 's| ||g' | grep -aEio ".........$" | sed 's|...|&\n|g') ; do N=$(expr $N + 1 ) ; declare "V$N=$(expr $(echo $t | sed -e 's|r| + 4 |g' -e 's|w| + 2 |g' -e 's|[x,s,t]| + 1 |g' -e 's|-| + 0 |g'))" ; if [ "$N" -eq 1 ] && [ "$(echo $t | grep -aEio S )" != "" ] ; then V0=$(expr $V0 + 4 ) ; elif [ "$N" -eq 2 ] && [ "$(echo $t | grep -aEio S )" != "" ] ; then V0=$(expr $V0 + 2 ) ; elif [ "$N" -eq 3 ] && [ "$(echo $t | grep -aEio T )" != "" ] ; then V0=$(expr $V0 + 1 ) ; else V0=$(expr $V0 + 0 ) ; fi ; done ; printf "\t$V0$V1$V2$V3\n" ; done
elif [ "$TESTN" != "" ] && [ "$TESTL" = "" ] ; then
	for j in $LIST ; do i=$(echo "$j" | sed "s|+$||g") ; printf '%b' "$i\t" ; unset V0 V1 V2 V3 ; N=0 ; for x in $(echo $i | sed 's| ||g' | grep -aEio "...$" ) ; do N=$(expr $N + 1 ) ; declare "V$N=$(echo $x | sed -e 's|0|---|g' -e 's|1|--x|g' -e 's|2|-w-|g' -e 's|3|-wx|g' -e 's|4|r--|g' -e 's|5|r-x|g' -e 's|6|rw-|g' -e 's|7|rwx|g')" ; done ; if [ "$(printf $i | sed 's| ||g' | wc -m)" -eq 4 ] ; then V0=$(echo $i | sed 's| ||g' | grep -aEio "^.") ; if [ "$V0" -eq 0 ] ; then echo $V1$V2$V3 ; elif [ "$V0" -eq 1 ] ; then echo $V1$V2$V3 | sed 's|^\(.\{8\}\)x|\1t|' | sed 's|^\(.\{8\}\)-|\1T|' ; elif [ "$V0" -eq 2 ] ; then echo $V1$V2$V3 | sed 's|^\(.\{5\}\)x|\1s|' | sed 's|^\(.\{5\}\)-|\1S|' ; elif [ "$V0" -eq 3 ] ; then echo $V1$V2$V3 | sed 's|^\(.\{8\}\)x|\1t|' | sed 's|^\(.\{8\}\)-|\1T|' | sed 's|^\(.\{5\}\)x|\1s|' | sed 's|^\(.\{5\}\)-|\1S|' ; elif [ "$V0" -eq 4 ] ; then echo $V1$V2$V3 | sed 's|^\(.\{2\}\)x|\1s|' | sed 's|^\(.\{2\}\)-|\1S|' ; elif [ "$V0" -eq 5 ] ; then echo $V1$V2$V3 | sed 's|^\(.\{8\}\)x|\1t|' | sed 's|^\(.\{8\}\)-|\1T|' | sed 's|^\(.\{2\}\)x|\1s|' | sed 's|^\(.\{2\}\)-|\1S|' ; elif [ "$V0" -eq 6 ] ; then echo $V1$V2$V3 | sed 's|^\(.\{5\}\)x|\1s|' | sed 's|^\(.\{5\}\)-|\1S|' | sed 's|^\(.\{2\}\)x|\1s|' | sed 's|^\(.\{2\}\)-|\1S|' ; elif [ "$V0" -eq 7 ] ; then echo $V1$V2$V3 | sed 's|^\(.\{8\}\)x|\1t|' | sed 's|^\(.\{8\}\)-|\1T|' | sed 's|^\(.\{5\}\)x|\1s|' | sed 's|^\(.\{5\}\)-|\1S|' | sed 's|^\(.\{2\}\)x|\1s|' | sed 's|^\(.\{2\}\)-|\1S|' ; else echo $V1$V2$V3 ; fi ; else echo $V1$V2$V3 ; fi ; done
else
	echo $"ERROR: Badly formatted input."
	exit 1
fi

exit 0
