updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / mindless / mtgspoiler
blobe4382eaeb395e6454c9be3a729663ca7b5219bb6
1 #!/bin/bash
3 die()
5 case "${1}" in
6 "print-usage" )
7 echo "Oracle Spoiler Manager (by) Caelyl <caelyl@gmail.com>
8 A script to manage Oracle card data spoilers for Magic: The Gathering.
10 Usage: mtgspoiler < [ -? | --help ] | [ -d | --defaultdir ] | directory > ...
11 [ -q ]
13 This script will download the current Oracle spoiler in full from the Gatherer
14 online database (http://ww2.wizards.com/gatherer/index.aspx) and format it as a
15 flatfile database for use with Mindless Automaton. The filename will be
16 generated of the form 'oracle-MMddYYYY.txt', and a symlink 'oracle.txt' will be
17 created.
19 Command-Line Options:
21 -? | --help
22 Show this usage information.
24 -d | --defaultdir
25 Use the default directory of '/usr/share/mindless'.
27 [ directory ]
28 The directory in which to place the spoiler and symlink.
30 -q | --quiet
31 Suppress the warning about an invalid or missing directory.
33 The default spoiler directory is used if [ directory ] is either not specified
34 or is invalid." | less
36 "nomakedir" )
37 echo "Could not create directory '${_directory}'. Aborting."
39 "root" )
40 echo "This script is designed to run as a non-root user. Users must be in the 'games'
41 group in order to use this script without specifying an alternate starting
42 directory. (see 'mtgspoiler -?' for help on how to use the script)"
44 esac
45 exit
48 createdir()
50 mkdir -p "${_directory}" || die nomakedir
53 warning()
55 case "${1}" in
56 "nodir" )
57 if [[ "${_quiet}" = 0 ]]; then
58 echo "Warning: No directory specified, defaulting to '/usr/share/mindless'."
61 "invalid" )
62 echo "Warning: Invalid directory specified. Create it (else default to
63 '/usr/share/mindless')? [y/n]"
64 if [[ "${_quiet}" = 0 ]]; then
65 read _reply
66 else
67 ${_reply}="y"
69 if [[ "${_reply}" = "y" ]]; then
70 createdir
73 "definvalid" )
74 if [[ "${_quiet}" = 0 ]]; then
75 echo "Warning: Default directory ('/usr/share/mindless') does not exist.
76 Creating it..."
78 createdir
80 esac
83 fetch_spoiler()
85 if [[ "${_username}" = "root" ]]; then
86 die root
88 wget -O "${_spoiler}" "${_livespoiler}"
89 sed -n -i -e '/xmp/,/xmp/p' "${_spoiler}"
90 sed -i -e '/xmp/d' "${_spoiler}"
91 ln -sf "${_spoiler}" "${_spoilerlink}"
92 chown :games "${_spoiler}" "${_spoilerlink}"
93 chmod a=r,ug+w "${_spoiler}"
96 test_fetch()
98 echo wget -O "${_spoiler}" "${_livespoiler}"
99 echo sed -n -i -e '/xmp/,/xmp/p' "${_spoiler}"
100 echo sed -i -e '/xmp/d' "${_spoiler}"
101 echo ln -sf "${_spoiler}" "${_spoilerlink}"
102 echo chown :games "${_spoiler}"
103 echo chmod a=r,ug+w "${_spoiler}"
106 _username=`id -nu`
107 _debug=0
108 _quiet=0
109 _warntype=
110 _directory=
112 for _argument in ${*}; do
113 case "${_argument}" in
114 "--debug" )
115 _debug=1
117 "-?" | "--help" )
118 die "print-usage"
120 "-d" | "--defaultdir" )
121 _directory=/usr/share/mindless
123 "-q" | "--quiet" )
124 _quiet=1
127 _directory="${_argument}"
129 esac
130 done
132 if [[ -z "${_directory}" ]]; then
133 _warntype=nodir
134 elif [[ ! -d "${_directory}" ]]; then
135 _warntype=invalid
137 if [[ ! -z "${_warntype}" ]]; then
138 warning ${_warntype}
140 _warntype=
141 if [[ ! -d "${_directory}" ]] || [[ -z "${_directory}" ]]; then
142 _directory=/usr/share/mindless
144 if [[ ! -d "${_directory}" ]]; then
145 _warntype=definvalid
147 if [[ ! -z "${_warntype}" ]]; then
148 warning ${_warntype}
150 _warntype=
152 if [[ ${_directory: -1} = "/" ]]; then
153 _safedirectory=${_directory:0:${#_directory}-1}
154 else
155 _safedirectory=${_directory}
157 _spoiler=${_safedirectory%/}/oracle-`date +%m%e%Y`.txt
158 _spoilerlink=${_safedirectory%/}/oracle.txt
159 _livespoiler="http://ww2.wizards.com/gatherer/index.aspx?term=%20&Field_Name=on&Field_Rules=on&Field_Type=on&setfilter=All%20sets&output=Oracle%20Spoiler"
161 if [[ "${_debug}" = 0 ]]; then
162 fetch_spoiler
163 else
164 test_fetch