##
##  lsh.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package information
Name:         lsh
Summary:      GNU Secure Shell (SSH) Implementation
URL:          http://www.lysator.liu.se/~nisse/lsh/
Vendor:       Niels Möller
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        SSH
License:      GPL
Version:      2.1
Release:      20140209

#   package options
%option       with_fsl  yes

#   list of sources
Source0:      http://ftp.gnu.org/gnu/lsh/lsh-%{version}.tar.gz
Source1:      rc.lsh
Source2:      fsl.lsh
Source3:      lsh.cfg

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, gcc, m4
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  gmp, liboop, zlib, readline
PreReq:       gmp, liboop, zlib, readline
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif

%description
    LSH is GNU's free implementation of the SSH version 2 protocol,
    currently being standardised by the IETF SECSH working group.

%track
    prog lsh = {
        version   = %{version}
        url       = http://ftp.gnu.org/gnu/lsh/
        regex     = lsh-(__VER__)\.tar\.gz
    }

%prep
    %setup -q

%build
    #   configure program
    %{l_shtool} subst \
        -e 's;/var/run;%{l_prefix}/var/lsh;g' \
        -e 's;/var/spool/lsh;%{l_prefix}/var/lsh;g' \
        -e 's;/var/spool/lsh;%{l_prefix}/var/lsh;g' \
        src/daemon.c src/environ.h src/lsh-make-seed.c src/unix_random.c
    %{l_shtool} subst \
        -e '/^DOCTARGETS/s;nettle.info;;' \
        -e '/^install-here/s;install-info;;' \
        -e '/^des\.\$(OBJEXT):/s;des\.h;;' \
        -e 's;\(INSTALL_DATA = @INSTALL_DATA@\);\1 -c;' \
        src/nettle/Makefile.in
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags} %{l_fsl_ldflags}" \
    LIBS="%{l_fsl_libs}" \
    INSTALL="%{l_shtool} install" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --infodir=%{l_prefix}/info \
        --sysconfdir=%{l_prefix}/etc/lsh \
        --enable-tcp-forward \
        --enable-x11-forward \
        --enable-agent-forward \
        --enable-utmp \
        --enable-srp \
        --disable-gss \
        --disable-kerberos \
        --disable-pam \
        --disable-ipv6 \
        --disable-openssl \
        --disable-dependency-tracking

    #   build program
    %{l_make} %{l_mflags -O}

%install

    #   install program
    %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/bin/nettle*
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/include
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/info/dir
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/info/nettle.info

    #   resolve conflicts with other packages
    mv $RPM_BUILD_ROOT%{l_prefix}/sbin/sftp-server \
       $RPM_BUILD_ROOT%{l_prefix}/sbin/lsh-sftp-server
    mv $RPM_BUILD_ROOT%{l_prefix}/man/man8/sftp-server.8 \
       $RPM_BUILD_ROOT%{l_prefix}/man/man8/lsh-sftp-server.8

    #   install server configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/lsh
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE lsh.cfg} $RPM_BUILD_ROOT%{l_prefix}/etc/lsh/

    #   install run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.lsh} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install OSSP fsl configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE fsl.lsh} $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

    #   create data directory
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/lsh

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/fsl/fsl.lsh' \
        '%config %{l_prefix}/etc/lsh/*'

%files -f files

%clean

%post
    #   generate server key
    if [ ! -f "$RPM_INSTALL_PREFIX/etc/lsh/lsh.key" -o \
         ! -s "$RPM_INSTALL_PREFIX/etc/lsh/lsh.key" ]; then
        echo "Generating LSH RSA host key:"
        $RPM_INSTALL_PREFIX/bin/lsh-make-seed --server --sloppy --quiet || true
        $RPM_INSTALL_PREFIX/bin/lsh-keygen    --server | \
        $RPM_INSTALL_PREFIX/bin/lsh-writekey  --server \
            -o $RPM_INSTALL_PREFIX/etc/lsh/lsh.key
    fi

    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} lsh status 2>/dev/null`
    [ ".$lsh_active" = .yes ] && %{l_rc} lsh restart
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} lsh stop 2>/dev/null
    rm -f $RPM_INSTALL_PREFIX/var/lsh/*
    exit 0

