@@ -13,18 +13,18 @@ DEFAULT_OSA_LANG=AppleScript
1313OSA_LANG=$DEFAULT_OSA_LANG
1414FORBIDDEN_TEXT=" AppleScript Debugger" # colon seperated list
1515FORBIDDEN=()
16- DEBUG =0
16+ DO_LOG =0
1717WRITE_HEADER=1
1818FILE=
1919SCRATCH=
2020
2121function finish {
2222 # SCRATCH is initialized to a temp directory only at the moment it's needed
2323 if [[ $SCRATCH ]]; then
24- if [[ $DEBUG = 0 ]]; then
24+ if [[ $DO_LOG = 0 ]]; then
2525 rm -Rf " $SCRATCH "
2626 else # leave the temporary files when in debug or logging mode
27- debug " stopped: $( date ' +%Y-%m-%d %H:%M:%S' ) (temporary files have been left for inspection)"
27+ log_line " stopped: $( date ' +%Y-%m-%d %H:%M:%S' ) (temporary files have been left for inspection)"
2828 # make the log operation "atomic"
2929 cat $SCRATCH /tmp.log >> $LOG_PATH /$SCRIPT_NAME .log
3030 fi
@@ -45,11 +45,10 @@ function usage {
4545 echo " arguments (all optional):"
4646 echo " -f, --forbidden Provide (colon-seperated) forbidden languages. '-' for empty list, defaults to 'AppleScript Debugger'"
4747 echo " -n, --no-header Don't write a OSA-lang header for the default language (AppleScript)"
48- echo " -d, --debug Write debug info to stderr"
4948 echo " -l, --log Write debug info to '$LOG_PATH /$SCRIPT_NAME .log'"
5049 echo " -h, -?, --help Show this help message and exit"
5150 echo " -v, --version Show program's version number and exit"
52- echo " FILE Filename of current stream. Useful for debugging/logging only "
51+ echo " FILE Filename of current stream; not actually used but comes in handy when debugging "
5352 echo
5453 echo " This script translates input from stdin to stdout only. The options '--forbidden' and '--no-header' "
5554 echo " are only used with the 'clean' command."
@@ -63,10 +62,8 @@ function show_version {
6362 echo " $SCRIPT_NAME v$SCRIPT_VER "
6463}
6564
66- function debug {
67- if [[ $DEBUG = 1 ]]; then
68- >&2 printf " DEBUG: %s\n" " $@ "
69- elif [[ $DEBUG = 2 ]]; then
65+ function log_line {
66+ if [[ $DO_LOG = 1 ]]; then
7067 printf " %s\n" " $@ " >> $SCRATCH /tmp.log
7168 fi
7269}
@@ -75,7 +72,7 @@ function ERROR {
7572 ERR_NR=$1
7673 shift
7774 >&2 echo " ERROR: $@ "
78- debug " ERROR($ERR_NR ): $@ "
75+ log_line " ERROR($ERR_NR ): $@ "
7976 exit $ERR_NR
8077}
8178
@@ -94,8 +91,7 @@ while (( $# > 0 )) ; do
9491 case $1 in
9592 -f | --forbidden) [[ $# > 1 ]] || usage " FORBIDDEN argument expected after $1 " ; FORBIDDEN_TEXT=$2 ; shift ;;
9693 -n | --no-header) WRITE_HEADER=0;;
97- -d | --debug) DEBUG=1;;
98- -l | --log) DEBUG=2;;
94+ -l | --log) DO_LOG=1;;
9995 -h | -\? | --help) usage;;
10096 -v | --version) show_version; exit 0;;
10197 -* ) usage " Unrecognized switch '$1 '" ;;
@@ -115,15 +111,15 @@ IFS=$'\n\t'
115111[[ -d " $LOG_PATH " ]] || mkdir -p " $LOG_PATH "
116112SCRATCH=$( mktemp -d -t osagitfilter.tmp)
117113
118- debug " ---=[ $( show_version) ]=----------------------------------------------"
119- debug " started: $( date ' +%Y-%m-%d %H:%M:%S' ) "
120- debug " sw_vers: $( sw_vers | tr -d ' \t' | tr ' \n' ' ;' ) "
121- debug " call: $CALLED_WITH "
122- debug " caller: $( ps -o args= $PPID ) " # see: https://stackoverflow.com/a/26985984/56
123- debug " scratch: $SCRATCH "
124- debug " pwd: $( pwd) "
125- debug " command: $CMD "
126- debug " filename: '$FILE '"
114+ log_line " ---=[ $( show_version) ]=----------------------------------------------"
115+ log_line " started: $( date ' +%Y-%m-%d %H:%M:%S' ) "
116+ log_line " sw_vers: $( sw_vers | tr -d ' \t' | tr ' \n' ' ;' ) "
117+ log_line " call: $CALLED_WITH "
118+ log_line " caller: $( ps -o args= $PPID ) " # see: https://stackoverflow.com/a/26985984/56
119+ log_line " scratch: $SCRATCH "
120+ log_line " pwd: $( pwd) "
121+ log_line " command: $CMD "
122+ log_line " filename: '$FILE '"
127123
128124if [[ $CMD = clean ]]; then
129125
@@ -133,8 +129,8 @@ if [[ $CMD = clean ]]; then
133129
134130 # determine osa-language of input file
135131 OSA_LANG=$( $OSA_GET_LANG_CMD $CLEAN_SCPT_FILE )
136- debug " OSA lang: $OSA_LANG "
137- debug " forbidden langs: ${FORBIDDEN[@]:- } "
132+ log_line " OSA lang: $OSA_LANG "
133+ log_line " forbidden langs: ${FORBIDDEN[@]:- } "
138134
139135 # check if the osa-lang is forbidden
140136 for BL in " ${FORBIDDEN[@]:- } " ; do
@@ -145,7 +141,7 @@ if [[ $CMD = clean ]]; then
145141
146142 # write header
147143 if [[ $WRITE_HEADER = 0 && " $OSA_LANG " = " $DEFAULT_OSA_LANG " ]]; then
148- debug " default OSA language is AppleScript: writing no header because of --no-header"
144+ log_line " default OSA language is AppleScript: writing no header because of --no-header"
149145 else
150146 if [[ $OSA_LANG = " JavaScript" ]]; then
151147 comment=" //"
@@ -156,7 +152,7 @@ if [[ $CMD = clean ]]; then
156152 fi
157153
158154 # decompile to text, and strip tailing whitespace (never newlines, because of $) and finally remove last line if it's empty
159- debug " Starting osadecompile, strip trailing whitespace and remove last line if it's empty (which is added by osacompile)"
155+ log_line " Starting osadecompile, strip trailing whitespace and remove last line if it's empty (which is added by osacompile)"
160156 osadecompile $CLEAN_SCPT_FILE | sed -E ' s/[[:space:]]*$//' | perl -pe ' chomp if eof'
161157
162158elif [[ $CMD = smudge ]]; then
@@ -168,10 +164,10 @@ elif [[ $CMD = smudge ]]; then
168164 while IFS= read -r line; do
169165 if [[ -z $FIRST_LINE_READ ]]; then
170166 FIRST_LINE_READ=YES
171- debug " first line: '$line '"
167+ log_line " first line: '$line '"
172168 if [[ $line =~ ^(# |\/\/)@osa-lang: ]]; then
173169 OSA_LANG=$( echo $line | sed -E ' s/^(#|\/\/)@osa-lang:(.*)$/\2/' )
174- debug " osa-lang header: '$OSA_LANG '"
170+ log_line " osa-lang header: '$OSA_LANG '"
175171 continue
176172 fi
177173 fi
@@ -181,11 +177,11 @@ elif [[ $CMD = smudge ]]; then
181177 if [[ -n " $line " ]]; then
182178 printf " $line " >> $SMUDGE_TXT_FILE
183179 fi
184- debug " osa-lang: $OSA_LANG "
180+ log_line " osa-lang: $OSA_LANG "
185181 # Create a temporary file, for storing output of osacompile
186182 SMUDGE_SCPT_FILE=$SCRATCH /tmp_smudge_stdout.scpt
187183 # Perform the compilation
188- debug " Starting osacompilation"
184+ log_line " Starting osacompilation"
189185 cat $SMUDGE_TXT_FILE | osacompile -l " $OSA_LANG " -o $SMUDGE_SCPT_FILE
190186 # Put the output on the stdout
191187 cat $SMUDGE_SCPT_FILE
0 commit comments