#!/usr/bin/env python

import os
import subprocess
import sys

acts = []
config = 'desktop'

if len(sys.argv) > 1:
    if sys.argv[1] not in ('build', 'clean', 'rebuild', 'everything'):
        config = sys.argv[1]
    else:
        act = sys.argv[1]
        if act == 'rebuild':
            acts = ['distclean', 'build']
        else:
            acts = [act]
        if len(sys.argv) > 2:
            config = sys.argv[2]

homedir = os.getenv('HOME')
configfile = os.path.join(homedir, '.mozconfig-%s' % (config,))
if not os.path.exists(configfile):
    configfile = os.getenv('MOZCONFIG')
    if not configfile or not os.path.exists(configfile):
        sys.stderr.write("Can't find mozconfig\n")
        sys.exit(1)

os.environ['MOZCONFIG'] = configfile
print 'MOZCONFIG=%s' % configfile
for act in acts:
    print 'make -f client.mk %s' % act
    rval = subprocess.call(['make', '-f', 'client.mk', act], env=os.environ)
    if rval:
        print '!!! BUILD FAILED !!!'
        sys.exit(rval)
print '!!! BUILD SUCCEEDED !!!'
sys.exit(0)
