# Copyright AllSeen Alliance. All rights reserved.
#
#    Permission to use, copy, modify, and/or distribute this software for any
#    purpose with or without fee is hereby granted, provided that the above
#    copyright notice and this permission notice appear in all copies.
#
#    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
#    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
#    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
#    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# 

import os
import shutil
import urllib2

vars = Variables()

vars.Add('ANDROID_SDK_PATH', 'Andorid sdk path to be used','')

env = Environment()
vars.Update(env)
Help(vars.GenerateHelpText(env))

if env['ANDROID_SDK_PATH']=='': 
    if os.environ.has_key('ANDROID_SDK_PATH'):    	
	env.Append(ANDROID_SDK_PATH=os.environ['ANDROID_SDK_PATH'])
    else:	   	 
    	print "didn't find ANDROID_SDK_PATH"
	exit(1)	

print 'ANDROID_SDK_PATH='+env['ANDROID_SDK_PATH']

if os.path.exists('java/jar/alljoyn.jar'):
 
   if not os.path.exists('services/sample_apps/android/common_utils/build.xml'):
	 print "didn't find services/sample_apps/android/common_utils/build.xml terminate"
	 exit(1)

   if not os.path.exists('services/config/java/build.xml'):  	
	  print "didn't find services/config/java/build.xml terminate"
	  exit(1)

   if not os.path.exists('services/onboarding/java/OnboardingService/build.xml'):  	
   	  print "didn't find services/onboarding/java/OnboardingService/build.xml terminate"
   	  exit(1)
 
   if not os.path.exists('services/simulators/android/about_conf_onb_server/build.xml'):
	 print "didn't find services/simulators/android/about_conf_onb_server/build.xml terminate"
	 exit(1)

   if not os.path.exists('services/sample_apps/android/common_utils/libs'):
	os.makedirs('services/sample_apps/android/common_utils/libs')
   
   if not os.path.exists('services/config/java/libs'):
	os.makedirs('services/config/java/libs')

   if not os.path.exists('services/onboarding/java/OnboardingService/libs'):
	os.makedirs('services/onboarding/java/OnboardingService/libs')

   if not os.path.exists('services/simulators/android/about_conf_onb_server/libs'):
    	os.makedirs('services/simulators/android/about_conf_onb_server/libs')

   if not os.path.exists('services/simulators/android/about_conf_onb_server/libs/armeabi'):
    	os.makedirs('services/simulators/android/about_conf_onb_server/libs/armeabi')
   
   #### copy alljoyn.jar to all needed places
   shutil.copyfile('java/jar/alljoyn.jar',      'services/sample_apps/android/common_utils/libs/alljoyn.jar') 
   shutil.copyfile('java/jar/alljoyn_about.jar','services/sample_apps/android/common_utils/libs/alljoyn_about.jar') 

   shutil.copyfile('java/jar/alljoyn.jar',      'services/config/java/libs/alljoyn.jar') 
   shutil.copyfile('java/jar/alljoyn_about.jar','services/config/java/libs/alljoyn_about.jar') 

   shutil.copyfile('java/jar/alljoyn.jar',      'services/onboarding/java/OnboardingService/libs/alljoyn.jar') 
   shutil.copyfile('java/jar/alljoyn_about.jar','services/onboarding/java/OnboardingService/libs/alljoyn_about.jar') 

   shutil.copyfile('java/jar/alljoyn.jar',       'services/simulators/android/about_conf_onb_server/libs/alljoyn.jar')
   shutil.copyfile('java/jar/alljoyn_about.jar', 'services/simulators/android/about_conf_onb_server/libs/alljoyn_about.jar')
   shutil.copyfile('java/lib/liballjoyn_java.so','services/simulators/android/about_conf_onb_server/libs/armeabi/liballjoyn_java.so') 

   #### build Android common utils
   andrid_common_utils=os.system(' ant -Dsdk.dir='+env['ANDROID_SDK_PATH']+'  -buildfile services/sample_apps/android/common_utils/build.xml  jar')
   if (andrid_common_utils<>0):
   	print "erro generating Android common utils jars terminate"
   	exit(1) 

   #### build Config service
   config_ret=os.system(' ant -buildfile services/config/java/build.xml jar')
   if (config_ret<>0):
   	print "erro generating Config jars terminate"
   	exit(1) 
  
   #### build Onboarding service
   shutil.copyfile('services/config/java/build/deploy/alljoyn_config.jar', 'services/onboarding/java/OnboardingService/libs/alljoyn_config.jar')
   onboarding_ret=os.system(' ant -buildfile services/onboarding/java/OnboardingService/build.xml jar')
   if (onboarding_ret<>0):
   	print "erro generating Onboardig jars terminate"
   	exit(1) 

   ### Prepare Android for eclipse
   dest_path='services/simulators/android/about_conf_onb_server/libs/'			 
   
   shutil.copyfile('services/config/java/build/deploy/alljoyn_config.jar',   					dest_path+'alljoyn_config.jar')
   shutil.copyfile('services/onboarding/java/OnboardingService/build/deploy/alljoyn_onboarding.jar',   		dest_path+'alljoyn_onboarding.jar')
   shutil.copyfile('services/sample_apps/android/common_utils/build/deploy/alljoyn_apps_android_utils.jar',	dest_path+'alljoyn_apps_android_utils.jar')  
  
   #os.system(' ant -Dsdk.dir='+env['ANDROID_SDK_PATH']+' -buildfile services/simulators/android/about_conf_onb_server/build.xml release')
   os.system(' ant -Dsdk.dir='+env['ANDROID_SDK_PATH']+' -buildfile services/simulators/android/about_conf_onb_server/build.xml ')
else:
   print "didn't find alljoyn.jar terminate"
   exit(1)
	
   print "found about"

#if ('java' in bindings) and os.path.exists('alljoyn_java/SConscript'):
#    installedFiles += env.SConscript(['alljoyn_java/SConscript'])
