#!/usr/bin/env sh
# Copyright 2017-2022 (c) all rights reserved by SDRausty, please see LICENSE.
# https://sdrausty.github.io/au  Contributors: Glow/frumpylava, Hax4us
# In order to save native space on device the path /storage/emulated/0/Android/data/com.termux/txdebs can be changed to a microSDcard destination.
# dpkg --purge name.pkg ; dpkg --install name.pkg # can downgrade packages 
################################################################################
set -eu
_AUDIR_ () { [ -e /storage/emulated/0/Android/data/com.termux/txdebs ] || { cd /storage/emulated/0/Android/data/com.termux/ && mkdir -p txdebs ; } || _PRIT_ "Please create directory '/storage/emulated/0/Android/data/com.termux/' and if an external micro sdcard is being used, '/storage/1234-5678/Android/{data,media}/com.termux/' can be used after modifying this path;  " ; }
# update, upgrade and then install packages if any or print message
_AUGET_ () { { apt update && apt -o APT::Keep-Downloaded-Packages="true" upgrade -y && apt -o APT::Keep-Downloaded-Packages="true" install "$@" -y  && find /data/data/com.termux/cache/apt/archives/ -maxdepth 1 -type f -name "*.deb" -exec mv -f {} /storage/emulated/0/Android/data/com.termux/txdebs/ \; ; } || _PRIT_ "Something unexpected occured;  Please study the first lines of the error output, if any, and run ${0##*/} again to install and auto update packages!  " ; }
_PD_ () { _PRNT_ "Command '${0##*/}';  DONE" ; }
_PRIT_ () { printf '%s' "$1" ; } # print message with no trialing newline
_PRNT_ () { printf '%s\n' "$1" ; } # print message with one trialing newline
_PB_ () { _PRNT_ "Command '${0##*/}';  BEGUN... " ; }
_PB_ && _AUDIR_ && _AUGET_ "$@" && _PD_
# WAE/au FE
