#!/bin/bash
# This script takes the maps extracted from the gpgnet vault
# archive, extracts them from their .vault format and then
# renames them to mapname.v0001 and zips them up
# -Cursix
# NOTE: This script uses 7-zip to do extract/create zips
SOURCEDIR="/path/to/mods"
MIRRORDIR="/path/to/mirror"
STAGE1DIR="/path/to/stage1"
STAGE2DIR="/path/to/stage2"
LOGFILE="/path/to/log"
test -e $MIRRORDIR || mkdir -p $MIRRORDIR
test -e $STAGE1DIR || mkdir -p $STAGE1DIR
echo "===Log Start: `date "+%F %H:%M:%S"`===" > $LOGFILE
echo "---Begin Extraction---" >> $LOGFILE
cd $SOURCEDIR
# Find the latest version of each map and extract them to stagedir
for map in *; do
# Since we can have 1.vault, 2.vault, etc, find the highest version
# Probably could be done more efficiently
mapver=0
for (( i=1; i<20; i++ )); do
if [ -f "$map/$i.vault" ]; then
mapver=$i
fi
done
# if mapver is 0 we obviously found no .vault file
# save map name to logfile to look at manually
if [ $mapver -lt 1 ]; then
echo "ERROR! No vault in: $map" >> $LOGFILE
continue
fi
echo "----------------------------------"
echo "Extracting $map..."
mkdir -p "$STAGE1DIR/$map"
7za x "$map/$mapver.vault" -o"$STAGE1DIR/$map"
done
echo "Extraction Complete, begining rename and zip."
echo "This will take a while..."
echo "---Begin Rename and Zip---" >> $LOGFILE
cd $STAGE1DIR
# Look inside each extracted map, find the senario file and
# rename directory to map name. Should come out to something
# thismap.v0001 or similar according to Ze_PilOt
for map in *; do
cd "$STAGE1DIR/$map"
# Find the senario file, don't assume its ${map}_senario.lua
i=0
for scenario in "$( find . -maxdepth 1 -name "*_scenario.lua" )"; do
mapname=''
if [ -z "$scenario" ]; then
echo "WARNING! No scenario file in: $map" >> $LOGFILE
continue
fi
# Find the map variable and extract map name
mapname=`grep "^[[:space:]]*map[[:space:]]*=" "$scenario" | sed "s/.*maps\/\(.*\)\/.*/\1/"`
((i++))
# If i is greater than 1, we have more than one senario file. Complain!
if [ $i -gt 1 ]; then
echo "WARNING! $map may have more than one senario file!" >> $LOGFILE
fi
done
if [ -z "$mapname" ]; then
# Something went wrong, no mapname was found!
echo "ERROR! Unable to find mapname value for: $map" >> $LOGFILE
continue
fi
# Now rename it
echo "Renaming $map to $mapname"
mkdir -p "$STAGE2DIR/$map"
cd $STAGE1DIR
mv "$map" "$STAGE2DIR/$map/$mapname"
cd "$STAGE2DIR/$map"
echo "Zipping $map"
7za a -tzip "$MIRRORDIR/$map.zip" "$mapname" -mx=9
cd $STAGE1DIR
done
echo "==================="
echo "Done!"