#!/usr/bin/env python

import sys, os
from rapydcss.SASStoSCSS import parse_file
from scss import Scss

if __name__ == "__main__":
	orig_dir = os.getcwd()
        try:
	    base_dir, orig_file = os.path.split(sys.argv[1])
	    filename = orig_file.rsplit('.', 1)[0] # remove the extension
        except:
            print('Provide a valid input file')
            sys.exit(0)

	if base_dir:
		os.chdir(base_dir)
	
	# convert SASS to SCSS, if needed
	scss_is_temp = False
	if orig_file[-5:] == '.sass':
		scss_is_temp = True
		with open(filename+'.scss', 'w') as output:
			parse_file(orig_file, output)
	
	# convert SCSS to CSS
	css = Scss()
	with open(filename+'.scss', 'r') as temp:
		with open(filename+'.css', 'w') as output:
			output.write(css.compile(temp.read()))
	
	if scss_is_temp:
		os.remove(filename+'.scss')
	
