php4とphp5の切り替え(その2)

  • ~/.pearrcを差し替える
  • /usr/local/phpにsymlinkを張り直す。
  • apacheを再起動させる

property PEAR_RC : "/path/to/.pearrc"
property PHP_DIR : "/path/to/php"
property PHP_CURRENT : PHP_DIR --& "current"
property PHP : PHP_CURRENT & "/bin/php"


property PHP5 : PHP_DIR & "5.2-200612061930"
property PHP4 : PHP_DIR & "-4.4.4"

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 theCurrentVersion to getCurrentVersion()
  
  if theCurrentVersion starts with "5" then
    set theSwitch to  PHP4
    set theLabel to "4"
    set theNextVer to "4"
    set theCurVer to "5"
    set theIcon to thePath & ICON & ICON_PHP5
  else
    set theSwitch to  PHP5
    set theLabel to "5"
    set theNextVer to "5"
    set theCurVer to "4"
    set theIcon to thePath & ICON & ICON_PHP4
  end if
  
  set theLabel to "switch to version '" & theLabel & "'"
  if (dispStartMes(theCurrentVersion, theLabel, theIcon)) then
    copyPearRc(theNextVer, theCurVer)
    rmSymlink()
    doSudo("sudo ln -s " & theSwitch & space & PHP_CURRENT)
    StartApache()
    
    display dialog "Done." with title APP_NAME giving up after 3
  end if
end run

on copyPearRc(theNext, theCur)
  set theOrg to PEAR_RC & theNext
  set theBak to PEAR_RC & theCur
  do shell script "cp -f  " & PEAR_RC & " " & theBak
  do shell script "cp -f " & theOrg & " " & PEAR_RC
end copyPearRc

on rmSymlink()
  try
    doSudo("sudo 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("sudo apachectl restart")
  else
    doSudo("sudo 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 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