-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen-student
executable file
·109 lines (88 loc) · 3.05 KB
/
gen-student
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# The directory in which to create the home directories
location='/home'
if [[ $(id -u) -ne 0 ]]; then
echo "Please run as root"
exit 0
fi
# Create a group for all students
groupadd inmates
# (Yes, hostel residents are called 'inmates'
# as per the rulebooks. Rather fitting.)
# Convenience function to add aliases to a .bashrc
# Parameters - 1) the user's home directory 2) Username
_add_aliases() {
sudo -u "$2" touch "$1"/.bashrc
cat /usr/share/deltaT1/aliasdefs.mk >> "$1"/.bashrc
}
# Create HAD's account
useradd --create-home --shell /bin/bash -d "$location"/HAD HAD
cp /usr/share/deltaT1/mess.txt "$location"/HAD
chown HAD:HAD "$location"/HAD/mess.txt
_add_aliases "$location"/HAD HAD
# Read lines from studentDetails.txt or stdin, and
# perform the required tasks
IFS="\n" readarray lines < "${1:-/dev/stdin}"
for line in "${lines[@]:1}"; do
IFS=' ' read -r -a words <<<"$line"
name=${words[0]}
hostel=${words[2]}
roomno=${words[3]}
rollno=${words[1]}
mess=${words[4]}
messpref=${words[5]}
# Create hostel directory if not exists
hosteldir=$location/$hostel
wardenname=warden_$hostel
if ! [[ -d "$hosteldir" ]]; then
mkdir "$hosteldir"
# Add user for Warden
useradd --create-home --shell /bin/bash -d "$hosteldir/$hostel" "$wardenname"
_add_aliases "$hosteldir"/"$hostel" "$wardenname"
# Make the hostel directory belong to the Warden
chown "$wardenname" "$hosteldir" && chgrp "$wardenname" "$hosteldir"
# Add announcements.txt, feeDefaulters.txt
sudo -u "$wardenname" touch "$hosteldir/announcements.txt"
sudo -u "$wardenname" touch "$hosteldir/feeDefaulters.txt"
fi
# Create room directory if not exists
homebase=$hosteldir/$(printf '%.3d' $roomno)
if ! [[ -d "$homebase" ]]; then
sudo -u "$wardenname" mkdir "$homebase"
fi
# Create user
homedir=$homebase/$name
useradd --create-home --shell /bin/bash -d "$homedir" "$name"
_add_aliases "$homedir" "$name"
# ...and add them to the 'inmates'
# group. This will come in handy later.
usermod -a -G inmates "$name"
# Create fees.txt with the cumulative amounts
# set to 0
feesdir="${homedir}/fees"
feestxt="${feesdir}/fees.txt"
mkdir -p "$feesdir"
touch "$feestxt"
chown -R HAD:HAD "$feesdir" # (see README)
echo 'Cumulative amounts:' >> "$feestxt"
cat /usr/share/deltaT1/feeBreakup.txt | sed -E 's/[0-9]+%/0/' >> "$feestxt"
echo '----------' >> "$feestxt"
# Create userDetails.txt
# Assumptions:
# - all students are first-years
# - all students are from ECE (obv. since best department :P )
# - 'Month' refers to the month of account creation
# writes details to file
wd() {
echo "$@" >> "$homedir/userDetails.txt"
}
sudo -u "$name" touch "$homedir/userDetails.txt"
wd Name: "$name"
wd Roll no.: "$rollno"
wd Department: ECE
wd Year: 1
wd Hostel: "$hostel"
wd Mess: "$mess"
wd Month: "$(date +%m)"
wd Mess Pref: "$messpref"
done