##
##  rpm-4.0.2.patch.feature -- Annotated patch file
##  Copyright (c) 2000-2001 Cable & Wireless Deutschland GmbH
##  Copyright (c) 2000-2001 Ralf S. Engelschall <rse.com>
##
##  This file assembles changes to existing RPM source files between
##  the original RedHat RPM and the OpenPKG RPM variant. It can be
##  automatically applied to a vanilla RedHat RPM source tree with the
##  'patch' tool to upgrade those files. Each patch snippet is annotated
##  with a short description.
##
##  Created on: 23-Sep-2001
##

+---------------------------------------------------------------------------
| In OpenPKG, the RPM package contains own local versions of the
| "patch" and "tar" tools, so we cannot accept hard-coded names here.
| Instead we expand a variable to allow us to direct RPM to our tools.
| Also we allow %setup and %patch macros whitespace-indented in %prep,
| because both in OpenPKG we both have all scripts indented and this
| way it is more flexible and clean.
| Additionally add support for splitted source directories, i.e.,
| source files alternatively can be placed into the spec directory and
| are picked up there, too.
+---------------------------------------------------------------------------
Index: build/parsePrep.c
--- build/parsePrep.c	2001/01/15 23:10:04	1.1.1.7
+++ build/parsePrep.c	2001/09/21 12:24:12	1.4
@@ -65,6 +65,7 @@
     struct Source *sp;
     rpmCompressedMagic compressed = COMPRESSED_NOT;
     int urltype;
+    const char *patcher;
 
     for (sp = spec->sources; sp != NULL; sp = sp->next) {
 	if ((sp->flags & RPMBUILD_ISPATCH) && (sp->num == c)) {
@@ -76,7 +77,15 @@
 	return NULL;
     }
 
+#ifndef OPENPKG
     fn = urlfn = rpmGetPath("%{_sourcedir}/", sp->source, NULL);
+#else
+    fn = urlfn = rpmGetPath("%{_specdir}/", sp->source, NULL);
+    if (access(fn, F_OK) == -1) {
+        free(fn);
+        fn = urlfn = rpmGetPath("%{_sourcedir}/", sp->source, NULL);
+    }
+#endif
 
     args[0] = '\0';
     if (db) {
@@ -112,6 +121,9 @@
 	/*@notreached@*/ break;
     }
 
+    patcher = rpmGetPath("%{_patchbin}", NULL);
+    if (strcmp(patcher, "%{_patchbin}") == 0)
+        patcher = "patch";
     if (compressed) {
 	const char *zipper = rpmGetPath(
 	    (compressed == COMPRESSED_BZIP2 ? "%{_bzip2bin}" : "%{_gzipbin}"),
@@ -119,20 +131,20 @@
 
 	sprintf(buf,
 		"echo \"Patch #%d (%s):\"\n"
-		"%s -d < %s | patch -p%d %s -s\n"
+ 		"%s -d < %s | %s -p%d %s -s\n"
 		"STATUS=$?\n"
 		"if [ $STATUS -ne 0 ]; then\n"
 		"  exit $STATUS\n"
 		"fi",
 		c, (const char *) basename(fn),
 		zipper,
-		fn, strip, args);
+ 		fn, patcher, strip, args);
 	free((void *)zipper);
     } else {
 	sprintf(buf,
 		"echo \"Patch #%d (%s):\"\n"
-		"patch -p%d %s -s < %s", c, (const char *) basename(fn),
-		strip, args, fn);
+ 		"%s -p%d %s -s < %s", c, (const char *) basename(fn),
+ 		patcher, strip, args, fn);
     }
 
     free((void *)urlfn);
@@ -155,6 +167,7 @@
     struct Source *sp;
     rpmCompressedMagic compressed = COMPRESSED_NOT;
     int urltype;
+    const char *tar;
 
     for (sp = spec->sources; sp != NULL; sp = sp->next) {
 	if ((sp->flags & RPMBUILD_ISSOURCE) && (sp->num == c)) {
@@ -166,7 +179,19 @@
 	return NULL;
     }
 
+#ifndef OPENPKG
     fn = urlfn = rpmGetPath("%{_sourcedir}/", sp->source, NULL);
+#else
+    fn = urlfn = rpmGetPath("%{_specdir}/", sp->source, NULL);
+    if (access(fn, F_OK) == -1) {
+        free(fn);
+        fn = urlfn = rpmGetPath("%{_sourcedir}/", sp->source, NULL);
+    }
+#endif
+
+    tar = rpmGetPath("%{_tarbin}", NULL);
+    if (strcmp(tar, "%{_tarbin}") == 0)
+        tar = "tar";
 
     taropts = ((rpmIsVerbose() && !quietly) ? "-xvvf" : "-xf");
 
@@ -232,8 +257,13 @@
 	free((void *)zipper);
 	*t++ = ' ';
 	t = stpcpy(t, fn);
-	if (needtar)
-	    t = stpcpy( stpcpy( stpcpy(t, " | tar "), taropts), " -");
+	if (needtar) {
+	    t = stpcpy(t, " | ");
+	    t = stpcpy(t, tar);
+	    t = stpcpy(t, " ");
+	    t = stpcpy(t, taropts);
+	    t = stpcpy(t, " -");
+        }
 	t = stpcpy(t,
 		"\n"
 		"STATUS=$?\n"
@@ -242,7 +272,7 @@
 		"fi");
     } else {
 	buf[0] = '\0';
-	t = stpcpy( stpcpy(buf, "tar "), taropts);
+	t = stpcpy( stpcpy(stpcpy(buf, tar), " "), taropts);
 	*t++ = ' ';
 	t = stpcpy(t, fn);
     }
@@ -549,11 +579,14 @@
 
     saveLines = splitString(getStringBuf(buf), strlen(getStringBuf(buf)), '\n');
     for (lines = saveLines; *lines; lines++) {
+	char *cp;
+	for (cp = *lines; *cp == ' ' || *cp == '\t'; cp++)
+	    ;
 	res = 0;
-	if (! strncmp(*lines, "%setup", sizeof("%setup")-1)) {
-	    res = doSetupMacro(spec, *lines);
-	} else if (! strncmp(*lines, "%patch", sizeof("%patch")-1)) {
-	    res = doPatchMacro(spec, *lines);
+	if (! strncmp(cp, "%setup", sizeof("%setup")-1)) {
+	    res = doSetupMacro(spec, cp);
+	} else if (! strncmp(cp, "%patch", sizeof("%patch")-1)) {
+	    res = doPatchMacro(spec, cp);
 	} else {
 	    appendLineStringBuf(spec->prep, *lines);
 	}

+---------------------------------------------------------------------------
| Not everything on a system is RPM based (for instance OpenPKG is
| just an add-on to the system), so do not assume we can just require
| a package to provide "/bin/sh".
+---------------------------------------------------------------------------
Index: build/parseScript.c
--- build/parseScript.c	2001/01/15 23:10:04	1.1.1.9
+++ build/parseScript.c	2001/06/25 19:31:13	1.2
@@ -246,6 +246,7 @@
     stripTrailingBlanksStringBuf(sb);
     p = getStringBuf(sb);
 
+    if (progArgv[0] != NULL && strcmp(progArgv[0], "/bin/sh") != 0)
     addReqProv(spec, pkg->header, (tagflags | RPMSENSE_INTERP), progArgv[0], NULL, 0);
 
     /* Trigger script insertion is always delayed in order to */

+---------------------------------------------------------------------------
| RPM has two platform id canonicalizations: hard-coded ones in the
| "rpm" program and defined ones in the rpmrc files. The hard-coded
| ones make the defined ones inconsistent. Additionally in OpenPKG we
| know exactly what we do. So disable the hard-coded canonicalizations
| and use only the defined ones. The only thing we do inside "rpm" is
| to reduce the platform version to major and minor version numbers in
| order to simplify the "rpmrc" files.
+---------------------------------------------------------------------------
Index: lib/rpmrc.c
--- lib/rpmrc.c	2001/03/13 12:55:25	1.1.1.20
+++ lib/rpmrc.c	2001/09/23 08:15:32	1.3
@@ -931,6 +931,26 @@
     if (!gotDefaults) {
 	uname(&un);
 
+#ifdef OPENPKG
+        {
+            char *cp;
+            char *cpR;
+            int n;
+            cpR = un.release;
+            if ((n = strcspn(cpR, "0123456789")) > 0)
+                cpR += n;
+            if ((n = strspn(cpR, "0123456789.")) > 0) {
+                /* terminate after "N.N.N...." prefix */
+                cpR[n] = '\0';
+                /* shorten to "N.N" if longer */
+                if ((cp = strchr(cpR, '.')) != NULL) {
+                    if ((cp = strchr(cp+1, '.')) != NULL)
+                        *cp = '\0';
+                }
+                strcat(un.sysname, cpR);
+            }
+        }
+#else /* OPENPKG */
 #if !defined(__linux__)
 #ifdef SNI
 	/* USUALLY un.sysname on sinix does start with the word "SINIX"
@@ -1114,6 +1134,7 @@
 		un.machine[1] = class;
 	}
 #	endif
+#endif /* OPENPKG */
 
 	/* the uname() result goes through the arch_canon table */
 	canon = lookupInCanonTable(un.machine,

+---------------------------------------------------------------------------
| Add support for splitted source directories, i.e., source files
| alternatively can be placed into the spec directory and are picked
| up there, too.
+---------------------------------------------------------------------------
Index: build/build.c
--- build/build.c	2001/01/15 23:10:04	1.1.1.16
+++ build/build.c	2001/09/17 11:09:23	1.2
@@ -25,7 +25,15 @@
 
     for (p = spec->sources; p != NULL; p = p->next) {
 	if (! (p->flags & RPMBUILD_ISNO)) {
+#ifndef OPENPKG
 	    const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
+#else
+	    const char *fn = rpmGetPath("%{_specdir}/", p->source, NULL);
+            if (access(fn, F_OK) == -1) {
+                free(fn);
+	        fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
+            }
+#endif
 	    unlink(fn);
 	    free((void *)fn);
 	}
@@ -34,7 +42,15 @@
     for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
 	for (p = pkg->icon; p != NULL; p = p->next) {
 	    if (! (p->flags & RPMBUILD_ISNO)) {
+#ifndef OPENPKG
 		const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
+#else
+                const char *fn = rpmGetPath("%{_specdir}/", p->source, NULL);
+                if (access(fn, F_OK) == -1) {
+                    free(fn);
+                    fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
+                }
+#endif
 		unlink(fn);
 		free((void *)fn);
 	    }

+---------------------------------------------------------------------------
| Add support for splitted source directories, i.e., source files
| alternatively can be placed into the spec directory and are picked
| up there, too. Also add support for Provides headers in source RPMs.
+---------------------------------------------------------------------------
Index: build/files.c
--- build/files.c	2001/01/19 01:47:25	1.1.1.15
+++ build/files.c	2001/09/17 11:09:23	1.2
@@ -1591,6 +1591,9 @@
 	case RPMTAG_CHANGELOGTEXT:
 	case RPMTAG_URL:
 	case HEADER_I18NTABLE:
+ 	case RPMTAG_PROVIDENAME:
+ 	case RPMTAG_PROVIDEVERSION:
+ 	case RPMTAG_PROVIDEFLAGS:
 	    headerAddEntry(spec->sourceHeader, tag, type, ptr, count);
 	    break;
 	default:
@@ -1655,8 +1658,20 @@
 	}
 
       {	const char *s;
+#ifndef OPENPKG
 	s = rpmGetPath( ((srcPtr->flags & RPMBUILD_ISNO) ? "!" : ""),
 		"%{_sourcedir}/", srcPtr->source, NULL);
+#else
+        const char *s2;
+	s2 = rpmGetPath("%{_specdir}/", srcPtr->source, NULL);
+        if (access(s2, F_OK) == 0)
+	    s = rpmGetPath( ((srcPtr->flags & RPMBUILD_ISNO) ? "!" : ""),
+		    "%{_specdir}/", srcPtr->source, NULL);
+        else
+	    s = rpmGetPath( ((srcPtr->flags & RPMBUILD_ISNO) ? "!" : ""),
+		    "%{_sourcedir}/", srcPtr->source, NULL);
+        free(s2);
+#endif
 	appendLineStringBuf(sourceFiles, s);
 	free((void *)s);
       }
@@ -1665,8 +1680,20 @@
     for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
 	for (srcPtr = pkg->icon; srcPtr != NULL; srcPtr = srcPtr->next) {
 	    const char *s;
+#ifndef OPENPKG
 	    s = rpmGetPath( ((srcPtr->flags & RPMBUILD_ISNO) ? "!" : ""),
 		"%{_sourcedir}/", srcPtr->source, NULL);
+#else
+            const char *s2;
+            s2 = rpmGetPath("%{_specdir}/", srcPtr->source, NULL);
+            if (access(s2, F_OK) == 0)
+                s = rpmGetPath( ((srcPtr->flags & RPMBUILD_ISNO) ? "!" : ""),
+                        "%{_specdir}/", srcPtr->source, NULL);
+            else
+                s = rpmGetPath( ((srcPtr->flags & RPMBUILD_ISNO) ? "!" : ""),
+                        "%{_sourcedir}/", srcPtr->source, NULL);
+            free(s2);
+#endif
 	    appendLineStringBuf(sourceFiles, s);
 	    free((void *)s);
 	}

+---------------------------------------------------------------------------
| Add support for splitted source directories, i.e., source files
| alternatively can be placed into the spec directory and are picked
| up there, too.
+---------------------------------------------------------------------------
Index: build/parsePreamble.c
--- build/parsePreamble.c	2001/01/15 23:10:04	1.1.1.9
+++ build/parsePreamble.c	2001/09/17 11:09:23	1.2
@@ -317,7 +317,15 @@
     size_t nb, iconsize;
 
     /* XXX use rpmGenPath(rootdir, "%{_sourcedir}/", file) for icon path. */
+#ifndef OPENPKG
     fn = rpmGetPath("%{_sourcedir}/", file, NULL);
+#else
+    fn = rpmGetPath("%{_specdir}/", file, NULL);
+    if (access(fn, F_OK) == -1) {
+        free(fn);
+        fn = rpmGetPath("%{_sourcedir}/", file, NULL);
+    }
+#endif
 
     fd = Fopen(fn, "r.ufdio");
     if (fd == NULL || Ferror(fd)) {

+---------------------------------------------------------------------------
| Add support for splitted source directories, i.e., source files
| alternatively can be placed into the spec directory and are picked
| up there, too.
+---------------------------------------------------------------------------
Index: build/spec.c
--- build/spec.c	2001/01/15 23:10:04	1.1.1.16
+++ build/spec.c	2001/09/17 11:09:23	1.2
@@ -319,7 +319,15 @@
     spec->numSources++;
 
     if (tag != RPMTAG_ICON) {
+#ifndef OPENPKG
 	const char *body = rpmGetPath("%{_sourcedir}/", p->source, NULL);
+#else
+	const char *body = rpmGetPath("%{_specdir}/", p->source, NULL);
+        if (access(body, F_OK) == -1) {
+            free(body);
+	    body = rpmGetPath("%{_sourcedir}/", p->source, NULL);
+        }
+#endif
 
 	sprintf(buf, "%s%d",
 		(flag & RPMBUILD_ISPATCH) ? "PATCH" : "SOURCE", num);
