Added some hexadecimal display capabilities to aclox ascii clock
[mirrors/Programs.git] / misc / gbook / gbook.bash
CommitLineData
21c4e167
H
1#!/bin/bash
2#OpenGameBook engine implemented in BASH
3#<~~Harvie 2oo8
4
5title="OpenGameBook - BASH engine";
6game="hacker.txt";
7room="2";
8
9export XDIALOG_HIGH_DIALOG_COMPAT=true;
10dialog="dialog"; #(dialog|whiptail|Xdialog|gdialog)
11
12#Args
13if [ -z "$1" ]; then
14 echo "Usage: gbook game.txt [room] [dialog_binary]";
15 exit;
16fi;
17if [ -a "$1" ]; then
18 game="$1";
19 else echo "File not exist!";
20fi;
21if [ -n "$2" ]; then room="$2"; fi;
22if [ -n "$3" ]; then dialog="$3"; fi;
23
24#Functions
25#nop() { echo -n; }
26
27dial_escape() {
28 #sed "s/'/\\\\'/g" | sed 's/"/\\"/g' | sed 's/!/\!/g';
29 iconv -c | sed "s/'/\\\\'/g";
30}
31
32fline() {
33 head "$1" -n "$2" | tail -n 1 | dial_escape;
34}
35
36enter_room() {
37 line="$1";
38 #echo -n "$menu";
39 echo -n "$line" | grep -o '\[[^]]*\]' | while read item; do
40 mroom="$(echo "$item" | grep -o '[0-9][0-9]*' | head -n 1)";
41 echo "'$mroom'"; echo "'$item'";
42 done;
43 echo '>'; echo "' '";
44 echo 'c'; echo 'Cheat!';
45}
46
47xcompat() {
48 grep -v 'Gtk-'
49}
50
51#Init
52tmpfile="/tmp/ogb-$$.tmp";
53dtmpfile="/tmp/ogbd-$$.tmp";
54head="$(fline "$game" 1)";
55
56#Code
57"$dialog" --msgbox "$head" 0 0 --backtitle "$title: $game" --title "$game intro";
58while true; do
59 line="$(fline "$game" "$room")";
60
61 menuargs="--backtitle '$title: $game' --title '$game/$room' --menu '#$room: $line' 0 0 10"
62 echo "$(echo "$menuargs" && enter_room "$line")" | xargs "$dialog" 2>"$tmpfile";
63 set="$?"; seterr=123; #123 in xargs == 1 in command
64 if [ "$set" == "$seterr" ]; then
65 "$dialog" --backtitle "$title: $game" --title "Exiting $game" --yesno "End? - Konec?" 0 0
66 if [ "$?" == 0 ]; then break; fi;
67 fi;
68
69 if [ "$(cat "$tmpfile" | xcompat)" == "c" ]; then
70 "$dialog" --backtitle "$title: $game" --title "Cheating in $game" --inputbox "Room? - Mistnost?" 0 0 2>"$tmpfile";
71 set="$?";
72 fi;
73
74 echo -n "$(cat "$tmpfile" | xcompat)" | grep "[^0-9]" > /dev/null
75 if [[ "$?" != 0 && "$set" != "$seterr" && -n "$(cat "$tmpfile" | xcompat)" ]]; then room="$(cat "$tmpfile" | xcompat)"; fi;
76done;
77
78clear;
79rm -f "$tmpfile";
80rm -f "$dtmpfile";
81echo "$title";
82echo Game "$game" aborted by user in room "$room";
83exit;
This page took 1.208607 seconds and 4 git commands to generate.