#!/bin/bash -ue

# $1 = 0 : disable workaround
# $1 = 1 : enable workaround

trap 'rm -f fifo[01] exe exe.o ; kill 0' EXIT
mkfifo fifo0
mkfifo fifo1

xterm -e bash -c 'tty > fifo0 ; cat < fifo1' &
PTS=$( cat fifo0 )

g++ -x c++ -c -o exe.o -I/usr/include/python2.7 - << EOF
#include <Python.h>
#include <stdio.h>
#include <assert.h>
int main()
{
	Py_Initialize();
	FILE *file = fopen("$PTS", "rw");
	PyRun_InteractiveLoop(file, "$PTS");
	Py_Finalize();
}
EOF
g++ -o exe exe.o -lutil -lpython2.7

if [[ $1 == "0" ]] ; then ./exe ; else ./exe >$PTS <$PTS 2>$PTS ; fi
exec {a}>fifo1 ; exec {a}>&-
wait
