mkgitrel.setprev: factor out helper scripts, extend it
[releases.git] / mkgitrel.setprev
diff --git a/mkgitrel.setprev b/mkgitrel.setprev
new file mode 100644 (file)
index 0000000..7c9eb62
--- /dev/null
@@ -0,0 +1,70 @@
+#! /bin/bash
+
+# Copyright 2021-2022 Alexandre Oliva <lxoliva@fsfla.org>
+#
+# This program is part of GNU Linux-libre, a GNU project that
+# publishes scripts to clean up Linux so as to make it suitable for
+# use in the GNU Project and in Free System Distributions.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+# USA
+
+if test -d $reldir/$rel; then
+  echo $reldir/$rel exists >&2
+  continue
+fi
+
+rup=$(echo "$rel" | sed 's,-gnu[0-9]*$,,')
+cbr=$(echo "$rup" | sed 's,\(\.\|-rc\)[0-9]*$,,')
+
+if test -h $reldir/LATEST-$cbr.0; then
+  prev0=$(readlink $reldir/LATEST-$cbr.0)
+  prevn=$(readlink $reldir/LATEST-$cbr.N)
+elif test -h $reldir/LATEST-$rup.N; then
+  cbr=$rup
+  prev0=$(readlink $reldir/LATEST-$cbr.0)
+  prevn=$(readlink $reldir/LATEST-$cbr.N)
+else
+  case $rup in
+  [3-9].0-rc* | [1-9][0-9].0-rc*)
+    prevbase=$(echo "$cbr" | sed 's,\.[0-9]*$,,')
+    if test -h $reldir/LATEST-$prevbase.N; then
+      prev0=$(readlink $reldir/LATEST-$prevbase.N)
+      prevn=$prev0
+    elif pbase=$(expr "$prevbase" - 1) && test -h $reldir/LATEST-$pbase.N; then
+      prevbase=$pbase
+      prev0=$(readlink $reldir/LATEST-$prevbase.N)
+      prevn=$prev0
+    else
+      echo neither $reldir/LATEST-$prevbase.N nor $reldir/LATEST-$pbase.N exist>&2
+      exit 1
+    fi
+    ;;
+  *-rc*)
+    prevbase=$(echo "$cbr" | sed 's,\.[0-9]*$,,')
+    if test -h $reldir/LATEST-$prevbase.N; then
+      prev0=$(readlink $reldir/LATEST-$prevbase.N)
+      prevn=$prev0
+    else
+      echo $reldir/LATEST-$prevbase.N does not exit >&2
+      exit 1
+    fi
+    ;;
+  *)
+    echo Neither $reldir/LATEST-$cbr.0 nor $reldir/LATEST-$rup.N exist >&2
+    exit 1
+    ;;
+  esac
+fi