summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: a9b541cd227a068a8e482cba1e1700d25b8eb828 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# Maintainer: Chris Severance aur.severach aATt spamgourmet dott com

# As of 4.11 repowering the serial server crashes the kernel on TCP connection loss
# The 4.11 patch is disabled to prevent it from compiling on 4.11

_opt_DKMS=1            # This can be toggled between installs
#_opt_defaultmode='666' # default: 660

# Todo: Implement defaultmode
# Todo: Test secure mode

# Todo: python tools should be updated to python3
# Todo: nslinkadmin ethdevname needs to be adjustable when launched from command line, not "eth0".
# Todo: nslinktool add systemd support
# Todo: nslinktool needs an eth selector
# Todo: nslinktool what does the "Load" button do?
# Todo: nslinktool Config Driver needs an up down button to reorder entries
# Todo: separate firmware updater
# Todo: Comtrol icons for nslinktool and the firmware updater
# Todo: The IP is backwards in /proc/driver/nslink/status
# Todo: nslinktool set the mouse pointer to a spinning circle far too long after startup

# Uninstall cleanup: sudo rm -f /etc/nslink.conf*

# Configure with the NS-Link Manager GUI, Config Driver
# Ports are /dev/ttySI0 through /dev/ttySI31

set -u
pkgname='nslink'
pkgver='7.15'
pkgrel='3'
pkgdesc='tty driver and firmware update for Comtrol DeviceMaster, RTS, LT, PRO, 500, UP, RPSH-SI, RPSH, and Serial Hub console terminal device server'
# UP is not explicitly supported by NS-Link, only by the firmware updater.
_pkgdescshort="Comtrol DeviceMaster ${pkgname} TTY driver"
arch=('i686' 'x86_64')
url='http://www.comtrol.com/'
license=('GPL')
depends=('glibc' 'openssl' 'python2' 'sed' 'groff' 'tcl' 'tk' 'util-linux') # python is also needed for the firmware updater
optdepends=(
  'gksu: NS-Link Manager GUI'
)
makedepends=()
backup=("etc/nslink.conf")
options=('!zipman')
install="${pkgname}-install.sh"
_verwatch=('http://downloads.comtrol.com/html/DM_PRO_RTS_SERIALHUB_drivers.htm' '.*/devicemaster-linux-\([0-9\.]\+\)\.tar\.gz' 'l')
_srcdir="devicemaster-linux-${pkgver}"
_srcdir2="DM-Firmware-Updater-1.06"
#source=("ftp://ftp.comtrol.com/dev_mstr/rts/drivers/linux/devicemaster-linux-${pkgver}.tar.gz")
source=("http://downloads.comtrol.com/dev_mstr/rts/drivers/linux/devicemaster-linux-${pkgver}.tar.gz")
#source+=('ftp://ftp.comtrol.com/dev_mstr/rts/utility/linux_firmware_uploader/DM-Firmware-Updater-1.06.tar.gz')
source+=('http://downloads.comtrol.com/dev_mstr/rts/utility/linux_firmware_uploader/DM-Firmware-Updater-1.06.tar.gz')
source+=('dmupdate.py.usage.patch' 'nslinktool.systemd.patch' 'nslink-patch-signal_pending-kernel-4-11.patch')
sha256sums=('f166dc53d53c856a790b4c21f40f3f9df66684aa687fa84b502a795a8130dafc'
            'd21c5eeefdbf08a202a230454f0bf702221686ba3e663eb41852719bb20b75fb'
            '5a4e2713a8d1fe0eebd94fc843839ce5daa647f9fa7d88f62507e660ae111073'
            'c6e4b4c3ee31cbad8f18db5973d0b0c677443ce9d55f86326557e51e94752097'
            '20c01753d9d32fdc88853a42ead7c5e8cffb1f4c9ab1c2ba028b3c5e89e1d711')

if [ "${_opt_DKMS}" -ne 0 ]; then
  depends+=('linux' 'dkms' 'linux-headers')
else
  makedepends+=('linux-headers')
fi

_opt_ethernet_data='eno_____________________________'
_opt_ethernet_keystring='<-<-<-ArchLinux_ethernet_keystring->->->'

prepare() {
  set -u
  cd "${_srcdir}"

  # Fix permissions
  find -type 'f' -perm '/111' -exec chmod 644 '{}' '+'
  chmod 755 *.sh *.py 'nslinktool'
  local _ver="$(sed -n -e 's:^#define\sSI_VERSION\s"\([^"]\+\).*$:\1:p' 'version.h')"
  if [ "${pkgver}" != "${_ver}" ]; then
    echo "Version mismatch ${pkgver} != ${_ver}"
    set +u
    false
  fi
  unset _ver

  # Minor fixes to Makefile
  sed -e '# Fix a path' \
      -e 's:/lib/:/usr/lib/:g' \
      -e '# These lines look dangerous so well disable them' \
      -e 's:^ifneq "$(wildcard /usr/src/.*$:ifneq "ArchLinux" "ArchLinux":g' \
      -e '# We can force this one true' \
      -e 's:^ifneq "$(wildcard /usr/lib/.*$:ifneq "ArchLinux" "0":g' \
    -i 'Makefile'

  #cp -p 'install.sh' 'install.sh.Arch' # testmode for diff comparison
  sed -e '# Fix some paths' \
      -e 's: /lib/: /usr/lib/:g' \
      -e 's: /sbin: /usr/bin:g' \
      -e 's:"/sbin:"/usr/bin:g' \
      -e 's:/usr/sbin:/usr/bin:g' \
      -e 's:/usr/local/man:/usr/share/man:g' \
      -e '# I suspect this folder for systemd is wrong even on RedHat' \
      -e 's:/etc/systemd/system:/usr/lib/systemd/system:g' \
      -e '# make package compatible' \
      -e 's:"/etc/:"${DESTDIR}/etc/:g' \
      -e 's: /etc/: "${DESTDIR}"/etc/:g' \
      -e 's:"/usr/:"${DESTDIR}/usr/:g' \
      -e 's: /usr/: "${DESTDIR}"/usr/:g' \
      -e '# Disable module management and message' \
      -e 's:^\s\+echo\s-n\s"Rebuilding:# &:g' \
      -e 's:^.*depmod -a:# &:g' \
      -e '# Disable systemd management' \
      -e 's:^\s\+systemctl:# &:g' \
      -e '# Get rid of the start messages lest someone believes them' \
      -e 's:^\s\+if\s\[\s"$WillStart"\s=\syes\s\]:return\n&:g' \
    -i 'install.sh'
  if [ -s 'install.sh.Arch' ]; then
    echo 'Disable testmode to build'
    set +u
    false
  fi

  # Switch to python2
  sed -e '# Why using local on just this one?' \
      -e 's:/usr/local/bin/python:/usr/bin/python:g' \
      -e 's:/usr/bin/python:&2:g' \
    -i *.py 'nslinktool'

  # The command line tool is unusable without the proper ethernet card in it.
  # There's a parameter but who would know! We'll add a searchable string
  # that a launcher can modify.
  sed -e 's:"eth0":"'"${_opt_ethernet_data}${_opt_ethernet_keystring}"'":g' -i 'nslinkadmin.c'

  # Branding in dmesg
  sed -e '/^int nrp_init(/,/^}/ s:version %s %s:& Arch Linux:g' -i 'nslink.c'

  # nslinktool has the same problem but it's a GUI app. The best we can
  # do is start with the right network card. It really needs to become
  # a dropdown.
  local _neweth
  local _neweths=($(ip addr | grep ' UP ' | cut -d':' -f2))
  if [ "${#_neweths[@]}" -ne 0 ]; then
    _neweth="s:eth0:${_neweths[0]}:g"
  else
    _neweth='#'
  fi

  # Fix nslinktool. We'll get rid of the tabs while we're here.
  cp -p 'nslinktool' 'nslinktool.orig'
  sed -e '# Fix paths and enable our fixed admin tool:g' \
      -e 's:/usr/sbin/nslinkadmin:/usr/bin/nslinkadmin.exec:g' \
      -e 's:/usr/local/man:/usr/share/man:g' \
      -e '# Cosmetic cleanup for simpler patch editing' \
      -e 's:\s\+$::g' \
      -e '# Ethernet fix from above' \
      -e "${_neweth}" \
    'nslinktool.orig' | expand -i > 'nslinktool'
  rm 'nslinktool.orig'
  unset _neweth _neweths

  # Patch systemd code into the GUI tool
  #diff -pNau5 nslinktool{.orig,} > '../nslinktool.systemd.patch'
  patch -Nbup0 < "${srcdir}/nslinktool.systemd.patch"

  # Convert the live config entries to samples. This eliminates
  # special handling for detecting a blank configuration.
  sed -e 's:^:#&:g' -i 'nslink.conf'

  #diff -pNau5 nslink.c{.orig,} > '../nslink-patch-signal_pending-kernel-4-11.patch'
  #patch -Nbup0 < "${srcdir}/nslink-patch-signal_pending-kernel-4-11.patch"

  # Fix up the firmware downloaders
  cd "${srcdir}/${_srcdir2}"

  # Fix permissions
  find -type 'f' -perm '/111' -exec chmod 644 '{}' '+'
  chmod 755 *.py

  sed -e '# Cosmetic cleanup for simpler patch editing' \
      -e 's:\s\+$::g' \
      -e '# Switch to python2' \
      -e 's:/usr/bin/python:&2:g' \
    -i *.py

  # Patch usage and help into command line tool
  #diff -pNau5 dmupdate.py{.orig,} > '../dmupdate.py.usage.patch'
  patch -Nbup0 < "${srcdir}/dmupdate.py.usage.patch"

  set +u
}

build() {
  set -u
  cd "${_srcdir}"
  make -s -j1 QUIET=0
  set +u
}

package() {
  set -u
  cd "${_srcdir}"

  if [ "${_opt_DKMS}" -eq 0 ]; then
    # I don't want Linux version info showing on AUR web. After a few months 'linux<0.0.0' makes it look like an out of date package.
    local _kernelversionsmall="$(uname -r)"
    _kernelversionsmall="${_kernelversionsmall%%-*}"
    _kernelversionsmall="${_kernelversionsmall%\.0}" # trim 4.0.0 -> 4.0, 4.1.0 -> 4.1
    # prevent the mksrcinfo bash emulator from getting these vars!
    eval 'conf''licts=("linux>${_kernelversionsmall}" "linux<${_kernelversionsmall}")'
    eval 'dep''ends+=("linux=${_kernelversionsmall}")'
  fi

  # This installer expects the system folders to already exist
  install -dm755 "${pkgdir}"{'/usr/bin','/etc',"/usr/lib/modules/$(uname -r)/misc",'/usr/lib/systemd/system'}

  DESTDIR="${pkgdir}" \
  INIT='systemd' \
  sh -e -u 'install.sh' "$(uname -r)" install

  # Fix paths in the service file
  sed -e 's:/sbin/:/usr/bin/:g' -i "${pkgdir}/usr/lib/systemd/system/nslink.service"

  # Root only permissions on executables. Easier than hacking UID 0 into them.
  find "${pkgdir}/usr/bin/" -type 'f' -perm /111 -exec chmod 744 '{}' '+'

  # Preserve an original copy of the config file
  install -Dpm644 "${pkgdir}/etc/nslink.conf" "${pkgdir}/usr/share/nslink/nslink.conf.orig"

  # Desktop file for NS-Link
  install -Dm644 <(cat << EOF
[Desktop Entry]
Name=Comtrol NS-Link Administration
GenericName=Device Server Manager
Comment=DeviceMaster NS-Link for Comtrol Device Servers
Exec=gksudo -k -u root nslinktool
Terminal=false
Type=Application
#Icon=
Categories=Application;Utilities;
MimeType=application/x-executable
EOF
  ) "${pkgdir}/usr/share/applications/nslinktool.desktop"

  # Generate launcher
  mv "${pkgdir}/usr/bin"/{'nslinkadmin','nslinkadmin.exec'}
  install -Dm755 <(cat << EOF
#!/bin/bash
# Automatically generated by ${pkgname}-${pkgver} PKGBUILD from Arch Linux AUR
# https://aur.archlinux.org/

# A launcher to detect UP ethernet interfaces and sed the selected on into a temp executable.
# The executable has been compiled with a special string that we can replace.

set -e
set -u

neweths=(\$(ip addr | grep ' UP ' | cut -d':' -f2))

DEBUG=0
if [ "\${DEBUG}" -ne 0 ]; then
  if [ "\${EUID}" -eq 0 ]; then
    echo 'Must not be root'
    exit 1
  fi
  adminbin='./nslinkadmin.exec'
  admintmp="\${adminbin}.tmp"
  cp -pn "\${adminbin}" "\${adminbin}.bak"
  neweths+=(Arch Linux)
else
  if [ "\${EUID}" -ne 0 ]; then
    echo 'Must be root'
    exit 1
  fi
  adminbin='/usr/bin/nslinkadmin.exec'
  admintmp="/tmp/nslinkadmin.tmp.\$$"
fi

case "\${#neweths[@]}" in
0)
  echo 'No network cards are available in the UP state. Try ip addr'
  exit 2
  ;;
1)
  neweth="\${neweths[0]}"
  #echo "Auto selecting only NIC \${neweth}" # The next program shows this so we don't need to.
  ;;
*)
  echo 'Available UP network cards'
  idx=0
  for neweth in "\${neweths[@]}"; do
    idx=\$((idx+1))
    echo -n "\${idx}-\${neweth}  "
  done
  echo ''
  wantidx=-1
  while [ "\${wantidx}" -lt 0 ]; do
    echo -n "Select network card or 0 to quit:"
    read wantidx
    if [ -z "\${wantidx}" ] || [[ ! "\${wantidx}" =~ ^[0-9]+$ ]] || [ "\${wantidx}" -lt 0 ] || [ "\${wantidx}" -gt "\${idx}" ]; then
      wantidx=-1
    fi
    if [ "\${wantidx}" -eq 0 ]; then
      exit 0
    fi
  done
  neweth="\${neweths[\$((wantidx-1))]}"
esac

if [ "\${DEBUG}" -ne 0 ]; then
  set -x
fi

# These values are compiled into the executable by the PKGBUILD and must not be changed here
neweth="\$(printf "%-${#_opt_ethernet_data}s" "\${neweth}")"
neweth="\${neweth// /\\\\x00}" # binary replace

set +e
(
umask 077
rm -f "\${admintmp}"
sed -e 's:${_opt_ethernet_data}${_opt_ethernet_keystring}:'"\${neweth}${_opt_ethernet_keystring}:" "\${adminbin}" > "\${admintmp}"
)
chmod 700 "\${admintmp}"

rv=0
if [ "\${DEBUG}" -eq 0 ] || [ "\$(stat -c '%s' "\${adminbin}")" -eq "\$(stat -c '%s' "\${adminbin}.bak")" ]; then
  trap "{ rm -f "\${admintmp}"; exit 255; }" SIGINT
  set +e
  "\${admintmp}" "\$@"
  rv="\$?"
  if [ "\${DEBUG}" -eq 0 ]; then
    rm "\${admintmp}"
  fi
fi
exit "\${rv}"
EOF
  ) "${pkgdir}/usr/bin/nslinkadmin"

  # DKMS
  if [ "${_opt_DKMS}" -ne 0 ]; then
    rm -rf "${pkgdir}/usr/lib/modules/"
    local _dkms="${pkgdir}/usr/src/${pkgname}-${pkgver}"
    #install -d "${_dkms}"
    install -Dpm644 'nslink.h' 'nslink_int.h' 'version.h' 'nslink.c' 'Makefile' -t "${_dkms}/"
    install -Dm644 <(cat << EOF
# Automatically generated by ${pkgname}-${pkgver} PKGBUILD from Arch Linux AUR
# https://aur.archlinux.org/

PACKAGE_NAME="${pkgname}"
PACKAGE_VERSION="${pkgver}"
AUTOINSTALL="yes"

BUILT_MODULE_NAME[0]="nslink"
BUILT_MODULE_LOCATION[0]=""
# Using all processors doesn't compile this tiny module any faster.
MAKE[0]="make -j1 all QUIET=1"
CLEAN[0]="make -j1 clean"
# Placing the DKMS generated module in a different location than the standard install prevents conflicts when PKGBUILD _opt_DKMS is toggled
DEST_MODULE_LOCATION[0]="/kernel/drivers/misc"
EOF
    ) "${_dkms}/dkms.conf"
    #make -C "${_dkms}/" clean
    sed -e '# No DKMS instructions say to do this but it works and keeps the MAKE line real simple' \
        -e 's:$(shell uname -r):$(KERNELRELEASE):g' \
        -e 's:`uname -r`:$(KERNELRELEASE):g' \
        -e 's:$(KVER):$(KERNELRELEASE):g' \
        -e '# Get rid of make lines so make all makes the module' \
        -e 's:^\s\+make\s:#&:g' \
       -i "${_dkms}/Makefile"
  fi

  # Install firmware updaters
  cd "${srcdir}/${_srcdir2}"
  install -Dpm755 *.py -t "${pkgdir}/usr/bin/"
  install -Dpm644 'README.txt' -t "${pkgdir}/usr/share/nslink/"

  # Desktop file for GUI firmware updater
  install -Dm644 <(cat << EOF
[Desktop Entry]
Name=Comtrol Update Firmware
GenericName=Device Server Manager
Comment=Manage Firmware on Comtrol Device Servers
Exec=DM-Firmware-Updater.py
Terminal=false
Type=Application
#Icon=
Categories=Application;Utilities;
MimeType=application/x-executable
EOF
  ) "${pkgdir}/usr/share/applications/DM-Firmware-Updater.py.desktop"
  set +u
}

set +u