#!/usr/bin/env python3

import sys
import subprocess

def doit(todo):
  print('')

  input = ''
  expectedtowork = False
  for line in todo:
    expectedtowork = ' # ok' in line
    # note: only the last line matters
    print('< '+line)
    input += line.split(' #')[0].strip()+'\n'
  if todo == ['HOLTrace 1']: expectedtowork = True

  cmds = './holtrace-prune','./holtrace-prune-simple'

  data = {}
  for cmd in cmds:
    print(cmd)
    result = subprocess.run([cmd],input=input,text=True,capture_output=True)
    for label,what in ('stdout:',result.stdout),('>',result.stderr):
      what = what.splitlines()
      if len(what) == 0: continue
      if label == 'stdout:':
        for line in what:
          print(f'{label} {line}')
      else:
        if len(what) > 1: print(f'{label} ...')
        print(f'{label} {what[-1]}')
    if result.returncode == 0:
      print('success')
      data[cmd] = 'success',result.stdout
    else:
      print('failure',result.returncode)
      data[cmd] = 'failure'
    if expectedtowork != (result.returncode == 0):
      print('ALERT')

  if any(data[cmd] != data[cmds[0]] for cmd in cmds):
    print('ALERT mismatch across commands')

def main():
  doit([])
  doit([''])
  doit(['b0bool'])
  doit(['HOLTrace'])
  doit(['HOLTrace 1'])

  todo = ['HOLTrace 1']
  for line in sys.stdin:
    line = line.strip()
    if len(line) == 0:
      if todo != ['HOLTrace 1']: doit(todo)
      todo = ['HOLTrace 1']
      continue
    if line[0] == '#': continue
    todo += [line]
  if todo != ['HOLTrace 1']: doit(todo)
  todo = []

if __name__ == '__main__':
  main()
