#!/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: {}".format(fn))
    print("usage: sage [options]")
    print("Try 'sage -h' for more information.")
    sys.exit(1)


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