This is the script provided by Microsoft (from //support.microsoft.com/kb/918992), modified to check that the login doesn’t exist and that the default database does exists before creating the login.