#!/bin/bash
##   This program is a 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; either version 2, or (at your option)
##   any later version.
##   This program is distributed in the hope that it will be useful,
##   but WITHOUT ANY WARRANTY

Text="View/cahnge attributes for: '$1'\n"
if [ -d "$1" ] ; then
  Attr=(`cd "$1" ; /usr/bin/lsattr -d`)
elif [ -e "$1" ] ; then
  Attr=(`/usr/bin/lsattr "$1"`)
else
  exit 1 
fi

if [ "$UID" = "0" ] ; then
  echo
elif [ -O "$1" ] ; then
  Text="${Text}You are not root, so you can't change some attributes(i,a,j).\n"
else
  Text="${Text}You are not owner, so you can't change attributes.\n"
fi
Text="${Text}Attributes:"

Str=${Attr[0]}
echo $Str 
TF=(FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE)
for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 ; do
  ch=${Str:$i:1}
  #echo $ch
  if [[ $ch != '-' ]] ; then
    TF[i]='TRUE' 
  fi
done
Sym=(s u S D i a d A c j ? t T) # '?' means 'Unknown'
Desc[0]='Secure_delete'					#s
Desc[1]='Undeletable'					#u
Desc[2]='Synchronous_update'				#S
Desc[3]='Synchronous_directory_update'			#D
Desc[4]='Immutable'					#i
Desc[5]='Append_only'					#a
Desc[6]='No_dump'					#d
Desc[7]='Dont_update_atime(access_time)'		#A
Desc[8]='Compress'					#c
Desc[9]='Data_journal'					#j
Desc[10]='Unknown'					#
Desc[11]='No_definition!'				#t
Desc[12]='No_definition!'				#T

#only root can change these: a,i,j
#can not cahnge with chattr: E,X,Z (used by the experimental compression patches)
#only for directorys: D,T(?)

ZenityOPt=''
if [ -d "$1" ] ; then
  NumList="0 1 2 3 4 5 6 7 8 9   11 12"
else
  NumList="0 1 2   4 5 6 7 8 9   11 12"
fi
for i in $NumList ; do
  if [[ "$i" -lt 9 ]] ; then
    Order=0$[i+1]
  else
    Order=$[i+1]
  fi
  ZenityOPt="${ZenityOPt} ${TF[i]} ${Sym[i]} ${Desc[i]} $Order"
done


Ans=`zenity --title 'lsattr / chattr' --list --checklist --column choix --column Symbol --column Description --column Order --text "$Text" $ZenityOPt --width 600 --height 500 2>&1 || echo CANCELED`
if [ "$Ans" = "CANCELED" ] ; then
  exit 0
fi
echo "Nes attr: $Ans"
i=0
NewAtr=''
while [ 1 ] ; do
  S=${Ans:$i:1}
  case $S in
   '' ) break
     ;;
   '|' ) i=$[i+1] ; continue
     ;;
   * )   NewAtr=${NewAtr}$S  ; i=$[i+1]
     ;;
  esac
done
echo $NewAtr
/usr/bin/chattr =$NewAtr "$1"
#zenity --error --text "$Out"

