#!/usr/bin/env python
import os
import sys
import re
import commands

assert len(sys.argv) in [1, 2]

try:
    prog = sys.argv[1]
except IndexError:
    prog = sys.stdin.read()

# strip off empty lines
lines = filter(lambda x: not re.match(r'^\s*$', x), prog.split('\n'))

# hard-coded expansion rule: 1 tab is 4 spaces
lines = [l.replace('\t', '    ') for l in lines]

# find the indentation level of the first line
indent_level = len(re.findall('^ *', lines[0])[0])
# every line is unindented by this amount
lines = [re.sub('^ {,%d}' % indent_level, '', line) for line in lines]
prog = '\n'.join(lines)

os.execv(commands.getoutput('which python'), ['python', '-c', prog])
