#!/usr/bin/python
# launchpadduser.py
# To do:
#   * put stuff into functions, maybe
#   * launchpad apparently works with word-wrapped SSH keys, OpenSSH doesn't, deal with that somehow
#   * tests?  how do you test something like this...?
#   * error reporting
#   * verify that the user exists in launchpad before creating them
#   * get GECOS fields from some launchpad structured thing (does launchpad export that?)

import sys
import os
import pwd
from subprocess import call

def do(*x):
    call(x)

do("adduser", sys.argv[1], "--disabled-password", "--gecos", sys.argv[1])
userinfo = pwd.getpwnam(sys.argv[1])
sshdir = os.path.join(userinfo.pw_dir, ".ssh")
authkeyz = os.path.join(sshdir, "authorized_keys")

os.mkdir(sshdir)

do("wget", "-O", authkeyz, "https://launchpad.net/~%s/+sshkeys" % (sys.argv[1],))

os.chown(sshdir, userinfo.pw_uid, userinfo.pw_gid)
os.chown(authkeyz, userinfo.pw_uid, userinfo.pw_gid)

os.chmod(sshdir, 0700)
os.chmod(authkeyz, 0644)

