-
Notifications
You must be signed in to change notification settings - Fork 1
/
mess-allocation
executable file
·84 lines (60 loc) · 2.59 KB
/
mess-allocation
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
#!/bin/bash
location='/home' # where the homedirs were created
messtxt="$location"/HAD/mess.txt
# `sed -i` creates a new file with the current user's
# UID/GID, which results in users not being able to
# edit their files as they are now owned by HAD.
# The workaround is to write to a temporary file, then
# `cat` that file into the original one.
tempfile=$(mktemp)
# If user is HAD, allocate messes to students
#
# Order of filing preferences is determined by
# the order of the rollnos appended to mess.txt
if [[ $(id -u) -eq $(id -u 'HAD') ]]; then
messCapacities=() #index->messno.; value->capacity
while IFS=' ' read -r num cap; do
messCapacities[num]=$cap
done < "$messtxt"
IFS=$'\n' echo "${messCapacities[@]}" dfdf
# List of paths of all the userDetails.txt files
readarray userfiles < <(find "$location" -name 'userDetails.txt')
# The rollnos. of students who have filed their preferences
readarray filed < <(grep -o -E '[0-9]{9}' mess.txt)
# We rely on grep to have outputted the rollnos. in order
for rollno in ${filed[@]}; do
# As the deletion command at the end of this loop
# removes any duplicates, we must ensure that the
# current rollno. was not removed for this reason
[[ -z "$(grep -o -E '[0-9]{9}' mess.txt)" ]] && continue
# Find the user file corresponding to the roll no.
userfile=$(grep "$rollno" ${userfiles[@]} | cut -f1 -d:)
pref=$(sed -nE 's/Mess Pref: ([0-9]+)/\1/p' "$userfile")
if [[ ${messCapacities[pref]} -gt 0 ]]; then
# Allocate mess
((messCapacities[pref]-=1))
# update student's userDetails.txt
sed -E 's/(Mess:).*/\1 '"${pref}/" "$userfile" > "$tempfile"
cat "$tempfile" > "$userfile"
fi
# Remove the roll no. from mess.txt
sed -i "s/${rollno}//" "$messtxt"
done
# Update the mess capacities in the file
for ((i=1;i<"${#messCapacities[@]}";i++)); do
sed -Ei "$(($i+1))"',/Student Preferences/ s/([0-9]+) [0-9]+/\1 '"${messCapacities[i]}/" "$messtxt"
done
# If user is an inmate (heh, inmates), file preference
elif [[ $(id -nG) =~ 'inmates' ]]; then
# Mess pref should be given as argument
{ [[ -n "$1" ]] && [[ "$1" != 'help' ]]; } && pref=$1 || {
echo 'Usage: messAllocation mess_number';
exit 1;
}
# update userDetails.txt
userfile=${HOME}/userDetails.txt
sed -Ei 's/(Mess Pref: ).*/\1'"${pref}/" "$userfile"
# update mess.txt
echo $(sed -nE 's/Roll no\.: ([0-9]+)/\1/p' "$userfile") >> "$messtxt"
fi
rm -f "$tempfile"