#!/bin/bash
##################################################### VERSION INFO
#v simple itunes command line interface, v 0.1, by duus
#v
#v 	  type <itunes help> for more information
#v    or open source code with a text editor and read it
#v    http://thedu.us/scripts/itunes/itunes/
#v
###################################################### INSTALL INFO
#i How to install itunes cli v. 0.1 by duus
#i  1. put the itunes bash script on your path (for example, ~/bin/)
#i  2. navigate to that directory
#i  3. type "chmod +x itunes" to make the script executable
#i  4. open the itunes script with your favorite text editor (pico itunes, for example)
#i  5. Navigate to the line called #SETTINGS
#i  6. Adjust settings as necessary.  Save the file.
#i  7. type "itunes help" for usage information
#i
##################################################### SETTINGS
# Please enter the directory where you are keeping the itunes script here:
_SCRIPT_LOCATION=~/bin/
_SCRIPT_NAME=itunes
_itunes_folder=~/.itunes/
if [ ! -e ${_itunes_folder} ]
	then
	mkdir ${_itunes_folder}
fi
_tmp=$1

_notunes=0
if [ "${_tmp}" = "notunes" ]
	then
	_notunes=1
	shift
elif [ "${_tmp}" = "on" ]
	then
	shift
	if [ -e ${_itunes_folder}/noitunes ]
		then
		rm ${_itunes_folder}/noitunes
	fi
	echo "itunes on"
	if [ "$1" = "" ]
		then
		exit 1;
	fi
elif [ "${_tmp}" = "off" ]
	then
	shift 
	touch ${_itunes_folder}/noitunes
	echo "itunes off"
	if [ "$1" = "" ]
		then
		exit 1;
	fi
fi
	
if [ -e ${_itunes_folder}/noitunes ]
	then
	_notunes=1
fi
#
##################################################### HELP/USAGE
#h Usage of itunes cli v. 0.1 by duus
#h  type itunes <args> at the prompt
#h  where args are:
#h
#h   <off/on> -- toggle activity of itunes.sh
#h
# if no arguments, toggle play status and exit.
if [ "$1" = "" -a ${_notunes} -eq 0 ]
	then
	itunes playpause
fi
#
#
#  initial values.
_getnext=0
_nextword=""
_playlist=0
_get=0
_rating=0
_getword="tmp"
##############################################################  main loop through args.
for arg in "$@"
	do
	__command=""
###############################      get next word here...
	if [ ${_getnext} -eq 1 ]
		then
		_nextword=$arg
		if [ ${_playlist} -eq 1 ]
			then
			__command="play playlist \"$_nextword\""
			echo "play itunes playlist $_nextword"
			#echo $__command
			_playlist=0
		elif [ ${_get} -eq 1 ]
			then
			if [ "${_nextword}" = "song" ]
				then
				if [ $_notunes -eq 1 ]
					then
					itunes notunes song
					else
					itunes song
				fi
				__command="no_command"
			elif [ "${_nextword}" = "ratingstars" -o "${_nextword}" = "stars" ]
				then
				if [ $_notunes -eq 1 ]
					then
					_songrating=`itunes notunes get rating`
					else
					_songrating=`itunes get rating`
				fi
				i=0
				echo -n "" > ${_itunes_folder}/ratingstars
				while [ $i -lt $_songrating ]
					do
					echo -n "*"
					echo -n "*" > ${_itunes_folder}/ratingstars
					i=`expr $i + 20`
					done
				else
				__command=" get ${_nextword} of current track "
				_getword=${_nextword}
			fi
			_get=0
		elif [ ${_rating} -eq 1 ]
			then
			_rating=`expr ${_nextword} \* 20`
			__command="set rating of current track to ${_rating}"
			echo ${__command}
			_rating=0
		fi
		_getnext=0
#############################
	else
#############################         commands
		if [ "$arg" = "pp" -o "$arg" = "playpause" ]
			then
#h
#h  _Play Control_
#h    no arguments, or <pp> or <playpause> will toggle itunes' play status
			__command="playpause"
			echo "toggle itunes' play status"
		fi
		if [ "$arg" = "p" -o "$arg"  = "play" ]
			then
#h    <(p)lay> will play itunes
			__command="play"
			echo "play itunes"
		fi
		
		if [ "$arg"  = "pause" ]
			then
#h    <pause> will pause itunes
			__command="pause"
			echo "pause itunes"
		fi
		
		if [ "$arg"  = "next" -o "$arg" = "new" ]
			then
#h    <next> or <new> will forward one track
			__command="next track"
			echo "go to next track in itunes"
		fi
		
		if [ "$arg"  = "prev" -o "$arg" = "previous" -o "$arg" = "back" ]
			then
#h    <(prev)ious> or <back> will go back one track
			__command="previous track"
			echo "go to last track in itunes"
		fi
				
		if [ "$arg" = "s" -o "$arg"  = "stop" ]
			then
#h    <(s)top> will stop itunes
			__command="stop"
			echo "stop itunes"
		fi

if [ "$arg" = "get" -o "$arg" = "g" ]
			then 
#h    <g>et WORD will get WORD of currently playing track (where WORD = name, index, album, artist, etc)
			_get=1
			_getnext=1
		fi

if [ "$arg" = "song"  ]
			then 
#h    <song> will get 'name' by artist from the _album_ (year) of currently playing track
		if [ $_notunes -eq 0 ]
			then
			echo "'`itunes get name`' by `itunes get artist`, from the album _`itunes get album`_ (`itunes get year`): `itunes get ratingstars`"  > ${_itunes_folder}/song
		fi
		cat ${_itunes_folder}/song
		__command="no_command"
		fi

if [ "$arg" = "podcasts" -o "$arg" = "pod" -o "$arg" = "podcast" ]
			then 
#h    <(pod)casts> will update all podcasts
			__command="updateAllPodcasts"
			echo "update all podcasts"
		fi
		
 if [ "$arg" = "r" -o "$arg" = "rating" ]
 			then 
#h    <r>ating VALUE will set the rating of currently playing track to VALUE (0-5 goes to 0-100)
 			_rating=1
 			_getnext=1
 		fi



#h
#h _iPod Control_
		if [ "$arg" = "sync" ]
			then
#h    <sync> will sync an attached ipod
			__command="update"
			#eject ipod
			echo "sync attached iPod..."
		fi
						
		if [ "$arg" = "e" -o "$arg"  = "eject" ]
			then
#h    <(e)ject> will eject an attached ipod
			__command="eject"
			#eject ipod
			echo "eject iPod..."
		fi
			
#h
#h  _Playlists_
		if [ "$arg" = "playlist" -o "$arg" = "l" -o "$arg" = "list" ]
			then 
#h    <playlist NAME> will play the playlist named NAME (or <(l)ist> will do the same)
			_playlist=1
			_getnext=1
		fi

		
		if [ "$arg" = "party" -o "$arg" = "partyshuffle" -o "$arg" = "dj" -o "$arg" = "itunesdj"  ]
			then 
#h    <(dj)> will play iTunes DJ (formerly Party Shuffle) also: <(party)shuffle>
#			__command="play playlist \"Party Shuffle\""
#			echo "itunes: play Party Shuffle"
			__command="play playlist \"iTunes DJ\""
			echo "itunes: play iTunes DJ"

		fi
#h
#h _Application Control_
		if [ "$arg" = "hide"  ]
			then
#h    <hide> hides itunes
		echo "hide itunes"
echo | osascript <<EOF
   tell application "System Events"
   	set visible of process "iTunes" to false
   end tell
EOF
			__command="no_command"
		fi

		if [ "$arg" = "open" -o "$arg" = "start" ]
			then
#h    <open>/<start> starts iTunes
			echo "start itunes"
			open -a iTunes
			__command="no_command"
		fi

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


################################################# information commands
		
#h 
#h _itunes 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 itunes as a command
#h       (please see the Script Editor iTunes Dictionary for more commands)
			__command="$arg"
		fi
		
	fi # if playlist
	#
	# run command
	#
#######################################  EXECUTE COMMAND
if [ `isapprunning exact iTunes` -eq 0 ] # if iTunes is not running
	then
	if [ "${__command}" = "pause" -o "${__command}" = "stop" ]
		then
			echo "iTunes not running, so ${__command} ignored."
			_notunes=1
	else
		echo "opening iTunes (please wait)"
		open -a iTunes
		sleep 3;
		echo "done.  continuing."
	fi
fi
	if [ ${_getnext} -eq 0 -a "$__command" != "no_command" ]
		then
		if [ ${_notunes} -eq 0 ]
			then
echo | osascript  > ${_itunes_folder}/${_getword} <<EOF
tell application "iTunes"
	$__command
end tell
EOF
		fi
		if [ -e ${_itunes_folder}/${_getword} ]
			then
			cat ${_itunes_folder}/${_getword}
		fi
		_getword="tmp"
	fi
	#
done
#h


