2016-01-21 17:28:02 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
BASEURL="http://localhost:8081"
|
2016-01-21 22:05:36 +00:00
|
|
|
BASEURI="https://srs.epita.fr/owncloud/remote.php/webdav/FIC 2016"
|
2016-02-26 00:31:00 +00:00
|
|
|
BASEFILE="/files"
|
2016-01-21 22:05:36 +00:00
|
|
|
CLOUDPASS=fic:'f>t\nV33R|(+?$i*'
|
2016-01-21 17:28:02 +00:00
|
|
|
|
|
|
|
new_theme() {
|
2016-01-21 20:45:25 +00:00
|
|
|
NAME=`echo $1 | sed 's/"/\\\\"/g'`
|
2016-01-23 11:21:02 +00:00
|
|
|
AUTHORS=`echo $2 | sed 's/"/\\\\"/g'`
|
|
|
|
curl -f -s -d "{\"name\": \"$NAME\", \"authors\": \"$AUTHORS\"}" "${BASEURL}/api/themes/" |
|
2016-01-21 17:28:02 +00:00
|
|
|
grep -Eo '"id":[0-9]+,' | grep -Eo "[0-9]+"
|
|
|
|
}
|
|
|
|
|
|
|
|
new_exercice() {
|
|
|
|
THEME="$1"
|
2016-01-21 20:45:25 +00:00
|
|
|
TITLE=`echo "$2" | sed 's/"/\\\\"/g'`
|
2016-01-21 22:05:36 +00:00
|
|
|
STATEMENT=`echo "$3" | sed 's/"/\\\\"/g' | sed ':a;N;$!ba;s/\n/<br>/g'`
|
|
|
|
HINT=`echo "$4" | sed 's/"/\\\\"/g' | sed ':a;N;$!ba;s/\n/<br>/g'`
|
2016-01-21 17:28:02 +00:00
|
|
|
DEPEND="$5"
|
|
|
|
GAIN="$6"
|
|
|
|
VIDEO="$7"
|
|
|
|
|
|
|
|
curl -f -s -d "{\"title\": \"$TITLE\", \"statement\": \"$STATEMENT\", \"hint\": \"$HINT\", \"depend\": $DEPEND, \"gain\": $GAIN, \"videoURI\": \"$VIDEO\"}" "${BASEURL}/api/themes/$THEME" |
|
|
|
|
grep -Eo '"id":[0-9]+,' | grep -Eo "[0-9]+"
|
|
|
|
}
|
|
|
|
|
|
|
|
new_file() {
|
|
|
|
THEME="$1"
|
|
|
|
EXERCICE="$2"
|
|
|
|
URI="$3"
|
|
|
|
|
2016-02-26 00:31:00 +00:00
|
|
|
curl -f -s -d "{\"URI\": \"${BASEFILE}${URI}\"}" "${BASEURL}/api/themes/$THEME/$EXERCICE/files" |
|
2016-01-21 17:28:02 +00:00
|
|
|
grep -Eo '"id":[0-9]+,' | grep -Eo "[0-9]+"
|
|
|
|
}
|
|
|
|
|
|
|
|
new_key() {
|
|
|
|
THEME="$1"
|
|
|
|
EXERCICE="$2"
|
|
|
|
NAME="$3"
|
2016-01-22 20:23:10 +00:00
|
|
|
KEY=`echo $4 | sed 's/"/\\\\"/g' | sed 's#\\\\#\\\\\\\\#g'`
|
2016-01-21 17:28:02 +00:00
|
|
|
|
|
|
|
curl -f -s -d "{\"name\": \"$NAME\", \"key\": \"$KEY\"}" "${BASEURL}/api/themes/$THEME/$EXERCICE/keys" |
|
|
|
|
grep -Eo '"id":[0-9]+,' | grep -Eo "[0-9]+"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Theme
|
2016-01-23 11:21:02 +00:00
|
|
|
curl -f -s -X PROPFIND -u "${CLOUDPASS}" "${BASEURI}" | xmllint --format - | grep 'd:href' | sed -E 's/^.*>(.*)<.*$/\1/' | sed 1d | tac | while read f; do basename "$f"; done | while read THEME_URI
|
2016-01-21 17:28:02 +00:00
|
|
|
do
|
2016-01-23 11:21:02 +00:00
|
|
|
THM_BASEURI="/${THEME_URI}/"
|
2016-01-22 20:23:10 +00:00
|
|
|
THEME_NAME=$(echo "${THEME_URI}" | sed -E 's/%20/ /g' | sed -E 's/%c3%a9/é/g' | sed -E 's/%c3%a8/è/g')
|
2016-01-23 11:21:02 +00:00
|
|
|
THEME_AUTHORS=$(curl -f -s -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}/AUTHORS.txt" | sed 's/$/,/' | xargs)
|
|
|
|
THEME_ID=`new_theme "$THEME_NAME" "$THEME_AUTHORS"`
|
2016-01-21 20:45:25 +00:00
|
|
|
if [ -z "$THEME_ID" ]; then
|
|
|
|
echo -e "\e[31;01m!!! An error occured during theme add\e[00m"
|
|
|
|
continue
|
|
|
|
else
|
2016-01-22 20:23:10 +00:00
|
|
|
echo -e "\e[33m>>> New theme created:\e[00m $THEME_ID - $THEME_NAME"
|
2016-01-21 20:45:25 +00:00
|
|
|
fi
|
2016-01-21 17:28:02 +00:00
|
|
|
|
|
|
|
LAST=null
|
2016-01-22 20:23:10 +00:00
|
|
|
EXO_NUM=0
|
|
|
|
curl -f -s -X PROPFIND -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}" | xmllint --format - | grep 'd:href' | sed -E 's/^.*>(.*)<.*$/\1/' | sed -E 's/%20/ /g' | sed -E 's/%c3%a9/é/g' | sed -E 's/%c3%a8/è/g' | sed 1d | while read f; do basename "$f"; done | while read EXO_NAME
|
2016-01-21 17:28:02 +00:00
|
|
|
do
|
2016-01-22 20:23:10 +00:00
|
|
|
if ! echo $EXO_NAME | grep Exercice > /dev/null
|
2016-01-21 17:28:02 +00:00
|
|
|
then
|
2016-01-22 20:23:10 +00:00
|
|
|
continue
|
2016-01-21 17:28:02 +00:00
|
|
|
fi
|
|
|
|
|
2016-01-22 20:23:10 +00:00
|
|
|
EXO_NUM=$((EXO_NUM + 1))
|
|
|
|
echo
|
|
|
|
echo -e "\e[36m--- Filling exercice ${EXO_NUM} in theme ${THEME_NAME}\e[00m"
|
2016-01-21 22:05:36 +00:00
|
|
|
|
2016-01-22 20:23:10 +00:00
|
|
|
EXO_BASEURI="${EXO_NAME}/"
|
2016-01-21 22:05:36 +00:00
|
|
|
|
2016-01-22 20:23:10 +00:00
|
|
|
FILES=$(curl -f -s -X PROPFIND -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}${EXO_BASEURI}" | xmllint --format - | grep 'd:href' | sed -E 's/^.*>(.*)<.*$/\1/' | sed 1d | while read f; do basename $f; done)
|
2016-01-21 22:05:36 +00:00
|
|
|
|
2016-01-22 20:23:10 +00:00
|
|
|
EXO_VIDEO=$(echo "$FILES" | grep -E "\.(mov|mkv|mp4|avi|flv|ogv|webm)$" | tail -1)
|
2016-01-21 22:05:36 +00:00
|
|
|
|
|
|
|
if [ "${LAST}" = "null" ]; then
|
|
|
|
echo ">>> Assuming this exercice has no dependency"
|
|
|
|
else
|
|
|
|
echo ">>> Assuming this exercice depends on the last entry (id=${LAST})"
|
|
|
|
fi
|
|
|
|
|
2016-01-22 20:23:10 +00:00
|
|
|
EXO_GAIN=$((3 * (2 ** $EXO_NUM) - 1))
|
2016-01-21 22:05:36 +00:00
|
|
|
echo ">>> Using default gain: ${EXO_GAIN} points"
|
|
|
|
|
2016-01-22 20:23:10 +00:00
|
|
|
EXO_DESC=$(curl -f -s -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}${EXO_BASEURI}/description.txt")
|
|
|
|
EXO_HINT=$(curl -f -s -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}${EXO_BASEURI}/hint.txt")
|
2016-01-21 17:28:02 +00:00
|
|
|
|
2016-01-21 19:44:37 +00:00
|
|
|
EXO_ID=`new_exercice "${THEME_ID}" "${EXO_NAME}" "${EXO_DESC}" "${EXO_HINT}" "${LAST}" "${EXO_GAIN}" "${THM_BASEURI}${EXO_BASEURI}${EXO_VIDEO}"`
|
2016-01-21 20:45:25 +00:00
|
|
|
if [ -z "$EXO_ID" ]; then
|
|
|
|
echo -e "\e[31;01m!!! An error occured during exercice add.\e[00m"
|
|
|
|
continue
|
|
|
|
else
|
2016-01-22 20:23:10 +00:00
|
|
|
echo -e "\e[32m>>> New exercice created:\e[00m $EXO_ID - $EXO_NAME"
|
2016-01-21 20:45:25 +00:00
|
|
|
fi
|
2016-01-21 17:28:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Keys
|
2016-01-22 20:23:10 +00:00
|
|
|
curl -f -s -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}${EXO_BASEURI}/keys.txt" | while read KEYLINE
|
2016-01-21 17:28:02 +00:00
|
|
|
do
|
2016-01-22 20:23:10 +00:00
|
|
|
KEY_NAME=$(echo "$KEYLINE" | cut -d : -f 1)
|
|
|
|
KEY_RAW=$(echo "$KEYLINE" | cut -d : -f 2-)
|
2016-01-21 17:28:02 +00:00
|
|
|
|
2016-01-22 20:23:10 +00:00
|
|
|
if [ -z "${KEY_NAME}" ]; then
|
|
|
|
KEY_NAME="Flag"
|
|
|
|
fi
|
2016-01-21 17:28:02 +00:00
|
|
|
|
2016-01-22 20:23:10 +00:00
|
|
|
KEY_ID=`new_key "${THEME_ID}" "${EXO_ID}" "${KEY_NAME}" "${KEY_RAW}"`
|
2016-01-21 20:45:25 +00:00
|
|
|
if [ -z "$KEY_ID" ]; then
|
2016-01-22 20:23:10 +00:00
|
|
|
echo -e "\e[31;01m!!! An error occured during key import!\e[00m (name=${KEYNAME};raw=${KEY_RAW})"
|
2016-01-21 20:45:25 +00:00
|
|
|
else
|
2016-01-22 20:23:10 +00:00
|
|
|
echo -e "\e[32m>>> New key added:\e[00m $KEY_ID - $KEY_NAME"
|
|
|
|
fi
|
2016-01-21 17:28:02 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
# Files
|
2016-01-22 20:23:10 +00:00
|
|
|
for f in $FILES; do echo $f; done | grep -vEi "(ressources|readme|description.txt|hint.txt|keys.txt|${EXO_VIDEO})" |
|
|
|
|
while read FBASE
|
2016-01-21 17:28:02 +00:00
|
|
|
do
|
2016-01-22 20:23:10 +00:00
|
|
|
echo "Import file ${BASEURI}${THM_BASEURI}${EXO_BASEURI}${FBASE}"
|
|
|
|
FILE_ID=`new_file "${THEME_ID}" "${EXO_ID}" "${THM_BASEURI}${EXO_BASEURI}${FBASE}"`
|
|
|
|
if [ -z "$FILE_ID" ]; then
|
|
|
|
echo -e "\e[31;01m!!! An error occured during file import! Please check path.\e[00m"
|
|
|
|
else
|
|
|
|
echo -e "\e[32m>>> New file added:\e[00m $FILE_ID - $FBASE"
|
2016-01-21 20:45:25 +00:00
|
|
|
fi
|
2016-01-21 17:28:02 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
LAST=$EXO_ID
|
|
|
|
done
|
2016-01-21 20:45:25 +00:00
|
|
|
echo
|
2016-01-21 17:28:02 +00:00
|
|
|
done
|