#!/bin/sh
set -e

MIRROR=http://archive.debian.org/debian
DIST=buster

ARCH=`dpkg --print-architecture`
PACKAGES="libx264-1 libx265-1 libaom0 libvpx5 libcodec2-0 libwebp6 libavcodec5 libavformat5 libavutil5 libswresample3 libswscale5"
if [ "$ARCH" = "amd64" ]; then
  PACKAGES="libcrystalhd3 $PACKAGES"
fi
PLIST=""
PLIST2=""

WORK="`mktemp -d`"

echo "will get packages from debian $DIST for $ARCH, using temp path $WORK"
echo -n "hit ctrl+c to cancel, otherwise return"
read a

echo "fetching package list"
wget -q -O "$WORK/Packages.gz" "$MIRROR/dists/$DIST/main/binary-$ARCH/Packages.gz"
gzip -d "$WORK/Packages.gz"

get_package() {
  echo "getting $1"
  filename=$( awk "/^Package: $1/ { in_pkg = 1 }
    in_pkg && /^Filename: / { print \$2; exit }
    /^$/   { in_pkg = 0 }" $WORK/Packages)
  if [ "v$filename" = "v" ]; then
    echo "ERROR: package $1 not found in package list"
    exit 1
  fi
  archive="$WORK/$1.deb"
  rm -f $archive
  wget -q -O $archive "$MIRROR/$filename"
  PLIST2="$PLIST2 `echo $filename | sed 's/.*\///'`"
  PLIST="$PLIST $archive"
}

for a in $PACKAGES; do get_package $a ; done

echo "ready to install packages:"
for a in $PLIST2; do echo "  $a" ; done
echo -n "hit ctrl+c to cancel, otherwise return"
read a

sudo dpkg --install $PLIST
