#!/usr/bin/python

from pwd import getpwuid
from os import getenv, setuid, setgid, execv
from sys import exit

SQLUID = 537704221
SQLGID = 537704221
SQLBIN = '/afs/athena.mit.edu/contrib/sql/web_scripts/main/batch/signup.php'

caller = int(getenv('USERHELPER_UID'))
if caller is None or caller == 0:
	exit('No user specified.')
else:
	pw = getpwuid(caller)
	(user_name, user_uid, user_gid) = (pw[0], pw[2], pw[3])

	if len(user_name) and user_uid > 1000:
		setgid(SQLGID)
		setuid(SQLUID)
		execv(SQLBIN, [SQLBIN, str(user_name), str(user_uid), str(user_gid)])
	else:
		print 'Invalid UID:', user_uid
