-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-submission.sh
executable file
·70 lines (53 loc) · 1.77 KB
/
extract-submission.sh
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
#!/bin/bash
if [ -z "$1" ]; then
echo "usage: $0 <zipfile>"
fi
function get_field() {
grep "^$1:" "$f" | head -n1 | sed 's/^'"$1"': \(.*\)$'"/\1/" |tr -d "\r"
}
zipfile="$1"
tempdir="/tmp/extract-submission-$$-$USER"
ex1dir=$tempdir/ex1dir
prefix="submissions"
mkdir -p "$prefix"
logfile="$prefix/extract.log"
date > "$logfile"
infofile="$prefix/info.csv"
echo "extracted from $zipfile" > "$infofile"
rm -f "$infofile"
mkdir -p "$ex1dir"
unzip -d "$ex1dir" "$zipfile">> "$logfile" 2>&1 || echo "*** couldn't unzip $zipfile"
for f in "$ex1dir"/*.txt; do
name=`get_field Name`
date=`get_field "Date Submitted"`
#name=`grep '^Name:' "$f" | sed 's/^Name:\(.*\)$/\1/'`
files=`grep -P "^\tFilename:" "$f" | sed 's/^.*Filename: \(.*\)$/\1/'|tr -d "\r"`
echo " extracting $name"
sid=`echo $name | sed 's/.*(\([0-9]*\)).*/\1/'`
echo "$files"
clean_name=`echo "$name" | tr " " "_" | sed 's/_([0-9]*)$//'`
ex2dir="$prefix/${clean_name}_${sid}"
mkdir -p "$ex2dir"
rm -f "$ex2dir"/*
fileformat="none"
if echo "$files" | grep -i -q '.zip$'; then
unzip -j -d "$ex2dir" "$ex1dir/$files" >> "$logfile" 2>&1 || echo "*** couldn't unzip $ex1dir/$files"
fileformat="zip"
sub_files=`ls "$ex2dir"`
else
if echo "$files" | grep -i -q '.rar$'; then
echo "RARFILE $files" > "$ex2dir"/WRONGFILEFORMAT
(cd $ex2dir && unrar e "$ex1dir/$files") >> "$logfile" 2>&1 || echo "*** couldn't unrar $ex1dir/$files"
fileformat="rar"
else
echo "$files" > "$ex2dir"/WRONGFILEFORMAT
fi
fi
sub_files=`ls "$ex2dir" | tr "\n" " " | tr "\t" " " | tr -s " "`
cp "$f" "$ex2dir"/submission.txt
echo "\"$sid\",\"$name\",\"$date\",\"$fileformat\",\"$files\",\"$sub_files\"" >> $infofile
#unzip -d "$ex2dir" "$ex1dir/$files"
#ls "$ex1dir/$files"
#echo "Files: $files"
done
#rm -rf "$tempdir"