armdir = "/Users/ott/ottl/project/QFitsView/build/QFitsView.app/"
x64dir = "/Users/ott/QFitsView.app/"

// find frameworks and replace x64 code by Qt5
cd armdir
armframeworks = findfile("*.framework")

for i=1, nelements(armframeworks) {
  cd armdir + armframeworks[i] + "/Versions"
  print "ln -s Current 5"
  exec "ln -s Current 5"
  cd "Current"
  
  file = replace(replace(armframeworks[i], ".framework", ""), "Contents/Frameworks/", "")
  print "lipo " + file + " -remove x86_64 -o " + file
  exec "lipo " + file + " -remove x86_64 -o " + file
  if (fileexists(x64dir + armframeworks[i]) == 1) {
    print "lipo -create -output " + file + " " + x64dir + armframeworks[i] + "/Versions/Current/" + file + " " + file
    exec "lipo -create -output " + file + " " + x64dir + armframeworks[i] + "/Versions/Current/" + file + " " + file
  }
}

cd armdir
armlibs = findfile("*.dylib")
print "rsync -av --ignore-existing " + x64dir + "Contents/PlugIns/" + " Contents/PlugIns/"
exec "rsync -av --ignore-existing " + x64dir + "Contents/PlugIns/" + " Contents/PlugIns/"
for i=1, nelements(armlibs) {
  print "lipo " + armdir + armlibs[i] + " -remove x86_64 -o " + armdir + armlibs[i]
  exec "lipo " + armdir + armlibs[i] + " -remove x86_64 -o " + armdir + armlibs[i]
  if (fileexists(x64dir + armlibs[i]) == 1) {
    print "lipo -create -output " + armdir + armlibs[i] + " " + x64dir + armlibs[i]+ " " + armdir + armlibs[i]
    exec "lipo -create -output " + armdir + armlibs[i] + " " + x64dir + armlibs[i]+ " " + armdir + armlibs[i]
  }
}

cd armdir
file = "Contents/MacOS/QFitsView"
print "lipo " + file + " -remove x86_64 -o " + file
exec "lipo " + file + " -remove x86_64 -o " + file
print "lipo -create -output " + file + " " + x64dir + file + " " + file
exec "lipo -create -output " + file + " " + x64dir + file + " " + file

cd x64dir
file = "Contents/Info.plist"
cd armdir
print "cp " + x64dir + file + " " + armdir + file
exec "cp " + x64dir + file + " " + armdir + file
file = "Contents/Resources/telescope.icns"
print "cp " + x64dir + file + " " + armdir + file
exec "cp " + x64dir + file + " " + armdir + file

/*
cd x64dir
x64libs = findfile("*.dylib")
for i=1, nelements(x64libs) {
  if (fileexists(armdir + x64libs[i]) == 0) {
    print "file " + x64libs + " not there"
  }
}
*/

/*
appdir = pwd()
armdir = pwd() + "/QFitsView.app/Contents/Frameworks/"
x64dir = "/Users/ott/QFitsView.app/Contents/Frameworks/"

for i=1, nelements(frameworks) {
  print "cd " + armdir + frameworks[i] + ".framework/Versions/Current"
	print "lipo -remove x86_64 " + frameworks[i] + " -o " + frameworks[i] + ".arm"
	print "cp " + x64dir + frameworks[i] + ".framework/Versions/Current/" + frameworks[i] + " " + frameworks[i] + ".x64"
	print "rm " + frameworks[i]
	print "lipo -create -output " + frameworks[i] + " " + frameworks[i] + ".arm " + frameworks[i] + ".x64"
	print "cd .."
	print "ln -s current 5"
}

dylib = stringarray(0)
dylib += "Contents/PlugIns/platforminputcontexts/libqtvirtualkeyboardplugin.dylib"
dylib += "Contents/PlugIns/platforms/libqcocoa.dylib"
dylib += "Contents/PlugIns/styles/libqmacstyle.dylib"
dylib += "Contents/PlugIns/tls/libqsecuretransportbackend.dylib"
dylib += "Contents/PlugIns/tls/libqopensslbackend.dylib"
dylib += "Contents/PlugIns/tls/libqcertonlybackend.dylib"
dylib += "Contents/PlugIns/iconengines/libqsvgicon.dylib"
dylib += "Contents/PlugIns/imageformats/libqgif.dylib"
dylib += "Contents/PlugIns/imageformats/libqwbmp.dylib"
dylib += "Contents/PlugIns/imageformats/libqwebp.dylib"
dylib += "Contents/PlugIns/imageformats/libqico.dylib"
dylib += "Contents/PlugIns/imageformats/libqmacheif.dylib"
dylib += "Contents/PlugIns/imageformats/libqjpeg.dylib"
dylib += "Contents/PlugIns/imageformats/libqtiff.dylib"
dylib += "Contents/PlugIns/imageformats/libqsvg.dylib"
dylib += "Contents/PlugIns/imageformats/libqpdf.dylib"
dylib += "Contents/PlugIns/imageformats/libqicns.dylib"
dylib += "Contents/PlugIns/imageformats/libqtga.dylib"
dylib += "Contents/PlugIns/imageformats/libqmacjp2.dylib"
dylib += "Contents/PlugIns/networkinformation/libqscnetworkreachability.dylib"

print "cd " + appdir + "/QFitsView.app"
for i=1, nelements(dylib) {
  print "lipo -remove x86_64 " + dylib[i] + " -o " + dylib[i] + ".arm"
	print "cp " + x64dir + "../../" + dylib[i] + " " + dylib[i] + ".x64"
	print "lipo -create -output " + dylib[i] + " " + dylib[i] + ".arm " + dylib[i] + ".x64"
}

addlib = stringarray(0)
addlib += "Contents/PlugIns/printsupport/libcocoaprintersupport.dylib"
addlib += "Contents/PlugIns/bearer/libqgenericbearer.dylib"
addlib += "Contents/PlugIns/virtualkeyboard/libqtvirtualkeyboard_thai.dylib"
addlib += "Contents/PlugIns/virtualkeyboard/libqtvirtualkeyboard_openwnn.dylib"
addlib += "Contents/PlugIns/virtualkeyboard/libqtvirtualkeyboard_hangul.dylib"
addlib += "Contents/PlugIns/virtualkeyboard/libqtvirtualkeyboard_pinyin.dylib"
addlib += "Contents/PlugIns/virtualkeyboard/libqtvirtualkeyboard_tcime.dylib"

print "mkdir Contents/PlugIns/printsupport"
print "mkdir Contents/PlugIns/bearer"
print "mkdir Contents/PlugIns/virtualkeyboard"

for i=1, nelements(addlib) {
  print "cp " + x64dir + "../../" + addlib[i] + " " + addlib[i]
}

print "cp ../../resources/Info.plist Contents/"
print "cp ../../resources/telescope.icns Contents/Resources/"

print "lipo -remove x86_64 Contents/MacOS/QFitsView -o Contents/MacOS/QFitsView.arm"
print "lipo -create -output Contents/MacOS/QFitsView Contents/MacOS/QFitsView.arm " + x64dir + "../MacOS/QFitsView"
*/
