# Declarations for bash commander
if [ "$USER" = "root" ]; then
	# Use brown color to keep user aware of root privileges.
	COMMANDER='no=\33[m:nr=\33[0;37;43m:bo=\33[1;37m:br=\33[1;37;43m:di=\33[0;33m'
	PS1="\[\033[43m\](\h) \W\[\033[m\033[1m\] \\\$\[\033[m\] "
else
	COMMANDER='no=\33[m:nr=\33[0;37;43m:bo=\33[1;37m:br=\33[1;37;43m:di=\33[0;36m'
	PS1="\[\033[44m\](\h) \W\[\033[m\033[1m\] \\\$\[\033[m\] "
fi
declare -x DIALOGRC=/usr/local/etc/bash_dialog

function commander_start_file () {		# Start file
	if [[ `expr "$1" : '[a-zA-Z0-9_./,:@+-]*$'` != "0" ]]; then
		f="$1"
	else
		f="'$1'"
	fi
	case "$1" in
	*.c)		COMMANDER_LINE="${EDITOR:-vi} $f" ;;
	*.h)		COMMANDER_LINE="${EDITOR:-vi} $f" ;;
	*.cpp)		COMMANDER_LINE="${EDITOR:-vi} $f" ;;
	*.tar)		COMMANDER_LINE="tar tvf $f | less" ;;
	*.tar.Z)	COMMANDER_LINE="tar tvzf $f | less" ;;
	*.tar.gz)	COMMANDER_LINE="tar tvzf $f | less" ;;
	*.tgz)		COMMANDER_LINE="tar tvzf $f | less" ;;
	*.tar.bz2)	COMMANDER_LINE="tar tvjf $f | less" ;;
	*.zip)		COMMANDER_LINE="unzip -l $f | less" ;;
	*.rar)		COMMANDER_LINE="unrar vb $f | less" ;;
	*)		if [ -f "$1" -a -x "$1" ]; then
				COMMANDER_LINE="./$f"
				# Switch to line mode
				return 0
			fi ;;
	esac
	# Stay in panel mode
	return 1
}

function commander_f1 () {			# Help
	${DIALOG:-dialog} --title " Help " --cr-wrap --msgbox "\
Cursor Movement                   Delete\n\
   Char left        ^B               Char left        ^H or <BS>\n\
   Char right       ^F               Under cursor     ^D or <Del>\n\
   Start of line    ^A               All line         ^U\n\
   End of line      ^E               To end of line   ^K\n\
\n\
                                  Other\n\
                                     Reread directory F2\n\
Panels                               Select/Unselect  ^T or <Ins>\n\
   Switch panels    ^I or <Tab>      Previous command ^P\n\
   Panels on/off    ^O               Next command     ^N\n\
   Show dot files   ^X a             Run file or\n\
                                     Enter directory  ^M or <Ret>\n\
                                     Use file name    ^J or <LF>\n\
" 0 0
	return 0
}

function commander_f2 () {			# Change directory
	# Select fom list: $HOME or / or another panel,
	# or /mnt/foo or /Volume/bar or /media/xyz
	declare -a args
	args=("$2" "")
	if [ "$3" != "$2" -a "$3" != "/" -a "$3" != "$HOME" ]; then
		args[2]="$3"
		args[3]=""
	fi
	if [ "$HOME" != "$2" -a "$HOME" != "/" ]; then
		args[4]="$HOME"
		args[5]=""
	fi
	if [ "/" != "$2" ]; then
		args[6]="/"
		args[7]=""
	fi
	n=8
	for d in /mnt/* /media/* /Volumes/*; do
		case "$d" in
		*\*)
			;;
		*)
			args[$((n++))]="$d"
			args[$((n++))]=""
			;;
		esac
	done

	exec 3>&1
	value=`${DIALOG:-dialog} --title " Change directory " --menu \
	"Select directory:" 0 0 5 "${args[@]}" 2>&1 1>&3`
	retval=$?
	exec 3>&-
	unset args

	if [ $retval = 0 ]; then
		cd "$value"
		return 1 			# Re-read a directory
	fi
	return 0
}

function commander_f3 () {			# View current file
	if [ -f "$1" -a -r "$1" ]; then
		${VIEWER:-vi -R} "$1"
	fi
	return 0
}

function commander_f4 () {			# Edit current file
	if [ -f "$1" -a -w "$1"  ]; then
		${EDITOR:-vi} "$1"
		return 1			# Re-read a directory
	fi
	return 0
}

function commander_f5 () {			# Copy file(s)
	f="$1"; shift
	d="$1"; shift
	d2="$1"; shift
	if [ $# -gt 0 ]; then
		# Several tagged files/dirs
		n="$#"

		exec 3>&1
		value=`${DIALOG:-dialog} --title " Copy " --inputbox \
			"Copy $n files to:" 0 0 "$d2" 2>&1 1>&3`
		retval=$?
		exec 3>&-
		if [ $retval != 0 ]; then
			return 0
		fi
		# Check if the target already exists
		if [ ! -d "$value" ]; then
			${DIALOG:-dialog} --title " Error " --ok-label "Copying aborted"\
				--msgbox "$value - nonexistent directory"
			return 0
		fi
		all=0
		for f; do
			if [ $all != 1 -a -e "$value/$f" ]; then
				# Target already exists, ask user for overwrite
				${DIALOG:-dialog} --title " Copy " --ok-label "Overwrite" \
					--extra-button --extra-label "All" \
					--no-label "Cancel" \
					--yesno "File exists: $value/$f" 0 0
				case $? in
				0) ;;		# Overwrite
				3) all=1 ;;	# Overwrite All
				*) return 0 ;;	# Cancel
				esac
			fi
			${DIALOG:-dialog} --infobox "Copying $f\nto $value/" 0 0
			cp -p -R -f "$f" "$value"
		done
		return 1			# Re-read a directory
	fi

	# Single file or directory
	exec 3>&1
	value=`${DIALOG:-dialog} --title " Copy " --inputbox \
		"Copy file $f to:" 0 0 "$d2/$f" 2>&1 1>&3`
	retval=$?
	exec 3>&-
	if [ $retval != 0 ]; then
		return 0
	fi

	# Check if the target already exists
	if [ -d "$value" ]; then
		target="$value/$f"
	else
		target="$value"
	fi
	if [ -e "$target" ]; then
		# Target already exists, ask user for overwrite
		${DIALOG:-dialog} --title " Copy " --yes-label "Overwrite" \
			--no-label "Cancel" --yesno "File exists: $target" 0 0
		if [ $? != 0 ]; then
			return 0
		fi
	fi
	${DIALOG:-dialog} --infobox "Copying $f\nto $value" 0 0
	cp -p -R -f "$f" "$value"
	return 1				# Re-read a directory
}

function commander_f6 () {			# Move file(s)
	f="$1"; shift
	d="$1"; shift
	d2="$1"; shift
	if [ $# -gt 0 ]; then
		# Several tagged files/dirs
		n="$#"

		exec 3>&1
		value=`${DIALOG:-dialog} --title " Move " --inputbox \
			"Rename or move $n files to:" 0 0 "$d2" 2>&1 1>&3`
		retval=$?
		exec 3>&-
		if [ $retval != 0 ]; then
			return 0
		fi
		# Check if the target already exists
		if [ ! -d "$value" ]; then
			${DIALOG:-dialog} --title " Error " --ok-label "Renaming aborted"\
				--msgbox "$value - nonexistent directory"
			return 0
		fi
		all=0
		for f; do
			if [ $all != 1 -a -e "$value/$f" ]; then
				# Target already exists, ask user for overwrite
				${DIALOG:-dialog} --title " Move " --ok-label "Overwrite" \
					--extra-button --extra-label "All" \
					--no-label "Cancel" \
					--yesno "File exists: $value/$f" 0 0
				case $? in
				0) ;;		# Overwrite
				3) all=1 ;;	# Overwrite All
				*) return 0 ;;	# Cancel
				esac
			fi
			${DIALOG:-dialog} --infobox "Moving $f\nto $value/" 0 0
			rm -rf "$value/$f"
			mv -f "$f" "$value"
		done
		return 1			# Re-read a directory
	fi

	# Single file or directory
	exec 3>&1
	value=`${DIALOG:-dialog} --title " Move " --inputbox \
		"Rename or move file $f to:" 0 0 "$d2/$f" 2>&1 1>&3`
	retval=$?
	exec 3>&-
	if [ $retval != 0 ]; then
		return 0
	fi

	# Check if the target already exists
	if [ -d "$value" ]; then
		target="$value/$f"
	else
		target="$value"
	fi
	if [ -e "$target" ]; then
		# Target already exists, ask user for overwrite
		${DIALOG:-dialog} --title " Move " --yes-label "Overwrite" \
			--no-label "Cancel" --yesno "File exists: $target" 0 0
		if [ $? != 0 ]; then
			return 0
		fi
	fi
	${DIALOG:-dialog} --infobox "Moving $f\nto $value" 0 0
	rm -rf "$target"
	mv -f "$f" "$value"
	return 1				# Re-read a directory
}

function commander_f7 () {			# Make directory
	exec 3>&1
	value=`${DIALOG:-dialog} --title " Make directory " --inputbox \
		"Create the directory:" 0 0 "" 2>&1 1>&3`
	retval=$?
	exec 3>&-
	if [ $retval != 0 ]; then
		return 0
	fi
	mkdir -p "$value"
	return 1				# Re-read a directory
}

function commander_f8 () {			# Delete file(s)
	f="$1"; shift
	d="$1"; shift
	d2="$1"; shift
	if [ $# -gt 0 ]; then
		# Several tagged files/dirs
		n="$#"
		${DIALOG:-dialog} --title " Delete " --yes-label "Delete" \
			--no-label "Cancel" \
			--yesno "Do you wish to delete $n files?" 0 0
		if [ $? != 0 ]; then
			return 0
		fi
		${DIALOG:-dialog} --infobox "Deleting $n files" 0 0
		rm -rf "$@"
		return 1			# Re-read a directory
	fi

	# Single file or directory
	${DIALOG:-dialog} --title " Delete " --yes-label "Delete" --no-label "Cancel" \
		--yesno "Do you wish to delete: $f" 0 0
	if [ $? != 0 ]; then
		return 0
	fi
	${DIALOG:-dialog} --infobox "Deleting $f" 0 0
	rm -rf "$f"
	return 1				# Re-read a directory
}
