#!/bin/bash

AUTOCLOSE='true'
if [ "x""$1" == "x--noautoclose" ]; then
  AUTOCLOSE='false'
fi

CMD="$( basename "$0" )"

if [ "x""$AUTOCLOSE" == 'xtrue' ]; then
  PROCID="$$"
  TERMINAL_NAME="$CMD""$PROCID"
  echo -n -e "\033]0;""$TERMINAL_NAME""\007" # changes the running Terminal name for use when closing below
fi

ORIG_DIR=$( pwd )
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd "${DIR}"

if [[ -e SignStream3.app ]]; then
  xattr -d -r -s com.apple.quarantine SignStream3.app
fi
if [[ -f GetSignBank.command ]]; then
  xattr -d -r -s com.apple.quarantine GetSignBank.command
fi
if [[ -f Update.command ]]; then
  xattr -d -r -s com.apple.quarantine Update.command
fi
if [[ -f DisableQuarantine.command ]]; then
  xattr -d -r -s com.apple.quarantine DisableQuarantine.command
fi

cd "${ORIG_DIR}"

if [ "x""$AUTOCLOSE" == 'xtrue' ]; then 
  $( while pgrep -f "$CMD" | grep '^'"$PROCID"'$' &>/dev/null; do sleep 1; done; osascript -e 'tell application "Terminal" to close (first window whose name contains "'"$TERMINAL_NAME"'")' &> /dev/null ) &
fi

exit
