# spec for building a weewx rpm for redhat or suse systems
# License: GPLv3
# Author: (c) 2013-2020 Matthew Wall

# the operating system release number is specified externaly, so that we can
# do cross-release (but not cross-platform) packaging.
%define os_target OSREL

# suse 12: python2
%if 0%{?suse_version} && "%{os_target}" == "12"
%define relos .suse12
%define platform suse
%define initdir /etc/systemd/system
%define deps python , python-configobj , python-cheetah , python-imaging , python-pyserial, python-usb
%define python python
%define html_root /srv/www/htdocs/weewx
%endif

# suse 15: python3
%if 0%{?suse_version} && "%{os_target}" == "15"
%define relos .suse15
%define platform suse
%define initdir /etc/systemd/system
%define deps python3, python3-setuptools, python3-configobj, python3-Cheetah3, python3-Pillow, python3-pyserial, python3-usb
%define python python3
%define html_root /var/www/html/weewx
%endif

# suse linux enterprise server
%if 0%{?sles_version}
%define relos .sles
%define platform suse
%define initdir /etc/systemd/system
#define initdir /etc/init.d
%define deps python , python-configobj , python-cheetah , python-imaging , python-pyserial, python-usb
%define html_root /srv/www/htdocs/weewx
%define python python
%endif

# rh8: python3 on redhat, fedora, centos
%if "%{_vendor}" == "redhat" && "%{os_target}" == "8"
%define relos .el8
%define platform redhat
%define initdir %{_initrddir}
# must install cheetah from epel-release or using pip3
%define deps python3, python3-setuptools, python3-configobj, python3-pillow, python3-pyserial, python3-pyusb
%define python python3
%define html_root /var/www/html/weewx
# Disable Fedora's shebang mangling script,
# which errors out on any file with versionless `python` in its shebang
# See: https://github.com/atom/atom/issues/21937
%undefine __brp_mangle_shebangs
%endif


%global relnum RPMREVISION
%global release %{relnum}%{?relos:%{relos}}

%global dst_bin_dir  %{_datadir}/%{name}
%global dst_doc_dir  %{_defaultdocdir}/%{name}-%{version}
%global dst_cfg_dir  %{_sysconfdir}/%{name}
%global cfg_file     %{dst_cfg_dir}/weewx.conf
%global dst_user_dir %{dst_bin_dir}/user

%define entry_points weewxd weectl wee_import wee_reports

Summary: weather software
Name: weewx
Version: WEEWX_VERSION
Release: %{release}
Group: Applications/Science
Source: %{name}-%{version}.tar.gz
URL: http://www.weewx.com
License: GPLv3
AutoReqProv: no
Requires: %{deps}
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-%(%{__id_u} -n)
BuildArch: noarch

%description
weewx interacts with a weather station to produce graphs, reports, and HTML
pages. weewx can upload data to weather services such as WeatherUnderground,
PWSweather.com, or CWOP.

%prep
%setup -q

%build

%install
rm -rf %{buildroot}
mkdir -p %{buildroot}%{_bindir}
mkdir -p %{buildroot}%{dst_bin_dir}
mkdir -p %{buildroot}%{dst_doc_dir}
mkdir -p %{buildroot}%{dst_cfg_dir}
mkdir -p %{buildroot}%{initdir}
mkdir -p %{buildroot}/etc/default

# copy files from the source tree
cp -r bin/* %{buildroot}%{dst_bin_dir}
cp -r ../../docs/* %{buildroot}%{dst_doc_dir}
cp -r examples %{buildroot}%{dst_doc_dir}
cp -r skins %{buildroot}%{dst_cfg_dir}
cp -r util/apache %{buildroot}%{dst_cfg_dir}
cp -r util/import %{buildroot}%{dst_cfg_dir}
cp -r util/logrotate.d %{buildroot}%{dst_cfg_dir}
cp -r util/logwatch %{buildroot}%{dst_cfg_dir}
cp -r util/rsyslog.d %{buildroot}%{dst_cfg_dir}
cp -r util/udev %{buildroot}%{dst_cfg_dir}

# patch paths in files we care about
cat weewx.conf | sed \
 -e 's%^WEEWX_ROOT =.*%WEEWX_ROOT = /%' \
 -e 's%SKIN_ROOT =.*%SKIN_ROOT = /etc/weewx/skins%' \
 -e 's:HTML_ROOT = public_html:HTML_ROOT = %{html_root}:' \
 -e 's%SQLITE_ROOT = .*%SQLITE_ROOT = /var/lib/weewx%' \
 > %{buildroot}%{dst_cfg_dir}/weewx.conf
%if "%{initdir}" == "/etc/systemd/system"
cat util/systemd/weewx.service | sed \
 -e 's%ExecStart=.*%ExecStart=/usr/bin/weewxd /etc/weewx/weewx.conf%' \
 > %{buildroot}%{initdir}/weewx.service
%else
cp util/init.d/weewx.%{platform} %{buildroot}%{initdir}/weewx
%endif

# make a copy of the generic configuration file
cp %{buildroot}%{dst_cfg_dir}/weewx.conf %{buildroot}%{dst_cfg_dir}/weewx.conf.dist

# create the entry points
for f in %{entry_points}; do \
  cp util/scripts/$f %{buildroot}%{_bindir}/$f; \
done

# massage the shebangs in the entry points to pacify fascist redhat build tools
for f in %{entry_points}; do \
  sed -i -e 's:/usr/bin/env python:/usr/bin/%{python}:' %{buildroot}/usr/share/weewx/$f; \
done

# configure application layout
	cat util/default/weewx | sed \
 -e 's%WEEWX_PYTHON=.*%WEEWX_PYTHON=%{python}%' \
 -e 's%WEEWX_BINDIR=.*%WEEWX_BINDIR=/usr/share/weewx%' \
 -e 's%WEEWX_BIN=.*%WEEWX_BIN=/usr/bin/weewxd%' \
 -e 's%WEEWX_CFG=.*%WEEWX_CFG=/etc/weewx/weewx.conf%' \
 > %{buildroot}/etc/default/weewx

%pre
if [ "$1" = "2" ]; then
  # this is an upgrade
  if [ -f %{cfg_file} ]; then
    echo saving previous config as %{cfg_file}.prev
    cp -p %{cfg_file} %{cfg_file}.prev
  fi
fi

%post
# pre-compile the python code
%{python} -m compileall %{dst_bin_dir}
if [ "$1" = "1" ]; then
  # this is a new installation
  # create a sane configuration file with simulator as the station type
  /usr/bin/wee_config --install --dist-config=/etc/weewx/weewx.conf.dist --output=/etc/weewx/weewx.conf --driver=weewx.drivers.simulator --no-prompt --no-backup
  if [ -f /etc/systemd/system/weewx.service ]; then
    systemctl enable weewx
    systemctl start weewx
  else
    chkconfig weewx on
    %{initdir}/weewx start
  fi
elif [ "$1" = "2" ]; then
  # this is an upgrade
  # update previous config and merge with dist into to weewx.conf
  if [ -f %{cfg_file}.dist -a -f %{cfg_file}.prev ]; then
    OLDVER=`grep version %{cfg_file}.prev | sed -e 's/\s*version\s*=\s*//'`
    echo saving previous config as %{cfg_file}-$OLDVER
    mv %{cfg_file}.prev %{cfg_file}-$OLDVER
    echo merging configs into %{cfg_file}
    /usr/bin/wee_config --upgrade --config=%{cfg_file}-$OLDVER --dist-config=%{cfg_file}.dist --output=%{cfg_file} --no-prompt --no-backup
  fi
  # do a full restart not just a HUP
  if [ -f /etc/systemd/system/weewx.service ]; then
    systemctl stop weewx
    systemctl start weewx
  else
    %{initdir}/weewx stop
    %{initdir}/weewx start
  fi
fi

%preun
if [ "$1" = "0" ]; then
  # this is an uninstall, so stop and remove everything
  if [ -f /etc/systemd/system/weewx.service ]; then
    systemctl stop weewx
    systemctl disable weewx
  else
    %{initdir}/weewx stop
    chkconfig weewx off
  fi
fi
# otherwise this is an upgrade (1), so do nothing

%clean
rm -rf %{buildroot}

%files
%defattr(-,root,root)
%if "%{initdir}" == "/etc/systemd/system"
  %{initdir}/weewx.service
%else
  %attr(755,root,root) %{initdir}/weewx
%endif
%{_sysconfdir}/default/weewx
%{dst_bin_dir}/
%{_bindir}/weewxd
%{_bindir}/weectl
%{_bindir}/wee_import
%{_bindir}/wee_reports
%doc %{dst_doc_dir}/
%config(noreplace) %{dst_cfg_dir}/
%config(noreplace) %{dst_user_dir}/extensions.py
%changelog
