Files
weewx/pkg/debian/postinst
2023-11-23 11:55:49 -05:00

293 lines
10 KiB
Bash
Executable File

#!/bin/sh
# postinst script for weewx debian package
# Copyright 2013-2023 Matthew Wall
#
# ways this script might be invoked:
#
# postinst configure most-recently-configured-version
# old-postinst abort-upgrade new-version
# conflictor's-postinst abort-remove in-favour package new-version
# postinst abort-remove
# deconfigured's-postinst abort-deconfigure in-favour failed-install-package
# version [removing conflicting-package version]
# abort if any command returns error
set -e
# get debconf stuff so we can set configuration defaults
. /usr/share/debconf/confmodule
cfgfile=/etc/weewx/weewx.conf
cfgapp=/usr/bin/weectl
# FIXME: get these from /etc/default/weewx?
WEEWX_USER=weewx
WEEWX_GROUP=weewx
WEEWX_HOME=/var/lib/weewx
# insert the driver and stanza into the configuration file
insert_driver() {
if [ ! -f $cfgfile ]; then
return
fi
# FIXME: generalize this so it does not have to be modified every time a
# new station type is added or new station options are added.
db_get weewx/station_type
if [ "$RET" != "" ]; then
sed -i "s%station_type[ ]*=.*%station_type = $RET%" $cfgfile
if [ "$RET" = "AcuRite" ]; then
db_get weewx/acurite_model
sed -i "/\[AcuRite\]/,/\[.*\]/ s%[# ]*model[ ]*=.*% model = $RET%" $cfgfile
fi
if [ "$RET" = "CC3000" ]; then
db_get weewx/cc3000_model
sed -i "/\[CC3000\]/,/\[.*\]/ s%[# ]*model[ ]*=.*% model = $RET%" $cfgfile
db_get weewx/cc3000_port
sed -i "/\[CC3000\]/,/\[.*\]/ s% port[ ]*=.*% port = $RET%" $cfgfile
fi
if [ "$RET" = "FineOffsetUSB" ]; then
db_get weewx/fousb_model
sed -i "/\[FineOffsetUSB\]/,/\[.*\]/ s%[# ]*model[ ]*=.*% model = $RET%" $cfgfile
fi
if [ "$RET" = "TE923" ]; then
db_get weewx/te923_model
sed -i "/\[TE923\]/,/\[.*\]/ s%[# ]*model[ ]*=.*% model = $RET%" $cfgfile
fi
if [ "$RET" = "Ultimeter" ]; then
db_get weewx/ultimeter_model
sed -i "/\[Ultimeter\]/,/\[.*\]/ s%[# ]*model[ ]*=.*% model = $RET%" $cfgfile
db_get weewx/ultimeter_port
sed -i "/\[Ultimeter\]/,/\[.*\]/ s% port[ ]*=.*% port = $RET%" $cfgfile
fi
if [ "$RET" = "Vantage" ]; then
db_get weewx/vantage_type
sed -i "/\[Vantage\]/,/\[.*\]/ s% type[ ]*=.*% type = $RET%" $cfgfile
if [ "$RET" = "serial" ]; then
db_get weewx/vantage_port
sed -i "/\[Vantage\]/,/\[.*\]/ s% port[ ]*=.*% port = $RET%" $cfgfile
else
db_get weewx/vantage_host
sed -i "/\[Vantage\]/,/\[.*\]/ s% host[ ]*=.*% host = $RET%" $cfgfile
fi
fi
if [ "$RET" = "WMR100" ]; then
db_get weewx/wmr100_model
sed -i "/\[WMR100\]/,/\[.*\]/ s%[# ]*model[ ]*=.*% model = $RET%" $cfgfile
fi
if [ "$RET" = "WMR300" ]; then
db_get weewx/wmr300_model
sed -i "/\[WMR300\]/,/\[.*\]/ s%[# ]*model[ ]*=.*% model = $RET%" $cfgfile
fi
if [ "$RET" = "WMR9x8" ]; then
db_get weewx/wmr9x8_model
sed -i "/\[WMR9x8\]/,/\[.*\]/ s%[# ]*model[ ]*=.*% model = $RET%" $cfgfile
db_get weewx/wmr9x8_port
sed -i "/\[WMR9x8\]/,/\[.*\]/ s% port[ ]*=.*% port = $RET%" $cfgfile
fi
if [ "$RET" = "WS1" ]; then
db_get weewx/ws1_port
sed -i "/\[WS1\]/,/\[.*\]/ s% port[ ]*=.*% port = $RET%" $cfgfile
fi
if [ "$RET" = "WS23xx" ]; then
db_get weewx/ws23xx_model
sed -i "/\[WS23xx\]/,/\[.*\]/ s%[# ]*model[ ]*=.*% model = $RET%" $cfgfile
db_get weewx/ws23xx_port
sed -i "/\[WS23xx\]/,/\[.*\]/ s%[# ]*port[ ]*=.*% port = $RET%" $cfgfile
fi
if [ "$RET" = "WS28xx" ]; then
db_get weewx/ws28xx_model
sed -i "/\[WS28xx\]/,/\[.*\]/ s%[# ]*model[ ]*=.*% model = $RET%" $cfgfile
db_get weewx/ws28xx_frequency
sed -i "/\[WS28xx\]/,/\[.*\]/ s%[# ]*transceiver_frequency[ ]*=.*% transceiver_frequency = $RET%" $cfgfile
fi
fi
}
install_weewxconf() {
if [ ! -f $cfgfile ]; then
return
fi
driver=weewx.drivers.simulator
db_get weewx/station_type
if [ "$RET" = "AcuRite" ]; then
driver=weewx.drivers.acurite
elif [ "$RET" = "CC3000" ]; then
driver=weewx.drivers.cc3000
elif [ "$RET" = "FineOffsetUSB" ]; then
driver=weewx.drivers.fousb
elif [ "$RET" = "TE923" ]; then
driver=weewx.drivers.te923
elif [ "$RET" = "Ultimeter" ]; then
driver=weewx.drivers.ultimeter
elif [ "$RET" = "Vantage" ]; then
driver=weewx.drivers.vantage
elif [ "$RET" = "WMR100" ]; then
driver=weewx.drivers.wmr100
elif [ "$RET" = "WMR300" ]; then
driver=weewx.drivers.wmr300
elif [ "$RET" = "WMR9x8" ]; then
driver=weewx.drivers.wmr9x8
elif [ "$RET" = "WS1" ]; then
driver=weewx.drivers.ws1
elif [ "$RET" = "WS23xx" ]; then
driver=weewx.drivers.ws23xx
elif [ "$RET" = "WS28xx" ]; then
driver=weewx.drivers.ws28xx
fi
# default to US unit system
units=us
# get the system's unit system from debconf
db_get weewx/units
# sanitize the input. for backward compatibility, we recognize the older
# keywords 'US' and 'Metric', which might still be in the system's debconf
# otherwise, ensure that debconf contains one of 'us', 'metric', or
# 'metricwx'
if [ "$RET" = "US" ]; then
units=us
elif [ "$RET" = "Metric" ]; then
units=metric
elif [ "$RET" = "metric" ]; then
units=metric
elif [ "$RET" = "metricwx" ]; then
units=metricwx
fi
db_get weewx/location
location=$RET
db_get weewx/latlon
lat=$(echo $RET | cut -d, -f1 | sed 's/^ //g' | sed 's/ $//g')
lon=$(echo $RET | cut -d, -f2 | sed 's/^ //g' | sed 's/ $//g')
db_get weewx/altitude
altitude=$(echo $RET | cut -d, -f1 | sed 's/^ //g' | sed 's/ $//g')
altitude_unit=$(echo $RET | cut -d, -f2 | sed 's/^ //g' | sed 's/ $//g')
db_get weewx/register
if [ "$RET" = 'true' ]; then
register=y
db_get weewx/station_url
station_url_param="--station-url=$RET"
else
register=n
station_url_param=" "
fi
$cfgapp station reconfigure --config=$cfgfile \
--driver=$driver --units=$units --location="$location" \
--latitude=$lat --longitude=$lon --altitude=$altitude,$altitude_unit \
--register=$register $station_url_param \
--no-prompt --no-backup
insert_driver
}
# use weewx setup utilities to merge new features into existing weewx.conf
merge_weewxconf() {
if [ ! -f $cfgfile ]; then
return
fi
NEWVER=$($cfgapp --version | cut -d' ' -f2)
OLDVER=$(get_conf_version $cfgfile)
if dpkg --compare-versions $OLDVER lt $NEWVER; then
# this is an old config, so merge it into a new config
if [ -f $cfgfile.dist ]; then
DSTVER=$(get_conf_version $cfgfile.dist)
if dpkg --compare-versions $DSTVER eq $NEWVER; then
echo "Saving previous config file as $cfgfile-$OLDVER"
cp -p $cfgfile $cfgfile-$OLDVER
echo "Saving distribution config file as $cfgfile-$NEWVER"
cp -p $cfgfile.dist $cfgfile-$NEWVER
echo "Merging previous and distribution into $cfgfile"
# Upgrade the configuration file only.
$cfgapp station upgrade --config=$cfgfile --dist-config=$cfgfile-$NEWVER --what=config --no-prompt --no-backup
else
echo "Distribution config file is wrong version for merging"
fi
else
echo "No distribution config with which to merge"
fi
else
# this is a new config, so just insert debconf values into it
echo "Using debconf configuration values from previous install"
install_weewxconf
fi
}
# get the version number from the specified file, without the debian revisions
get_conf_version() {
echo $(grep '^version.*=' $1 | sed -e 's/\s*version\s*=\s*//' | sed -e 's/-.*//')
}
# create the user that the daemon will run as, but only if not already exist
create_user() {
if ! getent group | grep -q "^$WEEWX_GROUP:"; then
echo -n "Adding group $WEEWX_GROUP..."
addgroup --quiet --system $WEEWX_GROUP || true
echo "done"
fi
if ! getent passwd | grep -q "^$WEEWX_USER:"; then
echo -n "Adding system user $WEEWX_USER..."
adduser --quiet --system --ingroup $WEEWX_GROUP \
--no-create-home --disabled-password weewx || true
echo "done"
fi
test -d $WEEWX_HOME || mkdir $WEEWX_HOME
usermod -d $WEEWX_HOME -g $WEEWX_GROUP $WEEWX_USER > /dev/null 2>&1
}
# set the permissions on the data and logs so they are owned by weewx user
set_permissions() {
if getent passwd | grep -q "^$WEEWX_USER:"; then
chmod 2755 /etc/weewx
chmod 2755 /var/lib/weewx
chmod 2755 /var/log/weewx
chmod 2775 /var/www/html/weewx
chown -R $WEEWX_USER:adm /etc/weewx
chown -R $WEEWX_USER:adm /var/lib/weewx
chown -R $WEEWX_USER:adm /var/log/weewx
chown -R $WEEWX_USER:adm /var/www/html/weewx
fi
}
case "$1" in
configure)
if [ "$2" != "" ]; then
# this is an upgrade so attempt to merge
merge_weewxconf
else
# virgin install so insert debconf values into the config file
install_weewxconf
fi
# create weewx user and set permissions (if needed)
create_user
set_permissions
# precompile the bytecode
python3 -m compileall /usr/share/weewx > /dev/null
# configure for system startup
systemctl enable weewx
# start the weewx daemon
systemctl start weewx
;;
abort-remove)
# precompile the bytecode
#python -m compileall /usr/share/weewx
;;
esac
# let debconf know that we are finished
db_stop
#DEBHELPER#
exit 0