#!/bin/sh
# Startdate: 2024-04-14-1 14:01
# Purpose: make it easier for preseed to get a working start-stop-daemon which is diverted/deleted incorrectly
# Dependencies: already have wget, running as root
# Project: preseed
set -x

# short-circuit
test -e "/usr/sbin/start-stop-daemon" && exit 0
test -z "${MIRROR}" && MIRROR=http://server3
echo "${MIRROR}" | grep -qE "https?:\/\/" || MIRROR="http://${MIRROR}"
thisarch="$( dpkg --print-architecture )"
test -z "${this_arch}" && thisarch=amd64
fn="$( curl -L --silent "${MIRROR}/mirror/devuan/pool/DEBIAN/main/d/dpkg/" | awk -F'"' "/dpkg_[^ ]+_${thisarch}.deb/{print \$8}" | tail -n 1 )"
test -z "${fn}" && {
   echo "Fatal! Failed to find latest dpkg.deb from ${MIRROR} for arch ${thisarch}. Aborted."
   exit 1
}
echo "Using ${fn}"
wget "${MIRROR}/mirror/devuan/pool/DEBIAN/main/d/dpkg/${fn}"
mkdir -p ~/dpkg1
dpkg-deb --extract "${fn}" ~/dpkg1
cp -pf ~/dpkg1/usr/sbin/start-stop-daemon /usr/sbin/start-stop-daemon1 1>/dev/null 2>&1 || :
cp -pf ~/dpkg1/usr/sbin/start-stop-daemon /usr/sbin/ && rm -rf ~/dpkg1 "${fn:-NOTHINGTODEL%%.deb}.deb"
