Merge branch 'master' of git://cams.pavlovian.net/questhelper
[QuestHelper.git] / Development / decompress.sh
blobc3b1e8d3594b762302d99cbd0c5c0338fde5ddea
1 #!/bin/bash
3 # Will find all the archives in the current directory, extract the nested lua/archive files from them, and
4 # then delete the archives. At least, that's what it's supposed to do. No promises.
6 SOURCE_FOLDER=LocalInput
7 TEMP_FOLDER=./Temp
9 if [ -e "${TEMP_FOLDER}" ]; then
10 rm -rf "${TEMP_FOLDER}"
13 mkdir -p "${TEMP_FOLDER}"
15 function doFile {
16 if [ -e ${1} ]; then
17 if [ -d ${1} ]; then
18 find "${1}" | while read FILE; do
19 if [ "${FILE}" != "${1}" ]; then
20 doFile ${FILE}
22 done
23 rmdir ${1}
24 else
25 chmod -x "${1}"
27 HASH=`sha1sum -b "${1}" | sed -e 's/ .*//' -`
29 if [ $? != 0 ]; then
30 echo "Error calculating hash for ${DFILENAME}."
31 exit 1
34 TYPE=`file -b "${1}"`
36 case "${TYPE}" in
37 *\ text*) mv -f "${1}" "${SOURCE_FOLDER}/${HASH}.lua" ;;
38 Zip\ archive*) mv -f "${1}" "${SOURCE_FOLDER}/${HASH}.zip" ;;
39 RAR\ archive*) mv -f "${1}" "${SOURCE_FOLDER}/${HASH}.rar" ;;
40 7-zip\ archive*) mv -f "${1}" "${SOURCE_FOLDER}/${HASH}.7z" ;;
41 *) echo "Ignoring '${1}'" ; rm "${1}" ;;
42 esac
47 find "${SOURCE_FOLDER}" | sort | while read FILENAME; do
48 if [ "${FILENAME}" == "${SOURCE_FOLDER}" ]; then
49 continue
52 echo "Scanning ${FILENAME}..."
54 # Can figure out text far faster, but doesn't give us the archive info. We do this first, and if it's not text, we try again in detail.
55 # Why does this script even exist? Why don't we do this during download? Why don't we have an "uncompressed" staging area?
56 TYPE=`file -b -e soft "${FILENAME}"`
58 case "${TYPE}" in
59 UTF-8\ Unicode\ text) continue ;;
60 UTF-8\ Unicode\ English\ text) continue ;;
61 UTF-8\ Unicode\ text,\ with\ CRLF\ line\ terminators) continue ;;
62 UTF-8\ Unicode\ English\ text,\ with\ CRLF\ line\ terminators) continue ;;
63 ASCII\ English\ text) continue ;;
64 ASCII\ English\ text,\ with\ CRLF\ line\ terminators) continue ;;
65 esac
67 TYPE=`file -b "${FILENAME}"`
69 case "${TYPE}" in
70 Zip\ archive*) unzip "${FILENAME}" -d "${TEMP_FOLDER}" ;;
71 RAR\ archive*) unrar e -o- "${FILENAME}" "${TEMP_FOLDER}/" ;;
72 7-zip\ archive*) 7z e "-o${TEMP_FOLDER}" "${FILENAME}" ;;
73 *) continue ;;
74 esac
76 if [ $? != 0 ]; then
77 echo "${FILENAME} is not an archive."
78 continue
81 echo "Processing ${FILENAME}..."
83 find "${TEMP_FOLDER}" | while read DFILENAME; do
84 if [ "${DFILENAME}" != "${TEMP_FOLDER}" ]; then
85 doFile "${DFILENAME}"
87 done
89 rm "${FILENAME}"
90 done
92 rm -rf "${TEMP_FOLDER}"
94 echo "Finished."
95 sleep 5
96 exit 0