#           from gudgeon 0.0.git.244.c79e0e5
Name:       gudgeon
Version:    0.3.14
Release:    1%{?dist}
Summary:    A blocking DNS proxy/cache with host/subnet level rules and other features for managing home or small office DNS.

License:    MIT
URL:        https://github.com/chrisruffalo/gudgeon
VCS:        git+https://github.com/chrisruffalo/gudgeon.git#c79e0e56770ab268a0915c1a4431953adf9748d8:
Source:     gudgeon-0.0.git.244.c79e0e5.tar.gz

BuildRequires: git, make, automake, gcc, gcc-c++, upx, curl, sqlite, sqlite-devel, glibc, glibc-static, glibc-headers, glibc-devel

%description
A blocking DNS proxy/cache with host/subnet level rules and other features for managing home or small office DNS.

%global debug_package %{nil}

%prep
# rpkg macro for setup
%setup -q -n gudgeon-0.0.git.244.c79e0e5
# install go if not provided
if ! which go; then
    LOCALARCH=$(uname -m | sed 's/x86_64/amd64/' | sed 's/i686/386/' | sed 's/686/386/' | sed 's/i386/386/')
    REMARCHIVE=go1.11.5.linux-$LOCALARCH.tar.gz
    if [[ ! -f /tmp/$REMARCHIVE ]]; then
        curl https://dl.google.com/go/$REMARCHIVE -L -o /tmp/$REMARCHIVE
    fi
    rm -rf /tmp/golang
    mkdir -p /tmp/golang
    tar xf /tmp/$REMARCHIVE -C /tmp/golang
    chmod +x /tmp/golang/go/bin/go
fi

%build
export PATH="/tmp/golang/go/bin:$PATH"
go version
export VERSION=0.3.14
export NUMBER=0.3.14
export GITHASH=c79e0e5
export GOOS_LIST=linux
export GARCH_LIST=$(uname -m)
make prepare
make download
make build
make compress

%install
%make_install

%files
%license LICENSE
/bin/gudgeon
%config(noreplace) /etc/gudgeon/gudgeon.yml
%config(noreplace) /etc/gudgeon
/var/lib/gudgeon
%config(noreplace) /lib/systemd/system/gudgeon.socket
%config(noreplace) /lib/systemd/system/gudgeon.service

%pre
USER_EXISTS=$(id -u gudgeon)
if [[ "0" != "$?" ]]; then
    useradd gudgeon -s /sbin/nologin || true
fi

%post
# change ownership of directories
chown -R :gudgeon /etc/gudgeon
chown -R gudgeon:gudgeon /var/lib/gudgeon

# mod gudgeon user for files created/owned by install
usermod gudgeon -d /var/lib/gudgeon || true

# reload daemon files
systemctl daemon-reload

# restart service only if it is already running to pick up the new version
IS_RUNNING=$(systemctl is-active gudgeon)
if [[ "active" == "${IS_RUNNING}" ]]; then
    systemctl restart gudgeon
fi