summarylogtreecommitdiffstats
path: root/idos-timetable-browser.sh
blob: fb9be01d72e873d6ca0cbba7aea96bb69278cf1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash

### Wrapper for the windows-software "IDOS timetable browser".
#   This wrapper does start the software with 'wine' from the correct
#   directory (where it can find it's .dll files), and does some
#   command line argument reformatting so that filenames can be
#   specified both absolutely and relatively to the directory the user
#   starts this wrapper from.
#
#   Also, it starts with a Czech locale, if available, in order to make
#   diacritical characters display correctly.



### Global variables:

# Where this wrapper is started from:
_cwd="$(pwd)"

# Where the software is installed:
_installdir="/opt/idos-timetable"

# What the executable of the software is:
_executable="${_installdir}/TT.exe"

# Print some debug messages? Can be controlled via environment; if not
# specified in the environment, set it here:
if [ -z "${DEBUG}" ]; then
  DEBUG="true"
fi



### Some helper functions:

debug() {
  case "${DEBUG}" in
    "1"|[yY][eE][sS]|[tT][rR][uU][eE])
      echo "DEBUG info: $@" > /dev/stderr
    ;;
  esac
}

rebase_absolute_path() {
  # Arguments:
  #  $1: The new base path (assumed to be absolute and not containing double-'/'),
  #  $2: The absolute path which should be made a relative path based on $1 (needs to be an absolute path).
  _newbase="$1"
  _oldpath="$2"
  
  _depth_new="$(echo "${_newbase}" | tr -d '\n' | tr '/' '\n' | wc -l)" # Get the depth of $_newbase.
  _updir_string=''
  for i in $(seq 1 "${_depth_new}"); do # Put as much '../' into a string in order to reach / from $_newbase
    _updir_string="../${_updir_string}"
  done
  _updir_string="$(echo "${_updir_string}" | head -c-2)" # Remove the trailing '/'.
  
  _newpath="${_updir_string}${_oldpath}"
  
  echo "${_newpath}"
}

rebase_relative_path() {
  # Arguments:
  #   $1: Old base path (assumed to be absolute and not containing double-'/'),
  #   $2: New base path (assumed to be absolute and not containing double-'/'),
  #   $3: Relative path based on $1 to be rebased to $2.
  _oldbase="$1"
  _newbase="$2"
  _oldpath="$3"
  
  echo "$(rebase_absolute_path "${_newbase}" "${_oldbase}/${_oldpath}")"
}



### Do some option parsing:
#   * Convert "-h", "-help" or "--help" to "/?",
#   * convert pathnames to relative ones with regard to $_installdir,
#     which is necessary since
#     - the windows software would treat absolute pathnames, in the
#       Unix-world starting with '/', as options to the software,
#     - relative pathnames specified by the user need to be relative
#       with regard to the directory the software is run from. (We need
#       to change the directory so the software can find necessary
#       .dll files.)

# First, copy the positional parameters into a new (modifiable) array;
# with indices numbered in the same way as the positional paramaters
# (exept that we won't have 0):

argsnew=()

for j in $(seq "$(($#-1))" -1 0); do
  i="$(($#-$j))"
  argsnew[$i]="${BASH_ARGV[$j]}"
done

# Now, parse the positional arguments. The software understands the
# following arguments:
#   - Filenames to *.tt-files: Timetable files to use.
#   - /?: Help on arguments.
#   - /W:[CATZ], /L:*, /D:*, /C, /P, /I, /X:[23], /M:B, /O, /1, /2:*, /Z:*
#
# So, we will go as follows:
#   - Convert any '-h', '-help' or '--help' to '/?,
#   - If we encounter anything that does not begin with '/' or is not
#     '-h', '-help' or '--help', assume that it is a filename relative
#     to the directory $_cwd, and make it a filename relative to the
#     directory $_installdir.
#   - If we encounter something which mathes any of the options above,
#     leave it as it is,
#   - Assume that the rest is an absolute filename, and make it a
#     filename relative to the directory $_installdir.

for i in $(seq 1 "$#"); do
  case "${argsnew[$i]}" in
    "-h"|"-help"|"--help")
      argsnew[$i]="/?"
    ;;
    [^/]*)
      argsnew[$i]="$(rebase_relative_path "${_cwd}" "${_installdir}" "${argsnew[$i]}")"
    ;;
    "/?"|"/W:"[CATZ]|"/L:"*|"/D:"*|"/C"|"/P"|"/I"|"/X:"[23]|"/M:B"|"/O"|"/1"|"/2:"*|"/Z:"*)
      true
    ;;
    *)
      # Now, everything left is assumed to be an absolute filename.
      argsnew[$i]="$(rebase_absolute_path "${_installdir}" "${argsnew[$i]}")"
    ;;
  esac
done



### Check for locale, and if present, set Czech locale:

if locale -a | grep -qE '^cs_CZ'; then
  if locale -a | grep -qE '^cs_CZ' | grep -qi utf8; then
    LC_CTYPE="$(locale -a | grep -E '^cs_CZ' | grep -i utf8 | head -n 1)"
  else
    LC_CTYPE="$(locale -a | grep -E '^cs_CZ' | tail -n 1)"
  fi
  export LC_CTYPE
fi



### Launch the software with the parsed and maybe reformatted arguments:

cd "${_installdir}"

debug '$LC_CTYPE is set to: '"'${LC_CTYPE}' (if it does not begin with 'cs_CZ', try to enable Czech locale on your system to display diacritical characters correctly)."
debug "We are running from the directory: '$(pwd)'."
debug "Executing the following command: 'wine ${_executable} ${argsnew[@]}'."

wine "${_executable}" "${argsnew[@]}"