summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: f442a76ce330f212c73bd1961b4d2781b92564ec (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# Maintainer: Chris Severance aur.severach aATt spamgourmet dott com

# Todo: rewrite dgrp_cfg_node to be systemd compatible.
# Todo: add systemd getty support to drpadmin
# Todo: Default user, group, and mode are in the backing store. These do not work. They are set by udev and apply to all ports.
# Digi bug: terminal freezes when viewing /proc/dgrp/mon
# Digi bug: drpd terminates after the first tty open when launched from dgrp_cfg_node. It stays working when launched by systemd restart
# Digi bug: occasional Can't open serial /dev/ttyaf00: Resource temporarily unavailable for PortServer TS (not II)
# Digi bug: occasional dropped characters for PortServer II, PortServer TS, Digi One
# Digi bug: Digi RealPort Manager (java) is unable to add new Realport because it uses the wrong options
# Digi bug: mbrowse reports a few parsing errors in MIB
# Digi bug: make compatible with OpenSSL 1.1
# Digi bug: transfer hangs if unit is repowered during live connection. Tested in 4.11, 4.9, and 4.4
# Digi bug: tty* takes up to an hour to reappear after unit is powered up after a long time being off
#  Nov 29 06:16:50 springport drpd[715]: drpd(ag,128.0.0.92) Cannot connect to server - Connection timed out
#  Nov 29 07:21:21 springport drpd[715]: drpd(ag,128.0.0.92) Cannot connect to server - Connection timed out
#  Nov 29 08:25:51 springport drpd[715]: drpd(ag,128.0.0.92) Cannot connect to server - Connection timed out
#  Nov 29 09:30:22 springport drpd[715]: drpd(ag,128.0.0.92) Cannot connect to server - Connection timed out
#  Nov 29 10:34:53 springport drpd[715]: drpd(ag,128.0.0.92) Cannot connect to server - Connection timed out
#  Nov 29 11:39:24 springport drpd[715]: drpd(ag,128.0.0.92) Cannot connect to server - Connection timed out
#  Nov 29 12:43:54 springport drpd[715]: drpd(ag,128.0.0.92) Cannot connect to server - Connection timed out
#  Nov 29 13:48:25 springport drpd[715]: drpd(ag,128.0.0.92) Cannot connect to server - Connection timed out
#  Nov 29 14:52:56 springport drpd[715]: drpd(ag,128.0.0.92) Cannot connect to server - Connection timed out
#  Nov 29 15:57:26 springport drpd[715]: drpd(ag,128.0.0.92) Cannot connect to server - Connection timed out
# Arch Kernel 4.16: do_IRQ: 7.36 No irq handler for vector
#  does not occur in Manjaro Kernel 4.16

# Digi Realport driver for Arch Linux. See Digi release notes for supported products.

# Build instructions: (Skip these first two sections)

# This only applies if you installed a long time ago when this driver would still
# compile and haven't upgraded in a while. Now this can't be built on Arch without a
# patch so new users skip to the next section.

# This PKGBUILD does not clean up the slop from the Digi supplied make.
# If you have already installed according to the the instructions
# log on as root and go through the process in reverse from the original
# unmodified Digi tgz. You won't lose your configuration.
# make preuninstall; make uninstall
# To clean up you may want to remove the folders /share/ and /etc/init.d/
# Arch Linux does not use these folders though they may have spurious files from
# other misdirected installs.

# The next section: ... Now onto the real install ...

# Set the mode for all created devices.
_opt_defaultmode="0660" # default: 0600
_opt_defaultgroup="uucp" # default: root
# If you need more granular control.
# See http://knowledge.digi.com/articles/Knowledge_Base_Article/HOW-TO-Preserve-device-permissions-settings-with-RealPort-in-Linux/
# Once set copy /etc/udev/rules.d/10-dgrp.rules to the PKGBUILD folder
# and it will be made into the package.

# Digi's product name is inconsistent. Here you can choose. RealPort seems the most popular.
_opt_RealPort='RealPort' # Can also be Realport

_opt_DKMS=1           # This can be toggled between installs

_opt_SSL10=1

# Since the kernel module isn't loaded until you have a device
# configured, these services are automatically enabled and started
# for immediate hardware support. They will be reenabled each time the
# package is installed or upgraded.
# systemctl enable dgrp_daemon.service
# systemctl start dgrp_daemon.service
# systemctl enable dgrp_ditty.service
# systemctl start dgrp_ditty.service

# To stop these services from loading:
# systemctl disable dgrp_daemon.service
# systemctl stop dgrp_daemon.service
# systemctl disable dgrp_ditty.service
# systemctl stop dgrp_ditty.service

# To get started using your Digi products:
# man -Ik "Digi "
# man dgrp_gui
# man dgrp_cfg_node
# man ditty
# man drpadmin

# The man page for dgrp_cfg_node lacks some detail. See this page for more info
# ftp://digiftp.digi.com/support_archive/support/manuals/psts/rp-linux-conf-managing-portservers-with-dgrp-config-node.html

# To get your equipment up faster on servers that don't have X installed
# I've supplied a console shell script "drpadmin".
# It is adapted from Digi's RealPort "drpadmin" for Sun, HP UX, and SCO and has the same
# look and feel.

# Show available Digi devices
#   addp.pl -Q

# For information about using dgrp_ditty.service to maintain your ditty settings
# across reboots see http://knowledge.digi.com/articles/Knowledge_Base_Article/How-do-I-configure-my-ditty-rp-port-settings-to-stay-after-rebooting-when-using-RealPort-in-Linux/

# UnInstall cleanup:
# rm /etc/dgrp.backing.store* /usr/bin/dgrp/config/ditty.commands
# rmdir /usr/bin/dgrp/config
# rmdir /usr/bin/dgrp

set -u
pkgname='dgrp'
#_pkgver='1.9-36'; _dl='81000137_X.tgz'
#_pkgver='1.9-38'; _dl='81000137_Y.tgz'
_pkgver='1.9-39'; _dl='40002086_Z.tgz'
pkgver="${_pkgver//-/.}"
pkgrel='2'
pkgdesc="tty driver for Digi ${_opt_RealPort} ConnectPort EtherLite Flex One CM PortServer TS IBM RAN serial console terminal servers"
#_pkgdescshort="Digi ${_opt_RealPort} driver for Ethernet serial servers" # For when we used to generate the autorebuild from here
arch=('i686' 'x86_64')
url='https://www.digi.com/'
license=('GPL' 'custom') # OpenSSL=Apache. Arch is always new enough to not need their version.
depends=('grep' 'awk' 'systemd' 'procps-ng' 'psmisc' 'perl')
if [ "${_opt_SSL10}" -ne 0 ]; then
  depends+=('openssl-1.0')
fi
optdepends=(
  {tk,gksu}': Digi RealPort Manager GUI'
  'java-runtime: Digi Device Discovery Tool GUI'
  'mbrowse: SNMP browser GUI'
)
backup=('etc/dgrp.backing.store')
options=('!docs' '!emptydirs')
install="${pkgname}-install.sh"
_vercheck() {
  local _versed2="${_verwatch[1]//:/\\:}" # Escape the two things that the PKGBUILD is not permitted to do
   _versed2="${_versed2//$/\\$}" # End of line (though sed doesn't seem to require this), and end of search
  curl -s -l "${_verwatch[0]}" | sed -ne "s:^${_versed2}"'$:\1:p' | sed -e 's:-:.:g' | sort -V
}
_verwatch=('https://www.digi.com/support/includes/drivers.aspx?pid=1954&osvid=218' '<li>.*Realport Driver for Linux ver\. \([^,]\+\), tgz version.*' 'f')
_mibs=(
  '40002014_a.mib' # DIGI Connectware Manager Notifications
  '40002194_H.mib' # Portserver TS MIB File
  '40002195_P.mib' # Digi Structures of Management (SMI) MIB
  '40002236_b.mib' # Digi MODBUS MIB
  '40002237_c.mib' # Digi Power Supply MIB
  '40002238_b.mib' # Digi Multi-Electrical Interface (MEI) MIB
  '40002239_B.mib' # Digi Socket ID MIB File
  '40002240_B.mib' # Portserver TS Port Buffering MIB File
  '40002257_B.mib' # Digi IA MIB
  '40002258_B.mib' # Digi UDP Serial MIB
  '40002335_B.mib' # Portserver TS Secure Access MIB File
  '40002336_C.mib' # Digi Power Management MIB File
  '40002337_D.mib' # Digi Power Management Traps MIB
  '40002338_D.mib' # Digi Keyword Notification MIB
  '40002339_D.mib' # Digi Login Traps MIB
  '40002410_a.mib' # DIGI Connect Device Info Management Information Base
  '40002411_a.mib' # Digi Connect Serial Alarm Traps MIB
  '40002478_B.mib' # Digi NFS TRAPS MIB
  '40002479_F.mib' # CM Management Information Base
  '40002514_a.mib' # Digi Connectware Manager Notifications MIB
  '40002515_a.mib' # Digi Connect Device Identity MIB
  '40002520_a.mib' # Digi Power Traps MIB
  '40002709_C.mib' # Digi ConnectPort LTS MIB
  'rfc1316.mib' # Portserver II RFC1316-MIB Definitions
  'rfc1317.mib' # Portserver II RFC1317-MIB Definitions
)
# Let me know if these dnw (do not want) items are actually serial device servers and should be installed.
_mibsdnw=(
  '40002325_D.mib' # DIGI Wireless Lan Management Information Base
  '40002370_C.mib' # DIGI Serial Traps Management Information Base
  '40002436_B.mib' # DIGI Modem Test Traps Management Information Base
  '40002477_B.mib' # Management Information Base
  '40002519_F.mib' # Passport Management Information Base
  '40002521_a.mib' # DIGI IPMI Notification Feature Management Information Base
  '40002593_B.mib' # DIGI Connect Mobile Information Management Information Base
  '40002594_a.mib' # DIGI Mobile Traps Management Information Base
  '40002782_a.mib' # Passport Management Information Base
  '40002846_A.mib' # DIGI Connect Mobile Status Management Information Base
)
_mibsrc='http://ftp1.digi.com/support/utilities/'
_filever="${pkgver//\./-}"
_filever="${_filever/-/.}"
_srcdir="dgrp-${_filever%%-*}"
source=(
  #"${pkgname}-${pkgver}-81000137_X.tgz::http://ftp1.digi.com/support/driver/81000137_X.tgz"
  #"${pkgname}-${pkgver}-beta.tgz::ftp://ftp1.digi.com/support/driver/RealPort%20Linux%20Beta%20Driver/dgrp-${_filever}_y1p.tgz.rpm"
  "${pkgname##*-}-${pkgver}-${_dl}::http://ftp1.digi.com/support/driver/${_dl}"
  'drpadmin' 'drpadmin.1' # "autorebuild-${pkgname}.sh"
  'addp_perl-1.0.tgz::https://github.com/severach/addp/archive/f92a6fd2050c9f32a5a11cac18cd9def78138530.tar.gz'
  'ftp://ftp1.digi.com/support/utilities/AddpClient.zip'
  "${_mibs[@]/#/${_mibsrc}}"
  #'0000-Kernel-4-13-CLASS_ATTR_STRING.patch' # https://www.digi.com/support/forum/67157/realport-compile-error-with-fedora-27-kernel-4-14-14 https://www.digi.com/support/forum/65817/class_attr_driver_version-error-compiling-in-kernel-4-13
  #'0001-Kernel-4-15-timers.patch' # https://forum.blackmagicdesign.com/viewtopic.php?uid=16&f=3&t=68382&start=0
  '0002-kernel-5.0.0-do_gettimeofday.patch'
  '0003-kernel-5.0.0-dgrp_mon_ops-access_ok.patch' # https://lkml.org/lkml/2019/1/4/418
)
unset _mibsrc
#source_i686=('http://ftp1.digi.com/support/utilities/40002890_A.tgz')
#source_x86_64=('http://ftp1.digi.com/support/utilities/40002889_A.tgz') # compiled i686 therefore worthless
# addp and sddp are incomplete. I replaced them with addp.pl
sha256sums=('0db5204cc7d7806fde39b5ea7b6a465b4310739c380d7330131956e63af0f137'
            '42898b9d24262de27e9b1f3067d51d01373810b7c9e4991403a7f0a5dd7a26cf'
            '66f8b106a052b4807513ace92978e5e6347cef08eee39e4b4ae31c60284cc0a3'
            '9d79df8617e2bb1042a4b7d34311e73dc4afcdfe4dfa66703455ff54512427f5'
            '00d7b452a4f16599f7162f512a05599614115554992b872fc5302b521ea04468'
            '4b54148008b02a2544d8b33f07c471a068b0973ac5402967af8bf73a28b6a8b6'
            'a1833d877b07b0f424676241b3e1070e116d07965db3131a61a3b6ce0ff90063'
            '6fca5df11304d905f561a0c251419619960a8da8e724d36b34e9977e97f02528'
            '2dd7868acf66c14d67012a2263088b08d8f9320c64b64db94740fae54b485c78'
            '26159071b1b0df2af04d612e32ce72c8835f241c56b9fa2dadee53d9d127d0b7'
            'f686011f7db06f0632f6460566da751a00fccd04bb1527b8a83239aad46d4de5'
            '50130240e3ce85759aa99b3a268568de6a97084eeb40a92ef0b4937333808d8a'
            'f4a89790ad1413ecfc2b723a45fa0d0b93ae01cc433d930db4e689f82d0367fd'
            '3e2881ebf6866751a64e7cf948b85a3c2053f2d0a8799234c56b732cde17b853'
            'c54576fad5ccedfd50cebc22a6dd45bd8c4deb875282f5c7d1a89a33426c1746'
            '08eecc0e5d8f9fffe0fcf3b9f47e56c81bd713964bd7aeb0f4b6a6de3e0f3592'
            '75ba60917769c2cc2324050b74a4e618f0904464ece15646045fd931001c47e4'
            '43e7f12bb32343254f472260fd26566e8aab58764ba3e73d500542e462f27ac5'
            '241ef4a96b4d34652bfc7a9ce0bab317129e0123f05c68713a45d88624ddd19b'
            '21b8d7c50cacc418611b909159ed6258dc13026e8e55034e86965227b8176509'
            '471f1342398c4fce86e1d437c4f6053be75ae3a99337613d39c05e4e3c80ede9'
            '06a81a5dfaa1c61944d1a12d2efc8129c0ee72f630817f844879bd17d6cb4d80'
            'a3286df00ca3566361faf1f128752c56d046558277cd90025831f9840129e33f'
            '33b29ee72b590ecadd54e893317a279bb49a2dd4a189fd386491e1a67ef193a8'
            '4011005db626db67b50f13b6242db1fed0d11b0d89e56af7ae39f5719d7cd323'
            '679b081c31e5fc156ad9c55afc0bba9ec354f37e28eeeb453bcbd6b1cf68988e'
            '731e05fc551367faa6ad5dc317eedf305388ab12db196c0a1361a3d01bd35279'
            'c471cafa43503a40d43b42acd8bc6ef49db29e55a74e0494c85f729ea45fe243'
            '5cac7ce2e6f043127f314b93694af021ae7820ffb5bf3de343da7a240d05e9c8'
            '8654496d83c083e457e8bb9bae2b1e71804d156a38c284d89872d0125eba947d'
            '353d15624675c78dfd83318195d75bdb0507fd0476f5e22be1329bf257d2de1e'
            'acbcf462628daf4fa2dbee064969a158ccc0bb0ce9f286ceb3617e470eab1c1f')

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

_modulename='dgrp'

# Check for updates and case errors to the 2 mib lists
_fn_mibcheck() {
  echo 'Checking for newer and misnamed MIB'
  local line
  local linea
  local file='/tmp/PKGBUILD.mibs'
  # Get list of files
  while IFS='' read -r line || [[ -n "${line}" ]]; do
    read -r -a linea <<<"${line}"
    line="${linea[8]}" # filename
    case "${line}" in
    4000*.mib)
      echo "${line}" >> "${file}"
      ;;
    esac
  done < <(curl -s 'ftp://ftp1.digi.com/support/utilities/') # curl is sorted and case insensitive because Digi runs WSFTP. We can't assume this will be always so.
  # get list of latest files
  local lineu linep lineup
  lineup=''
  linep=''
  linea=()
  while IFS='' read -r line || [[ -n "${line}" ]]; do
    lineu="${line^^}"
    if [ "${lineup%%_*}" != "${lineu%%_*}" ] && [ ! -z "${linep}" ]; then
      linea+=("${linep}")
    fi
    lineup="${lineu}"
    linep="${line}"
  done < <(sort -f "${file}")
  rm -f "${file}"
      linea+=("${linep}")
  # Check against _mibs
  local found mib mibu
  for line in "${linea[@]}"; do
    lineu="${line^^}"
    found=0
    for mib in "${_mibs[@]}" "${_mibsdnw[@]}"; do
      mibu="${mib^^}"
      if [ "${lineu%%_*}" = "${mibu%%_*}" ]; then
        if [ "${lineu}" != "${mibu}" ]; then
          echo "${mib} has been updated to ${line}"
        elif [ "${line}" != "${mib}" ]; then
          echo "Case correction: ${mib} -> ${line}"
        fi
        found=1
        break
      fi
    done
    if [ "${found}" -eq 0 ]; then
      echo "${line} is missing"
    fi
  done
  return 1
}
#_fn_mibcheck
# bash -c 'source PKGBUILD; _fn_mibcheck'

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

  # Version check
  if [ "${_pkgver}" != "$(grep -e 'TRUE_VERSION=' ./Makefile.inc | cut -d'"' -f2)" ]; then
    set +u
    echo 'Version mismatch'
    false
  fi

  #cp -p driver/2.6.27/dgrp_mon_ops.c{,.orig}; false
  #diff -pNau5 driver/2.6.27/dgrp_mon_ops.c{.orig,} > '0002-kernel-5.0.0-do_gettimeofday.patch'
  patch -Nbup0 -i "${srcdir}/0002-kernel-5.0.0-do_gettimeofday.patch"

  #cp -pr driver/2.6.27{,.orig}
  #diff -pNaru5 driver/2.6.27{.orig,} > '0003-kernel-5.0.0-dgrp_mon_ops-access_ok.patch'
  patch -Nbup0 -i "${srcdir}/0003-kernel-5.0.0-dgrp_mon_ops-access_ok.patch"

  # Standardize name of RealPort
  sed -e "s/RealPort/${_opt_RealPort}/gI" -i $(grep -lrF $'RealPort\nRealport' .)
  # grep -ri realport . | grep -vF $'RealPort\nRealport'

  rm -f daemon/openssl-*.tar.gz # I don't want their version to build if OpenSSL version detection fails in the future

  # Fix configure
  sed -e '# Cosmetic fix for newer gcc compilers' \
      -e 's:\(3.9\*|4.\*\))$:\1|5.*|6.*|7.*|8.*):g' \
      -e "# I can't find any other way to fix the modules dir" \
      -e 's:/lib/modules/:/usr&:g' \
      -e '# Kill a harmless mkdir error. They mkdir the folder then dont use it.' \
      -e 's@^\(\s\+\)\(mkdir -p /usr/lib/modules/${osrel}/misc\)$@\1: #\2@g' \
    -i 'configure'

  # Some files ignore configure --sbindir
  sed -e 's:/usr/sbin:/usr/bin:g' -e 's:/sbin/:/usr/bin/:g' -i 'configure' Makefile* */Makefile scripts/{preun,post}install

  # make postinstall package compatible
  # some postinstall is done in install
  # cp -p 'scripts/postinstall'{,.Arch} # DEBUG for comparison
  sed -e '# Some security for root' \
      -e 's:^#!\s*/bin/sh$:&\nif [ "${EUID}" -ne 0 ]; then\n  echo "Must be root!"\n  exit 1\nfi:g' \
      -e '# Remove Install noise' \
      -e 's:^\(\s*\)\(echo "Running\):\1#\2:g' \
      -e '# Block the usage of chkconfig' \
      -e 's:/usr/bin/chkconfig:/usr/bin/true:g' \
      -e '# Remove noise for defunct chkconfig' \
      -e 's:^\(\s\+\)\(echo "Added\):\1#\2:g' \
      -e '# Automatically bring back pacsave file on reinstall' \
      -e 's:^if \[ -f /etc/dgrp:if [ -s "/etc/dgrp.backing.store.pacsave" -a ! -s "/etc/dgrp.backing.store" ]; then\n  mv "/etc/dgrp.backing.store.pacsave" "/etc/dgrp.backing.store"\nfi\n&:g' \
      -e '# No need to upgrade and back up a blank file' \
      -e 's:-f \(/etc/dgrp.backing.store\):-s \1:g' \
      -e '# Why depend on /tmp when we can use the /etc folder which is where admin will be looking to fix their non working hardware' \
      -e 's:/tmp/dgrp/dgrp.backing.store:/etc/dgrp.backing.store:g' \
      -e '# The rest is done in package.' \
      -e 's:^echo "Checking:exit 0\n&:g' \
      -e '# Prepare the links for package to use them' \
      -e 's:^\(\s\+\)ln -s /usr/bin/\([^ ]\+\) \(.\+\)$:\1ln -sf "\2" "${_DESTDIR}\3":g' \
      -e "# All that's left is config conversion" \
    -i 'scripts/postinstall'
  test ! -s 'scripts/postinstall.Arch'
  #cp -p 'scripts/preuninstall' 'scripts/preuninstall.Arch' # For comparison
  sed -e '# Some security for root' \
      -e 's:^#!\s*/bin/sh$:&\nif [ "${EUID}" -ne 0 ]; then\n  echo "Must be root!"\n  exit 1\nfi:g' \
      -e '# Remove UnInstall noise' \
      -e 's:^\(\s*\)\(echo "Running\):\1#\2:g' \
      -e '# Block the usage of chkconfig' \
      -e 's:/usr/bin/chkconfig:/usr/bin/true:g' \
      -e '# Remove more noise' \
      -e 's:^\(\s\+\)\(echo "Removed\):\1#\2:g' \
      -e '# No need to sleep. The daemons are shut down by systemd' \
      -e 's:^sleep :#&:g' \
      -e '# pacman handles the links and files' \
      -e 's;if \[ -L ;if ! : \&\& [ -L ;g' \
      -e 's;^\(\s*\)\(rm -f \);\1: #\2;g' \
      -e '# Fixing this file was almost useless. All it does after we disable everything is an rmmod' \
    -i 'scripts/preuninstall'
  test ! -s 'scripts/preuninstall.Arch'

  # Change insmod to modprobe
  sed -e 's:\${INSMOD}.*$:modprobe "${DGRP_DRIVER}" # &:g' -i 'config/dgrp_cfg_node'

  if [ "${_opt_SSL10}" -ne 0 ]; then
    # drpd makefile does not honor --with-ssl-dir. We convert the bogus folder to the one we need.
    sed -e 's:/usr/local/ssl/include:/usr/include/openssl-1.0:g' \
        -e 's:/usr/local/ssl/lib:/usr/lib/openssl-1.0:g' \
      -i 'daemon/Makefile.in'
  fi

  # new folder in gcc 8
  sed -e 's/^clean:$/&\n\trm -f .cache.mk/g' -i driver/*/Makefile*

  # Branding in dmesg
  sed -e 's@ please visit [^"]\+"@ please visit https://aur.archlinux.org/packages/dgrp/"@g' \
      -e '/^dgrp_init_module/,/^$/ s@version: %s@& Arch Linux@g' \
    -i driver/[0-9]*/dgrp_driver.c

  set +u
}

_configure() {
  if [ ! -s 'Makefile' ]; then
    # this generates a harmless error as it tries to make a folder in /usr/lib/modules...
    if [ "${_opt_SSL10}" -ne 0 ]; then
      # --with-ssl-dir supplies to -I but mainly for configure. CFLAGS goes everywhere.
      # --with-ssl-dir is supplied to -L too which is worthless. We amend with LDFLAGS.
      CFLAGS="${CFLAGS} -I/usr/include/openssl-1.0" \
      LDFLAGS="${LDFLAGS} -L/usr/lib/openssl-1.0" \
      ./configure -q --sbindir='/usr/bin' --prefix='/usr' --mandir='/usr/share/man' --with-ssl-dir='/usr/include/openssl-1.0'
    else
      ./configure -q --sbindir='/usr/bin' --prefix='/usr' --mandir='/usr/share/man'
    fi
  fi
}

build() {
  set -u
  cd "${_srcdir}"
  _configure

  #. 'config/file_locations.Arch'
  make -s all -j1 # This package doesn't support threaded make and it's too small to fix
  set +u
}

# Copy this line into the .install file
_daemons=('daemon' 'ditty')

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

  #. 'config/file_locations.Arch'

  make -s -j1 RPM_BUILD_ROOT="${pkgdir}" install
  install -m644 'dinc/dinc.1' -t "${pkgdir}/usr/share/man/man1/" # They bypass the Makefile that does this
  chmod 644 "${pkgdir}/usr/bin/dgrp/config"/{dgrp.gif,file_locations}
  chmod 744 "${pkgdir}/usr/bin/"{dgelreset,dgipserv}

  # Postinstall
  # Create the links, customized for us by prepare above
  grep 'ln -sf ' 'scripts/postinstall' |\
  _DESTDIR="${pkgdir}" \
  sh -e -u -s --
  #rmdir "${pkgdir}/usr/share/doc" # makepkg does this for us

  # Install MIB
  local _mib
  for _mib in "${srcdir}"/*.mib; do
    install -Dpm0644 "${_mib}" "${pkgdir}/usr/share/snmp/mibs/digi-serial-server-${_mib##*/}.txt"
  done

  # Prepend our message on the udev rules file
  install -dm755 "${pkgdir}/etc/udev/rules.d/"
  touch "${pkgdir}/${backup[0]}" # postinstall handles the pacsave file automatically
  chmod 644 "${pkgdir}/${backup[0]}"
  sed -e 's:^KERNEL=="cu_dgrp:#&:g' -i "${pkgdir}/tmp/dgrp/10-dgrp.rules" # Recommended by Digi
  cat > "${pkgdir}/etc/udev/rules.d/10-dgrp.rules" << EOF
# Automatically generated by ${pkgname}-${pkgver} PKGBUILD from Arch Linux AUR
# https://aur.archlinux.org/

# Generated: $(date +"%F %T")

# Warning: If you modify this file you should copy it into the folder with the
# PKGBUILD or you might lose the customizations on the next install.

# This file was customized by the PKGBUILD by setting the mode on all ports
# to MODE=(see below). If all you need is a different mode on all ports
# then set the option in the PKGBUILD. If you need more granular control
# see these customization instructions:

# http://knowledge.digi.com/articles/Knowledge_Base_Article/HOW-TO-Preserve-device-permissions-settings-with-RealPort-in-Linux/

# Then copy this file into the folder with PKGBUILD.

$(cat "${pkgdir}/tmp/dgrp/10-dgrp.rules")
EOF
  rm -f "${pkgdir}/tmp/dgrp/10-dgrp.rules"
  rmdir "${pkgdir}/tmp/dgrp" "${pkgdir}/tmp" # crashes if other files ever show up in /tmp/dgrp
  if [ ! -z "${_opt_defaultmode:-}" ]; then
    sed -e 's:^\(KERNEL=="tty_dgrp\)\(.*\)$:\1\2, MODE="'"${_opt_defaultmode}"'", GROUP="'"${_opt_defaultgroup}"'":g' -i "${pkgdir}/etc/udev/rules.d/10-dgrp.rules"
  fi
  if [ -s "${srcdir}/../10-dgrp.rules" ]; then
    cp "${srcdir}/../10-dgrp.rules" "${pkgdir}/etc/udev/rules.d/10-dgrp.rules" # no cp -p in case this file has any wrong user:group
  fi
  chmod 644 "${pkgdir}/etc/udev/rules.d/10-dgrp.rules"

  install -Dpm755 "${pkgdir}/etc/init.d"/{dgrp_daemon,dgrp_ditty} -t "${pkgdir}/usr/bin/dgrp/daemon/"
  rm -f "${pkgdir}/etc/init.d"/{dgrp_daemon,dgrp_ditty}
  rmdir "${pkgdir}/etc/init.d"

  # systemd integration.
  #install -dm755 "${pkgdir}/usr/lib/systemd/system/"
  local _daemon
  for _daemon in "${_daemons[@]}"; do
    install -Dm644 <(cat << EOF
# Automatically generated by ${pkgname}-${pkgver} PKGBUILD from Arch Linux AUR
# https://aur.archlinux.org/
[Unit]
Description="Digi ${_opt_RealPort} ${_daemon}"
After=network.target

[Service]
Type=forking
ExecStart=/usr/bin/dgrp/daemon/dgrp_${_daemon} start
ExecStop=/usr/bin/dgrp/daemon/dgrp_${_daemon} stop
ExecReload=/usr/bin/dgrp/daemon/dgrp_${_daemon} reload

[Install]
WantedBy=multi-user.target
EOF
  ) "${pkgdir}/usr/lib/systemd/system/dgrp_${_daemon}.service"
  #chmod 644 "${pkgdir}/usr/lib/systemd/system/dgrp_${_daemon}.service"
done

  # Install my custom drpadmin with man page.
  install -Dm755 "${srcdir}/drpadmin" -t "${pkgdir}/usr/bin/"
  sed -e 's/^#distro=:::$/g_distro="Arch Linux"/g' \
      -e "s/RealPort/${_opt_RealPort}/gI" -i "${pkgdir}/usr/bin/drpadmin"
  install -Dm444 "${srcdir}/drpadmin.1" -t "${pkgdir}/usr/share/man/man1/"
  # Standardize name of RealPort in man pages
  sed -e "s/RealPort/${_opt_RealPort}/gI" -i "${pkgdir}/usr/share/man/man8/"*.8 "${pkgdir}/usr/share/man/man1/"*.1

  # Desktop file for config tool
  install -Dm644 <(cat << EOF
[Desktop Entry]
Name=Digi RealPort Manager
GenericName=Device Server Manager
Comment=Manage tty connections to Digi serial device servers
Exec=gksudo -k -u root dgrp_gui
Terminal=false
Type=Application
#Icon=
Categories=Application;Utility;
MimeType=application/x-executable
EOF
  ) "${pkgdir}/usr/share/applications/dgrp_cfg.desktop"

  # addp tui and gui utilities
  #install -Dpm755 "${srcdir}/addp" -t "${pkgdir}/usr/bin/"
  install -Dpm755 "${srcdir}"/addp-*/'/addp.pl' "${pkgdir}/usr/bin/"
  install -Dpm644 "${srcdir}/AddpClient.jar" -t "${pkgdir}/usr/bin/dgrp/"

  # Desktop file for config tool
  install -Dm644 <(cat << EOF
# Automatically generated by ${pkgname}-${pkgver} PKGBUILD from Arch Linux AUR
# https://aur.archlinux.org/

[Desktop Entry]
Name=Digi Device Discovery Tool
GenericName=Device Server Manager
Comment=Manage Digi serial device servers
Exec=java -jar /usr/bin/dgrp/AddpClient.jar
Terminal=false
Type=Application
#Icon=
Categories=Application;Utility;
MimeType=application/x-executable
EOF
  ) "${pkgdir}/usr/share/applications/AddpClient.desktop"

  # DKMS
  if [ "${_opt_DKMS}" -ne 0 ]; then
    rm -r "${pkgdir}/usr/lib/modules/"
    local _dkms="${pkgdir}/usr/src/${pkgname}-${pkgver}"
    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]="${_modulename}"
BUILT_MODULE_LOCATION[0]="driver/build"
# Using all processors doesn't compile this tiny module any faster.
MAKE[0]="make -j1 -C 'driver/build'"
CLEAN[0]="make -j1 -C 'driver/build' 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"
    install -dm755 "${_dkms}/driver/build/"
    cp -pr 'driver/build/' "${_dkms}/driver/"
    cp -pr 'commoninc/' "${_dkms}/"
    install -pm644 'Makefile.inc' -t "${_dkms}/"
    sed -e 's:/usr/lib/modules/[^/]\+/:/usr/lib/modules/$(KERNELRELEASE)/:g' \
       -i "${_dkms}/driver/build/Makefile"
    make -C "${_dkms}/driver/build/" clean
  fi
  set +u
}

set +u