Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

Bash, Dungeon Crawl Save Game Skimmer

what is crawl?

crawl is a free dungeon hack and slash terminal game available in ubuntu repo

Install Crawl

sudo apt update && sudo apt upgrade -y && sudo apt install crawl

Crawl is a rogue like game and you die all the time until you learn strategies even after you learn strategies you die A LOT! This script allows you to skim saved games or have restore points if abused, you could beat the RNG too by saving before testing something repeatedly but that kinda defeats the fun of the game

ASCII Graphics?

(you are the @ symbol on the map in a 4x5 wide with 1 door) Deep dwarves can sense some of the map around them even if they haven’t seen it:

,   ,  ,,     , ,   ,  *,     ,      Duke Moleb the Cleaver
,     ,,     ,  ,   ,                Deep Dwarf of Makhleb ******
 ,         ** ,   ,     ,            Health: 116/121   =======================-
   ,  ,  ,     ,     ,     , ,  ,    Magic:  13/13     ========================
     ,  ,,  ,* ######,   ,   ,       AC: 28 (39%)      Str: 28
, ,     ,,,    #....#                EV: 11            Int: 11
      , , ,, * +....#    ,           SH:  6            Dex: 8
      ,     ,  #....#,,,     ,       XL: 13 Next: 83%  Place: Dungeon:15
   , ,   ,     #@...#  ,  ,,,        Noise:  0 ------  Time: 10399.6 (1.7)
  *  , *  * *  #....#,, ,,, ,        e) +2 broad axe (vamp)
      ,        ###### ,  , ,         v) 8 boomerangs
,  ,      *,           ,   ,, ,      Fly 
 + ,                          ,
,            ,     ,                 
             *                       
 , ,             , ,,            
,   *          ,,,,, ,           
_6 large rocks; a cyclops skeleton
_There is an open door here.
_You see here a shortbow.
_There is a stone staircase leading down here.
_You fly downwards.
_There is a stone staircase leading up here.

The Backup Script

#!/bin/bash

# runs on pi or any linux system.  Just sete your home directory and make a new
# directory in it called "crawl".
# copy this file into it.


# get their /home/someuser/ directory scriptomatically
USERPATH=`printenv HOME`


# define some easier to read colors
# fixme: can this be a library?
# normal
WHT='\033[0;37m'
RED='\033[0;31m'
YEL='\033[0;33m'
GRN='\033[0;32m'
BLU='\033[0;34m'
PUR='\033[0;35m'

# Bold
BWHT='\033[1;37m'
BRED='\033[1;31m'
BYEL='\033[1;33m'
BGRN='\033[1;32m'
BBLU='\033[1;34m'
BPUR='\033[1;35m'

# underlines
UWHT='\033[4;37m'
URED='\033[4;31m'
UYEL='\033[4;33m'
UGRN='\033[4;32m'
UBLU='\033[4;34m'
UPUR='\033[4;35m'

# reset
RST='\033[0m'

# define some global variables
# flags
B_FLAG=0        # used to set backup flag 0 or 1
R_FLAG=0        # used to set restore flag 0 or 1
F_FLAG=0        # used to set filename flag 0 or 1
NAME=''         # used to hold the filename we are backing up or restoring
DEBUG=1         # in case you want to see some verbosity

# ego check
echo -e  "${GRN}#################"
echo -e "${GRN}# ${UGRN}CRAWL SKIMMER ${GRN}#"
echo -e  "${GRN}#################"

# show people how to use it
show_help() {

        # fixme: add a save games folder in case they didn't make "crawl" in their home
        if [ ! -d ${USERPATH}/crawl/ ]; then
                echo -e "${RED}${USERPATH}/crawl/ doesn't exist! Creating it now"
                mkdir ${USERPATH}/crawl/
        else
                if [ $DEBUG == 1 ]; then
                        echo -e "${GRN}${USERPATH}/crawl/ exists already, skipping creation"
                fi
        fi
        printf "\n${YEL}RESTORE: (FILENAME REQUIRED):\n"
        printf "${WHT}${0} ${YEL}-r YYYY-MM-DD_tttt\n"
        printf "\n${YEL}BACKUP: (FILENAME OPTIONAL):\n"
        printf "${WHT}${0} ${YEL}-b ${WHT}-f <optional name>\n\n"
        echo -e "${YEL}RECENT FILE EXAMPLES (LAST 5):${WHT}"

        # fixme: assumes only year 2021
        ls -1bAFtr | grep / | tail -n 5

        FILES=`ls -l | grep -c ^d`
        SIZE=`du -sh |  awk '{print $1}'`
        echo -e "\n${YEL}(We Found ${RED}$FILES ${YEL}backups using ${RED}$SIZE ${YEL}on disk, showing the last 5)"
        exit 1
}


# if no args, then educate
if [ $# -eq 0 ]; then
        echo -e "${RED}ERROR: no arguments given, show help message"
        show_help
        exit 1
fi

# if args, set them and validate them


while getopts 'brhf:' FLAG; do
        case "${FLAG}" in
                b) B_FLAG=1 ;;
                r) R_FLAG=1 ;;
                f) F_FLAG=1; NAME="${OPTARG}" ;;
                h) show_help ;;
                *) show_help
                exit 1 ;;
        esac
done



# fixme: is this the best way?
cd ${USERPATH}/crawl/
pwd

# Are they trying to backup and restore?
if [[ $B_FLAG -gt 0 && $R_FLAG -gt 0 ]]; then
                echo -e "${RED}ERROR: Cannot backup and restore together, that does not make sense."
                show_help

# they are trying to backup
elif [ $B_FLAG -gt 0 ]; then
        # this is how they want to name it
        if [ $F_FLAG -gt 0 ]; then
                echo -e "${GRN}SUCCESS: Saving File-> $NAME"
        else
                # get the default file name
                NAME=$(date +"%Y-%m-%d_%H%M")
                echo -e "${GRN}SUCCESS: Saving File-> $NAME"
        fi


        # make sure they don't overwrite an existing
        if [ -d $NAME ]; then
                echo -e "${RED}ERROR: File ${NAME} already exists!"
                                show_help
                # this is how they want to name it
        else

                # show user what we are doing
                if [ $DEBUG == 1 ]; then
                        echo "mkdir $NAME"
                        echo "mkdir ${NAME}/morgue/"
                        echo "mkdir ${NAME}/saves/"
                        echo "cp -r ${USERPATH}/.crawl/morgue/* $NAME/morgue/"
                        echo "cp -r ${USERPATH}/.crawl/saves/* $NAME/saves/"
                fi

                # but always do the work
                mkdir $NAME
                mkdir ${NAME}/morgue/
                mkdir ${NAME}/saves/
                cp -r ${USERPATH}/.crawl/morgue/* $NAME/morgue/
                cp -r ${USERPATH}/.crawl/saves/* $NAME/saves/

                # restart crawl
                /usr/games/crawl
        fi
# they are trying to restore
elif [ $R_FLAG -gt 0 ]; then
        # we assume they told us which file to restore?
        if [ $F_FLAG -gt 0 ]; then
                # make sure file exists before we try
                if [ -d "$NAME" ]; then
                        echo -e "${GRN}SUCCESS: Restoring File-> $NAME"
                        # put a file success check here
                        # if debug flag, show more details
                        if [ $DEBUG == 1 ]; then
                                echo "cp -r ${USERPATH}/crawl/${NAME}/morgue/* ${USERPATH}/.crawl/morgue/"
                                echo "cp -r ${USERPATH}/crawl/${NAME}/saves/* ${USERPATH}/.crawl/saves/"
                        fi

                        # but always do the work
                        # restore files
                        cp -r ${USERPATH}/crawl/${NAME}/morgue/* ${USERPATH}/.crawl/morgue/
                        cp -r ${USERPATH}/crawl/${NAME}/saves/* ${USERPATH}/.crawl/saves/
                        # restart game
                        /usr/games/crawl
                else
                        echo -e "${RED}ERROR: Path ${NAME} doesn't seem to exist"
                        show_help
                fi
        else
                LATEST=`ls -1bAFtr | grep / | tail -n 1`
                echo -e "${GRN}SUCCESS: Restoreing Files-> $LATEST"

                # if debug flag, show more detail
                if [ $DEBUG == 1 ]; then
                        echo -e "cp -r ${USERPATH}/crawl/${LATEST}morgue/* ${USERPATH}/.crawl/morgue/"
                        echo -e "cp -r ${USERPATH}/crawl/${LATEST}saves/* ${USERPATH}/.crawl/saves/"
                fi

                # but always do the work
                cp -r ${USERPATH}/crawl/${LATEST}morgue/* ${USERPATH}/.crawl/morgue/
                cp -r ${USERPATH}/crawl/${LATEST}saves/* ${USERPATH}/.crawl/saves/
                # restart game
                /usr/games/crawl


                fi
fi
Last updated on 27 Aug 2023
Published on 27 Aug 2023