#!/bin/bash
dver="2.8.6"
# Updated: 11/25/2023
# Package: Debra
# Purpose: Deb Repackaging Assistant
# Authors: Sudo & Paisseon

# colors
red="\033[38;5;196m"
gry="\033[38;5;249m"
dgr="\033[38;5;244m"
neon="\033[38;5;154m"
mnt="\033[38;5;85m"
blu="\033[38;5;39m"
pnk="\033[38;5;141m"
grn="\033[38;5;46m"
yel="\033[38;5;226m"
orn="\033[38;5;172m"
pch="\033[38;5;222m"
prp="\033[38;5;69m"
nco="\033[0m" #no color

##########################
##  Menu and Functions  ##
##########################

if [[ $1 = "-h" ]]; then
    pkg="help"
    clear
    yes '' | sed 7q
    echo -e ${blu}"┌────────────────────────────────────${neon}debra${dver}${blu}─┐"
        printf  ${blu}"│ ${mnt}%-45s${blu} │\n" "Usage:"
    echo -e ${blu}"├───────────────────────────────────────────────┤"
        printf  ${blu}"│ ${mnt}%-45s${blu} │\n" "debra            : main menu interface"
        printf  ${blu}"│ ${mnt}%-45s${blu} │\n" "debra -f         : fix dpkg locked error"
        printf  ${blu}"│ ${mnt}%-45s${blu} │\n" "debra -h         : display this help menu"
        printf  ${blu}"│ ${mnt}%-45s${blu} │\n" "debra tweaklist  : list all installed tweaks"
        printf  ${blu}"│ ${mnt}%-45s${blu} │\n" "debra deblist    : list all saved debs"
        printf  ${blu}"│ ${mnt}%-45s${blu} │\n" "debra <bundleid> : tweak bundle id"
        printf  ${blu}"│ ${mnt}%-45s${blu} │\n" "debra <filename> : deb filename"
echo -e ${blu}"└───────────────────────────────────────────────┘"
    echo -ne ${gry}" Press [Return] to continue to Main Menu: ${neon}"
    read something
    :
fi

if [[ $1 = "-f" ]]; then
    pkg="69"
else
    pkg=$1
fi
if [[ $pkg = "-h" ]]; then
    unset pkg
fi
if [[ $1 = *.deb ]]; then
unset pkg
deb=$1
fi
base_dir=/var/mobile/Documents/Debra
pkg_dir=/var/mobile/Documents/Debra/repackedDebs
deb_dir=/var/mobile/Documents/Debra/savedDebs
cydown_dir=/var/mobile/Documents/CyDown #only if exists
ext_dir=/var/mobile/Documents/Debra/extractedDebs
stg_dir=/var/mobile/Documents/Debra/staging
bak_dir=/var/mobile/Documents/Debra/BACKUP
ctime=$(date "+%m.%d.%Y-%H.%M.%S")

mkdir -p $base_dir
chown -R 501:501 $base_dir 2> /dev/null

## if CyDown deb library exists, symlink to Debra deb library.
## if not, then ignore CyDown and create Debra deb library.
if [ -d $cydown_dir ]; then
    if [ -d $deb_dir ]; then
        if [[ -L "${deb_dir}" ]] && [[ "$(readlink ${deb_dir})" == "$cydown_dir" ]]; then
            :
        else
            mv -f $deb_dir $deb_dir.bak
            ln -s $cydown_dir $deb_dir
            cp -f $deb_dir.bak/*.deb $deb_dir 2> /dev/null
            rm -rf $deb_dir.bak
        fi
    else
        ln -s $cydown_dir $deb_dir
    fi
else
    mkdir -p $deb_dir
fi
if [ ! -d $stg_dir ]; then
    mkdir -p $stg_dir
fi
if [ ! -d $bak_dir ]; then
    mkdir -p $bak_dir
fi

pkgendnumber=`j=1;for i in $(dpkg --get-selections | grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | awk '{print $1}');do echo -e $j:$i;j=$[j+1];done|tail -1|awk -F ":" '{print $1}'`
debendnumber=`h=1;for g in $(ls -1 $deb_dir | grep .deb | grep -v " " | awk '{print $1}');do echo -e $h:$g;h=$[h+1];done|tail -1|awk -F ":" '{print $1}'`

function showmenu() {
    echo -e ${blu}"┌────────────────────────────────────${neon}debra${dver}${blu}─┐"
if [ -z "$pkg" ] || [ "$pkg" = "tweaklist" ]; then
    if [ -z "$deb" ]  || [ "$deb" = "deblist" ]; then
        printf  ${blu}"│ ${dgr}%-45s${blu} │\n" "No Tweak or Deb selected yet"
    else
        printf  ${blu}"│ ${neon}Deb: ${mnt}%-40s${nco}${blu} │${nco}\n" "$deb"
    fi
else
    if [ "$pkg" = "69" ]; then
        printf  ${blu}"│ ${neon}Fix dpkg lock ${pnk}%-31s${nco}${blu} │${nco}\n"
    else
        printf  ${blu}"│ ${neon}Tweak: ${pnk}%-38s${nco}${blu} │${nco}\n" "$pkg"
    fi
fi
    echo -e ${blu}"├───────────────────────┬───────────────────────┤"
    echo -e ${blu}"│      ${gry}Tweak Menu       ${blu}│       ${gry}Deb Menu        ${blu}│"
    echo -e ${blu}"├───────────────────────┼───────────────────────┤"
    printf  ${blu}"│ ${pnk}1. Select Tweak       ${blu}│ ${mnt}4. Select Deb%-9s${nco}${blu}│${nco}\n"
    printf  ${blu}"│ ${pnk}2. Repack Tweak       ${blu}│ ${mnt}5. Extract Deb%-8s${nco}${blu}│${nco}\n"
    printf  ${blu}"│ ${pnk}3. Uninstall Tweak    ${blu}│ ${mnt}6. Install Deb%-8s${nco}${blu}│${nco}\n"
    printf  ${blu}"├──────────────────┬────┴────┬──────────────────┤\n"
if [[ "${pkgendnumber}" -eq "0" ]] || [[ -z "${pkgendnumber}" ]]; then
    printf  ${blu}"│ ${gry}Inst. Pkgs: ${pnk}$pkgendnumber     ${blu}│ ${dgr}7. More ${blu}│ ${gry}Saved debs: ${mnt}%-5s${nco}${blu}│${nco}\n" "$debendnumber"
elif [[ "${pkgendnumber}" -ge "1" ]] && [[ "${pkgendnumber}" -le "9" ]]; then
    printf  ${blu}"│ ${gry}Inst. Pkgs: ${pnk}$pkgendnumber    ${blu}│ ${dgr}7. More ${blu}│ ${gry}Saved debs: ${mnt}%-5s${nco}${blu}│${nco}\n" "$debendnumber"
elif [[ "${pkgendnumber}" -ge "10" ]] && [[ "${pkgendnumber}" -le "99" ]]; then
    printf  ${blu}"│ ${gry}Inst. Pkgs: ${pnk}$pkgendnumber   ${blu}│ ${dgr}7. More ${blu}│ ${gry}Saved debs: ${mnt}%-5s${nco}${blu}│${nco}\n" "$debendnumber"
else
    printf  ${blu}"│ ${gry}Inst. Pkgs: ${pnk}$pkgendnumber  ${blu}│ ${pch}7. More ${blu}│ ${gry}Saved debs: ${mnt}%-5s${nco}${blu}│${nco}\n" "$debendnumber"
fi
    echo -e ${blu}"└──────────────────┴─────────┴──────────────────┘"
}

clear
yes '' | sed 7q

## display message and pause
pause(){
    local m="$@"
    echo "$m"
}

## choose an installed tweak package from list
tweak1list(){
echo
echo -e ${blu}" Filter results by full or partial tweak name."
echo -e ${blu}" Leave blank [return] to choose from All results."
echo
echo -ne ${gry}" Filter: ${neon}"
    read gp
    clear
    unset pkg deb
    yes '' | sed 7q
if [ -z "$gp" ]; then
    pkgendnumber=`j=1;for i in $(dpkg --get-selections |grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | awk '{print $1}');do echo -e $j:$i;j=$[j+1];done|tail -1|awk -F ":" '{print $1}'`
    echo -e ${blu}"┌────────────────────────────────────${neon}debra${dver}${blu}─┐"
    printf  ${blu}"│ ${gry}All installed Tweaks: ${pnk}%-24s${nco}${blu}│${nco}\n" "$pkgendnumber"
    echo -e ${blu}"├───────────────────────────────────────────────┤"
    j=1;for i in $(dpkg --get-selections | grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | awk '{print $1}');do echo -e "$(printf  ${blu}"│ ${nco}%-59s${blu} │${nco}\n" "$j:${pnk}$i")";j=$[j+1];done
    echo -e ${blu}"└───────────────────────────────────────────────┘"
    echo -e ${gry}" Type line# ${nco}[1-$pkgendnumber]${gry} of ${pnk}pkg ${gry}& press [Return]:${nco} \c"
else
    filterpkgnumber=`l=1;for k in $(dpkg --get-selections |grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | grep -i "$gp" | awk '{print $1}');do echo -e $l:$k;l=$[l+1];done|tail -1|awk -F ":" '{print $1}'`
    echo -e ${blu}"┌────────────────────────────────────${neon}debra${dver}${blu}─┐"
    printf  ${blu}"│ ${gry}Pkg Filter: ${pnk}%-34s${nco}${blu}│${nco}\n" "$gp"
    echo -e ${blu}"├───────────────────────────────────────────────┤"
    l=1;for k in $(dpkg --get-selections | grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | grep -i "$gp" | awk '{print $1}');do echo -e "$(printf  ${blu}"│ ${nco}%-59s${blu} │${nco}\n" "$l:${pnk}$k")";l=$[l+1];done
    echo -e ${blu}"└───────────────────────────────────────────────┘"
    if [ -z "$filterpkgnumber" ]; then
    echo -ne ${yel}"    No Results! Return to Main Menu [return]"
    pause
    else
    echo -e ${gry}" Type line# ${nco}[1-$filterpkgnumber]${gry} of ${pnk}pkg ${gry}& press [Return]:${nco} \c"
    fi
fi
    read packageNum
    clear
    yes '' | sed 5q

    case "$packageNum" in  #this includes negative numbers
        ''|*[!0-9]*)
        clear
        yes '' | sed 5q
if [ -z "$gp" ]; then
        echo -e ${yel}"! Invalid number. It must be between 1 and $pkgendnumber !"
else
        if [ -z "$filterpkgnumber" ]; then
        :
        echo
        else
        echo -e ${yel}"! Invalid number. It must be between 1 and $filterpkgnumber !"
        fi
fi
        echo
        ;; #ending of the invalid number case
        *)
if [ -z "$gp" ]; then
        pkgendnumber=`j=1;for i in $(dpkg --get-selections |grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | awk '{print $1}');do echo $j:$i;j=$[j+1];done|tail -1|awk -F ":" '{print $1}'`
            if [[ "$packageNum" -gt "$pkgendnumber" ]] ; then
                clear
                yes '' | sed 5q
                echo -e ${yel}"! Invalid number. It must be between 1 and $pkgendnumber !"
                echo
            else
                pkg=`j=1;for i in $(dpkg --get-selections |grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | awk '{print $1}');do echo $j:$i;j=$[j+1];done | grep -e "$packageNum:" | head -1 |awk -F ":" '{print $2}'`
                echo
            fi
else
        filterpkgnumber=`l=1;for k in $(dpkg --get-selections |grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | grep -i "$gp" | awk '{print $1}');do echo $l:$k;l=$[l+1];done|tail -1|awk -F ":" '{print $1}'`
            if [[ "$packageNum" -gt "$filterpkgnumber" ]] ; then
                clear
                yes '' | sed 5q
                echo -e ${yel}"! Invalid number. It must be between 1 and $filterpkgnumber !"
                echo
            else
                pkg=`l=1;for k in $(dpkg --get-selections |grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | grep -i "$gp" | awk '{print $1}');do echo $l:$k;l=$[l+1];done | grep -e "$packageNum:" | head -1 |awk -F ":" '{print $2}'`
                echo
            fi
fi
        ;; #ending of the valid number case
    esac
}

## Start repackaging
tweak2repkg(){
ver=`dpkg-query -s "$pkg" | grep Version | awk '{print $2}'`
arc=`dpkg-query -s "$pkg" | grep Architecture: | awk '{print $2}'`
if [ ! -z "$pkg" ] && [ "$pkg" != "tweaklist" ]; then
    echo
    if test -z "$(dpkg --get-selections | grep -i "$pkg")"; then
        :
        clear
        yes '' | sed 3q
        echo -e "${red}[Error] ${yel}No tweak exists on device with that name!"
        echo
        echo -e "${yel} Try selecting a tweak from list using Option 1${nco}"
        echo -e ${nco}
    else
        if [ -d /var/jb/xina ] && [ ! -f /var/jb/.installed_xina15 ]; then
	    cp /var/lib/dpkg/info/"$pkg".list /var/lib/dpkg/info/"$pkg".list.debra
	    cat /var/lib/dpkg/info/"$pkg".list | grep -v "/var" > /var/lib/dpkg/info/"$pkg".list.nonvar
            sed -i -e 's#^#/var/jb#' /var/lib/dpkg/info/"$pkg".list.nonvar
	    cat /var/lib/dpkg/info/"$pkg".list | grep "/var" > /var/lib/dpkg/info/"$pkg".list.var
            cat /var/lib/dpkg/info/"$pkg".list.var >> /var/lib/dpkg/info/"$pkg".list.nonvar
            rm -f /var/lib/dpkg/info/"$pkg".list.var
            rm -f /var/lib/dpkg/info/"$pkg".list
            mv -f /var/lib/dpkg/info/"$pkg".list.nonvar /var/lib/dpkg/info/"$pkg".list
	fi
        mkdir -p "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN
        dpkg-query -s "$pkg" | grep -v Status>>"$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/control
        if [ -d /var/jb/var/lib/dpkg/info ];then
            cp /var/jb/var/lib/dpkg/info/"$pkg".postinst "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/postinst 2> /dev/null
            cp /var/jb/var/lib/dpkg/info/"$pkg".preinst "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/preinst 2> /dev/null
            cp /var/jb/var/lib/dpkg/info/"$pkg".postrm "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/postrm 2> /dev/null
            cp /var/jb/var/lib/dpkg/info/"$pkg".prerm "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/prerm 2> /dev/null
            cp /var/jb/var/lib/dpkg/info/"$pkg".extrainst_ "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/extrainst_ 2> /dev/null
            cp /var/jb/var/lib/dpkg/info/"$pkg".extrainst "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/extrainst 2> /dev/null
            cp /var/jb/var/lib/dpkg/info/"$pkg".control-e "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/control-e 2> /dev/null
            cp /var/jb/var/lib/dpkg/info/"$pkg".triggers "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/triggers 2> /dev/null
            cp /var/jb/var/lib/dpkg/info/"$pkg".conffiles "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/conffiles 2> /dev/null
            cp /var/jb/var/lib/dpkg/info/"$pkg".ldid "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/ldid 2> /dev/null
            cp /var/jb/var/lib/dpkg/info/"$pkg".crash_reporter "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/crash_reporter 2> /dev/null
        else
            cp /var/lib/dpkg/info/"$pkg".postinst "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/postinst 2> /dev/null
            cp /var/lib/dpkg/info/"$pkg".preinst "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/preinst 2> /dev/null
            cp /var/lib/dpkg/info/"$pkg".postrm "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/postrm 2> /dev/null
            cp /var/lib/dpkg/info/"$pkg".prerm "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/prerm 2> /dev/null
            cp /var/lib/dpkg/info/"$pkg".extrainst_ "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/extrainst_ 2> /dev/null
            cp /var/lib/dpkg/info/"$pkg".extrainst "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/extrainst 2> /dev/null
            cp /var/lib/dpkg/info/"$pkg".control-e "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/control-e 2> /dev/null
            cp /var/lib/dpkg/info/"$pkg".triggers "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/triggers 2> /dev/null
            cp /var/lib/dpkg/info/"$pkg".conffiles "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/conffiles 2> /dev/null
            cp /var/lib/dpkg/info/"$pkg".ldid "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/ldid 2> /dev/null
            cp /var/lib/dpkg/info/"$pkg".crash_reporter "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/crash_reporter 2> /dev/null
        fi
#        if [ -d /var/jb/xina ] && [ ! -f /var/jb/.installed_xina15 ]; then
#            sed -i 's+!/var+!/bin+g' "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/postinst 2> /dev/null
#            sed -i 's+!/var+!/bin+g' "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/preinst 2> /dev/null
#            sed -i 's+!/var+!/bin+g' "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/postrm 2> /dev/null
#            sed -i 's+!/var+!/bin+g' "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/prerm 2> /dev/null
#            sed -i 's+!/var+!/bin+g' "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/extrainst_ 2> /dev/null
#            sed -i 's+!/var+!/bin+g' "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/extrainst 2> /dev/null
#            sed -i 's+!/var+!/bin+g' "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/control-e 2> /dev/null
#            sed -i 's+!/var+!/bin+g' "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/triggers 2> /dev/null
#            sed -i 's+!/var+!/bin+g' "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/conffiles 2> /dev/null
#            sed -i 's+!/var+!/bin+g' "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/ldid 2> /dev/null
#            sed -i 's+!/var+!/bin+g' "$pkg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/crash_reporter 2> /dev/null
#        fi

        SAVEIFS=$IFS
        IFS=$'\n'
        files=$(dpkg-query -L "$pkg"|sed "1 d")
        for i in $files; do
            if [ -d "$i" ]; then
                mkdir -p "$pkg_dir"/"$pkg"_"$ver"_"$arc"/"$i"
                echo
            elif [ -f "$i" ]; then
                cp -p "$i" "$pkg_dir"/"$pkg"_"$ver"_"$arc"/"$i"
                echo
            fi
        done
        IFS=$SAVEIFS

        rootdir="$pkg_dir"/"$pkg"_"$ver"_"$arc"
        if [ -d /var/jb/xina ] && [ ! -f /var/jb/.installed_xina15 ]; then
          if [ -d "$rootdir"/var/jb ]; then
	    mkdir -p "$rootdir"/temp
	    mv -f "$rootdir"/var/jb/.* "$rootdir"/var/jb/* "$rootdir"/temp >/dev/null 2>&1 || true
	    rm -rf "$rootdir"/var/jb
	    [ -d "$rootdir"/var ] && [ "$(ls -A "$rootdir"/var)" ] && : || rm -rf "$rootdir"/var
	    mv -f "$rootdir"/temp/.* "$rootdir"/temp/* "$rootdir" >/dev/null 2>&1 || true
	    rm -rf "$rootdir"/temp
	fi
          mv -f /var/lib/dpkg/info/"$pkg".list.debra /var/lib/dpkg/info/"$pkg".list
        fi

        dpkg-deb -b "$pkg_dir"/"$pkg"_"$ver"_"$arc" 2>&1
	clear
	echo -ne " ${orn}Press [return] to continue... "
        read prtc
        unset prtc
        clear
        yes '' | sed 3q
        echo -e "${neon} DONE! ${nco}Debra successfully repackaged your tweak."
        echo
        echo -e "${neon} Go to: ${nco}/var/mobile/Documents/Debra/repackedDebs"
        echo
        sleep 2
        if [ -f "/Applications/Filza.app/Filza" ] || [ -f "/var/jb/Applications/Filza.app/Filza" ]; then
        mrecentf=$(ls "${pkg_dir}"/*.deb -Art | tail -n 1)
            if [ -f  "${mrecentf}" ]; then
                dtf "${mrecentf}"
            else
                dtf "${pkg_dir}"
            fi
        elif [ -f "/var/containers/Bundle/Application"/*/Filza.app/Filza ]; then
        mrecentf=$(ls "${pkg_dir}"/*.deb -Art | tail -n 1)
            if [ -f  "${mrecentf}" ]; then
                dtf "${mrecentf}"
            else
                dtf "${pkg_dir}"
            fi
        fi
    fi
    rm -rf "$pkg_dir"/"$pkg"_"$ver"_"$arc"
else
    clear
    yes '' | sed 5q
    echo -e "${yel}! You must first select a Tweak using Opt 1 !${nco}"
    echo
fi
}

## uninstall selected tweak
tweak3uninst(){

if [ -z "$pkg" ]; then
    clear
    yes '' | sed 5q
    echo -e "${yel}! You must first select a tweak using Opt 1 !${nco}"
    echo
else
    if [ ! -z "$pkg" ] && [ "$pkg" != "tweaklist" ]; then
        echo
        if test -z "$(dpkg --get-selections | grep -i "$pkg")"; then
            :
            clear
            yes '' | sed 3q
            echo -e "${red}[Error] ${yel}No tweak exists on device with that name!"
            echo
            echo -e "${yel} Try selecting a tweak from list using Option 1${nco}"
            echo
        else
            echo -e "${yel} Are you sure you want to ${red}UNINSTALL${yel}?${nco}"
            echo
            echo -e " [y] ${neon}Yes - Proceed with uninstall${nco}"
            echo -e " [n] ${neon}No  - Return to main menu${nco}"
            echo
            while true; do
                echo -ne " (y/n): ${neon}"
                read yn
                case $yn in
                    [Yy]* ) yn=y;
                    echo -e ${nco}" Proceeding with request";
                    echo;
                    break;;
                    [Nn]* ) yn=n;
                    echo -e ${nco}" Returning to Main Menu";
                    break;;
                    * ) echo -e ${neon}" Please answer yes or no."${nco};
                esac
            done
            if [ $yn = y ]; then
                if [ -d /var/jb/xina ] && [ ! -f /var/jb/.installed_xina15 ]; then
                    dpkg -r $pkg 2>&1
                else
                    if [[ $EUID -ne 0 ]]; then
                        echo -e ${yel}"This uses sudo permissions for Uninstalling."
                        echo
                    fi
                    sudo dpkg -r $pkg 2>&1
                fi
                unset pkg
                echo
                echo -ne " ${orn}Press [return] to continue... "
                read prtc
                unset prtc
                clear
                yes '' | sed 3q
                echo -e "${neon} DONE! ${nco}Debra successfully uninstalled your tweak."
                echo
                echo -e "${neon}  [99] to Respring or [999] to run 'uicache -r'"
                echo
             else
                clear
                yes '' | sed 5q
                echo -e "${neon} Uninstall cancelled!${nco}"
                echo
            fi
        fi
    fi
fi

}

## choose a deb from Debra savedDebs library
deb4list(){
echo
echo -e ${blu}" Filter results by full or partial deb name."
echo -e ${blu}" Leave blank [return] to choose from All results."
echo
echo -ne ${gry}" Filter: ${neon}"
    read gd
    clear
    unset pkg deb
    yes '' | sed 7q
if [ -z "$gd" ]; then
    debendnumber=`h=1;for g in $(ls -1 $deb_dir | grep .deb | grep -v " " | awk '{print $1}');do echo -e $h:$g;h=$[h+1];done | tail -1 | awk -F ":" '{print $1}'`
    echo -e ${blu}"┌─────────────────────────────────────────${neon}${dver}${blu}─┐"
    printf  ${blu}"│ ${gry}All saved Debs in Debra library: ${mnt}%-13s${nco}${blu}│${nco}\n" "$debendnumber"
    echo -e ${blu}"├───────────────────────────────────────────────┤"
    h=1;for g in $(ls -1 $deb_dir | grep .deb | grep -v " " | awk '{print $1}');do echo -e "$(printf ${blu}"│ ${nco}%-58s${blu} │${nco}\n" "$h:${mnt}$g")";h=$[h+1];done
    echo -e ${blu}"└───────────────────────────────────────────────┘"
    echo -e ${gry}" Type line# ${nco}[1-$debendnumber]${gry} of ${mnt}deb ${gry}& press [Return]:${nco} \c"
else
    filterdebnumber=`n=1;for m in $(ls -1 $deb_dir | grep .deb | grep -v " " | grep -i "$gd" | awk '{print $1}');do echo -e $n:$m;n=$[n+1];done | tail -1 | awk -F ":" '{print $1}'`
    echo -e ${blu}"┌─────────────────────────────────────────${neon}${dver}${blu}─┐"
    printf  ${blu}"│ ${gry}Deb Filter: ${mnt}%-34s${nco}${blu}│${nco}\n" "$gd"
    echo -e ${blu}"├───────────────────────────────────────────────┤"
    n=1;for m in $(ls -1 $deb_dir | grep .deb | grep -v " " | grep -i "$gd" | awk '{print $1}');do echo -e "$(printf ${blu}"│ ${nco}%-58s${blu} │${nco}\n" "$n:${mnt}$m")";n=$[n+1];done
    echo -e ${blu}"└───────────────────────────────────────────────┘"
    if [ -z "$filterdebnumber" ]; then
    echo -ne ${yel}"    No Results! Return to Main Menu [return]"
    pause
    else
    echo -e ${gry}" Type line# ${nco}[1-$filterdebnumber]${gry} of ${mnt}deb ${gry}& press [Return]:${nco} \c"
    fi
fi
    read debNum
    clear
    yes '' | sed 5q

    case "$debNum" in  #includes negative numbers
        ''|*[!0-9]*)
        clear
        yes '' | sed 5q
if [ -z "$gd" ]; then
        echo -e ${yel}"! Invalid number. It must be between 1 and $debendnumber !"
else
        if [ -z "$filterdebnumber" ]; then
        :
        echo
        else
        echo -e ${yel}"! Invalid number. It must be between 1 and $filterdebnumber !"
        fi
fi
        echo
        ;; #ending of invalid number case
        *)
if [ -z "$gd" ]; then
        debendnumber=`h=1;for g in $(ls -1 $deb_dir | grep .deb | grep -v " " | awk '{print $1}');do echo $h:$g;h=$[h+1];done|tail -1|awk -F ":" '{print $1}'`
            if [[ "$debNum" -gt "$debendnumber" ]] ; then
                clear
                yes '' | sed 5q
                echo -e ${yel}"! Invalid number. It must be between 1 and $debendnumber !"
                echo
            else
                deb=`h=1;for g in $(ls -1 $deb_dir | grep .deb | grep -v " " | awk '{print $1}');do echo $h:$g;h=$[h+1];done | grep -e "$debNum:" | head -1 |awk -F ":" '{print $2}'`
                echo
            fi
else
        filterdebnumber=`n=1;for m in $(ls -1 $deb_dir | grep .deb | grep -v " " | grep -i "$gd" | awk '{print $1}');do echo $n:$m;n=$[n+1];done|tail -1|awk -F ":" '{print $1}'`
            if [[ "$debNum" -gt "$filterdebnumber" ]] ; then
                clear
                yes '' | sed 5q
                echo -e ${yel}"! Invalid number. It must be between 1 and $filterdebnumber !"
                echo
            else
                deb=`n=1;for m in $(ls -1 $deb_dir | grep .deb | grep -v " " | grep -i "$gd" | awk '{print $1}');do echo $n:$m;n=$[n+1];done | grep -e "$debNum:" | head -1 |awk -F ":" '{print $2}'`
                echo
            fi
fi
        ;; #ending of valid number case
    esac
}

## extract selected deb
deb5extract(){

if [ ! -d "$ext_dir" ]; then
    mkdir -p "$ext_dir"
    echo "Created folder: $ext_dir"
fi

if [ ! -z "$deb" ] && [ "$pkg" != "deblist" ]; then
    echo
    if test -z "$(ls -1 $deb_dir | grep "$deb")"; then
        :
        clear
        yes '' | sed 3q
        echo -e "${red}[Error] ${yel}No Deb in Cydown folder with that name!"
        echo
        echo -e "${yel} Try selecting a deb from list using Option 4${nco}"
        echo -e ${nco}
    else
        if [ -d /var/jb/xina ] && [ ! -f /var/jb/.installed_xina15 ]; then
            dpkg -x "$deb_dir/$deb" "$ext_dir/${deb%.deb}" 2> /dev/null
            dpkg -e "$deb_dir/$deb" "$ext_dir/${deb%.deb}/DEBIAN" 2> /dev/null
        else
            if [[ $EUID -ne 0 ]]; then
                echo -e ${yel}"This uses sudo permissions for Extracting."
                echo
            fi
            sudo dpkg -x "$deb_dir/$deb" "$ext_dir/${deb%.deb}" 2> /dev/null
            sudo dpkg -e "$deb_dir/$deb" "$ext_dir/${deb%.deb}/DEBIAN" 2> /dev/null
        fi
        if [ $? -eq 2 ]; then
            mkdir -p "$ext_dir/${deb%.deb}"
            cd "$ext_dir/${deb%.deb}"
            ar x "$deb_dir/$deb"
            rm -f debian-binary
            for i in $(ls -1 "$ext_dir/${deb%.deb}" | grep .zst); do
                tar --use-compress-program=unzstd -xf "$ext_dir/${deb%.deb}"/$i
                rm -f "$ext_dir/${deb%.deb}"/$i
            done
            mkdir -p "$ext_dir/${deb%.deb}/DEBIAN"
            chmod 644 "$ext_dir/${deb%.deb}"/control
            mv -f "$ext_dir/${deb%.deb}"/control "$ext_dir/${deb%.deb}"/DEBIAN/ 2> /dev/null
            mv -f "$ext_dir/${deb%.deb}"/postinst "$ext_dir/${deb%.deb}"/DEBIAN/ 2> /dev/null
            mv -f "$ext_dir/${deb%.deb}"/preinst "$ext_dir/${deb%.deb}"/DEBIAN/ 2> /dev/null
            mv -f "$ext_dir/${deb%.deb}"/postrm "$ext_dir/${deb%.deb}"/DEBIAN/ 2> /dev/null
            mv -f "$ext_dir/${deb%.deb}"/prerm "$ext_dir/${deb%.deb}"/DEBIAN/ 2> /dev/null
            mv -f "$ext_dir/${deb%.deb}"/extrainst_ "$ext_dir/${deb%.deb}"/DEBIAN/ 2> /dev/null
            mv -f "$ext_dir/${deb%.deb}"/extrainst "$ext_dir/${deb%.deb}"/DEBIAN/ 2> /dev/null
            mv -f "$ext_dir/${deb%.deb}"/control-e "$ext_dir/${deb%.deb}"/DEBIAN/ 2> /dev/null
            mv -f "$ext_dir/${deb%.deb}"/triggers "$ext_dir/${deb%.deb}"/DEBIAN/ 2> /dev/null
            mv -f "$ext_dir/${deb%.deb}"/conffiles "$ext_dir/${deb%.deb}"/DEBIAN/ 2> /dev/null
            mv -f "$ext_dir/${deb%.deb}"/ldid "$ext_dir/${deb%.deb}"/DEBIAN/ 2> /dev/null
            mv -f "$ext_dir/${deb%.deb}"/crash_reporter "$ext_dir/${deb%.deb}"/DEBIAN/ 2> /dev/null
            rm -f md5sums
        fi
        sleep 3
        clear
        yes '' | sed 3q
        echo -e "${neon} DONE! ${nco}Debra successfully extracted your deb to:"
        echo
        echo -e " ${nco}$ext_dir"
        echo
        sleep 2
        if [ -f "/Applications/Filza.app/Filza" ] || [ -f "/var/jb/Applications/Filza.app/Filza" ]; then
        mrecentf=$(ls "${ext_dir}"/*.deb -Art | tail -n 1)
            if [ -f  "${mrecentf}" ]; then
                dtf "${mrecentf}"
            else
                dtf "${ext_dir}"
            fi
        elif [ -f "/var/containers/Bundle/Application"/*/Filza.app/Filza ]; then
        mrecentf=$(ls "${ext_dir}"/*.deb -Art | tail -n 1)
            if [ -f  "${mrecentf}" ]; then
                dtf "${mrecentf}"
            else
                dtf "${ext_dir}"
            fi
        fi
    fi
else
    clear
    yes '' | sed 5q
    echo -e "${yel}! You must first select a deb using option 4 !${nco}"
    echo
fi

}

## install selected deb
deb6inst(){

if [ -z "$deb" ]; then
    clear
    yes '' | sed 5q
    echo -e "${yel}! You must first select a deb using option 4 !${nco}"
    echo
else
    if [ ! -z "$deb" ] && [ "$deb" != "deblist" ]; then
        echo
        if test -z "$(ls -1 $deb_dir | grep "$deb")"; then
            :
            clear
            yes '' | sed 3q
            echo -e "${red}[Error] ${yel}No Deb in Debra library with that name!"
            echo
            echo -e "${yel} Try selecting a deb from list using Option 4${nco}"
            echo -e ${nco}
        else
            if [ -d /var/jb/xina ] && [ ! -f /var/jb/.installed_xina15 ]; then
                dpkg -i "$deb_dir"/"$deb" #2>&1
            else
                if [[ $EUID -ne 0 ]]; then
                    echo -e ${yel}"This uses sudo permissions for Installing."
                    echo
                fi
                sudo dpkg -i "$deb_dir"/"$deb" #2>&1
            fi
            echo
            echo -ne " ${orn}Press [return] to continue... "
            read prtc
            unset prtc
            clear
            yes '' | sed 3q
            echo -e "${neon} DONE! ${nco}Debra successfully installed your deb!!!!"
            echo
            echo -e "${neon}  [99] to Respring or [999] to run 'uicache -r'"
            echo
        fi
    fi
fi

}

## fix dpkg locked error in package managers
fdl(){
    killall Cydia 2> /dev/null
    killall apt 2> /dev/null
    killall apt-get 2> /dev/null
    rm -rf /var/log/apt/* 2> /dev/null
    rm -rf /var/jb/var/log/apt/* 2> /dev/null
    rm -f /var/lib/apt/lists/lock 2> /dev/null
    rm -f /var/jb/var/lib/apt/lists/lock 2> /dev/null
    rm -f /var/lib/dpkg/lock-frontend 2> /dev/null
    rm -f /var/jb/Library/dpkg/lock-frontend 2> /dev/null
    rm -f /var/cache/apt/archives/lock 2> /dev/null
    rm -f /var/jb/var/cache/apt/archives/lock 2> /dev/null
    rm -f /var/lib/dpkg/lock 2> /dev/null
    rm -f /var/jb/Library/dpkg/lock 2> /dev/null
    dpkg --configure -a  2> /dev/null
    echo
    echo -ne ${neon}"  Fixing dpkg locked....                        \r"
    sleep .4
    echo -ne ${neon}"  Fixing dpkg locked........                    \r"
    sleep .4
    echo -ne ${neon}"  Fixing dpkg locked............                \r"
    sleep .4
    echo -ne ${neon}"  Fixing dpkg locked................            \r"
    sleep .4
    echo -ne ${neon}"  Fixing dpkg locked.....................       \r"
    sleep .4
    echo -ne ${neon}"  Fixing dpkg locked........................... \r"
    sleep 1
    echo -ne ${orn}"                 DPKG UNLOCKED!                      \r"
    echo -ne "\n"
    sleep 2
    echo -e ${grn}"           DEVICE WILL NOW RESPRING..."
    echo
    sleep 3
}

## respring
respring(){
  killall -9 SpringBoard
}

## Refresh icon cache (uicache -r or equivalent)
guicache(){
  echo
  echo " Icon cache refreshed! Device will now respring!"
  echo
  sleep 3
if [ -d /var/jb/xina ] && [ ! -f /var/jb/.installed_xina15 ]; then
  uicache -a 2>&1
  killall -9 SpringBoard
else
  uicache -r 2>&1
fi
}

## more options menu
moremenu(){
clear
yes '' | sed 7q
echo -e " ${pch}More actions...${neon}"
echo
PS3=$(echo -e ${blu}'\n Choose action [1-9]: '${neon})
actions=("Respring" "Refresh icon cache" "Export tweak list" "Export tweak prefs" "Backup all packages"  "Fix dpkg locked" "Jump to Filza" "Back to menu" "Exit")
select action in "${actions[@]}"; do
    case $action in
        "Respring")
            respring
break
;;
        "Refresh icon cache")
            guicache
break
;;
        "Export tweak prefs")
            if [ ! -L "/var/jb" ] || ([ -d "/var/jb/xina" ] && [ ! -f /var/jb/.installed_xina15 ]); then
                rm -rf $stg_dir/Preferences
                cp -rf /var/mobile/Library/Preferences $stg_dir
                rm -f $stg_dir/Preferences/com.apple*
                tar -cvf $bak_dir/Prefs_$ctime.tar -C $stg_dir/Preferences .
                rm -rf $stg_dir/Preferences
            else
                rm -rf $stg_dir/Preferences
                cp -rf /var/jb/var/mobile/Library/Preferences $stg_dir
                rm -f $stg_dir/Preferences/com.apple*
                tar -cvf $bak_dir/Rootless_Prefs_$ctime.tar -C $stg_dir/Preferences .
                rm -rf $stg_dir/Preferences
            fi
            if [ -f "/Applications/Filza.app/Filza" ] || [ -f "/var/jb/Applications/Filza.app/Filza" ]; then
                dtf "${bak_dir}"
            elif [ -f "/var/containers/Bundle/Application"/*/Filza.app/Filza ]; then
                dtf "${bak_dir}"
            fi
            clear
            yes '' | sed 7q
break
;;
        "Export tweak list")
            myarc=$(dpkg --print-architecture)
            if [ $myarc = iphoneos-arm64 ]; then
                dpkg-query -W -f '${status} ${package} ${version}\n' | \
                sed -n 's/^install ok installed //p' | grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | awk '{print $1 " - " $2}' > $bak_dir/Rootless_Tweaklist_$ctime.txt
            elif [ -d /var/jb/xina ]; then
                dpkg-query -W -f '${status} ${package} ${version}\n' | \
                sed -n 's/^install ok installed //p' | grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | awk '{print $1 " - " $2}' > $bak_dir/Xina_Tweaklist_$ctime.txt
            elif [ $myarc = iphoneos-arm ] && [ ! -d /var/jb/xina ]; then
                dpkg-query -W -f '${status} ${package} ${version}\n' | \
                sed -n 's/^install ok installed //p' | grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | awk '{print $1 " - " $2}' > $bak_dir/Tweaklist_$ctime.txt
            fi
#            dpkg --get-selections | grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | awk '{print $1}' > $bak_dir/tweaklist_$ctime.txt
            if [ -f "/Applications/Filza.app/Filza" ] || [ -f "/var/jb/Applications/Filza.app/Filza" ]; then
                dtf "${bak_dir}"
            elif [ -f "/var/containers/Bundle/Application"/*/Filza.app/Filza ]; then
                dtf "${bak_dir}"
            fi
            clear
            yes '' | sed 7q
break
;;
        "Backup all packages")
            if [[ $EUID -ne 0 ]]; then
                echo
                echo -e ${red}" This requires superuser privileges!"
                echo
                echo -e ${yel}" Re-run debra as ${orn}root ${yel}user or run ${orn}sudo debra"
                echo
                exit
            fi
            echo
            echo -e "${yel} Backup can take 5-10 minutes depending on size..."
            echo -e "${yel} Do not exit until you see the ${grn}Completed ${yel}message.${nco}"
            echo
            echo -e " [y] ${neon}Yes - Proceed with backup${nco}"
            echo -e " [n] ${neon}No  - Return to main menu${nco}"
            echo
            while true; do
                echo -ne " (y/n): ${neon}"
                read yn
                case $yn in
                    [Yy]* ) yn=y;
                    echo -e ${nco}" Proceeding with backup";
                    echo;
                    break;;
                    [Nn]* ) yn=n;
                    echo -e ${nco}" Returning to Main Menu";
                    break;;
                    * ) echo -e ${neon}" Please answer yes or no."${nco};
                esac
            done
            if [ $yn = y ]; then
	        for pkg in $(dpkg --get-selections | grep -v -E 'deinstall|gsc\.|cy\+|swift-|build-|llvm|clang' | grep -vw 'git' | awk '{print $1}'); do
	        ver=`dpkg-query -s "$pkg" | grep Version | awk '{print $2}'`
                arc=`dpkg-query -s "$pkg" | grep Architecture: | awk '{print $2}'`
                if [ -d /var/jb/xina ] && [ ! -f /var/jb/.installed_xina15 ]; then
                    cp /var/lib/dpkg/info/"$pkg".list /var/lib/dpkg/info/"$pkg".list.debra
                    cat /var/lib/dpkg/info/"$pkg".list | grep -v "/var" > /var/lib/dpkg/info/"$pkg".list.nonvar
                    sed -i -e 's#^#/var/jb#' /var/lib/dpkg/info/"$pkg".list.nonvar
                    cat /var/lib/dpkg/info/"$pkg".list | grep "/var" > /var/lib/dpkg/info/"$pkg".list.var
                    cat /var/lib/dpkg/info/"$pkg".list.var >> /var/lib/dpkg/info/"$pkg".list.nonvar
                    rm -f /var/lib/dpkg/info/"$pkg".list.var
                    rm -f /var/lib/dpkg/info/"$pkg".list
                    mv -f /var/lib/dpkg/info/"$pkg".list.nonvar /var/lib/dpkg/info/"$pkg".list
                fi
                mkdir -p "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN
                dpkg-query -s "$pkg" | grep -v Status>>"$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/control
                if [ -d /var/jb/var/lib/dpkg/info ];then
                    cp /var/jb/var/lib/dpkg/info/"$pkg".postinst "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/postinst 2> /dev/null
                    cp /var/jb/var/lib/dpkg/info/"$pkg".preinst "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/preinst 2> /dev/null
                    cp /var/jb/var/lib/dpkg/info/"$pkg".postrm "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/postrm 2> /dev/null
                    cp /var/jb/var/lib/dpkg/info/"$pkg".prerm "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/prerm 2> /dev/null
                    cp /var/jb/var/lib/dpkg/info/"$pkg".extrainst_ "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/extrainst_ 2> /dev/null
                    cp /var/jb/var/lib/dpkg/info/"$pkg".extrainst "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/extrainst 2> /dev/null
                    cp /var/jb/var/lib/dpkg/info/"$pkg".control-e "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/control-e 2> /dev/null
                    cp /var/jb/var/lib/dpkg/info/"$pkg".triggers "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/triggers 2> /dev/null
                    cp /var/jb/var/lib/dpkg/info/"$pkg".conffiles "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/conffiles 2> /dev/null
                    cp /var/jb/var/lib/dpkg/info/"$pkg".ldid "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/ldid 2> /dev/null
                    cp /var/jb/var/lib/dpkg/info/"$pkg".crash_reporter "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/crash_reporter 2> /dev/null
                else
                    cp /var/lib/dpkg/info/"$pkg".postinst "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/postinst 2> /dev/null
                    cp /var/lib/dpkg/info/"$pkg".preinst "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/preinst 2> /dev/null
                    cp /var/lib/dpkg/info/"$pkg".postrm "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/postrm 2> /dev/null
                    cp /var/lib/dpkg/info/"$pkg".prerm "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/prerm 2> /dev/null
                    cp /var/lib/dpkg/info/"$pkg".extrainst_ "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/extrainst_ 2> /dev/null
                    cp /var/lib/dpkg/info/"$pkg".extrainst "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/extrainst 2> /dev/null
                    cp /var/lib/dpkg/info/"$pkg".control-e "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/control-e 2> /dev/null
                    cp /var/lib/dpkg/info/"$pkg".triggers "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/triggers 2> /dev/null
                    cp /var/lib/dpkg/info/"$pkg".conffiles "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/conffiles 2> /dev/null
                    cp /var/lib/dpkg/info/"$pkg".ldid "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/ldid 2> /dev/null
                    cp /var/lib/dpkg/info/"$pkg".crash_reporter "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/crash_reporter 2> /dev/null
                fi
#                if [ -d /var/jb/xina ] && [ ! -f /var/jb/.installed_xina15 ]; then
#                    sed -i 's+!/var+!/bin+g' "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/postinst 2> /dev/null
#                    sed -i 's+!/var+!/bin+g' "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/preinst 2> /dev/null
#                    sed -i 's+!/var+!/bin+g' "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/postrm 2> /dev/null
#                    sed -i 's+!/var+!/bin+g' "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/prerm 2> /dev/null
#                    sed -i 's+!/var+!/bin+g' "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/extrainst_ 2> /dev/null
#                    sed -i 's+!/var+!/bin+g' "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/extrainst 2> /dev/null
#                    sed -i 's+!/var+!/bin+g' "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/control-e 2> /dev/null
#                    sed -i 's+!/var+!/bin+g' "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/triggers 2> /dev/null
#                    sed -i 's+!/var+!/bin+g' "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/conffiles 2> /dev/null
#                    sed -i 's+!/var+!/bin+g' "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/ldid 2> /dev/null
#                    sed -i 's+!/var+!/bin+g' "$stg_dir"/"$pkg"_"$ver"_"$arc"/DEBIAN/crash_reporter 2> /dev/null
#                fi

                SAVEIFS=$IFS
                IFS=$'\n'
                files=$(dpkg-query -L "$pkg"|sed "1 d")
                for i in $files; do
                    if [ -d "$i" ]; then
                        mkdir -p "$stg_dir"/"$pkg"_"$ver"_"$arc"/"$i"
                        echo
                    elif [ -f "$i" ]; then
                        cp -p "$i" "$stg_dir"/"$pkg"_"$ver"_"$arc"/"$i"
                        echo
                    fi
                done
                IFS=$SAVEIFS

                rootdir="$stg_dir"/"$pkg"_"$ver"_"$arc"
                if [ -d /var/jb/xina ] && [ ! -f /var/jb/.installed_xina15 ]; then
                  if [ -d "$rootdir"/var/jb ]; then
	            mkdir -p "$rootdir"/temp
	            mv -f "$rootdir"/var/jb/.* "$rootdir"/var/jb/* "$rootdir"/temp >/dev/null 2>&1 || true
	            rm -rf "$rootdir"/var/jb
	            [ -d "$rootdir"/var ] && [ "$(ls -A "$rootdir"/var)" ] && : || rm -rf "$rootdir"/var
	            mv -f "$rootdir"/temp/.* "$rootdir"/temp/* "$rootdir" >/dev/null 2>&1 || true
	            rm -rf "$rootdir"/temp
	        fi
                  mv -f /var/lib/dpkg/info/"$pkg".list.debra /var/lib/dpkg/info/"$pkg".list
                fi

                echo -e ${neon}" Please be patient, this could take a while...${nco}"
                dpkg-deb -b "$stg_dir"/"$pkg"_"$ver"_"$arc" 2>&1
                rm -rf "$stg_dir"/"$pkg"_"$ver"_"$arc" 2>&1
                clear

                done

                clear
                unset pkg
                echo
                echo -ne " ${orn}Press [return] to finish... "
                read prtc
                unset prtc
                myarc=$(dpkg --print-architecture)
                if [ $myarc = iphoneos-arm64 ]; then
                    tar -cvf $bak_dir/Rootless_Tweaks_Backup_$ctime.tar -C $stg_dir --exclude=backup*.tar --exclude=tweaklist*.txt --exclude=Preferences*.tar .
                elif [ -d /var/jb/xina ]; then
                    tar -cvf $bak_dir/Xina_Tweaks_Backup_$ctime.tar -C $stg_dir --exclude=backup*.tar --exclude=tweaklist*.txt --exclude=Preferences*.tar .
                elif [ $myarc = iphoneos-arm ] && [ ! -d /var/jb/xina ]; then
                    tar -cvf $bak_dir/Tweaks_Backup_$ctime.tar -C $stg_dir --exclude=backup*.tar --exclude=tweaklist*.txt --exclude=Preferences*.tar .
                fi
                rm -f $stg_dir/*.deb
                clear
                yes '' | sed 3q
                echo -e "${neon} DONE! ${nco}Debra successfully backed up all packages."
                echo
                echo -e "${neon} Go to: ${nco}/var/mobile/Documents/Debra/BACKUP"
                echo
                sleep 2
                if [ -f "/Applications/Filza.app/Filza" ] || [ -f "/var/jb/Applications/Filza.app/Filza" ]; then
                    dtf "${bak_dir}"
                elif [ -f "/var/containers/Bundle/Application"/*/Filza.app/Filza ]; then
                    dtf "${bak_dir}"
                fi
            else
                clear
                yes '' | sed 5q
                echo -e "${neon} Backup cancelled!${nco}"
                echo
            fi
break
;;
        "Fix dpkg locked")
            pkg="69"
            clear
            yes '' | sed 7q
break
;;
        "Jump to Filza")
            if [ -f "/Applications/Filza.app/Filza" ] || [ -f "/var/jb/Applications/Filza.app/Filza" ]; then
              dtf "${base_dir}"
            elif [ -f "/var/containers/Bundle/Application"/*/Filza.app/Filza ]; then
              dtf "${base_dir}"
            else
              echo
              echo -e ${red}" Filza is not installed! ${yel}Returning to main menu."
              echo
              sleep 5
            fi
            clear
            yes '' | sed 7q
break
;;
        "Back to menu")
            clear
            yes '' | sed 7q
break
;;
        "Exit")
            clear && yes '' | sed 4q
exit 0
;;
        *) echo "invalid option $REPLY";;
    esac
done

}

## display the menu in loop until exit
while :
do
showmenu
if [ "$pkg" = "tweaklist" ] || [ "$pkg" = "deblist" ]; then
    if [ "$pkg" = "tweaklist" ]; then
        unset pkg
        c=1
    fi
    if [ "$pkg" = "deblist" ]; then
        unset pkg
        c=4
    fi
elif [ "$pkg" = "69" ]; then
    unset pkg
    c=69
else
    echo -ne ${gry}" Enter your choice [1-7] and press [Return]: ${neon}"
    read c
fi

## take action
    case $c in
    1) tweak1list;;
    2) tweak2repkg;;
    3) tweak3uninst;;
    4) deb4list;;
    5) deb5extract;;
    6) deb6inst;;
    7) moremenu;;
    8) clear && yes '' | sed 4q && break;;
    69) fdl && sleep 3 && respring && break;;
    99) respring && break;;
    999) guicache && break;;
    .) clear && yes '' | sed 4q && break;;
    *) clear && yes '' | sed 4q && pause && echo -e "${yel}! You must select an option between 1 & 7 only !${nco}" && echo
    esac
done
