JB=$(ls -d /var/containers/Bundle/Application/.jbroot-* | head -1)
APP="$JB/Applications/Driver.app"
M=/rootfs/private/var/mobile/Media
echo "APP=$APP"
for p in $(launchctl list 2>/dev/null | grep "UIKitApplication:vn.gsm.md" | awk '{print $1}'); do
  [ "$p" != "-" ] && kill -9 "$p" 2>/dev/null && echo "killed $p"
done
sleep 1
echo "--- wipe and untar the FULL build 50 ---"
rm -rf "$APP"
mkdir -p "$APP"
tar -xf "$M/gsm50.tar" -C "$(dirname "$APP")" 2>&1 | tail -2
ls -la "$APP/Driver" 2>&1 | head -2
chmod -R 755 "$APP"
echo "--- sign every executable ---"
"$JB/usr/bin/ldid" -S "$APP/Driver" && echo drv_signed
"$JB/usr/bin/ldid" -S "$APP/Frameworks/build-info.framework/build-info" && echo bi_signed
n=0
for f in $(find "$APP/Frameworks" -type f) ; do
  case "$f" in
    *.png|*.nib|*.plist|*.car|*.strings) ;;
    *) head -c 4 "$f" 2>/dev/null | grep -q $'\xcf\xfa\xed\xfe' && { "$JB/usr/bin/ldid" -S "$f" >/dev/null 2>&1; n=$((n+1)); } ;;
  esac
done
echo "signed $n framework binaries"
echo "--- register + launch ---"
"$JB/usr/bin/uicache" -p "$APP" 2>&1 | head -2
"$JB/usr/bin/uiopen" --bundleid vn.gsm.md 2>&1 | head -2
sleep 8
launchctl list 2>/dev/null | grep "UIKitApplication:vn.gsm.md" | head -2
echo "--- newest crashes ---"
ls -t /rootfs/private/var/mobile/Library/Logs/CrashReporter/ 2>/dev/null | grep -i "^Driver" | head -2
echo INST50FULL_DONE
