#!/bin/bash
# barium helper scripts
# author: rosalinux.ru: betcher_
if [ -f $(dirname $0)/lib ] ;  then
    . $(dirname $0)/lib
else
    . $(which lib) || exit 1
fi

HLP(){
echo "$(basename $0) - утилита для чтения метаинформации модуля"
echo "Использование:"
echo "$(basename $0) имя_модуля"
echo "$(basename $0) имя_модуля -s ':::' - разделитель полей между подмодулями"
exit 1
}

[ "$1" ] || HLP
[ "$1" == '-h' -o "$1" == '--help' ] && HLP
DELIMETER='/'
SHOW_FILE=''
while [ -n "$1" ]; do
  case "$1" in
    "-s" | "--separator" ) shift ; DELIMETER="$1" ;;
    "-h" | "--help" ) HLP ;;
    "-f" | "--file" ) shift ; SHOW_FILE="$1" ;;
    "-"*[A-Za-z]*) echo "$(basename "$0"): invalid option -- '$(echo ${1} | tr -d '-')'" >&2; exit 1;;
    *)  inpack="${1}";;
  esac
  shift
done 

found_mounted () {
    local bundle=''
    if [[ "$1" =~ / ]]; then
        bundle=$(barium ls --raw '$source:$bundle' | grep -w "$1" | cut -f 2 -d ':')
    else
        bundle=$(barium ls --raw '$bname_source:$bundle' | grep -w "$1" | cut -f 2 -d ':')
    fi
    echo "$bundle" |head -n1
}

bundle2mod () {
    local module=''
    module=$(barium ls --raw '$bundle:$source' | grep -Ew "^$1" | cut -f 2 -d ':')
    echo "$module" |head -n1
}

if [ -f "${inpack}" ]; then 
    module="$(realpath "${inpack}")"
    COMPALG=$(unsquashfs -s "${module}" 2>/dev/null |grep ompression |awk '{print $NF}' | tr "\n" " ")
    [ -z "$COMPALG" ] && exitmsg "$module is non squashfs module" ${LINENO}
    bundle=$(found_mounted $module)
    if [ -z $bundle ] ; then
        TMP=/tmp/$$/$(basename ${module})
        mkdir -p $TMP
        mount -t squashfs -o loop -o ro "${module}" $TMP
        trap 'umout $TMP || umount -l $TMP' 1 2 3 15
    else
        TMP=$bundle
    fi
else
    bundle=$(found_mounted ${inpack})
    if [ -z $bundle ] ; then
        exitmsg "Module \"${inpack}\" not found" ${LINENO}
        exit
    else
        module=$(bundle2mod "$bundle")
        COMPALG=$(unsquashfs -s "${module}" 2>/dev/null |grep ompression |awk '{print $NF}' | tr "\n" " ")
        TMP="$bundle"
    fi
fi

if [ -z "$SHOW_FILE" ] ; then
    Msize=$(du -s ${module} |cut -f1) 
    Usize=$(du -s  ${TMP} |cut -f1)
    echo "Module: $module"
    echo "Compression algorithm: $COMPALG"
    echo "Module size: $(du -hs ${module} |cut -f1)"
    echo "Uncompressed size: $(du -hs  ${TMP} |cut -f1)"
    [ "$Usize" -ne 0 ] && echo "Compression ratio: $( echo "$((Usize/$Msize))") ($( echo "$((100*$Msize /$Usize))")%)"
    if [ -d ${TMP}${METAINFODIR}/mount ] ; then
        Deps=$(find ${TMP}${METAINFODIR}/mount  -type f -name pfs.depends -exec cat "{}" \; |sort |uniq |grep -v "^$" | tr "\n" "   ")
        Submods=$(find ${TMP} | grep ${METAINFODIR}/.*/pfs.files |sed -e "s:${TMP}${METAINFODIR}/mount/::" \
                -e 's:/submod/mount::g' -e 's:/pfs.files$::' -e 's#/#'$DELIMETER'#g' | tr "\n" "   ")
        [ -f ${TMP}${METAINFODIR}/INFO ] && Info=$(cat  ${TMP}${METAINFODIR}/INFO |sed 's/=/: /')
        echo "Dependenses: $Deps"
        echo "Submodules: $Submods"
        echo ''
        standard_files="pfs.files pfs.depends pfs.dirs.empty"
        for file in $(find ${TMP}${METAINFODIR}/mount  -type f) ; do
            if ! echo $standard_files | grep -q $(basename $file) ; then
                echo "[ $(echo $file |sed "s:$TMP::")  ]"
                cat $file |grep -v "^$"
                echo ''
            fi
        done
    fi
    if [ -d ${TMP}/var/lib/dnf2mod ] || [ -d ${TMP}/var/lib/chroot2mod ] ; then
        builders_info ${TMP} cmdline os-release date stack.order
    fi
else 
    find ${TMP}/var/lib/*2mod -name "$SHOW_FILE" 2>/dev/null |while read f ; do
        echo -ne "==> file: " 
        echo $f |sed "s#${TMP}##"
        cat $f
        echo
    done 
fi

if [ -z $bundle ] ; then
    umount $TMP
    rmdir  $TMP
fi
