#!/bin/sh
#
# multilib.sh
# 2024-05-12
# by Gernot WALZL
#
# Creates a true multilib Slackware64 as explained there:
# http://alien.slackbook.org/dokuwiki/doku.php?id=slackware:multilib

SLACK32DIR=${SLACK32DIR:-"/mnt/cdrom/slackware/"}

REPO="http://www.slackware.com/~alien/multilib/"
#REPO="http://taper.alienbase.nl/mirrors/people/alien/multilib/"
GPG_KEY_ID="0xA75CBDA0"

set -e

SLACKVER=$(awk '{print $2}' /etc/slackware-version)

if [ ! -d "$SLACK32DIR" ]; then
  echo "ERROR: SLACK32DIR=$SLACK32DIR is not valid."
  echo "Please insert and mount a Slackware(32) DVD or mount an ISO file:"
  echo "mount -o loop slackware-${SLACKVER}-install-dvd.iso /mnt/cdrom"
  exit 1
fi

mkdir -p multilib
cd multilib
wget -O "CHECKSUMS.md5" "${REPO}${SLACKVER}/CHECKSUMS.md5"
PKGS=$(grep "\./.*\.t.z$" CHECKSUMS.md5 | grep -v "\./.*/.*" | awk '{print $2}' | sed 's/\.\///')

for PKG in $PKGS; do
  if [ ! -f "$PKG" ]; then
    wget -O "${PKG}" "${REPO}${SLACKVER}/${PKG}"
    wget -O "${PKG}.asc" "${REPO}${SLACKVER}/${PKG}.asc"
  fi
done

if ! gpg --list-keys "$GPG_KEY_ID" ; then
  wget -O GPG-KEY "${REPO}${SLACKVER}/GPG-KEY"
  gpg --import GPG-KEY
fi

for PKG in $PKGS; do
  gpg --verify "${PKG}.asc" || exit 1
done

if [ "$SLACKVER" = "14.0" -o "$SLACKVER" = "14.1" -o "$SLACKVER" = "14.2" ]; then
  slackpkg blacklist gcc glibc
else
  if ! grep '^aaa_glibc-solibs' /etc/slackpkg/blacklist ; then
    echo 'aaa_glibc-solibs' >> /etc/slackpkg/blacklist
  fi
  if ! grep '^gcc' /etc/slackpkg/blacklist ; then
    echo 'gcc.*' >> /etc/slackpkg/blacklist
  fi
  if ! grep '^glibc' /etc/slackpkg/blacklist ; then
    echo 'glibc.*' >> /etc/slackpkg/blacklist
  fi
fi

for PKG in $PKGS; do
  upgradepkg --install-new "$PKG"
done

massconvert32.sh -i "$SLACK32DIR"
upgradepkg --install-new ./*-compat32/*.t?z