summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: 1be944397a3c91605039a4f77c8a92cb558e6125 (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
# Maintainer: Chris Severance aur.severach aATt spamgourmet dott com

_opt_DKMS=1           # This can be toggled between installs
_opt_MAXINSTPORTS=16  # Maximum install ports.
                      # Default=256 which is plenty for normal size installations.
                      # It's best to use a small number. Nodes are created for
                      # /dev/ttyADV{0..n-1}
#_opt_defaultmode='666' # default: 666

# Products, Intelligent Connectivity, Industrial Communication, Serial Device Servers

# Todo: misspellings all over
# Todo: what does vcinot do?
# Todo: What does vcomd SERVICE do?
# Todo: The daemon shouldn't require kill -9
# Todo: The port opens without error when the unit cannot be connected to, possibly due to wrong configuration.
# Todo: vcom does not announce itself in dmesg
# Todo: advadd does not check for duplicate adds
# Todo: /dev/ttyADV should be created as needed, not all at once
# Todo: implement automatically assigned major number

# Enable at boot
#   systemctl enable --now 'advtty-vcom.service'
#   systemctl status 'advtty-vcom.service'

# Add a 1 port Adam-4571L-CE
# See README.txt for table of device codes.
#   advadd -t 'B571' -a '192.168.50.222' -p 1 -m 6
#   advman -o restart
# or
#   systemctl restart 'advtty-vcom.service'

# Show the config
#   advls

# Show the tty. The number in the first column of advls is the ttyADV#
# ls /dev/ttyADV*

# Show the running module
#   lsmod | grep 'advvcom'

# Show the running driver
#  ps -ef | grep vcomd

# remove port
#   advrm -a '192.168.50.222'
#   advman -o restart

# More help
#   less -S '/usr/share/advtty/readme.txt'

# Uninstall cleanup: sudo rm -f /etc/advttyd.conf* /var/log/advttyd.log

set -u
# Earlier versions of Advantech TTY were called ICOM
# http://advdownload.advantech.com/productfile/Downloadfile1/1-NZ17GY/ICOM_LINUX_PSEUDO_TTY_DRIVER_V1.4.1.ZIP
pkgname='advantech-vcom'
#pkgver='2.1.0'; _dl='4/1-15OSOW4'
#pkgver='2.2.0'; _dl='4/1-1LPJPGD'
pkgver='2.2.1'; _dl='5/1-1NOKMCV'
pkgrel='1'
pkgdesc='tty driver for Advantech Adam EKI serial console terminal servers'
_pkgdescshort="Advantech ${pkgname} TTY driver"
arch=('i686' 'x86_64')
url='http://www.advantech.com/intelligent-connectivity/' # Serial Device Servers
license=('GPL')
depends=('glibc' 'gawk' 'psmisc' 'sed' 'grep')
backup=('etc/advttyd.conf')
install="${pkgname}-install.sh"
_srcdir="VCOM_LINUX_${pkgver}.TAR.BZ2"
_srcdir="${_srcdir,,}"
source=("http://advdownload.advantech.com/productfile/Downloadfile${_dl}/${_srcdir}")
#source=("${_srcdir,,}::http://downloadt.advantech.com/download/downloadsr.aspx?File_Id=${_dl}") # redirect works sooner but can be changed arbitrairly
_srcdir="${_srcdir%\.tar*}"
#source=("http://advdownload.advantech.com/productfile/Downloadfile4/${_dl}/${_srcdir}.rar")
source+=('0000-advman.systemd.patch')
sha256sums=('e5e313a1542e227a654fd1a497f8846ccb90df5490a888929826cb82becb5b0f'
            '02f504a23fbef07f666aaa595faba0513d9ffec5e99ebca7b7fe2299a0179e32')

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

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

  # Cosmetic correction of CRLF for Linux
  sed -e 's:\r$::g' -i 'readme.txt'

  # Fix umbrella Makefile
  #cp -p Makefile{,.Arch}
  sed -e '# Cosmetic correction of spaces' \
      -e 's:\s\+$::g' \
      -e '# Make package compatible' \
      -e '/ln -s/ ! s:$(INSTALL_PATH):$(DESTDIR)/&:g' \
      -e '# fix paths and more for package' \
      -e 's: /sbin/: $(DESTDIR)/usr/bin/:g' \
      -e '# Dont want the Makefile. Probably there for uninstall' \
      -e 's:^\s\+cp ./Makefile:#&:g' \
      -e '# Move config to /etc' \
      -e '/advttyd.conf/ s:\$(INSTALL_PATH):etc/:g' \
    -i 'Makefile'
  test ! -s 'Makefile.Arch' || echo "${}"

  # Fix driver Makefile
  sed -e '# Cosmetic correction of spaces' \
      -e 's:\s\+$::g' \
      -e '# Clean missed some files' \
      -e 's:^\s\+rm -.*$:& *.order *.symvers:g' \
      -e '# Adding the current dir (.) to the include path seems unnecessary' \
      -e 's:^ccflags-y += -I:#&:g' \
    -i 'driver/Makefile'

  local _lines='
set -u
if [ "${EUID}" -ne 0 ]; then
  echo "Must be root"
  exit 1
fi
'
  # Fix the scripts
  sed -e '#Get rid of some UTF-8 quotes, even though they seem to work' \
      -e "s:\xE2\x80\x9C:':g" \
      -e "s:\xE2\x80\x9D:':g" \
      -e '#Get rid of repeat hash bang' \
      -e '2,$ s:#!/bin.*$::g' \
      -e '# Catch misspellings that are code errors and root trap' \
      -e "1 a\\${_lines//$'\n'/\\\n}" -e '# 2 i also works' \
      -e '# misspelled variable, caught with set -u' \
      -e 's:INSTALLPTATH:INSTALLPATH:g' \
      -e '#Fix /usr/local' \
      -e 's:/usr/local/:/usr/share/:g' \
      -e '#Move config to /etc' \
      -e 's:$INSTALLPATH/advttyd.conf:/etc/advttyd.conf:g' \
      -e '#Move sort tmp to secure location in /tmp' \
      -e '/advttyd.tmp/ s:$INSTALLPATH/:/tmp/advmon/:g' \
      -e '#Create secure tmp folder.' \
      -e '/^cleanUp()/,/^}/ s:^sed :install -dm700 -oroot -groot "/tmp/advmon/"\nrm -f "/tmp/advmon/advttyd.tmp"\n&:g' \
      -e '#Clean up code' \
      -e 's:^HASARG=$:&0:g' \
    -i script/*
  sed -e '#Allow non root to run advls' \
      -e '/if \[ "\${EUID}"/,/^fi/ d' \
      -e 's:^cleanUp\s*$:if [ "${EUID}" -eq 0 ]; then\n  &\nfi:g' \
      -e '# Prevent samples from showing' \
      -e 's:^PATERN="$MINOR:PATERN="^$MINOR:g' \
    -i 'script/advls'

  # Fix start/stop
  sed -e '# Permanently disable a kernel well never see' \
      -e 's:\[ \$KERNEL = "2\.4" ]:false # &:g' \
      -e '# No need to detect kernel version' \
      -e 's:^KERNEL=:#&:g' \
      -e '# The internet says modprobe is better' \
      -e 's:insmod\s:modprobe :g' \
      -e '# The module is installed by the PKGBUILD so this is unnecessary' \
      -e 's:^DKMSFILE=:#&:g' \
      -e 's:\[ -e \$DKMSFILE \]:false # &:g' \
      -e '# Non zero exit code wont work for systemd' \
      -e '/^case $OBJECTIVE/,/^esac/ s:^\(\s\+\)exit 1:\1# systemd-notify --ready\n\1exit 0:g' \
      -e '# Modprobe just wants the name' \
      -e 's:^\(DRIVERFILE\)=.*\.ko$:\1=$DRIVER:g' \
      -e '# Secure our temp folder' \
      -e '\:/tmp/advmon: s:777:700 -oroot -groot:g' \
    -i 'script/advman'

  # Fix daemon
  sed -e '#Move config to /etc' \
      -e '/CF_CONFNAME/ s:work_path:"/etc":g' \
      -e '#Move log to /var' \
      -e '/CF_LOGNAME/ s:work_path:"/var/log":g' \
    -i 'initd/advttyd.c'

  # Cosmetic correction of spaces
  sed -e 's:\s\+$::g' -i 'readme.txt' script/* inotify/* initd/adv* driver/adv* daemon/*

  # Add device table to config
  sed -n -e '/Device-Type Table/,/install/ p' 'readme.txt' | \
    expand | head --lines=-1 | tail --lines=+2 | \
    sed -e 's:^:#:' -e 's:\(Device Name\) : \1:' >> 'config/advttyd.conf'

  # Convert the live config entries to samples. This eliminates
  # special handling for detecting a blank configuration.
  sed -e 's:^[0-9]:# Sample  &:g' \
      -e '/Minor/ s:^#:&00:g' \
    'config/advttyd.conf' > 'config/advttyd.conf.Arch'
  sort -g 'config/advttyd.conf.Arch' > 'config/advttyd.conf'
  rm 'config/advttyd.conf.Arch'

  # Make a less noisy start/stop to not clog up the systemd logs
  sed -e 's@^\(\s*\)echo@\1: # echo@g' 'script/advman' > 'script/advman.quiet'

  # Change original advman to systemd
  #cp -p script/advman{,.orig}; false
  #diff -pNau10 script/advman{.orig,} > '../0000-advman.systemd.patch'
  patch -Nbup0 -i "${srcdir}/0000-advman.systemd.patch"

  # Tame the port count
  sed -e 's:^\(#define VCOM_PORTS\)\s.*$:'"\1 ${_opt_MAXINSTPORTS}:g" -i 'driver/advconf.h'

  # The compiled files should not have been included
  'ma'ke -s -j1 clean # keep git-aurcheck quiet
  set +u
}

build() {
  set -u
  cd "${_srcdir}"
  make -j1
  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 install expects the system folders to exist
  install -d "${pkgdir}"{'/usr/bin','/etc','/var/log'}

  make -s -j1 DESTDIR="${pkgdir}" INSTALL_PATH="/usr/share/advtty/" install_daemon

  # Most utils are fixed to terminate on non root.
  find "${pkgdir}/usr/share/advtty/" -type 'f' -perm /111 -exec chmod 755 '{}' '+'
  # Root only permissions that don't block non root
  chmod 744 "${pkgdir}/usr/share/advtty/advttyd"

  # The makefile installed the module into the wrong place so we disabled it
  install -Dpm644 'driver/advvcom.ko' -t "${pkgdir}/usr/lib/modules/$(uname -r)/extra/char/"

  install -Dpm755 'script/advman.quiet' -t "${pkgdir}/usr/share/advtty/"
  install -Dpm644 'readme.txt' -t "${pkgdir}/usr/share/advtty/"

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

[Unit]
Description=${_pkgdescshort}
After=network.target

[Service]
#Type=notify
Type=forking
ExecStart=/usr/share/advtty/advman.quiet -o start
ExecStop=/usr/share/advtty/advman.quiet -o remove
ExecReload=/usr/share/advtty/advman.quiet -o restart
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF
  ) "${pkgdir}/usr/lib/systemd/system/advtty-vcom.service"

  if [ "${_opt_DKMS}" -ne 0 ]; then
    rm -r "${pkgdir}/usr/lib/modules/"
    local _dkms="${pkgdir}/usr/src/${pkgname}-${pkgver}"
    # It's easier to make my own config than fix theirs
    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]="advvcom"
BUILT_MODULE_LOCATION[0]="driver"
# Using all processors doesn't compile this tiny module any faster.
# make modules doesn't work
MAKE[0]="make -C 'driver' -j1 DEBUG=0"
CLEAN[0]="make -C 'driver' -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"

    install -Dm644 driver/* -t "${_dkms}/driver/"
    make -C "${_dkms}/driver/" clean
    rm "${_dkms}/driver/dkms.conf"
    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 '# DKMS sets KERNELRELEASE which accidentally launches phase 2 of this Makefile' \
        -e '# Fix by changing the detection var.' \
        -e 's:^ifneq ($(KERNELRELEASE),):ifneq ($(ARCHLINUX),):g' \
        -e '# Put the detection var in' \
        -e 's:^\s\+\$(MAKE)\s-C\s\$(KERNELDIR):& ARCHLINUX=1:g' \
       -i "${_dkms}/driver/Makefile"
  fi
  set +u
}

set +u