#!/bin/sh
#211022 sfs

# $1 - action
# $2 - type of file

action=$1
filetype=$2


do_view_action() {
    filetype=$1

    case "${filetype}" in
    trpm)
        rpm -qivl --scripts `basename "${MC_EXT_BASENAME}" .trpm`
        ;;
    src.rpm|rpm)
        #if rpm --nosignature --version >/dev/null 2>&1; then 
        #    RPM="rpm --nosignature"
        #else
        #    RPM="rpm"
        #fi
        #$RPM -qivlp --scripts "${MC_EXT_FILENAME}"
        bsdtar -tvvvf "${MC_EXT_FILENAME}"
extract_rpm ()
{
	leadsize=96
	o=`expr $leadsize + 8`
	set `od -j $o -N 8 -t u1 $pkg`
	il=`expr 256 \* \( 256 \* \( 256 \* $2 + $3 \) + $4 \) + $5`
	dl=`expr 256 \* \( 256 \* \( 256 \* $6 + $7 \) + $8 \) + $9`

	sigsize=`expr 8 + 16 \* $il + $dl`
	o=`expr $o + $sigsize + \( 8 - \( $sigsize \% 8 \) \) \% 8 + 8`
	set `od -j $o -N 8 -t u1 $pkg`
	il=`expr 256 \* \( 256 \* \( 256 \* $2 + $3 \) + $4 \) + $5`
	dl=`expr 256 \* \( 256 \* \( 256 \* $6 + $7 \) + $8 \) + $9`
	hdrsize=`expr 8 + 16 \* $il + $dl`
	o=`expr $o + $hdrsize`
#mkdir $d && cd $d
#	dd if=../$pkg ibs=$o skip=1 2>/dev/null | bsdtar -xf -
	dd if=$pkg ibs=$o skip=1 2>/dev/null | xz -d| cpio -tvv 2>/dev/null ||
	dd if=$pkg ibs=$o skip=1 2>/dev/null | zstd -d| cpio -tvv 2>/dev/null #-i -d
}


pkg="${MC_EXT_FILENAME}"
#d="`expr $1 : '.*/\(.*\)[.].*' \| $1 : '\(.*\)[.].*' \| $1`"
#extract_rpm $pkg
        ;;

    deb)
        #dpkg-deb -I "${MC_EXT_FILENAME}" && echo && dpkg-deb -c "${MC_EXT_FILENAME}"
        #bsdtar -xOf "${MC_EXT_FILENAME}" data.tar.gz |tar -tvvzf -
        ar p "${MC_EXT_FILENAME}" data.tar.gz |tar -tzvf -
        ;;
    debd)
        dpkg -s `echo "${MC_EXT_BASENAME}" | sed 's/\([0-9a-z.-]*\).*/\1/'`
        ;;
    deba)
        apt-cache show `echo "${MC_EXT_BASENAME}" | sed 's/\([0-9a-z.-]*\).*/\1/'`
        ;;
    *)
        ;;
    esac
}

do_open_action() {
    filetype=$1

    case "${filetype}" in
    *)
        ;;
    esac
}

case "${action}" in
view)
    do_view_action "${filetype}"
    ;;
open)
    xdg-open "${MC_EXT_FILENAME}" 2>/dev/null || \
        do_open_action "${filetype}"
    ;;
*)
    ;;
esac
