#!/bin/sh
##################################################### VERSION INFO
#v simple isync command line interface, v 0.1, by duus
#v
#v 	  type <isync help> for more information
#v    or open source code with a text editor and read it
#v    http://www.andreasduuspape.com/Post/isynccli/
#v
###################################################### INSTALL INFO
#i How to install isync cli v. 0.1 by duus
#i  1. put the isync bash script on your path (for example, ~/bin/)
#i  2. navigate to that directory
#i  3. type "chmod +x isync" to make the script executable
#i  4. open the isync script with your favorite text editor (pico isync, for example)
#i  5. Navigate to the line called #SETTINGS
#i  6. Adjust settings as necessary.  Save the file.
#i  7. type "isync help" for usage information
#i
##################################################### SETTINGS
# Please enter the directory where you are keeping the isync script here:
_SCRIPT_LOCATION=~/bin/
_SCRIPT_NAME=isync
#_SCRIPT_LOCATION=
#
#
##################################################### HELP/USAGE
#h Usage of isync cli v. 0.1 by duus
#h  type isync <args> at the prompt
#h  where args are:
#
#
# if no arguments, toggle play status and exit.
if [ "$1" = "" ]
	then
	__command="activate"
	echo "open, activate isync."
	echo | osascript <<EOF
		tell application "iSync"
			$__command
		end tell
EOF
fi
#
##### main loop
# change to _grabnext
#  if [ grabnext = 1 ]; then ; __next = ${arg}; <insert what to do with it> ; else <everything else>
_playlist=0
for arg in "$@"
	do
	__command=""

#h	
if [ "$arg" = "a" -o "$arg" = "activate" -o "$arg" = "open"  -o "$arg" = "o"  ]
 		then
#h    <a>ctivate will open/activate isync
			__command="synchronize"
			echo "synchronize - v : Start a sync."
		fi
	
if [ "$arg" = "s" -o "$arg" = "synchronize" -o "$arg" = "sync"  ]
 		then
#h    <s>ync  will synchronize - v : Start a sync.
			__command="synchronize"
			echo "synchronize - v : Start a sync."
		fi
				
#h
#h _Application Control_t
		if [ "$arg" = "hide"  ]
			then
#h    <hide> hides isync
echo | osascript <<EOF
   tell application "System Events"
   	set visible of process "iSync" to false
   end tell
EOF
			__command="no_command"
		fi

		if [ "$arg" = "quit" ]
			then 
#h    <quit> will quit isync
			__command="quit"
			echo "quit isync"
		fi

################################################# information commands
		
#h 
#h _isync cli information_		
		if [ "$arg" = "i" -o "$arg" = "install" ]
			then
#h    <(i)nstall> displays install information
			grep "^#i" $_SCRIPT_LOCATION/${_SCRIPT_NAME} | more
			__command="no_command"
		fi		

		if [ "$arg" = "v" -o "$arg" = "version" ]
			then
#h    <(v)ersion> displays version information
			grep "^#v" $_SCRIPT_LOCATION/${_SCRIPT_NAME} | more
			__command="no_command"
		fi
		if [ "$arg" = "h" -o "$arg" = "help" ]
			then
#h    <(h)elp> displays this help file
			grep "^#h" $_SCRIPT_LOCATION/${_SCRIPT_NAME} | more
			__command="no_command"
		fi
		if [ "$arg" = "src" -o "$arg" = "source" -o "$arg" = "code" ]
			then
#h    <src> displays the source code
			more $_SCRIPT_LOCATION/${_SCRIPT_NAME}
			__command="no_command"
		fi
		

#h
#h _Other_
		if [ "$__command" = "" ]
			then
#h    an unrecognized argument will be passed to isync as a command
#h       (please see the Script Editor iSync Dictionary for more commands)
			__command="$arg"
		fi
		
	#
	# run command
	#
#######################################  EXECUTE COMMAND
	if [ $_playlist = 0 -a "$__command" != "no_command" ]
		then
echo | osascript <<EOF
	tell application "iSync"
		$__command
	end tell
EOF
	fi
	#
	#
done
#h

