#!/bin/bash ########################################### # I recommend Jedit for coding in Linux ########################################### #debug option for use if something's amiss: ########################################### # set -x ########################################### # Housekeeping # ----------------------------------------- # Variables which may be modified as needed # ----------------------------------------- foreground=7 fg_cyan="$(tput setaf 6)" # ##where the executable is found game=/usr/games/crawl-tiles ##where the saves directory may be found crawldir=$HOME/.crawl ## #config directory configdir=/usr/share/crawl/settings ##where init.txt is found settings=$configdir/init.txt ##where macro file is found macro=$crawldir/macro.txt # ----------------------------------------- #This variable is used to test for the existence of a save. #After a game, if a save does not exist, the character died. #----------------------------------------- save_file=$crawldir/saves #----------------------------------------- #Specify location of primary and secondary backup directory #----------------------------------------- backup1=$crawldir/backup_saves backup2=$crawldir/backup_saves_2 # #define 'liberal permissions' for a directory #some may prefer a more restrictive definition than 777 liberality=777 #keypress that controls regeneration exitchar="r" #controls whether user is using two monitors and two desktops and whether #to adjust display settings accordingly wmctrl_fix=YES #users with only one monitor might wish to turn this off cd "$crawldir" ### Functions displayscreen () { tput setb 0 tput setf $foreground tput clear tput bold echo "regen.sh for Dungeon Crawl Stone Soup" tput sgr0 tput setb 0 tput setf $foreground echo "Linux version, updated June 15th, 2014" echo "by igor at http://techlorebyigor.blogspot.com" echo echo "File Locations:" echo "The crawl executable is located at "$game echo "Settings are located at "$settings echo "Save files are located at "$crawldir echo echo "Observations:" } intro () { displayscreen ### Preliminary checks ## check for missing programs ##maximize window if wmctrl is available if [ -z "$(command -v wmctrl)" ]; then echo "- wmctrl is not installed, but it is not required." wmctrl_fix=NO else # echo "+ wmctrl is available, allowing regen to maximize its window...let's try!" # wmctrl -r :ACTIVE: -b toggle,maximized_vert,maximized_horz ## give the computer time enough to finish executing wmctrl # sleep 1 ## adjust number of columns available # displayscreen echo "+ wmctrl is available, allowing regen to maximize its window." fi if [ "$wmctrl_fix" == "YES" ] ; then echo "+ multi-desktop fix is enabled." else echo "- multi-desktop fix is disabled." fi if [ -z "$(command -v grep)" ]; then echo "- grep is not installed. Regen uses grep to make observations." else echo "+ grep is available." fi savefile=0 if [ -d $backup1 ]; then tput setf 5 savefile=1 echo "+ Crawl save created by regen.sh found ("$backup1")." tput setf $foreground fi crawlfound=0 if test -n "$(find "/usr/games" -maxdepth 1 -name 'crawl*' -print -quit)" then crawlfound=1 fi if [ $crawlfound -eq 0 ]; then echo "Fatal Error: " $game " not found!" echo "It appears that you have not installed Crawl yet." echo "Install Crawl and run it once prior to using this script." read -n1 -s -p"Press any key to exit." exit fi #test whether there is any file matching pattern *.cs in the saves dir checksave errorflag=0 permission_fault=0 perm=`stat -c '%a' $settings` if [ "$perm" == "$liberality" ]; then echo "+ liberal permissions set for " $settings else echo "- liberal permissions not set for " $settings permission_fault=1 fi perm=`stat -c '%a' $crawldir` if [ "$perm" == "$liberality" ]; then echo "+ liberal permissions set for " $crawldir else echo "- liberal permissions not set for " $crawldir permission_fault=1 fi if [ "$(whoami)" = "root" ]; then echo "+ This script is running with root privileges." else echo "- This script is not running with root privileges." if [ $permission_fault -gt 0 ]; then errorflag=1 echo echo "Linux may complain about permissions unless the permissions to two directories are liberalized via" echo "sudo chmod -R "$liberality" "$settings " ; chmod -R "$liberality" " $crawldir echo echo "Enter your root password, below, to liberalize permissions now." sudo chmod -R $liberality $configdir sudo chmod -R $liberality $crawldir fi fi errorflag=0 if [ ! -f $game ]; then errorflag=1 echo "Fatal Error: " $game " not found" fi if [ ! -d $crawldir ]; then echo "***Possible Problem: " $crawldir " not found" echo "Crawl may not have created the directory yet, so this is not a fatal error." fi if [ ! -f $settings ]; then errorflag=$((errorflag+1)) echo "Fatal Error: " $settings " not found" fi if [ $errorflag -gt 0 ]; then echo $errorflag " Fatal Errors found. Suggestion: modify the script, correcting the file paths." read -n1 -s -p"Press any key to exit." exit fi if grep -q "restart_after_game = false" "$settings" then ## word found in filename echo "+ Crawl will return to regen.sh at end of game. " else echo "##" >> "$settings" echo "## Modification inserted by regen.sh $(date)" >> "$settings" echo "##" >> "$settings" echo "restart_after_game = false" >> "$settings" if grep -q "restart_after_game = false" "$settings" then echo "+ Now Crawl will return to regen.sh at end of game. " else echo "Warning:" echo "regen.sh needs to modify the crawl settings to not restart after game." echo "In order to do so, it must be run with root privileges." fi fi } checksave() { savefile=0 #test whether there is any file matching pattern *.cs in the saves dir if test -n "$(find "$save_file" -maxdepth 1 -name '*.cs' -print -quit)" then savefile=1 tput setf 5 echo "+ Crawl save created by Crawl found ("$save_file")." tput setf $foreground fi if test -n "$(find "$save_file/sprint" -maxdepth 1 -name '*.cs' -print -quit)" then savefile=1 tput setf 5 echo "+ Sprint save created by Crawl found ("$save_file"/sprint)." tput setf $foreground fi if test -n "$(find "$save_file/zotdef" -maxdepth 1 -name '*.cs' -print -quit)" then savefile=1 tput setf 5 echo "+ Zotdef save created by Crawl found ("$save_file"/zotdef)." tput setf $foreground fi if [ $savefile -eq 0 ]; then echo "- game save file not found." fi return "$savefile" } regen() { if [ -d $backup1 ]; then #spare the directories deb and des, because they do not change and are time-consuming for Crawl to create. rm -f $save_file/*.* rm -rf $save_file/sprint rm -rf $save_file/zotdef cp -r $backup1/* saves fi echo echo "You are playing the game. . ." echo if [ "$wmctrl_fix" == "YES" ] ; then #load the game, and after that has been done, sleep 2 seconds, then fix the window to span but one desktop rather than two #if this is not working, try increasing the sleep period to as long as it takes to load and execute crawl $game & sleep 2 && wmctrl -x -r crawl-tiles.crawl-tiles -b toggle,fullscreen echo echo "(Pause)" echo read -n1 -s -p"When you have finished playing the game, press any key to continue." else #there may be no need for that sort of thing $game fi echo echo ". . .and now you have exited from the game." echo } pause() { keypoll="a" while [ "$keypoll" != "$exitchar" ] ; do echo #In Linux, it is awkward to use the spacebar for input. So r to continue. echo "Press q to quit, c to clear screen, ! to delete all saves and backups, m to install mods, or "$fg_cyan$exitchar" to regenerate. . ." tput setf $foreground read -n1 -s keypoll echo $keypoll if [ "$keypoll" == "q" ] || [ "$keypoll" == "Q" ]; then echo "Q was pressed." echo "Quitting." echo "See ya later!" exit fi if [ "$keypoll" == "c" ] || [ "$keypoll" == "C" ]; then intro fi if [ "$keypoll" == "!" ]; then #spare the directories deb and des, because they do not change and are time-consuming for Crawl to create. if [ -d saves ]; then rm -f $save_file/*.* rm -rf $save_file/sprint rm -rf $save_file/zotdef fi if [ -d $backup1 ]; then rm -rf $backup1 fi if [ -d $backup2 ]; then rm -rf $backup2 fi echo "All saves and backups have been deleted." fi if [ "$keypoll" == "m" ]; then if grep -q "inserted by regen.sh" "$macro" then ## echo "$word found in $filename" echo "igor's macros and mods have already been installed:" cat "$macro" else echo "##" >> "$macro" echo "## Begin of Modifications inserted by regen.sh $(date)" >> "$macro" echo "##" >> "$macro" echo "M:\{-282}" >> "$macro" echo "A:Zap" >> "$macro" echo "M:\{-289}" >> "$macro" echo "A:Vap" >> "$macro" echo "##" >> "$macro" echo "## End of Modifications inserted by regen.sh $(date)" >> "$macro" echo "##" >> "$macro" echo "##" >> "$settings" echo "## Begin Modification inserted by regen.sh $(date)" >> "$settings" echo "##" >> "$settings" echo "tile_skip_title = true" >> "$settings" echo "tile_full_screen = true" >> "$settings" #this causes conflicts with the screensaver # echo "tile_update_rate = 99000" >> "$settings" echo "tile_runrest_rate = 1000" >> "$settings" echo "##" >> "$settings" echo "## End Modification inserted by regen.sh $(date)" >> "$settings" echo "##" >> "$settings" echo "igor's macros and mods have now been installed" fi fi done echo $exitchar" pressed. Regenerating. . ." } ### First Run errorflag=1 intro pause ### Main Loop while [ 1 ]; do regen tput setf 5 checksave retval=$? if [ "$retval" -gt 0 ]; then ##a save file exists if [ -d $backup2 ]; then rm -rf $backup2 fi mkdir $backup2 if [ -d $backup1 ]; then mv $backup1/* $backup2 rm -rf $backup1 fi mkdir $backup1 cp -f $save_file/*.* $backup1 cp -rf $save_file/zotdef $backup1 cp -rf $save_file/sprint $backup1 echo "Your save game has been backed up." echo "Press "$exitchar" to resume your game or Q to quit for now." else # a save file does NOT exist echo echo "You were defeated by the reactionaries!" if [ -d $backup1 ]; then echo "Liberalism has science on its side!" echo "We have the technology to bring you back to life!" echo "Press "$exitchar" to rise from the dead!" else echo "No backup available. You're monster food!" echo "Burp!" fi fi tput setf $foreground pause intro done ### End