@echo off
setlocal EnableDelayedExpansion

set SDK_INSTALLPKG_DIR=Native-SDK
set SDK_REMOVEPKG_DIR=Albacore
set REMOTE_PDK_DIR=/tmp/pdk-install

@rem this script's parent dir
set HERE=%~dp0

set COMMAND=%1%
if not defined COMMAND goto :usage
if %COMMAND% == versions goto :start_versions
if %COMMAND% == install goto :start_install
if %COMMAND% == remove goto :start_remove
goto :usage

:start_versions
set PKG_DIR=%SDK_INSTALLPKG_DIR%
goto :start

:start_install
set PKG_DIR=%SDK_INSTALLPKG_DIR%
goto :start

:start_remove
set PKG_DIR=%SDK_REMOVEPKG_DIR%
goto :start

:start
@rem first look in current dir for packages
set PACKAGES_DIR=%HERE%\%PKG_DIR%
if exist "%PACKAGES_DIR%" goto :check

@rem check if PDK installed
if not defined PalmPDK set PalmPDK=%ProgramFiles%\Palm\PDK
set PALM_PDK_DIR=%PalmPDK%
if not exist "%PALM_PDK_DIR%" goto :noPDK
set PACKAGES_DIR=%PALM_PDK_DIR%\share\packages\%PKG_DIR%
if not exist "%PACKAGES_DIR%" goto :noPackages

:check
set INSTALL_SCRIPT=%PACKAGES_DIR%\..\remote-pdk-install.sh
if not exist "%INSTALL_SCRIPT%" goto :noScript

@rem check if novacom installed
for /f "usebackq tokens=*" %%i in (`novacom -V`) do set NOVACOM_VERSION=%%i
if not defined NOVACOM_VERSION goto :noNovacom

@rem check the device type
for /f "usebackq tokens=*" %%i in (`novacom -l`) do set DEVICELIST=%%i
if not defined DEVICELIST goto :nodevices
if %DEVICELIST:~-12% == castle-linux goto :castle
if %DEVICELIST:~-11% == pixie-linux goto :pixie
if %DEVICELIST:~-8% == emulator goto :emulator
if %DEVICELIST:~-5% == linux goto :otherdev

:nodevices
echo Error: No device detected.
echo Connect device and make sure Developer Mode is enabled.
exit /b 1

:usage
echo Usage: %~n0 [install^|remove^|versions]
echo.
echo 'install' installs Palm PDK packages onto the device.
echo 'remove' removes Palm PDK packages from the device.
echo 'versions' shows the versions of the currently-installed packages.
echo It requires a USB-connected Palm Pre or Palm Pixi device.
echo Developer Mode must already be enabled on the device
echo.
goto :EOF

:castle
echo Found connected device
set DEVICE_TYPE=castle
goto :run_command

:pixie
echo Found connected device
set DEVICE_TYPE=pixie
goto :run_command

:otherdev
for /f "usebackq tokens=*" %%i in (`novacom run -- file:///bin/grep armv /etc/ipkg/arch.conf`) do set ARCH_STRING=%%i
if not defined ARCH_STRING goto :nodevices
set ARCH=%ARCH_STRING:~5,30%
set ARCH=%ARCH:~0,5%
if "%ARCH%" == "armv7" goto :castle
if "%ARCH%" == "armv6" goto :pixie
goto :nodevices

:run_command

@rem for /f "usebackq tokens=*" %%i in (`novacom run -- file:///bin/grep PRODUCT_VERSION_STRING /etc/palm-build-info`) do set VERSION_STRING=%%i
@rem if not defined VERSION_STRING goto :wrongVersion
@rem set VERSION=%VERSION_STRING:~23,30%
@rem set VERSION=%VERSION:~0,15%
@rem if not "%VERSION%" == "Palm webOS 1.4." goto :wrongVersion

set PKG_PATH=%PACKAGES_DIR%\%DEVICE_TYPE%

if %COMMAND% == versions goto :versions
if %COMMAND% == remove goto :remove
if %COMMAND% == install goto :install
goto :usage

:versions
cd %PKG_PATH%
novacom run -- file:///bin/rm -rf %REMOTE_PDK_DIR%
novacom run -- file:///bin/mkdir -p %REMOTE_PDK_DIR%/%PKG_DIR%/%DEVICE_TYPE%
for %%i in (*.ipk) do (echo Copying %%i && novacom put file://%REMOTE_PDK_DIR%/%PKG_DIR%/%DEVICE_TYPE%/%%i < %%i)

echo Copying scripts
novacom put file://%REMOTE_PDK_DIR%/remote-pdk-install.sh < "%INSTALL_SCRIPT%"

echo Running scripts
novacom run -- file:///bin/sh %REMOTE_PDK_DIR%/remote-pdk-install.sh versions %DEVICE_TYPE%

echo Done.

goto :EOF


:install
echo This script will install PDK packages onto your device.
echo.
echo Note that your device will be rebooted after this script completes.

:install_ask
set /p ANSWER="Do you wish to continue? (y/N):"
if not defined ANSWER goto :EOF
if %ANSWER% == n goto :EOF
if %ANSWER% == N goto :EOF
if %ANSWER% == y goto :install_go
if %ANSWER% == Y goto :install_go
goto :install_ask

:install_go
cd %PKG_PATH%
novacom run -- file:///bin/rm -rf %REMOTE_PDK_DIR%
novacom run -- file:///bin/mkdir -p %REMOTE_PDK_DIR%/%PKG_DIR%/%DEVICE_TYPE%
for %%i in (*.ipk) do (echo Copying %%i && novacom put file://%REMOTE_PDK_DIR%/%PKG_DIR%/%DEVICE_TYPE%/%%i < %%i)

echo Copying scripts
novacom put file://%REMOTE_PDK_DIR%/remote-pdk-install.sh < "%INSTALL_SCRIPT%"

echo Running scripts
novacom run -- file:///bin/sh %REMOTE_PDK_DIR%/remote-pdk-install.sh install %DEVICE_TYPE%

echo Done.

goto :EOF


:remove
echo This script will remove PDK packages from your device.
echo.
echo Note that your device will be rebooted after this script completes.

:remove_ask
set /p ANSWER="Do you wish to continue? (y/N):"
if not defined ANSWER goto :EOF
if %ANSWER% == n goto :EOF
if %ANSWER% == N goto :EOF
if %ANSWER% == y goto :remove_go
if %ANSWER% == Y goto :remove_go
goto :remove_ask

:remove_go
cd %PKG_PATH%
novacom run -- file:///bin/rm -rf %REMOTE_PDK_DIR%
novacom run -- file:///bin/mkdir -p %REMOTE_PDK_DIR%/%PKG_DIR%/%DEVICE_TYPE%
for %%i in (*.ipk) do (echo Copying %%i && novacom put file://%REMOTE_PDK_DIR%/%PKG_DIR%/%DEVICE_TYPE%/%%i < %%i)

echo Copying scripts
novacom put file://%REMOTE_PDK_DIR%/remote-pdk-install.sh < "%INSTALL_SCRIPT%"

echo Running scripts
novacom run -- file:///bin/sh %REMOTE_PDK_DIR%/remote-pdk-install.sh remove %DEVICE_TYPE%

echo Done.

goto :EOF


:emulator
echo Please exit the Palm Emulator and re-run this script.
exit /b 1

:noPDK
echo Error: Could not locate Palm PDK
exit /b 1

:noPackages
echo Error: Required PDK packages not found
exit /b 1

:noScript
echo Error: Missing install script
exit /b 1

:noNovacom
echo Error: Novacom must be installed in order for this script to work
exit /b 1

:wrongVersion
echo Error: Only devices with Palm webOS 1.4 are supported!
echo.
echo Run Updates app on device to update to Palm webOS 1.4.0, or
echo go to http://www.palm.com/rom to get webOS Doctor 1.4.0.
exit /b 1
