diff options
Diffstat (limited to '')
-rwxr-xr-x | scripts/common.sh | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/scripts/common.sh b/scripts/common.sh new file mode 100755 index 0000000..2ec19ce --- /dev/null +++ b/scripts/common.sh | |||
@@ -0,0 +1,76 @@ | |||
1 | #!/bin/echo Don't run this file, it's for common functions." | ||
2 | |||
3 | |||
4 | # Figure out where we are, most of this mess is to troll through soft links. | ||
5 | # PRGDIR=$(getPrgDir) | ||
6 | getPrgDir() | ||
7 | { | ||
8 | PRG="$0" | ||
9 | while [ -h "${PRG}" ] ; do | ||
10 | ls=$(ls -ld "${PRG}") | ||
11 | link=`expr "${ls}" : '.*-> \(.*\)$'` | ||
12 | if expr "${link}" : '.*/.*' > /dev/null; then | ||
13 | PRG="${link}" | ||
14 | else | ||
15 | PRG=$(dirname "${PRG}")/"${link}" | ||
16 | fi | ||
17 | done | ||
18 | PRGDIR=$(dirname "${PRG}") | ||
19 | pushd ${PRGDIR} >/dev/null | ||
20 | export PRGDIR=$(pwd) | ||
21 | popd >/dev/null | ||
22 | } | ||
23 | |||
24 | |||
25 | # Convert number to sim name | ||
26 | # name=$(num2name 1) | ||
27 | num2name() | ||
28 | { | ||
29 | # Using a string format, coz using a number format ends with an octal error, coz 08 isn't a valid octal number. | ||
30 | # Why isn't octal dead already? | ||
31 | printf 'sim%02s' "$1" | ||
32 | } | ||
33 | |||
34 | |||
35 | # Sanitize the name. Not removing [ or ], couldn't get that to work, only important for Windows. | ||
36 | # name=$(sanitize "the name") | ||
37 | sanitize() | ||
38 | { | ||
39 | echo "$1" | sed -e 's/[\\/:\*\?"<>\|@#$%&\0\x01-\x1F\x27\x40\x60\x7F. ]/_/g' -e 's/^$/NONAME/' | ||
40 | } | ||
41 | |||
42 | |||
43 | # Grab the first Section line of the sims .xml file, cut it down to the name. | ||
44 | # name=$(getSimName 1) | ||
45 | getSimName() | ||
46 | { | ||
47 | grep "<Section " ${PRGDIR}/../../config/$(num2name $1)/*.xml | head -n 1 | cut -d '"' -f 2 | ||
48 | } | ||
49 | |||
50 | |||
51 | # Calculate size of the sleep @ one second per megabyte of combined I/OAR file sizes. | ||
52 | # sleepPerSize o "the name" | ||
53 | # sleepPerSize i "the name" | ||
54 | sleepPerSize() | ||
55 | { | ||
56 | type="$1" | ||
57 | name=$(sanitize "$2") | ||
58 | |||
59 | rm -f ${PRGDIR}/../../backups/${name}-sleepPerSize | ||
60 | for file in ${PRGDIR}/../../backups/${name}-*.${type}ar; do | ||
61 | if [ -f ${file} ]; then | ||
62 | # We only loop through them coz bash sucks, we can find the total size now and jump out of the loop. | ||
63 | echo $(du -c -BM ${PRGDIR}/../../backups/${name}-*.${type}ar | tail -n 1 | cut -f 1 | cut -d 'M' -f 1) | ||
64 | touch ${PRGDIR}/../../backups/${name}-sleepPerSize | ||
65 | break | ||
66 | fi | ||
67 | done | ||
68 | |||
69 | # Sleep 200 instead if we can't find any files. | ||
70 | if [ -f ${PRGDIR}/../../backups/${name}-sleepPerSize ]; then | ||
71 | rm -f ${PRGDIR}/../../backups/${name}-sleepPerSize | ||
72 | else | ||
73 | echo 200 | ||
74 | fi | ||
75 | } | ||
76 | |||