php4とphp5の切り替え

property PHP5 : "path/to/php5"
property PHP4 : "path/to/php4"

property PHP_DIR : "/usr/local/php/"
property PHP_CURRENT : PHP_DIR & "current"
property PHP : PHP_CURRENT & "/bin/php"

property USER : "oppara"
property PASS : "pass"

property HTTPD : "/usr/sbin/httpd"
property APP_NAME : "php_switcher"
property ICON : "Contents/Resources/"
property ICON_PHP5 : "php5.icns"
property ICON_PHP4 : "php4.icns"

on run
	set thePath to getMe()
	set theCurVer to getCurrentVersion()
	
	if theCurVer starts with "5" then
		set theSwitch to PHP_DIR & PHP4
		set theLabel to "4"
		set theIcon to thePath & ICON & ICON_PHP5
	else
		set theSwitch to PHP_DIR & PHP5
		set theLabel to "5"
		set theIcon to thePath & ICON & ICON_PHP4
	end if
	
	set theLabel to "switch to version '" & theLabel & "'"
	if (dispStartMes(theCurVer, theLabel, theIcon)) then
		rmSymlink()
		doSudo("ln -s " & theSwitch & space & PHP_CURRENT)
		StartApache()
		
		display dialog "Done." with title APP_NAME giving up after 3
	end if
end run


on rmSymlink()
	try
		doSudo("rm " & PHP_CURRENT)
	end try
end rmSymlink

on StartApache()
	set theRes to do shell script "ps axww|grep httpd"
	if theRes contains HTTPD then
		doSudo("apachectl restart")
	else
		doSudo("apachectl start")
	end if
end StartApache

on getCurrentVersion()
	set thePerlCmd to "perl -ane '$F[1] =~ /([0-9]+[0-9.]+)/;print $1;'"
	return do shell script PHP & " -v |head -n 1 | " & thePerlCmd
end getCurrentVersion

on dispStartMes(theCurVer, theLabel, theIcon)
	set theLabel to theLabel & " ?"
	try
        -- 長いんで改行。ホントは一行。
		button returned of (
                        display dialog return & "<= current version. " & theCurVer 
                        buttons {"キャンセル", theLabel} with title APP_NAME 
                        with icon theIcon as POSIX file giving up after 10)
        -- ホントは一行ここまで。
		return true
	on error
		return false
	end try
end dispStartMes

on doSudo(theCmd)
	--display dialog (theCmd)
	do shell script "sudo " & theCmd user name USER password PASS with administrator privileges
end doSudo

on getMe()
	set thePath to POSIX path of ((path to me) as string) as string
	return thePath as string
end getMe