#!/bin/zsh
# OpenRAR's changes to XADMaster 1.10.8 (LGPL 2.1: modified files are listed here and in Licenses/README.txt).
# 1. `-p -` reads the password from the first line of standard input, so it never shows in the process list.
# 2. lsar/unar link libc++ instead of the long-gone libstdc++.6 (link fix only).
set -euo pipefail
cd "$1"
for f in unar.m lsar.m; do
  perl -0pi -e 's/(NSString \*password=\[cmdline stringValueForOption:\@"password"\];)/$1\n\tif(password&&[password isEqual:\@"-"]) { char buf[4096]; password=fgets(buf,sizeof(buf),stdin)?[[NSString stringWithUTF8String:buf] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]:nil; } \/\/ OpenRAR/' "$f"
  grep -q '// OpenRAR' "$f"
done
sed -i '' 's|1B34AABB11C30F5400396C26 /\* libstdc++.6.dylib \*/ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = "libstdc++.6.dylib"; path = "/usr/lib/libstdc++.6.dylib"; sourceTree = "<absolute>"; };|1B34AABB11C30F5400396C26 /* libstdc++.6.dylib */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = "libc++.tbd"; path = "usr/lib/libc++.tbd"; sourceTree = SDKROOT; };|' XADMaster.xcodeproj/project.pbxproj
sed -i '' 's/-lstdc++\.6/-lc++/g' XADMaster.xcodeproj/project.pbxproj
