<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="GoogleAuthenticator" basedir="." default="release">

	<typedef resource="bb-ant-defs.xml" />
	<taskdef resource="antenna.properties" />

	<property file="application.properties" />
	<property file="local.properties" />

	<property name="src.dir" value="src" />
	<property name="res.dir" value="res" />
	<property name="build.dir" value="build" />
	<property name="release.dir" value="release" />

	<path id="src.path">
		<fileset dir="${src.dir}">
			<include name="**/*.*" />
		</fileset>
	</path>

	<target name="clean">
		<delete failonerror="false" includeEmptyDirs="true">
			<fileset dir="${build.dir}" />
			<fileset dir="${release.dir}/google-authenticator-${version}" />
		</delete>
	</target>

	<target name="genfiles">
		<echo file="${src.dir}/com/google/authenticator/blackberry/Build.java">package com.google.authenticator.blackberry;

public interface Build {
  String DOWNLOAD_URL = "${appengine.url}";
}
</echo>
	</target>

	<target name="release" depends="clean,genfiles">
		<mkdir dir="${build.dir}" />
		<mkdir dir="${build.dir}/BlackBerry-45" />
		<mkdir dir="${build.dir}/BlackBerry-46" />
		<mkdir dir="${build.dir}/BlackBerry-47" />
		<mkdir dir="${build.dir}/BlackBerry-50" />
		<mkdir dir="${build.dir}/BlackBerry-60" />

		<!-- Note: The icon file size is limited, so use 8-bit PNGs -->
		<copy file="${res.dir}/drawable-mdpi/${icon}" todir="${build.dir}/BlackBerry-45" />
		<copy file="${res.dir}/drawable-hdpi/${icon}" todir="${build.dir}/BlackBerry-46" />
		<copy file="${res.dir}/drawable-hdpi/${icon}" todir="${build.dir}/BlackBerry-47" />
		<copy file="${res.dir}/drawable-hdpi/${icon}" todir="${build.dir}/BlackBerry-50" />
		<copy file="${res.dir}/drawable-hdpi/${icon}" todir="${build.dir}/BlackBerry-60" />
		<rapc nowarn="true" generatesourcelist="true" output="com_google_authenticator" destdir="${build.dir}/BlackBerry-45" jdehome="${jdehome45}">
			<jdp file="application.properties">
				<entry file="entry/startup.properties" />
			</jdp>
			<src refid="src.path" />
		</rapc>
		<rapc nowarn="true" generatesourcelist="true" output="com_google_authenticator" destdir="${build.dir}/BlackBerry-46" jdehome="${jdehome46}">
			<jdp file="application.properties">
				<entry file="entry/startup.properties" />
			</jdp>
			<src refid="src.path" />
		</rapc>
		<rapc nowarn="true" generatesourcelist="true" output="com_google_authenticator" destdir="${build.dir}/BlackBerry-47" jdehome="${jdehome47}">
			<jdp file="application.properties">
				<entry file="entry/startup.properties" />
			</jdp>
			<src refid="src.path" />
		</rapc>
		<rapc nowarn="true" generatesourcelist="true" output="com_google_authenticator" destdir="${build.dir}/BlackBerry-50" jdehome="${jdehome50}">
			<jdp file="application.properties">
				<entry file="entry/startup.properties" />
			</jdp>
			<src refid="src.path" />
		</rapc>
		<rapc nowarn="true" generatesourcelist="true" output="com_google_authenticator" destdir="${build.dir}/BlackBerry-60" jdehome="${jdehome60}">
			<jdp file="application.properties">
				<entry file="entry/startup.properties" />
			</jdp>
			<src refid="src.path" />
		</rapc>
		<sigtool jdehome="${jdehome}">
			<fileset dir="${build.dir}" includes="**/*.cod" />
		</sigtool>

		<mkdir dir="${release.dir}" />
		<mkdir dir="${release.dir}/google-authenticator-${version}" />
		<mkdir dir="${release.dir}/google-authenticator-${version}/alx" />
		<mkdir dir="${release.dir}/google-authenticator-${version}/ota" />
		<mkdir dir="${release.dir}/google-authenticator-${version}/alx/BlackBerry-45" />
		<mkdir dir="${release.dir}/google-authenticator-${version}/alx/BlackBerry-46" />
		<mkdir dir="${release.dir}/google-authenticator-${version}/alx/BlackBerry-47" />
		<mkdir dir="${release.dir}/google-authenticator-${version}/alx/BlackBerry-50" />
		<mkdir dir="${release.dir}/google-authenticator-${version}/alx/BlackBerry-60" />
		<mkdir dir="${release.dir}/google-authenticator-${version}/ota/BlackBerry-45" />
		<mkdir dir="${release.dir}/google-authenticator-${version}/ota/BlackBerry-46" />
		<mkdir dir="${release.dir}/google-authenticator-${version}/ota/BlackBerry-47" />
		<mkdir dir="${release.dir}/google-authenticator-${version}/ota/BlackBerry-50" />
		<mkdir dir="${release.dir}/google-authenticator-${version}/ota/BlackBerry-60" />
		<alx destdir="${release.dir}/google-authenticator-${version}/alx" filename="GoogleAuthenticator.alx">
			<application file="application.properties">
				<codset greaterthanequal="4.5" lessthan="4.6" dir="BlackBerry-45">
					<fileset dir="${build.dir}/BlackBerry-45" includes="*.cod" />
				</codset>
				<codset greaterthanequal="4.6" lessthan="4.7" dir="BlackBerry-46">
					<fileset dir="${build.dir}/BlackBerry-46" includes="*.cod" />
				</codset>
				<codset greaterthanequal="4.7" lessthan="5.0" dir="BlackBerry-47">
					<fileset dir="${build.dir}/BlackBerry-47" includes="*.cod" />
				</codset>
				<codset greaterthanequal="5.0" lessthan="6.0" dir="BlackBerry-50">
					<fileset dir="${build.dir}/BlackBerry-50" includes="*.cod" />
				</codset>
				<codset greaterthanequal="6.0" dir="BlackBerry-60">
					<fileset dir="${build.dir}/BlackBerry-60" includes="*.cod" />
				</codset>
			</application>
		</alx>
		<jadtool input="${build.dir}/BlackBerry-45/com_google_authenticator.jad" destdir="${release.dir}/google-authenticator-${version}/ota/BlackBerry-45">
			<fileset dir="${build.dir}/BlackBerry-45" includes="*.cod" />
			<override key="MIDlet-Name" value="${title}" />
		</jadtool>
		<jadtool input="${build.dir}/BlackBerry-46/com_google_authenticator.jad" destdir="${release.dir}/google-authenticator-${version}/ota/BlackBerry-46">
			<fileset dir="${build.dir}/BlackBerry-46" includes="*.cod" />
			<override key="MIDlet-Name" value="${title}" />
		</jadtool>
		<jadtool input="${build.dir}/BlackBerry-47/com_google_authenticator.jad" destdir="${release.dir}/google-authenticator-${version}/ota/BlackBerry-47">
			<fileset dir="${build.dir}/BlackBerry-47" includes="*.cod" />
			<override key="MIDlet-Name" value="${title}" />
		</jadtool>
		<jadtool input="${build.dir}/BlackBerry-50/com_google_authenticator.jad" destdir="${release.dir}/google-authenticator-${version}/ota/BlackBerry-50">
			<fileset dir="${build.dir}/BlackBerry-50" includes="*.cod" />
			<override key="MIDlet-Name" value="${title}" />
		</jadtool>
		<jadtool input="${build.dir}/BlackBerry-60/com_google_authenticator.jad" destdir="${release.dir}/google-authenticator-${version}/ota/BlackBerry-60">
			<fileset dir="${build.dir}/BlackBerry-60" includes="*.cod" />
			<override key="MIDlet-Name" value="${title}" />
		</jadtool>
		<replace dir="${release.dir}/google-authenticator-${version}/ota/" includes="**/*.jad" token="RIM-COD-URL: " value="RIM-COD-URL-0: " />
		<replace dir="${release.dir}/google-authenticator-${version}/ota/" includes="**/*.jad" token="RIM-COD-Size: " value="RIM-COD-Size-0: " />
		<zip destfile="${release.dir}/google-authenticator-${version}.zip" basedir="${release.dir}/google-authenticator-${version}" />
		<move file="${release.dir}/google-authenticator-${version}.zip" todir="${release.dir}/google-authenticator-${version}" />
		<echo file="${release.dir}/google-authenticator-${version}/app.yaml"># Google App Engine Python Application Configuration
application: ${appengine.application}
version:  ${appengine.version}
runtime: python
api_version: 1

handlers:
- url: /ota/(.*)\.jad
  static_files: ota/\1.jad
  upload: ota/(.*)\.jad
  mime_type: text/vnd.sun.j2me.app-descriptor

- url: /ota/(.*)\.cod
  static_files: ota/\1.cod
  upload: ota/(.*)\.cod
  mime_type: application/vnd.rim.cod

- url: /google-authenticator\.zip
  static_files: google-authenticator-${version}.zip
  upload: .*\.zip

- url: /
  script: main.py</echo>
		<echo file="${release.dir}/google-authenticator-${version}/main.py">#!/usr/bin/env python
import os
# Example: BlackBerry8700/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1
ua = os.environ.get('HTTP_USER_AGENT', 'unknown')
if ua.startswith('BlackBerry'):
  if '/6.' in ua:
    platform = 'BlackBerry-60'
  elif '/5.0.' in ua:
    platform = 'BlackBerry-50'
  elif '/4.7.' in ua:
    platform = 'BlackBerry-47'
  elif '/4.6.' in ua:
    platform = 'BlackBerry-46'
  elif '/4.5.' in ua:
    platform = 'BlackBerry-45'
  else:
    platform = None
else:
  platform = None

if platform:
  host = os.environ.get('HTTP_HOST')
  path = '/ota/%s/com_google_authenticator.jad' % platform
  if host:
    url = 'http://' + host + path
  else:
    url = path
  print 'Status: 302 Moved'
  print 'Location: %s' % url
  print
else:
  print 'Content-Type: text/plain'
  print
  print 'Unsupported Platform:', ua</echo>
	</target>
</project>
