s4

view scripts/add-user-csv.sh @ 600:f68f13fb226b

Usage format fixed
author HIROSE Yuuji <yuuji@gentei.org>
date Fri, 03 Apr 2020 19:50:45 +0900
parents 7ab2ceb1b8dd
children 7c78ebd2f39f
line source
1 #!/bin/sh
3 if [ -z "$1" ]; then
4 cat<<-EOF >&2
5 Usage: ./add-user-csv.sh CSVfile
6 Csv file should be the form of:
7 username(email),notify-email,gecos,IniPassword
8 Set \$DISABLED for setting disabled password
9 EOF
10 exit 1
11 fi
13 target=$(cd `dirname "$1"`; pwd)/`basename $1`
14 encp=${DISABLED:+DisabledPassword}
16 cd `dirname $0`/..
17 if ! . ./s4-funcs.sh; then
18 echo "Cannot find s4-funcs.sh, which should be located in $mydir/.."
19 exit 1
20 fi
22 sq $db<<-EOF |
23 .mode csv
24 CREATE TEMPORARY TABLE _newuser(user,email,gecos,inipswd);
25 .import $target _newuser
26 .mode list
27 .sepa ,
28 SELECT * FROM _newuser n
29 WHERE NOT EXISTS (SELECT * FROM user WHERE name=n.user);
30 EOF
31 while IFS=, read user email gecos inipswd
32 do
33 encpswd=${encp:-`mycrypt "$inipswd" 'StOpmE'`}
34 case "$user" in
35 *\'*) echo "Skipping [$user]"
36 continue ;;
37 *@*) ;; # OK
38 *) echo "Skipping [$user] because of lacking email pattern" >&2
39 continue ;;
40 esac
41 # echo "$user" "$user" "$inipswd" "$encpswd"
42 query "INSERT INTO user VALUES('$user');" &&
43 echo Adding $user
44 dbsetbyid user "$user" email "$email"
45 dbsetbyid user "$user" gecos "$gecos"
46 dbsetbyid user "$user" pswd "$encpswd"
47 done