#!/usr/bin/env python3

# Copyright (c) 2013, Ruslan Baratov
# All rights reserved.

import argparse
import glob
import os
import subprocess

parser = argparse.ArgumentParser(
    description='Workaround for not working ios install command'
)

parser.add_argument(
    '--target',
    type=str,
    required=True,
    help='install target'
)

parser.add_argument(
    '--destination',
    type=str,
    required=True,
    help='destination directory'
)

debug_prefix = 'd'

args = parser.parse_args()
target = args.target
dst = args.destination

os.makedirs(dst, exist_ok=True)

def run_xcode(configuration, sdk):
  if sdk == 'iphonesimulator':
    arch = '-arch i386'
  else:
    arch = ''
  subprocess.check_call(
      'xcodebuild -target {} -configuration {} -sdk {} {}'.format(
          target, configuration, sdk, arch
      ),
      shell=True
  )

run_xcode('Debug', 'iphoneos')
run_xcode('Release', 'iphoneos')
run_xcode('Debug', 'iphonesimulator')
run_xcode('Release', 'iphonesimulator')

def detect_file(dir):
  lib_list = glob.glob('./*/{}/*{}*'.format(dir, target))
  if len(lib_list) == 0:
    print('target "{}" not found in directory "{}"'.format(target, dir))
  if len(lib_list) != 1:
    print('multiple target "{}" found in directory "{}"'.format(target, dir))
  return lib_list[0]

debug_arm = detect_file('Debug-iphoneos')
debug_x86 = detect_file('Debug-iphonesimulator')

debug_result = '{}/lib{}{}.a'.format(dst, target, debug_prefix)
subprocess.check_call(
    'lipo -output {} -create {} {}'.format(
        debug_result, debug_arm, debug_x86
    ),
    shell=True
)

release_arm = detect_file('Release-iphoneos')
release_x86 = detect_file('Release-iphonesimulator')

release_result = '{}/lib{}.a'.format(dst, target)
subprocess.check_call(
    'lipo -output {} -create {} {}'.format(
        release_result, release_arm, release_x86
    ),
    shell=True
)

print(
    'Install universal library done:\n    {}\n    {}'.format(
        debug_result, release_result
    )
)

@@ -133,16 +133,27 @@
 bprocess
+%0Aimport sys
 %0A%0Aparser
@@ -462,98 +462,426 @@
 %0A)%0A%0A
-debug_prefix = 'd'%0A%0Aargs = parser.parse_args()%0Atarget = args.target%0Adst = args.destination
+parser.add_argument(%0A    '--verbose',%0A    action='store_true',%0A    help='print a lot info'%0A)%0A%0Aclass Log:%0A  def __init__(self, verbose):%0A    self.verbose = verbose%0A  def p(self, message):%0A    if self.verbose:%0A      print(message)%0A%0Adebug_prefix = 'd'%0A%0Aargs = parser.parse_args()%0Atarget = args.target%0Adst = args.destination%0A%0Alog = Log(args.verbose)%0A%0Alog.p('target: %7B%7D'.format(target))%0Alog.p('destination: %7B%7D'.format(dst))
 %0A%0Aos
@@ -1360,41 +1360,50 @@
 :%0A  
-lib_list = glob.glob('./*/%7B%7D/*%7B%7D*
+cwd = os.getcwd()%0A  target_file = 'lib%7B%7D.a
 '.fo
@@ -1411,119 +1411,168 @@
 mat(
-dir, 
 target)
-)
 %0A  
-if len(lib_list) == 0:%0A    print('target %22%7B%7D%22 not found in directory %22%7B%7D%22'.format(targe
+for root, dirs, files in os.walk(cwd):%0A    if not dir in dirs:%0A      continue%0A    for target_root, x, target_files in os.walk(os.path.join(roo
 t, dir))
 %0A  i
@@ -1571,65 +1571,179 @@
 ir))
+:
 %0A  
-if len(lib_list) != 1:%0A    print('multiple target
+    if target_file in target_files:%0A        return os.path.join(target_root, target_file)%0A  print('working directory: %7B%7D'.format(cwd))%0A  sys.exit(%0A      'file
  %22%7B%7D%22
+ not
  fou
@@ -1781,36 +1781,23 @@
 rget
+_file
 , dir)
-)
 %0A  
-return lib_list%5B0%5D
+)
 %0A%0Ade
@@ -1886,16 +1886,89 @@
 ator')%0A%0A
+log.p('detected debug files:%5Cn  %7B%7D%5Cn  %7B%7D'.format(debug_arm, debug_x86))%0A%0A
 debug_re
@@ -2258,16 +2258,95 @@
 ator')%0A%0A
+log.p('detected release files:%5Cn  %7B%7D%5Cn  %7B%7D'.format(release_arm, release_x86))%0A%0A
 release_

improve library search [skip ci]