From f7394acdce3129ead924502771bd42706b35339f Mon Sep 17 00:00:00 2001
From: Vitaly Lipatov <lav@etersoft.ru>
Date: Sun, 14 May 2023 04:06:29 +0300
Subject: [PATCH] epm repack: implement all pack related things via pack code

---
 bin/epm-repack        | 85 +++++++------------------------------------
 bin/epm-repack-rpm    | 14 ++-----
 pack.d/generic-tar.sh | 44 ++++++++++++++++++++++
 repack.d/generic.sh   | 26 +++++++++++++
 4 files changed, 87 insertions(+), 82 deletions(-)
 create mode 100755 pack.d/generic-tar.sh

diff --git a/bin/epm-repack b/bin/epm-repack
index 7b7e222a..17914b89 100644
--- a/bin/epm-repack
+++ b/bin/epm-repack
@@ -63,17 +63,6 @@ __epm_split_by_pkg_type()
 }
 
 
-# $spec $PKGNAME $VERSION
-__set_name_version()
-{
-    SPEC="$1"
-    PKGNAME="$2"
-    VERSION="$3"
-    [ -n "$PKGNAME" ] && subst "s|^Name:.*|Name: $PKGNAME|" $SPEC
-    [ -n "$VERSION" ] && subst "s|^Version:.*|Version: $VERSION|" $SPEC
-}
-
-
 __check_stoplist()
 {
     local pkg="$1"
@@ -84,34 +73,9 @@ __check_stoplist()
 }
 
 
-__set_version_pkgname()
-{
-    local alpkg="$1"
-    VERSION="$(echo "$alpkg" | grep -o -P '[-_.][0-9][0-9]*([.]*[0-9])*' | head -n1 | sed -e 's|^[-_.]||')" #"
-    [ -n "$VERSION" ] && PKGNAME="$(echo "$alpkg" | sed -e "s|[-_.]$VERSION.*||")"
-    # set version as all between name and extension
-    #local woext="$(echo "alpkg" | sed -e 's|\.tar.*||')"
-    #if [ "$woext" != "$alpkg" ] ; then
-    #    VERSION="$(echo "$woext" " | sed -e "s|^$PKGNAME[-_.]||")"
-    #fi
-}
-
-
-# AppImage version
-# hack for ktalk2.4.2 -> ktalk 2.4.2
-__set_version_apppkgname()
-{
-    local alpkg="$1"
-    VERSION="$(echo "$alpkg" | grep -o -P "[-_.a-zA-Z]([0-9])([0-9])*([.]*[0-9])*" | head -n1 | sed -e 's|^[-_.a-zA-Z]||' -e 's|--|-|g' )"  #"
-    [ -n "$VERSION" ] && PKGNAME="$(echo "$alpkg" | sed -e "s|[-_.]$VERSION.*||")"
-}
-
-
 # arg: <package file>
 # sets:
 #   alpkg      - package file name without path
-#   PKGNAME    - package name
-#   VERSION    - version of the package
 #   SUBGENERIC - name of generic file's extension
 __prepare_source_package()
 {
@@ -128,54 +92,33 @@ __prepare_source_package()
     VERSION=''
     SUBGENERIC=''
 
-    # convert tarballs to tar (for alien)
     if rhas "$alpkg" "\.(rpm|deb)$" ; then
+        # skip packing for supported: rpm and deb
         return
     fi
 
+    # convert tarballs to tar (for alien)
+    load_helper epm-pack
+
     if rhas "$alpkg" "\.AppImage$" ; then
-        __set_version_apppkgname $alpkg
-        [ -n "$VERSION" ] || fatal "Can't get version from $alpkg."
+        __epm_pack_run_handler generic-appimage "$pkg"
         SUBGENERIC='appimage'
-        # TODO: move repack archive to erc?
-        [ -x "$alpkg" ] || docmd chmod u+x $verbose "$alpkg"
-        ./$alpkg --appimage-extract || fatal
-        alpkg=$PKGNAME-$VERSION.tar
-        # make a tar for alien
-        erc a $alpkg squashfs-root
-        return
+    elif rhas "$alpkg" "\.snap$" ; then
+        __epm_pack_run_handler generic-snap "$pkg"
+        SUBGENERIC='snap'
+    else
+        __epm_pack_run_handler generic-tar "$pkg"
     fi
 
-    __set_version_pkgname $alpkg
-    if [ -n "$VERSION" ] ; then
-        # TODO: don't use erc for detect type? then we potentially can skip install it
-        pkgtype="$(erc type $alpkg)"
-        local newalpkg
-        newalpkg=$PKGNAME-$VERSION.$pkgtype
-        #[ -n "$PKGNAME" ] || PKGNAME=$(basename $alpkg .$pkgtype)
-        if [ "$pkgtype" = "tar" ] || [ "$pkgtype" = "tar.gz" ] || [ "$pkgtype" = "tgz" ] ; then
-            # just rename supported formats
-            if [ "$alpkg" != "$newalpkg" ] ; then
-                mv $alpkg $newalpkg
-            fi
-        else
-            # converts directly unsupported formats
-            newalpkg=$PKGNAME-$VERSION.tar
-            #newalpkg=$(basename $alpkg .$pkgtype).tar
-            erc repack $alpkg $newalpkg || fatal
-        fi
-        if [ "$alpkg" != "$newalpkg" ] ; then
-            rm -f $verbose $alpkg
-            alpkg=$newalpkg
-        fi
-    else
-        warning "Can't detect version in $alpkg. We have almost no chance it will supported in alien."
+    alpkg=$(basename $returntarname)
+    if [ "$(pwd)" != "$(dirname "$returntarname")" ] ; then
+        cp $verbose $returntarname $alpkg
+        [ -r "$returntarname.eepm.yaml" ] && cp $verbose $returntarname.eepm.yaml $alpkg.eepm.yaml
     fi
 }
 
 
 
-
 # FIXME: Нужно как-то обеспечить непродолжение выполнения.
 # used in epm install
 # fill repacked_pkgs
diff --git a/bin/epm-repack-rpm b/bin/epm-repack-rpm
index 7f1b2259..f22c0704 100644
--- a/bin/epm-repack-rpm
+++ b/bin/epm-repack-rpm
@@ -64,13 +64,6 @@ __fix_spec()
         fi
     done
 
-    # FIXME: where is a source of the bug with empty Summary?
-    subst "s|Summary: *$|Summary: $pkgname (was empty Summary after alien)|" $spec
-    subst "s|^\(Version: .*\)~.*|\1|" $spec
-    subst "s|^Release: |Release: epm1.repacked.|" $spec
-    subst "s|^Distribution:.*||" $spec
-    subst "s|^\((Converted from a\) \(.*\) \(package.*\)|(Repacked from binary \2 package with epm $EPMVERSION)\n\1 \2 \3|" $spec
-    #" hack for highlight
 }
 
 
@@ -158,6 +151,7 @@ __epm_repack_to_rpm()
         cp $verbose $pkg $tmpbuilddir/../$alpkg
 
         cd $tmpbuilddir/../ || fatal
+        # fill alpkg and SUBGENERIC
         __prepare_source_package "$pkg"
         cd $tmpbuilddir/ || fatal
 
@@ -179,14 +173,11 @@ __epm_repack_to_rpm()
         mv $spec $tmpbuilddir || fatal
         spec="$tmpbuilddir/$(basename "$spec")"
 
-        #__set_name_version $spec $PKGNAME $VERSION
         local pkgname="$(grep "^Name: " $spec | sed -e "s|Name: ||g" | head -n1)"
 
-        # for tarballs fix permissions
-        [ -n "$VERSION" ] && chmod $verbose -R a+rX $buildroot/*
-
         # run generic scripts and repack script for the pkg
         cd $buildroot || fatal
+
         __fix_spec $pkgname $buildroot $spec
         __apply_fix_code "generic" $buildroot $spec $pkgname $abspkg
         [ -n "$SUBGENERIC" ] && __apply_fix_code "generic-$SUBGENERIC" $buildroot $spec $pkgname $abspkg
@@ -201,6 +192,7 @@ __epm_repack_to_rpm()
         else
             a='' rpmbuild --buildroot $buildroot --target $TARGETARCH -bb $spec >/dev/null || fatal
         fi
+
         # remove copy of source binary package (don't mix with generated)
         rm -f $tmpbuilddir/../$alpkg
         local repacked_rpm="$(realpath $tmpbuilddir/../*.rpm)"
diff --git a/pack.d/generic-tar.sh b/pack.d/generic-tar.sh
new file mode 100755
index 00000000..2dcb6373
--- /dev/null
+++ b/pack.d/generic-tar.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+TAR="$1"
+RETURNTARNAME="$2"
+#VERSION+"$3"
+
+. $(dirname $0)/common.sh
+
+alpkg=$(basename $TAR)
+
+VERSION="$(echo "$alpkg" | grep -o -P '[-_.][0-9][0-9]*([.]*[0-9])*' | head -n1 | sed -e 's|^[-_.]||')" #"
+[ -n "$VERSION" ] && PRODUCT="$(echo "$alpkg" | sed -e "s|[-_.]$VERSION.*||")" || fatal "Can't get version from $TAR. We have almost no chance it will supported in alien."
+    # set version as all between name and extension
+    #local woext="$(echo "alpkg" | sed -e 's|\.tar.*||')"
+    #if [ "$woext" != "$alpkg" ] ; then
+    #    VERSION="$(echo "$woext" " | sed -e "s|^$PKGNAME[-_.]||")"
+    #fi
+
+pkgtype="$(erc type $alpkg)"
+PKGNAME=$PRODUCT-$VERSION.$pkgtype
+if [ "$pkgtype" = "tar" ] || [ "$pkgtype" = "tar.gz" ] || [ "$pkgtype" = "tgz" ] ; then
+    # just rename supported formats
+    if [ "$alpkg" != "$PKGNAME" ] ; then
+        mv $alpkg $PKGNAME
+    fi
+else
+    # converts directly unsupported formats
+    PKGNAME=$PRODUCT-$VERSION.tar
+    erc repack $alpkg $PKGNAME || fatal
+fi
+
+#if [ "$alpkg" != "$newalpkg" ] ; then
+#   rm -f $verbose $alpkg
+#   alpkg=$newalpkg
+#   fi
+
+# TODO: how to add to tarball?
+cat <<EOF >$PKGNAME.eepm.yaml
+name: $PRODUCT
+version: $VERSION
+upstream_file: $alpkg
+EOF
+
+return_tar $PKGNAME
diff --git a/repack.d/generic.sh b/repack.d/generic.sh
index 4429694c..f67a1d55 100755
--- a/repack.d/generic.sh
+++ b/repack.d/generic.sh
@@ -3,6 +3,7 @@
 BUILDROOT="$1"
 SPEC="$2"
 PRODUCT="$3"
+PKG="$4"
 
 # firstly, pack $PRODUCTDIR if used
 . $(dirname $0)/common.sh
@@ -48,3 +49,28 @@ subst "s|^\(Name: .*\)$|# Override repository package\nEpoch: 100\n\1|g" $SPEC
 subst "1i%global _build_pkgcheck_set %nil" $SPEC
 subst "1i%global _build_pkgcheck_srpm %nil" $SPEC
 
+set_rpm_field()
+{
+    local field="$1"
+    local value="$2"
+    local v="$(grep "^$field:.*" $SPEC | sed -e "s|$field: *||g" | head -n1)"
+    if [ -n "$v" ] ; then
+        [ -n "$value" ] || return
+        subst "s|^$field:.*|$field: $value|" $SPEC
+    else
+        [ -n "$value" ] || value="Stub"
+        subst "1i$field: $value" $SPEC
+    fi
+}
+
+
+# FIXME: where is a source of the bug with empty Summary?
+set_rpm_field "Summary" "$PRODUCT (fixme: was empty Summary after alien)"
+# clean version
+subst "s|^\(Version: .*\)~.*|\1|" $SPEC
+# add our prefix to release
+subst "s|^Release: |Release: epm1.repacked.|" $SPEC
+
+
+    subst "s|^\((Converted from a\) \(.*\) \(package.*\)|(Repacked from binary \2 package with $(epm --short --version))\n\1 \2 \3|" $SPEC
+
-- 
2.24.1