2020-03-25 05:32:38 +01:00
|
|
|
#!/bin/bash
|
|
|
|
###################################################################
|
|
|
|
#Script Name :generateRepo.sh
|
|
|
|
#Description :Generate meta data for ArmA 3 Launcher
|
|
|
|
#Date :24.03.2020
|
|
|
|
#Author :Niklas Schütrumpf
|
|
|
|
#Email :niklas@mc8051.de
|
|
|
|
###################################################################
|
|
|
|
|
|
|
|
if ! [ -x "$(command -v zsyncmake)" ]; then
|
|
|
|
echo 'Error: zsync is not installed.' >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [ -x "$(command -v jq)" ]; then
|
|
|
|
echo 'Error: jq is not installed.' >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [ -x "$(command -v strings)" ]; then
|
|
|
|
echo 'Error: binutils is not installed.' >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-03-26 15:28:37 +01:00
|
|
|
declare -A SHASUMS
|
|
|
|
|
2020-03-25 05:32:38 +01:00
|
|
|
echo "===== ===== ===== GENERATE .ZSYNC ===== ===== ====="
|
|
|
|
FILELIST=$(find . -type f ! -path "*/.sync*" ! -path "*.zsync")
|
|
|
|
while IFS= read -r line; do
|
|
|
|
mustgenerate=false
|
|
|
|
zsyncfile="${line}.zsync"
|
|
|
|
|
2020-03-26 02:26:31 +01:00
|
|
|
filebyte=$(wc -c < "${line}")
|
|
|
|
filedate=$(stat -c %Y "${line}")
|
2020-03-25 05:32:38 +01:00
|
|
|
|
2020-03-26 02:26:31 +01:00
|
|
|
zsyncfiledate=$(strings "${zsyncfile}" 2>/dev/null | grep -m 1 MTime | cut -d" " -f2-)
|
2020-03-25 05:32:38 +01:00
|
|
|
|
|
|
|
if [ ! -f "$zsyncfile" ]; then
|
|
|
|
echo "$zsyncfile does not exist"
|
|
|
|
mustgenerate=true
|
2020-03-26 02:26:31 +01:00
|
|
|
elif [[ ! $(strings "${zsyncfile}" | grep -m 1 Length | cut -d" " -f2) == $filebyte ]]; then # Check file length
|
2020-03-25 05:32:38 +01:00
|
|
|
echo "$zsyncfile does not have corret length"
|
|
|
|
mustgenerate=true
|
|
|
|
elif [[ ! $filedate == $(date -d "${zsyncfiledate}" +"%s") ]]; then # Check date
|
|
|
|
echo "$zsyncfile does not have corret date"
|
|
|
|
mustgenerate=true
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$mustgenerate" = true ]; then
|
|
|
|
echo "Generate $zsyncfile"
|
2020-03-26 02:26:31 +01:00
|
|
|
rm "${zsyncfile}" 2> /dev/null
|
|
|
|
dirfile=$(dirname "${line}")
|
|
|
|
filename=$(basename "${line}")
|
|
|
|
filenamezsync=$(basename "${zsyncfile}")
|
2020-03-29 21:01:22 +02:00
|
|
|
fileurl=$(python3 -c "import urllib.parse; print(urllib.parse.quote(input()))" <<< "${filename}")
|
|
|
|
$(cd "${dirfile}" && zsyncmake -u ${fileurl} -o "${filenamezsync}" "${filename}")
|
2020-03-25 05:32:38 +01:00
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo "Success: Generated ${zsyncfile}"
|
|
|
|
else
|
|
|
|
echo "Failure: Couldn't generate ${zsyncfile}" >&2
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Nothing changed for $line"
|
|
|
|
fi
|
|
|
|
|
2020-03-26 15:28:37 +01:00
|
|
|
SHA=$(strings "${zsyncfile}" | grep -m 1 SHA-1 | cut -d" " -f2)
|
|
|
|
SHASUMS[$(echo "${line}" | sed 's|^./||')]="${SHA}"
|
|
|
|
|
2020-03-25 05:32:38 +01:00
|
|
|
done <<< "$FILELIST"
|
|
|
|
echo -e "===== ===== ===== ===== ===== =====\n"
|
|
|
|
|
|
|
|
echo "===== ===== ===== DELETE SINGLE ZFILE WITHOUT FILE ===== ===== ====="
|
|
|
|
ZSYNCLIST=$(find . -name "*.zsync")
|
|
|
|
while IFS= read -r zfile; do
|
2020-03-26 02:26:31 +01:00
|
|
|
ORIG=$(echo "${zfile}" | rev | cut -c7- | rev)
|
2020-03-25 05:32:38 +01:00
|
|
|
if [ ! -f "$ORIG" ]; then
|
|
|
|
echo "$ORIG does not exist"
|
2020-03-26 02:26:31 +01:00
|
|
|
rm "${zfile}"
|
2020-03-25 05:32:38 +01:00
|
|
|
fi
|
|
|
|
done <<< "$ZSYNCLIST"
|
|
|
|
echo -e "===== ===== ===== ===== ===== =====\n"
|
|
|
|
|
|
|
|
echo "===== ===== ===== GENERATE METADATA ===== ===== ====="
|
|
|
|
FILELIST=$(find . -maxdepth 1 ! -path "*/.sync*" ! -path "*.zsync" ! -path "." | sed 's|^./||')
|
|
|
|
declare -a JSONDATA
|
|
|
|
while IFS= read -r folder; do
|
|
|
|
echo "${folder}"
|
|
|
|
|
|
|
|
if [ -d "$folder" ]; then
|
|
|
|
echo "is dir"
|
|
|
|
x=""
|
|
|
|
foldersize=0
|
2020-03-26 02:26:31 +01:00
|
|
|
FILEFOLDER=$(find "${folder}" -type f ! -path "*.zsync" | sed 's|^./||')
|
2020-03-25 05:32:38 +01:00
|
|
|
while IFS= read -r folderfile; do
|
2020-03-26 02:26:31 +01:00
|
|
|
filebyte=$(wc -c < "${folderfile}")
|
2020-03-28 05:17:50 +01:00
|
|
|
|
|
|
|
if [ $filebyte -eq 0 ]; then
|
|
|
|
echo "Skipping \"${folderfile}\" because file is empty"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2020-03-25 05:32:38 +01:00
|
|
|
foldersize=$(expr $foldersize + $filebyte)
|
2020-03-26 02:26:31 +01:00
|
|
|
name=$(echo "${folderfile}" | cut -d"/" -f2-)
|
2020-03-26 15:28:37 +01:00
|
|
|
x="\"${name}\":{\"size\": ${filebyte}, \"sha1\": \"${SHASUMS[$folderfile]}\"},${x}"
|
2020-03-25 05:32:38 +01:00
|
|
|
done <<< "$FILEFOLDER"
|
|
|
|
x=$(echo ${x} | rev | cut -c2- | rev)
|
2020-03-28 05:17:50 +01:00
|
|
|
|
|
|
|
if [ $foldersize -eq 0 ]; then
|
|
|
|
echo "Skipping complete folder \"${$folder}\" because all files are empty"
|
|
|
|
continue
|
|
|
|
fi
|
2020-03-25 05:32:38 +01:00
|
|
|
JSONDATA+=( "\"${folder}\": {\"size\":${foldersize},\"content\":{${x}}}" )
|
|
|
|
else
|
|
|
|
echo "is file"
|
2020-03-26 02:26:31 +01:00
|
|
|
filebyte=$(wc -c < "${folder}")
|
2020-03-28 05:17:50 +01:00
|
|
|
if [ $filebyte -eq 0 ]; then
|
|
|
|
continue
|
|
|
|
fi
|
2020-03-26 15:28:37 +01:00
|
|
|
JSONDATA+=( "\"${folder}\": {\"size\":${filebyte}, \"sha1\": \"${SHASUMS[$folder]}\"}" )
|
2020-03-25 05:32:38 +01:00
|
|
|
fi
|
|
|
|
done <<< "$FILELIST"
|
|
|
|
|
|
|
|
s=""
|
|
|
|
for i in "${JSONDATA[@]}"
|
|
|
|
do
|
|
|
|
s="${s},${i}"
|
|
|
|
done
|
|
|
|
|
|
|
|
s=$(echo ${s} | cut -c2-)
|
|
|
|
s="{${s}}"
|
|
|
|
|
|
|
|
echo $s | jq . > /dev/null
|
|
|
|
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo $s > ./.sync/modset.json
|
|
|
|
echo "Success: Generated metafile"
|
|
|
|
else
|
|
|
|
echo "Failure: invalid json generated" >&2
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo -e "===== ===== ===== ===== ===== =====\n"
|