#!/bin/bash

PS="$(docker ps -aq 2> /dev/null)"

if [ "$?" != 0 ] && [ "$EUID" != 0 ]; then
    sudo -- "$0" "$@"
    exit
fi

VOL="$(docker volume ls -q 2> /dev/null)"
IMG="$(docker images    -q 2> /dev/null)"


if [ -n "$PS" ]; then
    docker ps -a

    echo -n "Prune Containers? [Y/n/a] "
    read -r ANS
    case "${ANS,,}" in
        'y'|'yes'|'')
            echo "$PS" | xargs docker rm 2> /dev/null
            ;;
        'a'|'all')
            echo "$PS" | xargs docker rm -f
            ;;
    esac
fi

if [ -n "$VOL" ]; then
    echo
    docker volume ls

    echo -n "Prune Volumes? [Ya/n] "
    read -r ANS
    case "${ANS,,}" in
        'y'|'yes'|'a'|'all'|'')
            echo "$VOL" | xargs docker volume rm -f 2> /dev/null
            ;;
    esac
fi

if [ -n "$IMG" ]; then
    echo
    docker images

    echo -n "Prune Images? [Y/n/a] "
    read -r ANS
    case "${ANS,,}" in
        'y'|'yes'|'')
            docker image prune -f
            ;;
        'a'|'all')
            echo "$IMG" | xargs docker image rm -f 2> /dev/null
            ;;
    esac
fi
