Launch scripts for Linux, to update to latest experimental and handle save game backup/restore

Here are a couple of scripts I’ve written to make things a little easier in Linux, since there is no GUI launcher like for Windows.

The first one installs the latest experimental from github, if there is a new one available. The second one handles backing up and restoring save games before launch.

To use, place them in your PATH somewhere and edit the configuration sections. I hope someone finds these useful.

‘cdda-update’ script:

#!/bin/bash

# Copyright none.  Do what you want with it.
# Version 1.2, Sat Jan  8 04:58:12 PM EST 2022

# OVERVIEW:
# ---------
#
# This script fetches the most recent experimental version from github and
#   installs it.  Save games from the previous version are copied over.
#   Archives of the six most recent versions are kept, in case there are killer
#   bugs and you have to roll back manually.


# REQUIREMENTS:
# -------------
#
# The textmode Lynx web browser must be installed in order for this
#   script to work.


# INSTALLATION:
# -------------
#
# Place this script somewhere in your path (I like to use ~/bin/, but suit
#   yourself).  Change CDDA_WORKING_DIR below to something that suits your
#   particular system.


# CONFIGURATION:
# --------------
#
# Set this to your CDDA working directory.  This directory will contain
#   archives of the last six experimental versions you've had installed (so
#   that you can manually roll back if there are killer bugs).  The previously
#   installed version will be in a directory called "old", and the most
#   recently installed version will be in a directory called "new".
# If you change this, you will also need to change it in the "cdda" launch
#   script.
CDDA_WORKING_DIR=~/bin/cdda-dir


###############################################################################

# Use Lynx to dump the experimental version download URLs that match the
#   architecture we want.  Sort them, then prune off all but the last (newest)
#   entry.
# Then extract the filename we'll be working with from the URL.
# Then extract the version we'll be working with from the filename.
URL=`lynx --dump https://cataclysmdda.org/experimental/ | grep "https://github.com/CleverRaven/Cataclysm-DDA/releases/download/cdda-experimental-" | grep cdda-linux-tiles-x64 | cut -d. -f2- | sort -u | tail -n1`
FILE=`basename $URL`
VERSION=`echo $FILE | cut -d- -f5- | cut -d. -f1`

# Create the working directory if it doesn't exist, and enter it.
if ! [ -e $CDDA_WORKING_DIR ]; then
	mkdir $CDDA_WORKING_DIR
fi
cd $CDDA_WORKING_DIR

# Exit if there is no new version available.
if [ -e $FILE ]; then
	echo No new version available.
	exit 0
fi

echo Installing new version $VERSION.  One moment...

# Fetch new version.
wget -q $URL

# Back up old version.
rm -rf old 2>/dev/null
mv -f current old 2>/dev/null

# Extract new version into current/.
mkdir current
cd current
tar --strip-components=1 -xzf ../$FILE
cd ..

# Copy old saves over.
cp -af old/save* current/ 2>/dev/null
cp -af old/prerestore-save.tar.gz current/ 2>/dev/null

# Copy old config over.
cp -af old/config/ current/config/ 2>/dev/null

# Copy old templates over.
cp -af old/templates current/ 2>/dev/null

# Prune old archives until there are only six left.
while [ `ls cdda-linux-tiles-x64-*.tar.gz | sort -u | wc -l` -gt 6 ]; do
	rm -f `ls cdda-linux-tiles-x64-*.tar.gz | sort -u | head -n1`
done

‘cdda’ launch script:

#!/bin/bash

# Copyright none.  Do what you want with it.
# Version 1.0, Fri Sep 24 12:28:09 PM EDT 2021

# OVERVIEW:
# ---------
#
# This script handles save game backups before launching CDDA.  The user is
#   prompted as to whether or not they would like to restore the save backup
#   from the last launch.  If 'N', then the save game backups are rotated and
#   the current save/ directory is backed up, before the game is launched.  If
#   'Y', then the current save/ directory is backed up to
#   'prerestore-save.tar.gz', deleted, and the previously backed up save
#   archive is extracted, before the game is launched.
# The default is to not restore.
# Ten rolling archives of the save/ directory are kept, so that you can
#   manually roll back farther if needed.


# REQUIREMENTS:
# -------------
#
# You must install CDDA with the 'cdda-update' script before this one will
#   work.


# INSTALLATION:
# -------------
#
# Place this script somewhere in your path (I like to use ~/bin/, but suit
#   yourself).  Change CDDA_WORKING_DIR below to something that suits your
#   particular system.


# CONFIGURATION:
# --------------
#
# Set this to your CDDA working directory from the "cdda-update" script.  If
#   you change this, you will also need to change it there.
CDDA_WORKING_DIR=~/bin/cdda-dir
#
# Set this to 'Y' if you want this script to automatically install the newest
#   experimental version before launching.  The 'cdda-update' script must be
#   in your path for this to work.
AUTO_UPDATE=y


###############################################################################

# Auto update.
if [ "x$AUTO_UPDATE" == "xy" ] || [ "x$AUTO_UPDATE" == "xY" ]; then
	cdda-update
fi

# Change to working dir and prompt for restore.
cd $CDDA_WORKING_DIR/current
read -p "Restore (y/N)? " restore

if [ "x$restore" == "xy" ] || [ "x$restore" == "xY" ]; then
	# Back up the current save/ and restore the most recent one.
	rm -f prerestore-save.tar.gz
	tar -czf prerestore-save.tar.gz save
	rm -rf save
	tar -xzf save-1.tar.gz
elif [ -e save ]; then
	# Rotate saves and back up the current save/.
	mv -f save-8.tar.gz save-9.tar.gz 2>/dev/null
	mv -f save-7.tar.gz save-8.tar.gz 2>/dev/null
	mv -f save-6.tar.gz save-7.tar.gz 2>/dev/null
	mv -f save-5.tar.gz save-6.tar.gz 2>/dev/null
	mv -f save-4.tar.gz save-5.tar.gz 2>/dev/null
	mv -f save-3.tar.gz save-4.tar.gz 2>/dev/null
	mv -f save-2.tar.gz save-3.tar.gz 2>/dev/null
	mv -f save-1.tar.gz save-2.tar.gz 2>/dev/null
	tar -czf save-1.tar.gz save
fi

# Start CDDA.
./cataclysm-launcher
3 Likes

Updated the cdda-update script so that it correctly moves the old config to the newly installed version.

Interesting I would not use these myself since I update using git and compiling is easy, I must say though, very tidy, very nice.

I was doing that via script at first, but it was somewhat slow to do a git pull and then a compile. This method is about as fast as one’s internet connection, plus about 2 seconds to extract and move things around. I guess I am an impatient man, lol. Although I 'spect I’ll end up going back to compiling if I ever start making mods.

Fixed stupid bug in update script relating to pruning old archived versions.

Updated updater script to preserve character templates when updating.

Really stupid error in cdda-launch script fixed. Sorry about that.

Is anyone using this?

No, sorry. Why are you using old bourne shell syntax if your script is for Bash? It just makes it more error prone. Why are you parsing ls’s output? It’s not meant to be machine readable. Why are you not quoting variables? While I don’t see any obvious way in which this script could wipe your home directory, your way of programming is still very reckless.

https://mywiki.wooledge.org/BashGuide

There is also no license attached, which in our current legal systems means “all rights reserved” aka nobody can legally use it.

Lol, cool story bro.

I’m gonna keep using this for now, myself, but when you come out of your mom’s basement long enough to your vast intellect and experience to make something better, I’ll happily switch. ;D

This is truly the most amazing thing about this game: The game itself is so very good, yet the community surrounding it is generally so very toxic. It’s a really amusing cycle, that you’d think I’d know better than to participate in any longer:

  1. Hey, this game is pretty good. I should get involved a little and maybe contribute something back to the community.
  2. Oh Jesus Christ, these people are terrible. Maybe I’ll come back later.
  3. Wait a while, then goto 1.

Kek.

I’m sorry, I was too harsh there. The scripts that I wrote at first were also similarly flawed. That website that I linked is the go-to one for improving Bash programming skills.

Bash is one of those languages that makes it very easy to shoot yourself in the foot. Something like rm -rf $dir looks alright at a glance, but what if $dir has a space in it? It gets turned into two parameters to rm.

My bad too. I shouldn’t have been so aggressive. Some of my previous interactions with a vocal but toxic minority of community members may have primed me to assume the worst of others here too. I apologize.