#!/usr/bin/env bash
#
# pkgsize - shows disk usage for CRUX packages/ports
# Check man 1 pkgsize for more details 
#
# (c) Damir Saric <damir.saric@du.hinet.hr>
#
# May be redistributed and modified under the terms of the GPL
# only usable with CRUX Linux
#
# USE AT YOUR OWN RISK
#

function interrupted() {
    echo ""
    echo "Interrupted."
    exit 1
}

function error () {
    echo "ERROR! --> $1"
}

function usage() {
    echo "usage: pkgsize [options] [portname]"
    echo "options:"
    echo "  -v,   --verbose             show more information (each file's size)"
    echo "  -w,   --wget                show size to download (default if package not installed)"
    echo "  -r <mountpoint>             specify alternate root on which package was installed"
    echo "  -h,   --help                print help and exit"
    exit 0
}

function sizetodownload() {
    local dir="$1"
    portname=$(basename "$dir")
    . "$dir/Pkgfile"

    local nrbytes=0
    for FILE in "${source[@]}"; do
        sizeofdl=0
        if [[ "$FILE" =~ ^(http|https|ftp): ]]; then
           HOST="${FILE#*:\/\/}"
           HOST="${HOST%%/*}"
           sizeofdl=$(curl -I -s -L "$FILE" | awk -v IGNORECASE=1 '/content-length/ {printf("%i",$2)}')
	   [ -n "$sizeofdl" ] || { error "$HOST unreachable. "; continue; }

           [ "$verbose" = "true" ] && printf "%12s   %s\n" "$sizeofdl" "$FILE"

           if [ "$nrbytes" != "unknown" ] && [ "$sizeofdl" -gt 0 ]; then
               ((nrbytes=nrbytes+sizeofdl))
           else
               nrbytes="unknown"
           fi
        fi
    done
    echo "Total number of bytes to download for $portname is : $nrbytes"
}

function main() {

local portname="$1"
if [ "$online" = "true" ] || \
    $PRTGET isinst "$portname" 2>&1 | grep -qE "(provided by|not installed)"; then
    dir="$(prt-get path "$portname" 2>/dev/null)" || { error "$portname not a port!"; return; }
    sizetodownload "$dir"
else
    bytea=0
    while read -r file; do
    if [ -f "$ROOT/$file" ]; then
        size=$(/usr/bin/du -k "$ROOT/$file" | cut -f1)
        ((bytea=bytea+size))
        [ "$verbose" = "true" ] && printf "%10s   %s\n" "$size" "$file"
    fi
    done < <($PKGINFO -l "$portname")

    echo "Total kilobytes for $portname : $bytea"
fi
}

trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM

PKGINFO=/usr/bin/pkginfo
PRTGET=/usr/bin/prt-get
ROOT=""
targets=()
online="false"
verbose="false"
while [ "$1" ]; do
    case "$1" in
        -v|-V)
            verbose="true"
            shift
            ;;
        -h|-H|--help)
            usage
            ;;
        -w|-W|--wget)
            online="true"
            shift
            ;;
        -r)
            shift
            [ -e "$1/var/lib/pkg/db" ] || usage
            PKGINFO+=" -r $1"
            PRTGET+=" --install-root=$1"
            ROOT="$1"
            shift
            ;;
        *)
            targets+=("$1")
            shift
            ;;
    esac
done

[ ${#targets[@]} -gt 0 ] || usage

for t in "${targets[@]}"; do
    main "$t"
done
