#!/usr/bin/python3

import sys
import subprocess

def create_user(login, nomcomplet):
   cmds = ["useradd", "-m", "-c", nomcomplet, "-s", "/bin/bash", login]
   res = subprocess.run(cmds,stdout=subprocess.PIPE)
   print ("createusr : utilisateur ", login, " cree")
   res=subprocess.run(["pwgen", "8", "1"],stdout=subprocess.PIPE)
   pw = res.stdout.decode('utf8')
   print (pw)
   cmds = ["echo", login , ":" ,pw, "chpasswd"]
   cmds = ["echo", login , ":" ,pw >> users.txt.pw]
if  len(sys.argv) != 2 :
	print ("usage : ", sys.argv[0], " <fichier>")
	exit (1)

fichier =  sys.argv[1]
try:
    fh = open(fichier, "r")
except:
    print (sys.argv[0], "erreur ouverture fichier ", fichier)
    exit (2)
else:
    line = fh.readline ()
    while line:
        line = line.rstrip()
#        print (line)
        login,nomcomplet=line.split(':')
        print(login," ", nomcomplet)
        # use realine() to read next line
        create_user(login, nomcomplet)
        line = fh.readline ()
    fh.close()
    exit (0)
