-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc937-export.sh
executable file
·105 lines (85 loc) · 2.98 KB
/
mc937-export.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
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
#! /bin/bash
# Last edited on 2015-03-03 20:42:59 by stolfi
usage="$0 [ -tag TAG ] main.png main.pov [ OTHERFILES... ]"
# This script is executed by students in order to hand labwork in.
# Copies the named files from the current directory to the user's
# directory "public_html/mc937/YYYY-MM-DD/", creating it if necessary. Also
# sets the proper file and directory permissions.
# If "-tag" is given, the destination directory will be
# "public_html/mc937/YYYY-MM-DD-TAG/".
# Must be executed in a Linux environment.
# For static labs, IMAGE must be "main.png"
# For animation, IMAGE must be "main-s.png"
# ----------------------------------------------------------------------
# Parameter parsing:
if [[ $# -lt 2 ]]; then
echo "$0: bad arguments" 1>&2
echo "usage: ${usage}" 1>&2; exit 1
fi
tag=""
while [[ ( $# -gt 0 ) && ( "/$1" =~ /-.* ) ]]; do
if [[ ( $# -ge 2 ) && ( "/$1" == "/-tag" ) ]]; then
tag="-$2"; shift; shift;
else
echo 'invalid option "'"$1"'"' 1>&2;
echo "usage: ${usage}" 1>&2; exit 1
fi
done
image="$1"; shift
source="$1"; shift
extras=( $* )
# ----------------------------------------------------------------------
# Internal parameters:
disc="mc937"
dt=`/bin/date '+%Y-%m-%d'`
pubdir="${HOME}/public_html/${disc}/${dt}${tag}"
# ----------------------------------------------------------------------
# Consistency checks:
# Make sure the files have the right extension:
if [[ "@${image}" != "@main.png" ]]; then
echo "${image}"' should be "main.png"' 1>&2; exit 1
fi
if [[ "@${source##*.}" != "@pov" ]]; then
echo "${source}"' should be "main.pov"' 1>&2; exit 1
fi
# Make sure the target directory exists:
mkdir -p ${pubdir}
# Make sure the target directory is writable by user:
chmod u+w ${pubdir}
# Make sure that the target directory is readable through HTTP:
tmp="${pubdir}"
while [[ "@${tmp}" != "@${HOME}" ]]; do
# echo "tmp = ${tmp}"
chmod a+rX ${tmp}
tmp="${tmp%/*}"
done
chmod -v a+X ${HOME}
# Check whether we succeeded:
if [[ ! ( -d ${pubdir} ) ]]; then
echo "failed to create directory ${pubdir}" 1>&2; exit 1
fi
if [[ ! ( -w ${pubdir} ) ]]; then
echo "directory ${pubdir} is not writable" 1>&2; exit 1
fi
# ----------------------------------------------------------------------
# Exporting the files:
# Remove any previously exported version of the exported files:
( cd ${pubdir} && \
/bin/rm -fv main.png main-*.png main.gif main-*.gif main.pov ${image} ${source} ${extras} \
)
# Make sure the files exist and have nonzero length,
# then copy them to the target directory and make sure that
# they are accessible through HTTP:
for f in "${image}" "${source}" "${extras[@]}"; do
if [[ ! ( -r ${f} ) ]]; then
echo "warning - no file ${f} in the current directory" 1>&2;
if [[ ${f} != "main.png" ]]; then echo "** aborted" 1>&2; exit 1; fi
fi
if [[ -z ${f} ]]; then
echo "warning - file ${f} has zero length" 1>&2;
echo "** aborted" 1>&2; exit 1
fi
dest="${pubdir}/${f}"
cp -avi ${f} ${dest}
chmod og=r,u=rw ${dest}
done