#!/bin/bash
#
# Script to integrate ccrypt with GNOME-nautilus GUI environment
# VF - 2004

# Ccrypt installation path
CCRYPT_PATH=
# User default GNOME-nautilus environment path
NAUTILUS_USER_PATH=$HOME/.gnome2/nautilus-scripts

my_clear()
{
	clear
	echo "ccrypt-gnome2-install - VF - 2004"
	echo "Script to integrate ccrypt with GNOME-nautilus GUI environment"
}

# Check if ccrypt is installed, this means in the current search path
CCRYPT_PATH=`whereis ccrypt | cut -f2 -d: | cut -f2 -d" "`
if [ -z $CCRYPT_PATH ]
then
	my_clear
	echo
	echo "Ccrypt not found in current path!"
	echo "Please check ccrypt installation before proceeding..."
	echo
	exit 1
fi

# Check if installation path exists
if [ ! -d $NAUTILUS_USER_PATH ]
then
	my_clear
	echo
	echo "ERROR: $NAUTILUS_USER_PATH doesn't exist!"
	echo "GNOME-nautilus scripts directory not Found! Please check!"
	echo
	exit 1
fi

# Give some information before proceeding and ask confirmation
my_clear
echo
echo "You are going to perform ccrypt integration with"
echo "GNOME-nautilus, using the following parameters:"
echo
echo "Installation Path: $NAUTILUS_USER_PATH"
echo "Ccrypt Path......: $CCRYPT_PATH"
if [ -f $NAUTILUS_USER_PATH/Encrypt_files ]
then
	echo "Overwriting File.: $NAUTILUS_USER_PATH/Encrypt_files"
fi
if [ -f $NAUTILUS_USER_PATH/Decrypt_files ]
then
	echo "Overwriting File.: $NAUTILUS_USER_PATH/Decrypt_files"
fi
read -p "Do you want to proceed(y/n)? " ok
if [ `echo $ok | grep -i y` ]
then
	# Write script files
	cat > $NAUTILUS_USER_PATH/Encrypt_files <<-EOF
	#!/bin/sh
	#
	# This script encrypts file(s) or directories with ccrypt
	#
	# Distributed under the terms of GNU GPL version 2 or later
	#
	# From About Info:
	# All executable files in this folder will appear in the Scripts menu.
	# Choosing a script from the menu will run that script.
	#
	# When executed from a local folder, scripts will be passed the selected
	# file names. When executed from a remote folder (e.g. a folder showing
	# web or ftp content), scripts will be passed no parameters.
	#
	# In all cases, the following environment variables will be set by
	# Nautilus, which the scripts may use:
	#
	# NAUTILUS_SCRIPT_SELECTED_FILE_PATHS:
	#   newline-delimited paths for selected files (only if local)
	#
	# NAUTILUS_SCRIPT_SELECTED_URIS:
	#   newline-delimited URIs for selected files
	#
	# NAUTILUS_SCRIPT_CURRENT_URI:
	#   URI for current location
	#
	# NAUTILUS_SCRIPT_WINDOW_GEOMETRY: position and size of current window
	#
	#
	# Convert environment variable contents from newline-delimited to
	# quota-space-delimited to allow for spaces on file names.
	# Thanks for the solution to: http://g-scripts.sourceforge.net/
	#
	# We put ccrypt full path in environment variable here to avoid
	# problems with variables substitution when creatign files
	CCRYPT_PATH=$CCRYPT_PATH
	#
	EOF
	cat >> $NAUTILUS_USER_PATH/Encrypt_files <<-"EOF"
	quoted=$(echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | awk 'BEGIN { FS = "\n" } { printf "\"%s\" ", $1 }' | sed -e s#\"\"##)
	#
	# Call Gnome Terminal to execute ccrypt
	gnome-terminal -t "Encrypting File(s)" --hide-menubar -e "$CCRYPT_PATH -e -s -v -t -r -P 'Enter File(s) Encryption Key: ' $quoted"
	EOF

        cat > $NAUTILUS_USER_PATH/Decrypt_files <<-EOF
	#!/bin/sh
	#
	# This script decrypts file(s) or directories encrypted with ccrypt
	#
	# Distributed under the terms of GNU GPL version 2 or later
	#
	# From About Info:
	# All executable files in this folder will appear in the Scripts menu.
	# Choosing a script from the menu will run that script.
	#
	# When executed from a local folder, scripts will be passed the selected
	# file names. When executed from a remote folder (e.g. a folder showing
	# web or ftp content), scripts will be passed no parameters.
	#
	# In all cases, the following environment variables will be set by
	# Nautilus, which the scripts may use:
	#
	# NAUTILUS_SCRIPT_SELECTED_FILE_PATHS:
	#   newline-delimited paths for selected files (only if local)
	#
	# NAUTILUS_SCRIPT_SELECTED_URIS:
	#   newline-delimited URIs for selected files
	#
	# NAUTILUS_SCRIPT_CURRENT_URI:
	#   URI for current location
	#
	# NAUTILUS_SCRIPT_WINDOW_GEOMETRY: position and size of current window
	#
	#
	# Convert environment variable contents from newline-delimited to
	# quota-space-delimited to allow for spaces on file names.
	# Thanks for the solution to: http://g-scripts.sourceforge.net/
	#
	# We put ccrypt full path in environment variable here to avoid
	# problems with variables substitution when creatign files
	CCRYPT_PATH=$CCRYPT_PATH
	#
	EOF
	cat >> $NAUTILUS_USER_PATH/Decrypt_files <<-"EOF"
	quoted=$(echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | awk 'BEGIN { FS = "\n" } { printf "\"%s\" ", $1 }' | sed -e s#\"\"##)
	#
	# Call Gnome Terminal to execute ccrypt
	gnome-terminal -t "Decrypting File(s)" --hide-menubar -e "$CCRYPT_PATH -d -s -v -t -r -P 'Enter File(s) Decryption Key: ' $quoted"
	EOF

	# Change executable permissioons on scripts
	chmod u+x $NAUTILUS_USER_PATH/Encrypt_files
	chmod u+x $NAUTILUS_USER_PATH/Decrypt_files
else
	exit 1
fi
