###
# This script build a mobile app for the android and ios cordova projects.
# If the environment variable TEST_HOST is set it will be used as mobile app host
# for all the requests
#
#
###

require 'fileutils'
include FileUtils
#rm_rf Rails.root.join('public', 'assets') # now done in ./make_...
#`rake assets:precompile` now done in ./make_...
Rails.application.routes.default_url_options = {format: 'html'} # append .html extension to asset paths

rails_assets_path = Rails.root.join('public', 'assets')
android_root = '/Users/bterkuile/Documents/workspace/mozo/www'
ios_root = '/Users/bterkuile/Documents/iOS/Qwaiter/www'

# android_root_url = "file:///Users/bterkuile/Documents/workspace/Qwaiter/assets"
android_root_url = 'file:///android_asset'

# Cleanup and setup android structure
#mkdir_p File.join(android_root, 'user')
rm_rf File.join(android_root, 'assets')
Dir.glob(File.join('public', 'assets', '**', '*.gz')).each{|f| rm_f f}

# Cleanup and setup ios structure
rm_rf Dir.glob(File.join(ios_root, '**', '*'))
#mkdir_p File.join(ios_root, 'user')

# COPY iOS resources
copy File.join(ios_root, '..', 'www-original', 'cordova-2.2.0.js'), ios_root
ln_s File.join(ios_root, '..', 'www-original', 'qmobile.js'), ios_root
copy '/Users/bterkuile/Documents/iOS/phonegap-plugins/iOS/BarcodeScanner/barcodescanner.js', ios_root
# DO iOS ASSETS

cp_r rails_assets_path, File.join(ios_root, 'assets')
for css in Dir.glob(File.join(ios_root, "**", "*.css"))
  contents = File.read(css)
  contents.gsub!(/url\("\/assets\//, %|url("../|)
  contents.gsub!(/url\(\/assets\//, %|url(../|)
  File.open(css, 'w'){|f| f.puts contents}
end

move rails_assets_path, File.join(android_root, 'assets')

for css in Dir.glob(File.join(android_root, "**", "*.css"))
  contents = File.read(css)
  # contents.gsub!(/url\("/, %|url("#{android_root_url}|)
  # contents.gsub!(/url\(\//, %|url(#{android_root_url}/|)
  contents.gsub!(/url\("\/assets\//, %|url("../|)
  contents.gsub!(/url\(\/assets\//, %|url(../|)
  File.open(css, 'w'){|f| f.puts contents}
end

=begin
uc = UserController.new
class << uc
  def authenticate_user!
  end

  def current_user
    @current_user ||= User.first || User.new
    @current_user.id ||= 'bogus_user'
    @current_user
  end

  def request
    ActionDispatch::Request.new({})
  end
end
uc.instance_variable_set('@_request', uc.request)
for action in uc.action_methods
  next unless action == 'index'
  if File.exist?(Rails.root.join('app', 'views', 'user', "#{action}.html.slim"))
    puts "action #{action} found"
    result = uc.render_to_string(action: action.dup)

    ## WRITE TO IOS PATH
    ios_view = result.gsub /(href|src)="\/user\/([^"]+)/, %|\\1="./\\2| # replace absolute path names to relative ones
    ios_view = ios_view.gsub /(href|src)="\/assets\/([^"]+)/, %|\\1="./assets/\\2| # make assets calls relative
    ios_view.sub! /<\/title>/, '</title><script type="text/javascript" src="qmobile.js"></script><script type="text/javascript" src="cordova-2.2.0.js"></script><script type="text/javascript" src="barcodescanner.js"></script>'
    ios_view.gsub! '##assets_path##', './assets/'
    ios_view.gsub! '##platform##', 'ios'
    File.open(File.join(ios_root, "#{action}.html"), 'w'){|f| f.puts ios_view}

    ## MODIFY FOR ANDROID
    #result.sub! /<head>/, '<head><base href="file:///Users/bterkuile/Documents/workspace/Qwaiter/assets/" target="_self" />'
    android_view = result.gsub /(href|src)="\/user\/([^"]+)/, %|\\1="/\\2| # replace absolute path names to relative ones
    android_view.gsub! /(href|src)="([^#])/, %|\\1="#{android_root_url}\\2| # replace relative uri's with android specific one
    # android_view.sub! /<\/title>/, '</title><script type="text/javascript" src="qmobile.js"></script><script type="text/javascript" src="cordova-2.6.0rc1.js"></script><script type="text/javascript" src="barcodescanner.js"></script>'
    android_view.sub! /<\/title>/, '</title><script type="text/javascript" src="cordova.js"></script>'
    android_view.gsub! '##assets_path##', 'file:///android_asset/assets/'
    android_view.gsub! '##root_url##', 'file:///android_asset/user/index.html'
    android_view.gsub! '##platform##', 'android'
    if testhost = ENV['TEST_HOST'].presence
      android_view.gsub! /data.mozo.bar|www.mozo.bar/, "#{testhost}:3000"
      android_view.gsub! /events.mozo.bar/, "#{testhost}:9296"
    end
    # write to android path
    File.open(File.join(android_root, "#{action}.html"), 'w'){|f| f.puts android_view}
  end
end
view = view = ActionView::Base.new(uc.view_paths)
class << view
  include ApplicationHelper
  include Rails.application.routes.url_helpers
  def current_user
    User.first || User.new
  end


  def protect_against_forgery?
    false
  end
end
=end
# view.render()
