Skip to content

Instantly share code, notes, and snippets.

@aemx
Last active February 16, 2023 22:14
Show Gist options
  • Save aemx/50d79e84e0d0cb218cef74fc0115429c to your computer and use it in GitHub Desktop.
Save aemx/50d79e84e0d0cb218cef74fc0115429c to your computer and use it in GitHub Desktop.
Packaging script for simfiles and modfiles.
#!/bin/bash
###############################################################################
# package <-- package a pack/group of songs
# package srt <-- package directories with numbers prefixing only
# package deep <-- package directories at a deeper level
# package <name> <-- package a single song
###############################################################################
# 7Zip variable to call
zip="/c/Program Files/7-Zip/7z.exe"
# Create array to store all files in
declare -a copylist
# Save PWD for later
pwdEntry=$PWD
# Default subtype is packs, default cutscene count 0
subtype="packs"
cs=0
# If srt argument (all numbered directories; optionally, credits)
if [[ $1 == "srt" ]]; then
for dir in */ ; do
if [[ $dir =~ ^[0-9] ]]; then
copylist+=("$dir")
elif [[ $dir =~ ^c. ]]; then
read -r -p "Copy credits file? [y/N] " input
case $input in
[yY][eE][sS]|[yY]) copylist+=("$dir");;
esac
fi
done
# Normal mode (all non-extra (x.) directories)
elif [[ -z "$1" ]]; then
for dir in */ ; do
if [[ ! $dir =~ ^x. ]]; then
copylist+=("$dir")
fi
done
# Standalone mode (one folder and cutscene, if applicable)
else
subtype="standalone"
for dir in */ ; do
dirIn="${dir::-1}"
dirIn="${dirIn/ /}"
if [[ "${dirIn,,}" == "${1,,}" ]]; then
copylist+=("$dir")
elif [[ "${dirIn,,}" == "${1,,}(cutscene)" ]]; then
read -r -p "Copy cutscene file? [Y/n] " input
case $input in
[yY][eE][sS]|[yY]|"") copylist+=("$dir"); ((cs++));
esac
fi
done
fi
# Check for changelog, required
if compgen -G "CHANGELOG.md" > /dev/null; then
copylist+=("CHANGELOG.md")
else
echo "No CHANGELOG found! Exiting..."
exit 1
fi
# Check for credits
if compgen -G "credits.*" > /dev/null; then
copylist+=("credits.txt")
fi
# Check for groupbanner
if compgen -G "groupbanner.png" > /dev/null; then
copylist+=("groupbanner.png")
fi
# Check for public readme
readme=$(compgen -G "README-*")
readmeExt="${readme##*.}"
if compgen -G "README-*" > /dev/null; then
copylist+=("$readme")
fi
# Check for info (pdf or html)
if compgen -G "info.pdf" > /dev/null; then
copylist+=("info.pdf")
elif compgen -G "info.html" > /dev/null; then
copylist+=("info.html")
fi
# Print the first level of the directory to copy
base=$(basename "$PWD")
echo "$base"
for value in "${copylist[@]}"; do
[[ ! $value == $readme ]] && name="$value" || name="README.$readmeExt"
if [[ ! $value == ${copylist[-1]} ]]; then
echo "├───$name"
else
echo "└───$name"
fi
done
# Copy the files over to a temporary directory
type="$(basename "$(dirname "$PWD")")"
root="/c/luxe/itg/releases/$type/$subtype"
oldzip=$(compgen -G "$root/$base.zip")
baseDir="$root/$base"
baseDirWin="${baseDir//\//\\}"
baseDirWin="${baseDirWin:1:1}:${baseDirWin:2}"
baseDirWin="${baseDirWin^}"
mkdir "$baseDir"
for value in "${copylist[@]}"; do
if [[ ! $value == $readme ]]; then
cp -r "$value" "$baseDir/$value"
else
cp -r "$value" "$baseDir/README.$readmeExt"
fi
done
# For ITG files, adjust offset: for every .sm, add 0.009
if [[ $type == "sims" ]]; then
cd "$baseDir"
for sm in ./*/*.sm; do
perl -pi.bak -e 's/(#OFFSET:)(-?\d+.\d+)/$1 . ($2+0.009)/e' "$sm"
rm "$sm.bak"
done
cd "$pwdEntry"
fi
# Remove everything inside the zips, if they exist
# Add the included files into the zip, ignoring .old/.auto files
dists=(
$root
"/c/Users/luxe/Dropbox/itg-releases/$type/$subtype"
"/g/My Drive/itg/releases/$type/$subtype"
)
for dist in "${dists[@]}"; do
# For standlone files only
if [[ $subtype == "standalone" ]]; then
distDir="$dist/${copylist[$cs]}}"
distDir="${distDir::-2}"
distDirWin="${distDir//\//\\}"
distDirWin="${distDirWin:1:1}:${distDirWin:2}"
distDirWin="${distDirWin^}"
for value in "${copylist[@]}"; do
if [[ "${value: -1}" == "/" ]]; then
if compgen -G "$distDir.zip" > /dev/null; then
"$zip" d "$distDirWin.zip" "$value" -r > /dev/null
fi
subDir="$root/$base/$value"
subDir="${subDir::-1}"
subDirWin="${subDir//\//\\}"
subDirWin="${subDirWin:1:1}:${subDirWin:2}"
subDirWin="${subDirWin^}"
"$zip" a "$distDirWin.zip" "$subDirWin" -xr!*.old -xr!*.auto > /dev/null
fi
done
dist="$(dirname $dist)/packs"
fi
# Update selected pack
distDir="$dist/$base"
distDirWin="${distDir//\//\\}"
distDirWin="${distDirWin:1:1}:${distDirWin:2}"
distDirWin="${distDirWin^}"
if compgen -G "$distDir.zip" > /dev/null; then
# Standalone
if [[ $subtype == "standalone" ]]; then
for value in "${copylist[@]}"; do
"$zip" d "$distDirWin.zip" "$value" -r > /dev/null
done
# Pack
else
"$zip" d "$distDirWin.zip" * -r > /dev/null
fi
fi
"$zip" a "$distDirWin.zip" "$baseDirWin" -xr!*.old -xr!*.auto > /dev/null
done
# Remove the temporary directory
rm -rf "$baseDir"
# Pause for a bit
sleep 5
# Grab the latest version name from changelog
# cd to appropriate directory, then commit and push to repository
version="$(< 'CHANGELOG.md' grep -oP 'v\d+\.\d+\.\d+|v\d+\.\d+' | head -1)"
cd "/c/luxe/itg/releases/$type"
git add -A
git commit -am "Update $base to $version"
git push origin main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment