#!/usr/bin/env python

import os, sys
from subprocess import call

if len(sys.argv) < 2:
    print "You must give a file argument"
    sys.exit(1)

fn = sys.argv[1]
opts = sys.argv[2:]

if fn.startswith('-'):
    print "sage-run received unknown option: %s " % fn
    print "usage: sage [options]"
    print "Try 'sage -h' for more information."
    sys.exit(1)

binpath = os.path.join(os.environ['SAGE_LOCAL'], 'bin')

if fn.endswith('.sage'):
    if call(['sage', '-preparse', fn]) != 0:
        sys.exit(1)
    os.execv(os.path.join(binpath, 'sage-python'), ['sage-python', fn[:-5] + '.py'] + opts)
elif fn.endswith('.pyx') or fn.endswith('.spyx'):
    os.execv(os.path.join(binpath, 'sage-run-cython'), ['sage-run-cython', fn] + opts)
else:
    os.execv(os.path.join(binpath, 'sage-python'), ['sage-python', fn] + opts)
