##
##  urpmi.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 version
%define       V_urpmi               5.6
%define       V_urpmi_rpm           5.6-1mdv2008.1
%define       V_urpm                3.10
%define       V_urpm_rpm            3.10-1mdv2008.1
%define       V_mdv_distribconf     4.00
%define       V_mdv_distribconf_rpm 4.00-1mdv2008.1
%define       V_mdv_packdrakeng     1.13
%define       V_mdv_packdrakeng_rpm 1.13-5mdv2010.1
%define       V_rpmtools            5.3.6
%define       V_rpmtools_rpm        5.3.6-1mdv2008.1

#   package information
Name:         urpmi
Summary:      RPM Installer
URL:          http://www.urpmi.org/
Vendor:       Mandrakesoft
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Bootstrapping
License:      GPL
Version:      %{V_urpmi}
Release:      20100111

#   package options
%option       with_wget      no
%option       with_ssh       no
%option       with_rsync     no

#   list of sources
Source0:      ftp://ftp.free.fr/pub/Distributions_Linux/MandrivaLinux/devel/cooker/SRPMS/main/release/urpmi-%{V_urpmi_rpm}.src.rpm
Source1:      ftp://ftp.free.fr/pub/Distributions_Linux/MandrivaLinux/devel/cooker/SRPMS/main/release/perl-URPM-%{V_urpm_rpm}.src.rpm
Source2:      ftp://ftp.free.fr/pub/Distributions_Linux/MandrivaLinux/devel/cooker/SRPMS/main/release/perl-MDV-Distribconf-%{V_mdv_distribconf_rpm}.src.rpm
Source3:      ftp://ftp.free.fr/pub/Distributions_Linux/MandrivaLinux/devel/cooker/SRPMS/main/release/perl-MDV-Packdrakeng-%{V_mdv_packdrakeng_rpm}.src.rpm
Source4:      ftp://ftp.free.fr/pub/Distributions_Linux/MandrivaLinux/devel/cooker/SRPMS/main/release/rpmtools-%{V_rpmtools_rpm}.src.rpm
Patch0:       urpmi.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, perl, perl-openpkg, cpio
PreReq:       OpenPKG, openpkg >= 20160101, perl, coreutils, openssl
PreReq:       perl-parse, perl-crypto, perl-locale
%if "%{with_wget}" == "yes"
PreReq:       wget
%endif
%if "%{with_ssh}" == "yes"
PreReq:       openssh
%endif
%if "%{with_rsync}" == "yes"
PreReq:       rsync
%endif

%description
    URPMI is a frontend for the RedHat Package Manager (RPM).
    It was originally developed for Mandriva Linux.

%track
    prog urpmi:urpmi = {
        version   = %{V_urpmi_rpm}
        url       = ftp://ftp.free.fr/pub/Distributions_Linux/MandrivaLinux/devel/cooker/SRPMS/main/release/
        regex     = urpmi-(__VER__-\d+mdv[\d.]+)\.src\.rpm
    }
    prog urpmi:URPM = {
        version   = %{V_urpm_rpm}
        url       = ftp://ftp.free.fr/pub/Distributions_Linux/MandrivaLinux/devel/cooker/SRPMS/main/release/
        regex     = perl-URPM-(__VER__-\d+mdv[\d.]+)\.src\.rpm
    }
    prog urpmi:MDV-Packdrakeng = {
        version   = %{V_mdv_packdrakeng_rpm}
        url       = ftp://ftp.free.fr/pub/Distributions_Linux/MandrivaLinux/devel/cooker/SRPMS/main/release/
        regex     = perl-MDV-Packdrakeng-(__VER__-\d+mdv[\d.]+)\.src\.rpm
    }
    prog urpmi:MDV-Distribconf = {
        version   = %{V_mdv_distribconf_rpm}
        url       = ftp://ftp.free.fr/pub/Distributions_Linux/MandrivaLinux/devel/cooker/SRPMS/main/release/
        regex     = perl-MDV-Distribconf-(__VER__-\d+mdv[\d.]+)\.src\.rpm
    }
    prog urpmi:rpmtools = {
        version   = %{V_rpmtools_rpm}
        url       = ftp://ftp.free.fr/pub/Distributions_Linux/MandrivaLinux/devel/cooker/SRPMS/main/release/
        regex     = rpmtools-(__VER__-\d+mdv[\d.]+)\.src\.rpm
    }

%prep
    %setup -q -T -c
    %{l_rpm2cpio} %{SOURCE urpmi-%{V_urpmi_rpm}.src.rpm}                          | cpio -idvmu
    %{l_rpm2cpio} %{SOURCE perl-URPM-%{V_urpm_rpm}.src.rpm}                       | cpio -idvmu
    %{l_rpm2cpio} %{SOURCE perl-MDV-Distribconf-%{V_mdv_distribconf_rpm}.src.rpm} | cpio -idvmu
    %{l_rpm2cpio} %{SOURCE perl-MDV-Packdrakeng-%{V_mdv_packdrakeng_rpm}.src.rpm} | cpio -idvmu
    %{l_rpm2cpio} %{SOURCE rpmtools-%{V_rpmtools_rpm}.src.rpm}                    | cpio -idvmu
    %{l_bzip2} -d -c       urpmi-%{V_urpmi}.tar.bz2                               | %{l_tar} xf -
    %{l_bzip2} -d -c       URPM-%{V_urpm}.tar.bz2                                 | %{l_tar} xf -
    %{l_gzip}  -d -c       MDV-Packdrakeng-%{V_mdv_packdrakeng}.tar.gz            | %{l_tar} xf -
    %{l_gzip}  -d -c       MDV-Distribconf-%{V_mdv_distribconf}.tar.gz            | %{l_tar} xf -
    %{l_bzip2} -d -c       rpmtools-%{V_rpmtools}.tar.bz2                         | %{l_tar} xf -
    %patch -p0

%build

%install

    #   adjust URPMI sources
    ( cd urpmi-%{V_urpmi}
      #   adjust path to configuration files
      %{l_shtool} subst \
          -e "s;/etc/urpmi;%{l_prefix}/etc/urpmi;" \
          urpmq urpm/cfg.pm urpm.pm

      #   adjust cache and hdlist paths
      %{l_shtool} subst \
          -e "s;/var/lib/urpmi;%{l_prefix}/var/urpmi/lib;" \
          -e "s;/var/cache/urpmi;%{l_prefix}/var/urpmi/cache;" \
          -e "s;/var/lib/rpm;%{l_prefix}/RPM/DB;" \
          urpm.pm urpmi

      #   fix superuser message (l_muid)
      %{l_shtool} subst \
          -e "s;$< != 0;$< != %{l_muid};" \
          -e 's;Only superuser is allowed;Only %{l_musr} is allowed;' \
          urpmi.addmedia urpmi.update urpmi.removemedia rurpm* urpmi urpmq
      %{l_shtool} subst \
          -e "s;$<;($< != %{l_muid});" \
          -e 's;Only superuser is allowed;Only %{l_musr} is allowed;' \
          urpme urpmi.recover urpm.pm
      %{l_shtool} subst \
          -e "s;chown(0, 0,;chown(%{l_muid}, %{l_mgid},;" \
          urpm/util.pm

      #   fix common tool paths (coreutils)
      %{l_shtool} subst \
          -e "s;/bin/cp;%{l_prefix}/bin/gcp;" \
          -e "s;/bin/mv;%{l_prefix}/bin/gmv;" \
          -e "s;/usr/bin/md5sum;%{l_prefix}/bin/urpmi.md5sum;" \
          urpm/*.pm
      %{l_shtool} subst \
          -e "s;/usr/bin/curl;%{l_prefix}/lib/openpkg/curl;" \
%if "%{with_wget}" == "yes"
          -e "s;/usr/bin/wget;%{l_prefix}/bin/wget;" \
%endif
%if "%{with_rsync}" == "yes"
          -e "s;/usr/bin/rsync;%{l_prefix}/bin/rsync;" \
%endif
%if "%{with_ssh}" == "yes"
          -e "s;/usr/bin/ssh;%{l_prefix}/bin/ssh;" \
%endif
          urpm/download.pm
    ) || exit $?
    ( cd rpmtools-%{V_rpmtools}
      %{l_shtool} subst \
          -e "s;/bin/cp;%{l_prefix}/bin/gcp;" \
          -e "s;/bin/mv;%{l_prefix}/bin/gmv;" \
          -e "s;/usr/bin/md5sum;%{l_prefix}/bin/urpmi.md5sum;" \
          -e "s;/bin/gzip;%{l_prefix}/lib/openpkg/gzip;" \
          genhdlist gendistrib rpm2cpio.pl
    ) || exit $?

    #   install addon Perl modules
    %{l_prefix}/bin/perl-openpkg prepare
    %{l_prefix}/bin/perl-openpkg -d MDV-Distribconf-%{V_mdv_distribconf} configure build install
    %{l_prefix}/bin/perl-openpkg -d MDV-Packdrakeng-%{V_mdv_packdrakeng} configure build install
    %{l_prefix}/bin/perl-openpkg -d URPM-%{V_urpm} configure build install
    %{l_prefix}/bin/perl-openpkg -d urpmi-%{V_urpmi} configure build install
    %{l_prefix}/bin/perl-openpkg -d rpmtools-%{V_rpmtools} configure build install
    %{l_prefix}/bin/perl-openpkg -F perl-openpkg-files fixate cleanup

    #   install helper utility
    ( echo "#!/bin/sh"
      echo "%{l_prefix}/bin/openssl md5 \"\$@\" | sed -e 's;^MD5(\\(.*\\))= *\\(.*\\);\\2  \\1;'"
    ) >$RPM_BUILD_ROOT%{l_prefix}/bin/urpmi.md5sum
    chmod a+x $RPM_BUILD_ROOT%{l_prefix}/bin/urpmi.md5sum

    #   post-adjust installation
    mv $RPM_BUILD_ROOT%{l_prefix}/bin/packdrake \
       $RPM_BUILD_ROOT%{l_prefix}/bin/urpmi.packdrake
    mv $RPM_BUILD_ROOT%{l_prefix}/bin/dumpdistribconf \
       $RPM_BUILD_ROOT%{l_prefix}/bin/urpmi.dumpdistribconf
    mv $RPM_BUILD_ROOT%{l_prefix}/bin/checkdistrib \
       $RPM_BUILD_ROOT%{l_prefix}/bin/urpmi.checkdistrib
    mv $RPM_BUILD_ROOT%{l_prefix}/bin/parsehdlist \
       $RPM_BUILD_ROOT%{l_prefix}/bin/urpmi.parsehdlist
    mv $RPM_BUILD_ROOT%{l_prefix}/bin/gendistrib \
       $RPM_BUILD_ROOT%{l_prefix}/bin/urpmi.gendistrib
    mv $RPM_BUILD_ROOT%{l_prefix}/bin/genhdlist \
       $RPM_BUILD_ROOT%{l_prefix}/bin/urpmi.genhdlist
    mv $RPM_BUILD_ROOT%{l_prefix}/bin/rpm-find-leaves \
       $RPM_BUILD_ROOT%{l_prefix}/bin/urpmi.rpmfindleaves
    mv $RPM_BUILD_ROOT%{l_prefix}/bin/rpm2cpio.pl \
       $RPM_BUILD_ROOT%{l_prefix}/bin/urpmi.rpm2cpio
    mv $RPM_BUILD_ROOT%{l_prefix}/bin/rpm2header \
       $RPM_BUILD_ROOT%{l_prefix}/bin/urpmi.rpm2header

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} `cat perl-openpkg-files` \
        '%config %{l_prefix}/etc/urpmi/inst.list' \
        '%config %{l_prefix}/etc/urpmi/skip.list'

%files -f files

%clean

%post
    #   update media information
    $RPM_INSTALL_PREFIX/bin/perl -Murpm::media -e '
        if (-e "$ARGV[0]/etc/urpmi/urpmi.cfg") {
            $urpm = new urpm;
            urpm::media::read_config($urpm);
            urpm::media::update_media($urpm, nolock => 1, nopubkey => 1);
        }
    ' $RPM_INSTALL_PREFIX
    exit 0

%preun
    #   remove database files on deinstallation
    if [ ".$1" = .0 ]; then
        ( cd $RPM_INSTALL_PREFIX/var/urpmi/lib || exit $?
          rm -f compss provides depslist* descriptions.* *.cache hdlist.* synthesis.hdlist.* list.* >/dev/null 2>&1 || true
        ) || exit $?
        ( cd $RPM_INSTALL_PREFIX/var/urpmi/cache || exit $?
          rm -rf partial/* headers/* rpms/* >/dev/null 2>&1 || true
        ) || exit $?
    fi
    exit 0

