#!/bin/bash

TMP="/tmp/$(ls /tmp | grep calamares-root)"
if [ -d "$TMP/proc" ]; then
    CHROOT=$TMP
fi

if [ -z "$CHROOT" ]; then
    exit
fi

ORPH=$(chroot $CHROOT deborphan)
while [ ! -z "$ORPH" ]; do
    echo -e "\n> Remove orphaned packages: $ORPH\n"
    DEBIAN_FRONTEND=noninteractive chroot $CHROOT apt-get purge -q -y $ORPH
    DEBIAN_FRONTEND=noninteractive chroot $CHROOT apt-get autoremove -q -y --purge
    ORPH=$(chroot $CHROOT deborphan)
done
