#!/bin/bash

# afio-gui
# ver 0.0.2 (26 Nov 2010)
#	allow multi dirs files. allow filename contain space
# ver 0.0.1 (25 Nov 2010)
#	allow afio compress one dir.


MES=()
MES[0]="Make Archive at HOME Dir."							# home
MES[1]="Do not contain the top directory."						# nodir
MES[2]="Follow symbolic links, treating them as ordinary files and directories."	# -h
MES[3]="Write file contents with each hard link."					# -l
#
MES[4]="  Use absolute paths"								# abspath
MES[5]="  Do not turn absolute paths into relative paths."				# -A
MES[6]="  Archive as root."								# sudo
MES[7]="Always contain the top directory, if set or unsest.<----!!!"
MES[8]="  Already you are root, if set or unsest.          <----!!!"
MES[9]="  Always use absolute paths, if set or unset.      <----!!!"
#
MES[10]=""
MES[11]="Use gzip instead of lzop."							# -Z
MES[12]="Use bzip2 instead of lzop."							# -Z -P bzip2
MES[13]="No compress the files."							# nocomp
MES[14]="Refuse to create  an  archive  that is incompatible with cpio."		# -5
MES[15]="Write archive with the 'extended ASCII' format headers."			# -4
#
MES[20]=""
MES[21]="  Fastest with least compression.(default: slowest with best)"			# -G 1
MES[22]="  Medium speed with medium compression."					# -G 6
#
MES[31]=""
MES[32]=""
#
MES[41]="Do not process files match wildcard pattern."					# -Y
#
EVAL="eval"
SUDO="sudo"
#if which gksu ; then SUDO="gksu" ; fi  >/dev/null
COMMAND="afio -o"
OPTION1=""
OPTION2="-Z -P lzop"
OPTION3="-G 9"
OPTION4=""
ARCFILE="XXX.afio.lzo"
NODIR="False"
ABSPATH="False"
SOURCE=()
SOURCE2=()
#
NUM=$#
DIRNAME=`dirname "$1"`
#if [ "${DIRNAME}" == "" ] ; then DIRNAME="." ; fi
DIRNAME=`(cd "${DIRNAME}";pwd)`
BASENAME=`basename "$1"`
if [ "${NUM}" == "1" ] ; then
   NAME="${BASENAME}"
   SOURCE[0]="${BASENAME}"
   SOURCE2[0]="$1"
 else
   NAME="${BASENAME}_${NUM}"
   MES[1]="${MES[7]}"
   i=0
   while [ "$1" != "" ] ; do
     SOURCE[${i}]=`basename "$1"`
     SOURCE2[${i}]="$1"
     DIRNAME2=`dirname "$1"`
     DIRNAME2=`(cd "${DIRNAME2}";pwd)`
     if [ "${DIRNAME}" != "${DIRNAME2}" ] ; then
       MES[4]=${MES[9]}
       ABSPATH="True"
     fi
     let i="${i} + 1"
     shift 1
   done
fi
USER=`whoami`
if [ "${USER}" == "root" ] ; then
   MES[6]="${MES[8]}"
fi
ARCFILE="${NAME}.afio.lzo"
ARCPATH="${DIRNAME}"
SUBMENU=$(zenity --list --checklist --separator=" " --hide-column=2 \
                   --title="Afio Archive Name [${ARCFILE}] to [${ARCPATH}]" \
                   --width=500 --height=400 \
                   --column=" " --column=" " --column="Option MENU" \
            "--" "home"		"${MES[0]} [${HOME}]" \
            "--" "nodir"	"${MES[1]}"\
            "--" "-h"		"${MES[2]}" \
            "--" "-l"		"${MES[3]}" \
            "--" "abspath"	"${MES[4]}" \
            "--" "-A"		"${MES[5]}" \
            "--" "sudo"		"${MES[6]}" \
            "--" "-Z"		"${MES[11]} [${NAME}.afio.gz]" \
            "--" "bz2"		"${MES[12]} [${NAME}.afio.bz2]" \
            "--" "nocomp"	"${MES[13]} [${NAME}.afio]" \
            "--" "-5"		"${MES[14]} [${NAME}.cpio]" \
            "--" "-4"		"${MES[15]} [${NAME}.afio]" \
            "--" "-G 1"		"${MES[21]}" \
            "--" "-G 6"		"${MES[22]}" \
	) || exit
for i in ${SUBMENU} ; do
      case ${i} in
         "home")ARCPATH="${HOME}";;
         "abspath")ABSPATH="True";;
         "nodir")NODIR="True";;
         "sudo")if [ "${USER}" != "root" ] ; then
			if [ "${SUDO}" != "" ] ; then
				${SUDO} "$0" "$@" 
			else
				zenity --error --text="No sudo method."
				exit
			fi
		fi;;
         "-Z")OPTION2="-Z";			ARCFILE="${NAME}.afio.gz";;
         "bz2")OPTION2="-Z -P bzip2";		ARCFILE="${NAME}.afio.bz2";;
         "nocomp")OPTION2="";			ARCFILE="${NAME}.afio";;
         "-5")OPTION2="-5";			ARCFILE="${NAME}.cpio";;
         "-4")OPTION2="-4";			ARCFILE="${NAME}.afio";;
         "-G 1" | "-G 6")OPTION3="${OPTION1} ${i}";;
         *)OPTION1="${OPTION1} ${i}";;
      esac
done
if [ "${NODIR}" == "True" ] ; then		# Do not contain the top dir
  cd "${SOURCE2[0]}" && { find | ${COMMAND} ${OPTION1} ${OPTION2} ${OPTION3} "${ARCPATH}/${ARCFILE}" ; }
elif [ "${ABSPATH}" == "True" ] ; then	# Follow path from root
  for NN in ${!SOURCE2[@]} ; do find "${SOURCE2[${NN}]}" ; done | ${COMMAND} ${OPTION1} ${OPTION2} ${OPTION3} "${ARCPATH}/${ARCFILE}"
else					# Normal archive
  cd "${DIRNAME}" && { for NN in ${!SOURCE[@]} ; do find "${SOURCE[${NN}]}" ; done | ${COMMAND} ${OPTION1} ${OPTION2} ${OPTION3} "${ARCPATH}/${ARCFILE}" ; }
fi