Advertisement
s243a

Draft: pkg (woof-next s243a)

Jan 8th, 2020
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 319.94 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. #  pkg - a command line package manager for Puppy Linux
  4. #
  5. #  By Scott Jarvis (sc0ttman)
  6. #  #s243a: modified to use vars instead of explicit paths.
  7. #  Copyright (c) 2013 Puppy Linux Community
  8. #
  9. #  This program is free software; you can redistribute it and/or modify
  10. #  it under the terms of the GNU General Public License as published by
  11. #  the Free Software Foundation; either version 1 of the License, or
  12. #  (at your option) any later version.
  13. #
  14. #  This program is distributed in the hope that it will be useful,
  15. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. #  GNU General Public License for more details.
  18. #
  19. #  You should have received a copy of the GNU General Public License
  20. #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.  
  22.  
  23. #==================  setup script vars  =====================#
  24.  
  25. APPNAME="Pkg"
  26. APPVER="1.9.22"
  27. APPTITLE="$APPNAME $APPVER"
  28. list_deps_count=0 #Keeps track of the number of instances of the function list_deps(). Cleanup will be done when the last instance exists.
  29. dep_id=0 #
  30. # get current locale settings
  31. USER_LANG=$LANG
  32. USER_LC_ALL=$LC_ALL
  33. export HTTPS_CHECK=all
  34. SELF=$(basename $0)        # this script
  35. QTAG=''                    # gets set to (y/n) if ASK=-true
  36. LANG=C                     # speed up
  37. LC_ALL=C                   # speed up
  38. EDITOR=${EDITOR:-vi}       # just in case
  39. PAGER=${PAGER:-less}       # just in case
  40. CURDIR="${PWD}"            # get current dir, before cd to WORKDIR
  41. whoami=$(whoami)           # s243a copied from below. See issue: http://murga-linux.com/puppy/viewtopic.php?p=1030838#1030838
  42. TMPDIR=/tmp/pkg/${whoami}/$$  # set the tmp dir
  43. mkdir -p "${TMPDIR}"
  44. # set colours true by default
  45. green="\e[32m"; red="\e[91m"; magenta="\e[95m"; lightblue="\e[36m"; yellow="\e[93m"; bold="\e[1m"; endcolour="\e[0m"
  46.  
  47. # ENVIRONMENT VARS, which may be overridden later
  48. [ -z "$PKGRC" ] && export PKGRC=${HOME}/.pkg/pkgrc      # config file for this script
  49. [ -z "$ASK"   ] && ASK=false                            # if true, ask user before doing stuff. Overridden by --ask
  50. [ -z "$QUIET" ] && QUIET=false                          # if true, hide output that doesnt log well in Xdialog, Gtkdialog, etc
  51. [ -z "$FORCE" ] && FORCE=false                          # if true, force (re)install/remove pkgs. Overridden by --force
  52. [ -z "$HIDE_INSTALLED" ] && export HIDE_INSTALLED=false # if true, hide installed pkgs from pkg searches (-n, -na, -ss, -ssa)
  53. [ -z "$HIDE_BUILTINS"  ] && export HIDE_BUILTINS=true   # if true, remove builtins pkgs from dep lists, dont download/install them
  54. [ -z "$HIDE_USER_PKGS" ] && export HIDE_USER_PKGS=true  # if true, hide user installed pkgs from dep lists, dont download/install them
  55. [ -z "$NO_ALIASES"     ] && export NO_ALIASES=false     # if true, skip searching pkg alias names for pkg alternatives
  56. [ -z "$NO_INSTALL"     ] && export NO_INSTALL=false     # if true, skip installing of pkgs
  57. [ -z "$PKG_NO_COLOURS" ] && export PKG_NO_COLOURS=false # if true, disable coloured output or not
  58. [ -z "$PKG_CONFIGURE"  ] && export PKG_CONFIGURE=''     # reset
  59. [ -z "$PKG_CFLAGS"     ] && export PKG_CFLAGS=''        # reset
  60.  
  61. [ -z "$REPO_DIR" ] && export REPO_DIR="$(realpath "${HOME}/.packages")"
  62.  
  63. #These are files of the form
  64. #[ -z "$PET_SPEC_DIR_ROOT" ] && PET_SPEC_DIR_ROOT="$REPO_DIR"
  65. if [ -z "$REPO_DB_FILE_DIR" ]; then
  66.   if [ -d "$REPO_DIR/repo" ]; then #This imples mistfires ppm3 is installed
  67.     export REPO_DB_FILE_DIR="$(realpath "$REPO_DIR/repo")"
  68.   else
  69.     export REPO_DB_FILE_DIR="$REPO_DIR"
  70.   fi
  71. fi
  72. if [ -z "$PACKAGE_FILE_LIST_DIR" ]; then
  73.   if [ -d "$REPO_DIR/package-files" ]; then #This imples mistfires ppm3 is installed
  74.     export PACKAGE_FILE_LIST_DIR="$(realpath "$REPO_DIR/package-files")"
  75.   else
  76.     export PACKAGE_FILE_LIST_DIR="$(realpath "$REPO_DIR")"
  77.   fi
  78. fi
  79. [ -z "$BUILTIN_FILE_LIST_DIR" ] && BUILTIN_FILE_LIST_DIR="$REPO_DIR/builtin_files"
  80. #[ -z "$PET_SPEC_DIR_BUILTIN" ] && PET_SPEC_DIR_BUILTIN=\
  81. #  "$(realpath "$REPO_DIR/builtin_files")"
  82. #
  83. #[ -z "$PET_SPEC_DIR_USR_INST" ] && PET_SPEC_DIR_USR_INST="$REPO_DIR"
  84.  
  85.  
  86. #mk_spec_file_list_arry(){
  87.  declare -ga SPEC_DB_PATHS=()
  88.  #Can't put the loop in the loop in a pipeline if you want to assign array values:
  89.  #https://stackoverflow.com/questions/13091700/why-is-my-array-gone-after-exiting-loop  
  90.  #find / -wholename "$REPO_DIR"/'*-installed-packages' |
  91.  while read SPEC_FILE; do
  92.    bname="`basename "$SPEC_FILE"`"
  93.    case "$bname" in #Consider lowering case
  94.    "layers-installed-packages")
  95.      #Do Nothing
  96.      export LAYER_INST_PKGS_FILE="$SPEC_FILE" ;;
  97.    "woof-installed-packages")
  98.      export WOOF_INST_PKGS_FILE="$SPEC_FILE" ;;
  99.    "user-installed-packages")
  100.      export USER_INST_PKGS_FILE="$SPEC_FILE" ;;
  101.    "devx-only-installed-packages")
  102.      export DEVX_INST_PKGS_FILE="$SPEC_FILE" ;;    
  103.    *)
  104.      SPEC_DB_PATHS+=( "$SPEC_FILE" ) ;;
  105.    esac
  106.    #s243a: We don't need realpath for the next four vars but it will provide usefull debugging information.
  107.    #s243a: TODO search for the following files if they don't exist.
  108.    [ -z "$USER_INST_PKGS_FILE" ] && \
  109.      USER_INST_PKGS_FILE="$REPO_DIR/user-installed-packages"
  110.    [ -z "$WOOF_INST_PKGS_FILE" ] && \
  111.      WOOF_INST_PKGS_FILE="$REPO_DIR/woof-installed-packages"
  112.    [ -z "$LAYER_INST_PKGS_FILE" ] && \
  113.      LAYER_INST_PKGS_FILE="$REPO_DIR/layers-installed packages"
  114.    [ -z "$DEVX_INST_PKGS_FILE" ] && \
  115.      DEVX_INST_PKGS_FILE="$REPO_DIR/devx-only-installed-packages"
  116.    SPEC_DB_PATHS=( "$USER_INST_PKGS_FILE" "$WOOF_INST_PKGS_FILE" "${SPEC_DB_PATHS[@]}" )
  117.  done < <( find "$REPO_DIR" -name '*-installed-packages' )
  118. #}  
  119. #mk_spec_file_list_arry
  120. #echo "SPEC_DB_PATHS=${SPEC_DB_PATHS[@]}"
  121.  
  122.  
  123.  
  124. #mk_all_repo_file_list_arry(){
  125.  declare -gA ALL_REPO_DB_PATHS=()
  126.  #ALL_REPO_DB_PATHS=()
  127.  bname=''
  128.  
  129.  #Can't put the loop in the loop in a pipeline if you want to assign array values:
  130.  #https://stackoverflow.com/questions/13091700/why-is-my-array-gone-after-exiting-loop
  131.  #find / -wholename "$REPO_DB_FILE_DIR"/'Packages-*' |
  132.  while read REPO_FILE; do
  133.      #s243a: I was thinking of using realpath but it might cause issues.
  134.      #REPO_FILE="$(realpath "$REPO_FILE")"
  135.      bname="$(basename "$REPO_FILE")"  
  136.      ALL_REPO_DB_PATHS+=( ["$bname"]="$REPO_FILE" )
  137.      #echo "ALL_REPO_DB_PATHS=${ALL_REPO_DB_PATHS[@]}"
  138.      #echo "ALL_REPO_DB_PATHS=${ALL_REPO_DB_PATHS[@]}"
  139.  done < <( find "$REPO_DB_FILE_DIR" -name 'Packages-*' )
  140. #}  
  141. #mk_all_repo_file_list_arry
  142. #echo "exited: mk_all_repo_file_list_arry()"
  143. #echo "ALL_REPO_DB_PATHS=${ALL_REPO_DB_PATHS[@]}"
  144.  
  145.   [ -z "$REPO_DIR" ] && export REPO_DIR="$(realpath "${HOME}/.packages")"
  146.   [ -z "$HASHES_DIR" ] && export HASHES_DIR="$REPO_DIR/hashes" && mkdir -p "$HASHES_DIR"
  147.  
  148.  
  149. #Moved after the function repo_file_list
  150. #mk_repo_file_list_arry
  151. # but disable colours if called as gpkg, or the ENV variable PKG_NO_COLOURS is true
  152. if [ "`basename $0`" != "pkg" -o "$PKG_NO_COLOURS" = true ]; then
  153.   green='' red='' magenta='' lightblue='' yellow='' bold='' endcolour=''
  154. fi
  155.  
  156. # report errors better than just echo
  157. error(){
  158.   echo -e 1>&2 "${red}Error:${endcolour} $1"
  159. }
  160.  
  161. # for correct error output, trap commands early
  162. cleanup(){
  163.   rm -f ${TMPDIR}/missing_dep* ${TMPDIR}/installed_pkg* ${TMPDIR}/all_dep* ${TMPDIR}/DEP_DONE ${TMPDIR}/list_deps_busy ${TMPDIR}/pkg_file_list ${TMPDIR}/dependents_list ${TMPDIR}/DEP_DONE ${TMPDIR}/ldd_file_list &>/dev/null
  164.   rm -rf ${TMPDIR}/build_pkg/ &>/dev/null
  165.  
  166.   # prevent double msg output if error=130 (user Ctrl-C)
  167.   [ -f /tmp/pkg/error130 -a "$2" = '130' ] && exit "$2"
  168.   # make the tmp dir we'll use
  169.   mkdir -p /tmp/pkg &>/dev/null
  170.   # only add line numbers if it was given
  171.   [ "$1" != 'none' ] && lineno="line $1, " || lineno=''
  172.   # get the right error msg
  173.   case "$2" in
  174.   130) echo; msg="User cancelled operation!"; touch /tmp/pkg/error130;;
  175.   8)   msg="Package URL invalid.";;
  176.   7)   msg="Package extraction failed.";;
  177.   6)   msg="Missing package file.";;
  178.   5)   msg="Missing directory.";;
  179.   4)   msg="Copy failed.";;
  180.   3)   msg="Setup failed.";;
  181.   2)   msg="Code error!";;
  182.   1)   exit 1 ;; # user gave wrong options or pkg name, not serious
  183.   *)   msg="Error code = $2" ;;
  184.   esac
  185.   # print msg
  186.   [ "$2" != "0" ]  && echo -e 1>&2 "${red}Error${endcolour} ${2}: ${lineno}$msg"
  187.   exit $2
  188. }
  189. trap 'cleanup ${LINENO:-none} $?' EXIT INT TERM
  190.  
  191. #run as root only
  192. whoami=$(whoami) #s243a Copied to before line #40. See issue: http://murga-linux.com/puppy/viewtopic.php?p=1030838#1030838
  193. [ "$whoami" != "root" ] && echo "You must be root to run Pkg." && exit 1
  194.  
  195. #080413 make sure all config files are present, copy from /etc if need be
  196. if [ ! -f ${PKGRC} ]; then
  197.   echo "Restoring default settings from /etc/pkg"
  198.   [ ! -d /etc/pkg/ ] && error "Default config files  in /etc/pkg/ not found." && exit 3
  199.   mkdir -p "$HOME/.pkg"
  200.   [ ! -d "$HOME/.pkg" ] && error "Default user config dir '$HOME/.pkg/' could not be created." && exit 3
  201.   cp --force -a /etc/pkg/* "$HOME/.pkg/" || exit 3
  202. fi
  203.  
  204. # get puppy distro env vars
  205. [ -f /etc/rc.d/PUPSTATE ]                   && . /etc/rc.d/PUPSTATE                    #this has PUPMODE and SAVE_LAYER.
  206. [ -f /etc/DISTRO_SPECS ]                    && . /etc/DISTRO_SPECS                     #has DISTRO_BINARY_COMPAT, DISTRO_COMPAT_VERSION
  207. [ -f /etc/rc.d/BOOTCONFIG ]                 && . /etc/rc.d/BOOTCONFIG                  #has EXTRASFSLIST PREVUNIONRECORD, LASTUNIONRECORD (sfs stuff)
  208. [ -f /etc/xdg/menus/hierarchy ]             && . /etc/xdg/menus/hierarchy              #w478 has PUPHIERARCHY variable.
  209. [ -f $REPO_DIR/DISTRO_PKGS_SPECS ]    && . $REPO_DIR/DISTRO_PKGS_SPECS     #has lot sof essential info
  210. [ -f $REPO_DIR/PKGS_MANAGEMENT ]      && . $REPO_DIR/PKGS_MANAGEMENT       #has PKG_NAME_IGNORE, PKG_PET_THEN_BLACKLIST_COMPAT_KIDS, PKG_REPOS_ENABLED
  211. [ -f $REPO_DIR/DISTRO_COMPAT_REPOS ]  && . $REPO_DIR/DISTRO_COMPAT_REPOS   #has repo URL related vars
  212.  
  213. # set the package name suffix appended to combined pkgs (pkg+deps)
  214. CP_SUFFIX="WITHDEPS_${DISTRO_FILE_PREFIX}"
  215.  
  216. # set correct arch for repo URLs (used by some slack-pup repos)
  217. case "$DISTRO_TARGETARCH" in
  218.   x86)    DBIN_ARCH=i486                ;;
  219.   x86_64) DBIN_ARCH=x86_64 ; DSUFFIX=64 ;;
  220.   *)      DBIN_ARCH=i486                ;;
  221. esac
  222.  
  223. # needed for some debian based repos
  224. case $DISTRO_COMPAT_VERSION in
  225.   wheezy) DDB_COMP=bz2 ;; # older versions
  226.   *)      DDB_COMP=xz  ;;
  227. esac
  228.  
  229. # now create 'layers-installed': will contain builtins (and devx packages, if devx is loaded)
  230. #130511 need to include devx-only-installed-packages, if loaded...
  231. if which gcc &>/dev/null; then
  232.   [ ! -f /tmp/ppm-layers-installed-packages ] && cp -f "$WOOF_INST_PKGS_FILE" /tmp/ppm-layers-installed-packages &>/dev/null
  233.   cat "$PET_SPEC_DIR_DEFAULT/devx-only-installed-packages" >> /tmp/ppm-layers-installed-packages &>/dev/null
  234.   sort -u /tmp/ppm-layers-installed-packages > "$LAYER_INST_PKGS_FILE" &>/dev/null
  235. else
  236.   cp -f "$WOOF_INST_PKGS_FILE" "$LAYER_INST_PKGS_FILE" &>/dev/null
  237. fi
  238.  
  239. # set $DIRECTSAVEPATH (where we want to install pkgs)
  240. if [ $PUPMODE -eq 3 -o $PUPMODE -eq 7 -o $PUPMODE -eq 13 ]; then
  241.   DIRECTSAVEPATH="/initrd${SAVE_LAYER}" #SAVE_LAYER is in /etc/rc.d/PUPSTATE.
  242. elif [ "$PUPMODE" = "2" ]; then
  243.   DIRECTSAVEPATH=""
  244. fi
  245. # s243a: Need the real path to avoid overwriting sylinks. See:
  246. # http://murga-linux.com/puppy/viewtopic.php?p=1030958#1030958  
  247. # https://github.com/puppylinux-woof-CE/woof-CE/issues/1469#issuecomment-505706014
  248. [ -L "$DIRECTSAVEPATH" ] && DIRECTSAVEPATH="$(readlink "$DIRECTSAVEPATH")"
  249. # get repo details, workdir, search settings and so on.. you could
  250. # you can also add any ENVIRONMENT VARS above to PKGRC, to override the defaults
  251. . ${PKGRC}
  252. [ -z "$HTTPS_CHECK" ] && HTTPS_CHECK=all
  253. CHECKSUM_DIR=/var/packages/checksums
  254. export HTTPS_CHECK="$HTTPS_CHECK"
  255.  
  256. # set and create workdir if no valid dir set
  257. [ ! -d "$WORKDIR" ] && mkdir -p "$WORKDIR"
  258. if [ ! -d "$WORKDIR" ]; then
  259.   error "Can't create $WORKDIR. Please create it."
  260.   exit 3
  261. fi
  262. WORKDIR=~/pkg
  263.  
  264. # add to tab completion settings to bashrc and print hint
  265. if [ -z "$PKG_TAB_COMPLETION" ]; then
  266.   if [ "$(grep 'export PKG_TAB_COMPLETION=true' ~/.bashrc)" = "" ]; then
  267.     # add to bashrc
  268.     echo "" >> ~/.bashrc
  269.     echo "# enable $APPNAME $APPVER TAB completion" >> ~/.bashrc
  270.     echo "export PKG_TAB_COMPLETION=true" >> ~/.bashrc
  271.     echo ". /etc/bash_completion.d/pkg 2>/dev/null" >> ~/.bashrc
  272.   fi
  273. fi
  274.  
  275. # make tmp dir writable by everyone if needed
  276. if [ ! -d "$TMPDIR" ]; then
  277.   mkdir -p "$TMPDIR" 2>/dev/null
  278.   chmod -R 1777 /tmp/pkg/ 2>/dev/null
  279. fi
  280.  
  281. # if no tmp dir created or accessible, exit
  282. [ ! -d "$TMPDIR" ] && error "Cannot create temp dir ${lightblue}$TMPDIR${endcolour}" && exit 3
  283.  
  284. # support aliases, create ALIASES tmp file
  285. PKG_NAME_ALIASES='mozilla-firefox,firefox gtk+,gtk2 gtk2,gtk+2 dirac,schroedinger hunspell,myspell mp,mped mplayer,mplayer_*,mplayer-*,mplayer2,smplayer,gmplayer mrxvt,rxvt-unicode,xterm,urxvt,urxvt-unicode cxxlibs,glibc*,libc-* glibc-solibs,glibcsolibs alsalib,alsa-lib,alsa-lib2* alsautils,alsa-utils,alsa_utils,alsa-utils2* libungif,libgif,giflib zip,infozip dbus,libdbus*,libdbus-glib* hal,libhal* mesa,mesa_*,libgl1-mesa*,mesa-common* libxcb,libxcb_base sane,sane-backends samba,samba-tng,samba_*,mountcifs SDL_*,libsdl_* SDL,libsdl skype,skype_static util_linux_ng,util-linux-ng,util-linux,util_linux,utillinuxng vlc,vlc-nogui,vlc_nogui,VLC_Plus_Extras xf86-video-ati,xf86-video-ati-*,ati_fglrx xfdiff,xfdiff-cut xorg_base,xorg_base_t2,x11-common,x-dev,xorg,xorg73_base_t2 acl,libacl* xdg_puppy,xdg-utils perl_tiny,perl-base,perl-modules,perlapi* xorg-util-macros,util-macros portaudio,libportaudio jack-audio-connection-kit,libjack libjasper,jasper imlib2,libimlib2 imlib,libimlib'
  286. [ ! -f "$TMPDIR/pkg_aliases" ] && echo "$PKG_NAME_ALIASES" | tr ' ' '\n' > $TMPDIR/pkg_aliases
  287.  
  288. # get blacklisted packages (each on new line) from $PKG_NAME_IGNORE
  289. # and ~/.pkg/blacklisted_packages .. exports $PKG_BLACKLIST
  290. get_blacklist(){
  291.  
  292.   PKG_BLACKLIST="$(echo "$PKG_BLACKLIST $PKG_NAME_IGNORE $(cat ${HOME}/.pkg/blacklisted_packages 2>/dev/null)" \
  293.     | tr ' ' '\n' \
  294.     | grep -v ^$ \
  295.     | sort -u)"
  296.  
  297.  export PKG_BLACKLIST
  298. }
  299.  
  300. get_blacklist
  301. # create a list like "foo|bar|baz", to be used in grep commands
  302. PKG_BLACKLIST_REGEX="$(echo "$PKG_BLACKLIST" | tr '\n' '|' | sed -e 's/^|//' -e 's/|$//')"
  303. export PKG_BLACKLIST_REGEX
  304.  
  305. # remove error code flag if we got here
  306. rm -f /tmp/pkg/error130 &>/dev/null
  307.  
  308. # if not first run, and no options given, print the help
  309. [ ! -f ~/.pkg/firstrun -a "$1" = "" ] && $SELF -H && exit 1 #200913 more help by default
  310.  
  311. #==================  setup script vars  =====================#
  312.  
  313.  
  314.  
  315. #====================  main functions  ======================#
  316.  
  317. set -a
  318.  
  319. # utility funcs
  320.  
  321. print_usage(){                    # print usage text. Requires $1, a valid Pkg cmd FUNCLIST
  322.  
  323.  local examples_file=/usr/share/pkg/docs/examples.txt
  324.  
  325.  [ -f "$examples_file" ] && source "$examples_file"
  326.  
  327.  # get and print the usage info from its usage file
  328.  if [ -f /usr/share/pkg/docs/usage/$1 ] && [ "$1" ];then
  329.    source /usr/share/pkg/docs/usage/$1
  330.    echo -e "\n$USAGE"
  331.    # show examples, taken from $EXAMPLES, if they exist
  332.    if [ "$(echo "$EXAMPLES" | grep "$SELF" | grep "\-$1")" != '' ];then
  333.      echo -e "\n${bold}Usage Examples${endcolour}:\n"
  334.      echo -e "$EXAMPLES" | grep "$SELF " | grep "$1 "
  335.    fi
  336.  else
  337.    echo -e "Usage: ${bold}$SELF usage CMD${endcolour}"
  338.    echo
  339.    echo "Commands:"
  340.    echo -e "add-repo       get-only         repo-convert
  341. add-source     help             repo-dep-scope
  342. all-pkgs       help-all         repo-file-list
  343. add            remove           repo-info
  344. ask            install          repo-list
  345. autoclean      install-all      repo-pkg-scope
  346. bleeding-edge  list-deps        repo-update
  347. clean          list-downloaded  search
  348. contents       list-installed   search-all
  349. deb2pet        names            sfs2pet
  350. delete         names-all        sfs-combine
  351. delete-all     names-exact      show-config
  352. deps           names-exact-all  split
  353. deps-all       pet2sfs          tgz2pet
  354. deps-check     pet2tgz          uninstall
  355. deps-download  pet2txz          uninstall-all
  356. dir2pet        build            unpack
  357. dir2sfs        build-list       update-sources
  358. dir2tgz        pkg-combine      version
  359. download       installed        which
  360. examples       repack           which-repo
  361. extract        status           rdep-check
  362. force          update           what-needs
  363. func-list      workdir          rm-repo
  364. txz2pet        dir2repo         merge
  365. whitelist      blacklist
  366.  
  367. Usage: ${bold}$SELF usage CMD${endcolour}"
  368.    #cd /usr/share/pkg/docs/usage/; echo `ls` | fold -w 70 -s
  369.  fi
  370.  exit 1
  371. }
  372.  
  373.  
  374. func_list(){                      # list all functions available in this script FUNCLIST
  375.  [ -f $TMPDIR/func_list ] && sort $TMPDIR/func_list && exit 0
  376.  # shellcheck disable=SC2016
  377.  grep "(){" $0 | grep '#'    \
  378.    | grep FUNCLIST           \
  379.    | sed -e 's|^| |g'        \
  380.          -e 's|{||g'         \
  381.          -e 's|  | |g'       \
  382.          -e 's|# ||g'        \
  383.          -e 's| FUNCLIST||g' \
  384.    | grep -v 'FUNCLIST $0'   \
  385.    | sort > $TMPDIR/func_list
  386.  
  387.  cat $TMPDIR/func_list
  388.  exit 0
  389. }
  390.  
  391. update_system_cache() {
  392.  # $1 = PKGFILES [ex: ${REPO_DIR}/abiword.files]
  393.  local PKGFILES=$1
  394.  
  395.  if grep -q -m 1 '/usr/share/glib-2.0/schemas' $PKGFILES ;then
  396.    if [ -e /usr/bin/glib-compile-schemas ] ; then
  397.      /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
  398.    fi
  399.  fi
  400.  
  401.  if grep -q -m 1 '/usr/lib/gio/modules' $PKGFILES ;then
  402.    if [ -e /usr/bin/gio-querymodules ] ; then
  403.      /usr/bin/gio-querymodules /usr/lib/gio/modules
  404.    fi
  405.  fi
  406.  
  407.  if grep -q -m 1 '/usr/share/applications/' $PKGFILES ;then
  408.    if [ -e /usr/bin/update-desktop-database ] ; then
  409.      rm -f /usr/share/applications/mimeinfo.cache
  410.      /usr/bin/update-desktop-database /usr/share/applications
  411.    fi
  412.  fi
  413.  
  414.  if grep -q -m 1 '/usr/share/mime/' $PKGFILES ;then
  415.    if [ -e /usr/bin/update-mime-database ] ; then
  416.      /usr/bin/update-mime-database /usr/share/mime
  417.    fi
  418.  fi
  419.  
  420.  if grep -q -m 1 '/usr/share/icons/hicolor/' $PKGFILES ;then
  421.    if [ -e /usr/bin/gtk-update-icon-cache ] ; then
  422.      /usr/bin/gtk-update-icon-cache /usr/share/icons/hicolor
  423.    fi
  424.  fi
  425.  
  426.  if grep -q -m 1 '/usr/lib/gdk-pixbuf' $PKGFILES ;then
  427.    if [ -e /usr/bin/update-gdk-pixbuf-loaders ] ; then
  428.      update-gdk-pixbuf-loaders
  429.    elif [ -e /usr/bin/gdk-pixbuf-query-loaders ] ; then
  430.      gdk-pixbuf-query-loaders --update-cache
  431.    fi
  432.  fi
  433.  
  434.  if grep -q -m 1 '/usr/lib/gconv/' $PKGFILES ;then
  435.    iconvconfig
  436.  fi
  437.  
  438.  if grep -q -m 1 '/usr/lib/pango/' $PKGFILES; then
  439.    if [ -e /usr/bin/update-pango-querymodules ] ; then
  440.      update-pango-querymodules
  441.    elif [ -e /usr/bin/pango-querymodules ] ; then
  442.      pango-querymodules --update-cache
  443.    fi
  444.  fi
  445.  
  446.  if grep -m 1 "/usr/lib/gtk-2.0" $PKGFILES |grep -q "/immodules" ; then
  447.    if [ -e /usr/bin/update-gtk-immodules-2.0 ] ; then
  448.      update-gtk-immodules-2.0
  449.    elif [ -e /usr/bin/gtk-query-immodules-2.0 ] ; then
  450.      gtk-query-immodules-2.0 --update-cache
  451.    fi
  452.  fi
  453.  
  454.  if grep -m 1 "/usr/lib/gtk-3.0" $PKGFILES |grep -q "/immodules" ; then
  455.    if [ -e /usr/bin/update-gtk-immodules-3.0 ] ; then
  456.      update-gtk-immodules-3.0
  457.    elif [ -e /usr/bin/gtk-query-immodules-3.0 ] ; then
  458.      gtk-query-immodules-3.0 --update-cache
  459.    fi
  460.  fi
  461.  
  462.  if grep -q -m 1 '/usr/share/fonts/' $PKGFILES ;then
  463.    fc-cache -f
  464.  fi
  465.  
  466.  if grep -q -m 1 "/lib/modules/$(uname -r)/" $PKGFILES ;then
  467.    depmod -a
  468.  fi
  469. }
  470.  
  471.  
  472. get_pkg_ext(){                    # return file extension of $1 FUNCLIST
  473.  
  474.  # get valid usage or exit
  475.  [ ! "$1" ] && echo 'Usage: get_pkg_ext PKGNAME' && exit 1
  476.  
  477.  local pkg_installed=''
  478.  local pkg_filename=''
  479.  local pkg_ext=''
  480.  local pkg_name=''
  481.  
  482.  # check given file type, see if it matches our supported pkg types
  483.  case "$1" in
  484.    *.bz2)        echo bz2        ;;
  485.    *.pet)        echo pet        ;;
  486.    *.deb)        echo deb        ;;
  487.    *.pkg.tar.gz) echo pkg.tar.gz ;;
  488.    *.pkg.tar.xz) echo pkg.tar.xz ;;
  489.    *.pkg.tgz)    echo pkg.tgz    ;;
  490.    *.pkg.txz)    echo pkg.txz    ;;
  491.    *.tar.bz2)    echo tar.bz2    ;;
  492.    *.tar.lzma)   echo tar.lzma   ;;
  493.    *.tar.gz)     echo tar.gz     ;;
  494.    *.tar.xz)     echo tar.xz     ;;
  495.    *.tbz)        echo tbz        ;;
  496.    *.tgz)        echo tgz        ;;
  497.    *.tlz)        echo tlz        ;;
  498.    *.txz)        echo txz        ;;
  499.    *.gz)         echo gz         ;;
  500.    *.xz)         echo xz         ;;
  501.    *.rpm)        echo rpm        ;;
  502.    *.sfs)        echo sfs        ;;
  503.    #*.tcz)        echo tcz       ;;
  504.    #*.tpkg)       echo tpkg      ;;
  505.    #*.apk)        echo apk       ;;
  506.    *)
  507.      # no extension given, or extension not recognised, so..
  508.      # if it's an installed pkg, get the extension from ${REPO_DIR}/*
  509.      pkg_installed=$(list_installed_pkgs "$1" | grep -m1 "^$1")
  510.  
  511.      # if $pkg_check not empty, then $1 is an installed pkg
  512.      if [ "$pkg_installed" != '' ];then
  513.        pkg_filename=$(cut -f8 -d'|' ${REPO_DIR}/user-installed-packages |grep -m1 "^$1")
  514.        [ "$pkg_filename"  = '' ] && pkg_filename=$(cut -f8 -d'|' ${REPO_DIR}/*-installed-packages |grep -m1 "^$1")
  515.        [ "$pkg_filename" != '' ] && pkg_ext=$(get_pkg_ext "$pkg_filename")
  516.      else
  517.        # if it's a repo package, get it's extension from ${REPO_DIR}/*
  518.        # shellcheck disable=SC2086
  519.        pkg_filename=$(grep -m1 "^$1" ${REPO_DB_FILE_DIR}/Packages-* |cut -f8 -d'|')
  520.       [ "$pkg_filename" != '' ] && pkg_ext=$(get_pkg_ext "$pkg_filename")
  521.      fi
  522.      [ "$pkg_ext" != '' ] && echo $pkg_ext
  523.      ;;
  524.  esac
  525. }
  526.  
  527.  
  528. get_pkg_version(){                # returns version of PKGNAME ($1) FUNCLIST
  529.  
  530.  # exit if no pkg given
  531.  [ ! "$1" ] && exit 1
  532.  
  533.  local PKGNAME PKGNAME_ONLY PKG_VERSION
  534.  
  535.  PKGNAME=$(get_pkg_name "$1")
  536.  PKGNAME_ONLY=$(get_pkg_name_only "$PKGNAME")
  537.  
  538.  PKG_VERSION="$(grep -m1 "^$PKGNAME|" ${REPO_DIR}/user-installed-packages | cut -f3 -d'|')"
  539.  # cut pkg name only off start (field 3 of user-installed-packges might have full nanes, not versions)
  540.  PKG_VERSION="${PKG_VERSION/${PKGNAME_ONLY}${separator}/}"
  541.  
  542.  
  543.  if [ -z "$PKG_VERSION" ];then
  544.    PKG_VERSION="$(grep -m1 "^$PKGNAME|" "${REPO_DB_PATHS[@]}" | cut -f3 -d'|')" #TODO: maybe we don't want to search all repo db files.
  545.  fi
  546.  
  547.  if [ -z "$PKG_VERSION" ];then
  548.    PKG_VERSION="$(grep -m1 "|$PKGNAME_ONLY|" "${REPO_DB_PATHS[@]}" | cut -f3 -d'|')"
  549.  fi
  550.  
  551.  if [ -z "$PKG_VERSION" ];then
  552.    PKG_VERSION="$(grep -m1 "^$PKGNAME" "${REPO_DB_PATHS[@]}" | cut -f3 -d'|')"
  553.  fi
  554.  
  555.  if [ -z "$PKG_VERSION" ];then
  556.    PKG_VERSION="${PKGNAME/${PKGNAME_ONLY}${separator}/}"
  557.    # shellcheck disable=SC2006
  558.    PKG_VERSION=`echo  "$PKG_VERSION" \
  559.      | sed \
  560.        -e "s/^-//g" \
  561.        -e "s/^_//g" \
  562.        -e "s/^DEV-//g" \
  563.        -e "s/^DOC-//g" \
  564.        -e "s/^NLS-//g" \
  565.        -e "s/^dev-//g" \
  566.        -e "s/^doc-//g" \
  567.        -e "s/^nls-//g" \
  568.        -e "s/+deb.*//g" \
  569.        -e "s/+ubuntu.*//g" \
  570.        -e "s/-i[346].*//g" \
  571.        -e "s/-x[86].*//g" \
  572.        -e "s/-pup.*//g" \
  573.        -e "s/-q[0-9].*//g" \
  574.        -e "s/${CP_SUFFIX}.*//g" \
  575.        -e "s/-WITHDEPS.*//g" \
  576.        -e "s/-PLUSDEPS.*//g" \
  577.        -e "s/_all//g" \
  578.        -e 's@\\\@@g'`
  579.  fi
  580.  
  581.  [ ! -z "$PKG_VERSION" ] && echo "$PKG_VERSION" && return 0
  582.  return 1
  583. }
  584.  
  585.  
  586. get_pkg_name_only_from_ext(){     # return package name from $1 (must include file extension)
  587.  
  588.  # get valid usage or exit
  589.  [ ! "$1" ] && echo 'Usage: get_pkg_name_only_from_ext <PKG_FILENAME>' && exit 1
  590.  
  591.  # get the extension of the given package name, if any
  592.  local pkg_ext=$(get_pkg_ext "$1")
  593.  # get the name without path and extension
  594.  local pkg_name=$(basename "$1" ".$pkg_ext")
  595.  local PKGNAME=''
  596.  local PKGNAME_ONLY=''
  597.  local DB_pkgrelease=''
  598.  local prPATTERN=''
  599.  local DB_version=''
  600.  local xDB_version=''
  601.  local xPATTERN=''
  602.  
  603.  #split PKGMAIN, ex: FULLPKGNAME=xvidtune-1.0.1-i486-1.tgz has PKGNAME=xvidtune-1.0.1
  604.  case $pkg_ext in
  605.    deb)
  606.     #deb ex: xsltproc_1.1.24-1ubuntu2_i386.deb  xserver-common_1.5.2-2ubuntu3_all.deb
  607.     PKGNAME_ONLY="$(echo -n "$pkg_name" | cut -f 1 -d '_')"
  608.     DB_pkgrelease="$(echo -n "$pkg_name" | rev | cut -f 2 -d '_' | cut -f 1 -d '-' | rev)"
  609.     prPATTERN="s%\\-${DB_pkgrelease}.*%%"
  610.     PKGNAME="$(echo -n "$pkg_name" | sed -e "$prPATTERN" 2>/dev/null)"
  611.    ;;
  612.    pet)
  613.     PKGNAME="$pkg_name"
  614.     DB_version="$(echo -n "$PKGNAME" | grep -o '\\-[0-9].*' | sed -e 's%^\-%%')"
  615.     xDB_version="$(echo -n "$DB_version" | sed -e 's%\\-%\\\\-%g' -e 's%\\.%\\\\.%g')"
  616.     xPATTERN="s%${xDB_version}%%"
  617.     PKGNAME_ONLY="$(echo -n "$PKGNAME" | sed -e "$xPATTERN" -e 's%\\-$%%' 2>/dev/null)"
  618.    ;;
  619.    tgz|txz)
  620.     #slack ex: xvidtune-1.0.1-i486-1.tgz  printproto-1.0.4-noarch-1.tgz
  621.     PKGNAME="$(echo -n "$pkg_name" | sed -e 's%\\-i[3456]86.*%%' -e 's%\\-noarch.*%%')"
  622.     DB_version="$(echo -n "$PKGNAME" | grep -o '\\-[0-9].*' | sed -e 's%^\\-%%')"
  623.     xDB_version="$(echo -n "$DB_version" | sed -e 's%\\-%\\\\-%g' -e 's%\\.%\\\\.%g')"
  624.     xPATTERN="s%${xDB_version}%%"
  625.     PKGNAME_ONLY="$(echo -n "$PKGNAME" | sed -e "$xPATTERN" -e 's%\-$%%' 2>/dev/null)"
  626.    ;;
  627.    tar.gz)
  628.     #arch ex: xproto-7.0.14-1-i686.pkg.tar.gz  trapproto-3.4.3-1.pkg.tar.gz
  629.     PKGNAME="$(echo -n "$pkg_name" | sed -e 's%\\-i[3456]86.*%%' -e 's%\\.pkg$%%' | rev | cut -f 2-9 -d '-' | rev)"
  630.     DB_version="$(echo -n "$PKGNAME" | grep -o '\\-[0-9].*' | sed -e 's%^\\-%%')"
  631.     xDB_version="$(echo -n "$DB_version" | sed -e 's%\\-%\\\\-%g' -e 's%\\.%\\\\.%g')"
  632.     xPATTERN="s%${xDB_version}%%"
  633.     PKGNAME_ONLY="$(echo -n "$PKGNAME" | sed -e "$xPATTERN" -e 's%\\-$%%' 2>/dev/null)"
  634.    ;;
  635.    rpm) #110523
  636.     #exs: hunspell-fr-3.4-1.1.el6.noarch.rpm
  637.     PKGNAME="$pkg_name"
  638.     DB_version="$(echo -n "$PKGNAME" | grep -o '\\-[0-9].*' | sed -e 's%^\-%%')"
  639.     xDB_version="$(echo -n "$DB_version" | sed -e 's%\\-%\\\\-%g' -e 's%\\.%\\\\.%g')"
  640.     xPATTERN="s%${xDB_version}%%"
  641.     PKGNAME_ONLY="$(echo -n "$PKGNAME" | sed -e "$xPATTERN" -e 's%\\-$%%' 2>/dev/null)"
  642.    ;;
  643.  esac
  644.  
  645.  PKGNAME_ONLY="${PKGNAME_ONLY//.pet/}"
  646.  PKGNAME_ONLY="${PKGNAME_ONLY//\.${EX:-noextension}/}"
  647.  PKGNAME_ONLY="${PKGNAME_ONLY//\.${pkg_ext:-noextension}/}"
  648.  PKGNAME_ONLY="${PKGNAME_ONLY//_amd64/}"
  649.  PKGNAME_ONLY="${PKGNAME_ONLY//_arm64/}"
  650.  PKGNAME_ONLY="${PKGNAME_ONLY//_armel/}"
  651.  PKGNAME_ONLY="${PKGNAME_ONLY//_armhf/}"
  652.  PKGNAME_ONLY="${PKGNAME_ONLY//_i[3-6]86/}"
  653.  PKGNAME_ONLY="${PKGNAME_ONLY//_x84_64/}"
  654.  PKGNAME_ONLY="${PKGNAME_ONLY//_all/}"
  655.  
  656.  # remove the +20080738 from packages with dates in the version
  657.  PKGNAME_ONLY="$(echo "$PKGNAME_ONLY" | sed -e 's/+19.*//g' -e 's/+20.*//g')"
  658.  
  659.  [ "$PKGNAME_ONLY" != '' ] && echo "$PKGNAME_ONLY" || echo "$1"
  660. }
  661.  
  662.  
  663. get_pkg_name_only(){              # return pkg name only from $1, with no version or suffix FUNCLIST
  664.  
  665.  # exit if no valid options
  666.  [ ! "$1" ] && exit 1
  667.  
  668.  # get config settings, inc current repo file
  669.  #. ${PKGRC}
  670.  
  671.  local pkg_name=''
  672.  local pkg_name_only=''
  673.  local name_only=''
  674.  local pkg_ext=`get_pkg_ext "$1"`
  675.  local repo_of_pkg=''
  676.  local repo_pkg_with_ext=''
  677.  local repo_ext=$EX      # from $PKGRC
  678.  
  679.  # check the repos
  680.  local pkg_name_only="$(grep -m1 "|${1}|" "${REPO_DB_PATHS[@]}" 2>/dev/null | cut -f2 -d'|')"
  681.  [ "$pkg_name_only" = '' ] && pkg_name_only="$(grep -m1 "^${1}|" "${REPO_DB_PATHS[@]}" 2>/dev/null | cut -f2 -d'|' | head -1)"
  682.  [ "$pkg_name_only" = '' ] && pkg_name_only="$(grep -m1 "|${1}.${pkg_ext}|" "${REPO_DB_PATHS[@]}" 2>/dev/null | cut -f2 -d'|' | head -1)"
  683.  
  684.  pkg_name_only=${pkg_name_only// */}
  685.  
  686.  # if we found it, print it and exit
  687.  if [ "$pkg_name_only" != '' ]; then
  688.      echo ${pkg_name_only}
  689.      return 0
  690.  fi
  691.  
  692.  # if not, but we have an extension, try  get_pkg_name_only_from_ext()
  693.  if [ ! -z "$pkg_ext" ] || [ ! -z "$EX" ];then
  694.    case $DISTRO_BINARY_COMPAT in
  695.      ubuntu|trisquel|debian|devuan)
  696.        name_only=$(get_pkg_name_only_from_ext "$(basename "${1}.${pkg_ext:-$EX}")"| head -1)
  697.  
  698.         name_only="${name_only// */}"
  699.         name_only="${name_only//.pet/}"
  700.         name_only="${name_only//\.${EX:-noextension}/}"
  701.         name_only="${name_only//\.${pkg_ext:-noextension}/}"
  702.         name_only="${name_only//_amd64/}"
  703.         name_only="${name_only//_arm64/}"
  704.         name_only="${name_only//_armel/}"
  705.         name_only="${name_only//_armhf/}"
  706.         name_only="${name_only//_i[3-6]86/}"
  707.         name_only="${name_only//_x84_64/}"
  708.         name_only="${name_only//_all/}"
  709.  
  710.         [ "$name_only" != '' ] && [ "$name_only" != "$1" ] && echo "$name_only" && return 0
  711.         ;;
  712.     esac
  713.   fi
  714.  
  715.   ## ...if we didn't find the name using the above, do the horrible checks below
  716.  
  717.   # replace the dash before the version number with an @ symbol.
  718.   # INFO: sed /-[^-][^+][^a-zA-Z][0-9.]*/ (hopefully?) replaces '-$VER' with '@'
  719.   # not including when $VER starts with a number/letter
  720.   pkg_name_only="$(basename "${1/-[^-][^+][^a-zA-Z][0-9.]*/@}")"
  721.  
  722.   # do 'ioquake3+u20130504' => 'oquake3'
  723.   pkg_name_only="${pkg_name_only/+[Aa-Zz]/@}"
  724.  
  725.   # note, we haven't cut away version numbers preceeded with underscores (_) yet
  726.  
  727.   # if the version number was preceeded by an underscore, the version
  728.   # will still be in the $pkg_name_only - pkgname_2.3.4 - so cut it out
  729.   if [ "$(echo "$pkg_name_only" | grep -o "_[0-9]\.")" != '' ];then
  730.     pkg_name_only="${pkg_name_only/_[^a-z][^A-Z][0-9.]*/@}"
  731.  
  732.   # maybe the underscore preceeds a version that starts with a letter
  733.   elif [ "$(echo "$pkg_name_only" | grep -o "_[a-zA-Z][0-9]\.")" != '' ];then
  734.     pkg_name_only="${pkg_name_only/_[a-zA-Z][0-9.]*/@}"
  735.   fi
  736.  
  737.   # now cut away everything after the @, that will leave only the package name
  738.   pkg_name_only1="${pkg_name_only/@*/}"
  739.   pkg_name_only="$pkg_name_only1"
  740.  
  741.   # we might still have foo_bar_v1.2.3, so lets chop off * after the last _
  742.   if [ "$(echo "$pkg_name_only" | grep -o "_[a-zA-Z][0-9]\.")" != '' ];then
  743.     pkg_name_only="${pkg_name_only/_[a-zA-Z][0-9]*/}"
  744.   fi
  745.  
  746.   # another chop, we might have foo_bar_vv1.2.3, so lets chop off
  747.   # everything after the last underscore, if we still have version numbers
  748.   if [ "$(echo "$pkg_name_only" | grep -o "_[a-zA-Z][a-zA-Z][0-9]\.")" != '' ];then
  749.     pkg_name_only="${pkg_name_only/_[a-zA-Z][a-zA-Z][0-9]*/}"
  750.   fi
  751.  
  752.   # we might still have foo_bar_vvv1.2.3, so lets chop off
  753.   # everything after the last underscore, if we still have version numbers
  754.   if [ "$(echo "$pkg_name_only" | grep -o "_[a-zA-Z*][0-9]\.")" != '' ];then
  755.     pkg_name_only="${pkg_name_only/_[a-zA-Z*][0-9]*/}"
  756.   fi
  757.  
  758.   # chop again, we might have abc_xwy-zzz1.2.3-blah-etc, so lets chop off
  759.   # everything after the last dash-before-number
  760.   if [ "$(echo "$pkg_name_only" | grep -o "\-[a-zA-Z*]*[0-9]\.")" != '' ];then
  761.     pkg_name_only="${pkg_name_only/-[a-zA-Z*]*[0-9]*/}"
  762.   fi
  763.  
  764.   # another fix, if we still have PKGNAME-1.2, remove the '-1.2'
  765.   if [ "$(echo "$pkg_name_only" | grep -o "\-[0-9]")" != '' ];then
  766.     pkg_name_only="${pkg_name_only/-[0-9]*/}"
  767.   fi
  768.  
  769.   # remove the +20080738 from packages with dates in the version
  770.   pkg_name_only="$(echo "$pkg_name_only" | sed -e 's/\+19.*//g' -e 's/\+20.*//g')"
  771.  
  772.   pkg_name_only="${pkg_name_only//.pet/}"
  773.   pkg_name_only="${pkg_name_only//\.${EX:-noextension}/}"
  774.   pkg_name_only="${pkg_name_only//\.${pkg_ext:-noextension}/}"
  775.   pkg_name_only="${pkg_name_only//_amd64/}"
  776.   pkg_name_only="${pkg_name_only//_arm64/}"
  777.   pkg_name_only="${pkg_name_only//_armel/}"
  778.   pkg_name_only="${pkg_name_only//_armhf/}"
  779.   pkg_name_only="${pkg_name_only//_i[3-6]86/}"
  780.   pkg_name_only="${pkg_name_only//_x84_64/}"
  781.   pkg_name_only="${pkg_name_only//_all/}"
  782.  
  783.   # the sed bit changes 'liblzma5+20120614' to 'liblzma5'
  784.   # shellcheck disable=SC2001
  785.   [ "$pkg_name_only" != '' ] && echo "$pkg_name_only" | sed -e 's/\+[a-z0-9].*//g' || return 1
  786. }
  787. check_md5sum(){
  788.     local file_path="$1"
  789.     local repo_filename="$(basename $2)"
  790.     local md5sums_path="$HASHES_DIR/${repo_filename//Packages/MD5}"
  791.     local fname="$(basename "$md5sums_path")"
  792.     local fsum="$(md5sum "$file_path" | cut -f1 -d' ')"
  793.     #for aRepoFile in "$USER_INST_PKGS_FILE" "$WOOF_INST_PKGS_FILE" "${REPO_DB_PATHS[@]}"; do
  794.     #  md5sum "$file_path"
  795.     #done
  796.     #if [ -z "$(cut -f4 "$md5sums_path" | grep fsum -m1)" ]; then
  797.     if [ -z "$(grep "$fsum" "$md5sums_path" | cut -f1)" ]; then
  798.       return 1
  799.     else
  800.       return 0
  801.     fi
  802. }
  803. check_SHA256(){
  804.     local file_path="$1"
  805.     local repo_filename="$(basename $2)"
  806.     local SHA256_path="$HASHES_DIR/${repo_filename//Packages/SHA256}"
  807.     local fname="$(basename "$SHA256_path")"
  808.     local fsum="$(sha256sum "$file_path" | cut -f1 -d' ')"
  809.     #for aRepoFile in "$USER_INST_PKGS_FILE" "$WOOF_INST_PKGS_FILE" "${REPO_DB_PATHS[@]}"; do
  810.     #  md5sum "$file_path"
  811.     #done
  812.     #if [ -z "$(cut -f4 "$md5sums_path" | grep fsum -m1)" ]; then
  813.     if [ -z "$(grep "$fsum" "$SHA256_path" | cut -f1)" ]; then
  814.       return 1
  815.     else
  816.       return 0
  817.     fi
  818. }
  819. get_repo_contents_entry(){
  820.  
  821.   # get config settings, inc current repo file
  822.   . ${PKGRC}
  823.  
  824.   local pkg_ext=''
  825.   local pkg_name=''
  826.   local full_name=''
  827.   local supported_repo_files=''
  828.  
  829.   # get the extension of the given package name, if any
  830.   local pkg_ext=$(get_pkg_ext "$1")
  831.   # get the name without path and extension
  832.   local pkg_name="$(basename "$1" .${pkg_ext:-$EX})"
  833.  
  834.   # get the repos files in the correct (fall back) order, then add file paths
  835.   supported_repo_files="$(repo_file_list | sed -e "s#^#${REPO_DB_FILE_DIR}/#g")"
  836.  
  837.   # get the relevant repo contents (fields 1,2,8) from all repos,
  838.   # then add pipes to line start/end for easier parsing later, then do
  839.   # a basic search, for $pkg* (we'll refine it later), to avoid 'Argument list too long'..
  840.   cut -f1,2,8 $supported_repo_files ${REPO_DIR}/woof-installed-packages \
  841.     | sed \
  842.       -e "s/^/|/g" \
  843.       -e "s/$/|/g" 2>/dev/null \
  844.     | grep -i "|$pkg_name" > ${TMPDIR}/repo_contents
  845.  
  846.  
  847.   # get fields 1,2 and 8 (the 3 name fields) from all supported repo files, then
  848.   # add '|' to start and end of lines, then find exact match of $pkg_name..
  849.   # if no exact match, look for $pkg_name-*, then $pkg_name_*
  850.   if [ -f ${TMPDIR}/repo_contents ];then
  851.  
  852.       repo_contents_entry=$(  grep -m1 "|${pkg_name}|"      "${TMPDIR}/repo_contents" 2>/dev/null)  || \
  853.       repo_contents_entry=$(grep -m1 "|${pkg_name//-*/}|" "${TMPDIR}/repo_contents" 2>/dev/null)  || \
  854.       repo_contents_entry=$(grep -m1 "|${pkg_name//_*/}|" "${TMPDIR}/repo_contents" 2>/dev/null)  || \
  855.       repo_contents_entry=$(grep -m1 "|${pkg_name}-"      "${TMPDIR}/repo_contents" 2>/dev/null)  || \
  856.       repo_contents_entry=$(grep -m1 "|${pkg_name}_"      "${TMPDIR}/repo_contents" 2>/dev/null)  || \
  857.       repo_contents_entry=$(grep -m1 "|${pkg_name}"       "${TMPDIR}/repo_contents" 2>/dev/null)  || \
  858.       repo_contents_entry=$(grep -m1 "^${pkg_name}|"      "${TMPDIR}/repo_contents" 2>/dev/null)  || \
  859.       repo_contents_entry=$(grep -m1 "^${pkg_name}-"      "${TMPDIR}/repo_contents" 2>/dev/null)  || \
  860.       repo_contents_entry=$(grep -m1 "^${pkg_name}_"      "${TMPDIR}/repo_contents" 2>/dev/null)   
  861.       echo "${repo_contents_entry}"
  862.   fi #TODO verify whether or not we acctually need this if statment
  863. }
  864. function strip_arch_and_ex(){
  865.       local full_name="$1"
  866.       [ -z "$full_name" ] && read full_name
  867.       strip_d_ex "$full_name" | strip_arch
  868. }
  869. function strip_d_ex(){
  870.       local full_name="$1"
  871.       [ -z "$full_name" ] && read full_name
  872.       full_name="${full_name//.pet/}"
  873.       full_name="${full_name//.${EX:-noextension}/}"
  874.       full_name="${full_name//.${pkg_ext:-noextension}/}"  
  875.       echo "$full_name"
  876. }
  877. function strip_arch(){
  878.       local full_name="$1"
  879.       [ -z "$full_name" ] && read full_name
  880.       full_name="${full_name//_amd64/}"
  881.       full_name="${full_name//_arm64/}"
  882.       full_name="${full_name//_armel/}"
  883.       full_name="${full_name//_armhf/}"
  884.       full_name="${full_name//_i[3-6]86/}"
  885.       full_name="${full_name//_x84_64/}"
  886.       full_name="${full_name//_all/}"  
  887.       echo "$full_name"
  888. }
  889. get_pkg_name(){                   # return full pkg name (with version) from $1 FUNCLIST
  890.  
  891.   # get config settings, inc current repo file
  892.   . ${PKGRC}
  893.  
  894.   local pkg_ext=''
  895.   local pkg_name=''
  896.   local full_name=''
  897.   local supported_repo_files=''
  898.  
  899.   # get the extension of the given package name, if any
  900.   local pkg_ext=$(get_pkg_ext "$1")
  901.   # get the name without path and extension
  902.   local pkg_name="$(basename "$1" .${pkg_ext:-$EX})"
  903.   shift
  904.   while [ $# -gt 0 ]; do
  905.     local no_delete="${2:-false}"  
  906.     case "$1" in
  907.     --no_delete) local no_delete="true"; shift 1; ;;
  908.     --global_returns) local global_returns=false; shift 1; ;;
  909.     --rtn_var_name) local rtn_var_name=$1; shift 2; ;;
  910.     esac
  911.   done
  912.   local no_delete="${no_delete:-false}"  
  913.   local global_returns="${global_returns:-false}"
  914.   [ "$global_returns" = true ] && local rtn_var_name=
  915.   # get the repos files in the correct (fall back) order, then add file paths
  916.   supported_repo_files="$(repo_file_list | sed -e "s#^#${REPO_DB_FILE_DIR}/#g")"
  917.  
  918.   # get the relevant repo contents (fields 1,2,8) from all repos,
  919.   # then add pipes to line start/end for easier parsing later, then do
  920.   # a basic search, for $pkg* (we'll refine it later), to avoid 'Argument list too long'..
  921.   cut -f1,2,8 $supported_repo_files ${REPO_DIR}/woof-installed-packages \
  922.     | sed \
  923.       -e "s/^/|/g" \
  924.       -e "s/$/|/g" 2>/dev/null \
  925.     | grep -i "|$pkg_name" > ${TMPDIR}/repo_contents"_${pkg_name}"
  926.  
  927.  
  928.   # get fields 1,2 and 8 (the 3 name fields) from all supported repo files, then
  929.   # add '|' to start and end of lines, then find exact match of $pkg_name..
  930.   # if no exact match, look for $pkg_name-*, then $pkg_name_*
  931.   if [ -f ${TMPDIR}/repo_contents"_${pkg_name}" ]; then
  932.   # get fields 1,2 and 8 (the 3 name  ];then
  933.       full_name="$(get_repo_contents_entry "${pkg_name}" | cut -d '|' -f2 | strip_arch_and_ex)"
  934.       #rm ${TMPDIR}/repo_contents"_${pkg_name}" &>/dev/null
  935.       if [ "$global_return" = true ]; then
  936.         eval "${rtn_var_name}=${full_name}"
  937.       else
  938.         echo "${full_name}"
  939.         return 0 #TODO: probably can't return 0, if we implement global returns later.
  940.      fi
  941.   else
  942.     # if no pkg found in repos, we cant translate/lookup its longer name,
  943.     # so just return what we got, without path, without pkg extension
  944.     full_name="$(strip_arch_and_ex "$pkg_name")"  
  945.     full_name="$(basename "$pkg_name" .${pkg_ext:-${EX:-noextension}})"
  946.       if [ "$global_return" = true ]; then
  947.         eval "${rtn_var_name}=${full_name}"
  948.       else
  949.         echo "${full_name}"
  950.         return 0 #TODO: probably can't return 0, if we implement global returns later.
  951.      fi
  952.   fi
  953. }
  954.  
  955.  
  956. get_pkg_filename(){              # return filename of $1, if its a local file or repo package FUNCLIST
  957.   # exit if no valid options
  958.   [ ! "$1" ] || [ "$1" = "-" ] && exit 1
  959.  
  960.   if [ -f "$1" ];then
  961.     basename "$1"
  962.     return 0
  963.   fi
  964.  
  965.   # strip away extension and path
  966.   local PKGNAME="$(basename "$1" .$EX)"
  967.   local PKGNAME=$(get_pkg_name "$PKGNAME")
  968.   local PKGNAME_ONLY=$(get_pkg_name_only "$PKGNAME")
  969.  
  970.   # check repos in fallback order
  971.   all_supported_repofiles=$(repo_file_list | sed "s#^#${REPO_DB_FILE_DIR}/#" | tr '\n' ' ') #TODO use something like: ALL_REPO_DB_PATHS
  972.  
  973.   local pkgname
  974.  
  975.   for repofile in ${REPO_DB_FILE_DIR}/${REPOFILE} $REPO_DIR/*-installed-packages $all_supported_repofiles
  976.   do
  977.     pkgname="$(grep -m1 "^$PKGNAME|" "$repofile" | cut -f8 -d'|')"
  978.     [ ! -z "$pkgname" ] && break
  979.   done
  980.  
  981.   if [ -z "$pkgname" ];then
  982.     for repofile in ${REPO_DIR}/${REPOFILE} $REPO_DIR/*-installed-packages $all_supported_repofiles
  983.     do
  984.       pkgname="$(grep -m1 "|$PKGNAME_ONLY|" "$repofile" | cut -f8 -d'|')"
  985.       [ ! -z "$pkgname" ] && break
  986.     done
  987.   fi
  988.  
  989.   [ ! -z "$pkgname" ] && echo "$pkgname" | grep -v ^$ | head -1
  990. }
  991.  
  992. get_pkg_category(){
  993.   # $1 must be PKGNAME_ONLY ('gimp', 'vlc', etc)
  994.   [ -z "$1" ] && return 1
  995.  
  996.   local category=''
  997.   category="$(grep -i -m1 " ${1} " /usr/local/petget/categories.dat \
  998.    | cut -f1 -d'='  \
  999.    | cut -f2- -d'_' \
  1000.    | tr '_' ';')"
  1001.  
  1002.   # do this in a sub shell
  1003.   (
  1004.     \cd $REPO_DIR/ &>/dev/null || exit 1
  1005.  
  1006.     if [ -z "$category" ];then
  1007.       # search the repo files
  1008.       category="$(cut -f1,2,5 -d'|' $(repo_file_list) \
  1009.        | grep -i -m1 "^${1}.*|${1}|" \
  1010.        | cut -f3 -d'|')"
  1011.     fi
  1012.     [ ! -z "$category" ] && echo "$category" | grep -v ^$
  1013.   )
  1014. }
  1015.  
  1016. is_blacklisted_pkg(){             # return true if PKGNAME ($1) is blacklisted FUNCLIST
  1017.   # exit if no valid options
  1018.   [ ! "$1" ] || [ "$1" = "-" ] && exit 1
  1019.  
  1020.   # if we find an exact match of PKGNAME_ONLY in $PKG_BLACKLIST
  1021.   if [ "$(echo "$PKG_BLACKLIST" | grep -E "^$(get_pkg_name_only "$1")\$")" != '' ];then
  1022.    echo true
  1023.    return 0
  1024.  fi
  1025.  echo false
  1026.  return 1
  1027. }
  1028.  
  1029.  
  1030. is_installed_pkg(){               # return true if PKGNAME ($1) is installed, else false FUNCLIST
  1031.  
  1032.  # exit if no valid options
  1033.  [ ! "$1" ] || [ "$1" = "-" ] && print_usage pkg-installed && exit 1
  1034.  
  1035.  local EX=''
  1036.  local PKGNAME=''
  1037.  local PKGNAME_ONLY=''
  1038.  local PKGFILENAME=''
  1039.  local PKGFILENAME_NO_EX=''
  1040.  local check=''
  1041.  local repo_files="${REPO_DIR}/woof-installed-packages ${REPO_DIR}/user-installed-packages"
  1042.  
  1043.  # get pkg extension
  1044.  EX=$(get_pkg_ext "$1")
  1045.  
  1046.  # strip away extension and path
  1047.  PKGNAME="$(basename "$1" .$EX)"
  1048.  PKGNAME=$(get_pkg_name "$PKGNAME")
  1049.  
  1050.  # we cant rely on the strings the user passes us, so...
  1051.  [ -z "$check" ] && check="$(find ${PACKAGE_FILE_LIST_DIR}/ -name "${PKGNAME}.files")"
  1052.  [ -z "$check" ] && [ "${PACKAGE_FILE_LIST_DIR}" != "${BUILTIN_FILE_LIST_DIR}" ] && \
  1053.       check="$(find ${BUILTIN_FILE_LIST_DIR}/ -name "${PKGNAME}.files")"
  1054.  
  1055.  PKGFILENAME="$(get_pkg_filename ${PKGNAME})"
  1056.  PKGFILENAME_NO_EX="${PKGFILENAME//.$EX/}"
  1057.  [ -z "$check" ] && check="$(find ${REPO_DIR}/ -name "${PKGFILENAME_NO_EX}.files" -print -quit)"
  1058.  [ -z "$check" ] && check="$(cut -f1,2 -d'|' $repo_files | grep -m1 "^$PKGNAME|")"
  1059.  [ -z "$check" ] && check="$(cut -f8   -d'|' $repo_files | grep -m1 "^${PKGNAME}.$EX")"
  1060.  [ -z "$check" ] && check="$(cut -f8   -d'|' $repo_files | grep -m1 "^${PKGFILENAME}.deb)")"
  1061.  
  1062.  PKGNAME_ONLY=$(get_pkg_name_only "$PKGNAME")
  1063.  [ -z "$check" ] && check="$(cut -f1,2 -d'|' $repo_files | grep -m1 "^$PKGNAME" | grep -m1 "|$PKGNAME_ONLY\$")"
  1064.  
  1065.  # if any checks above returned a result, $check will not be empty (ash didn't like grep -q, not sure why?)
  1066.  [ ! -z "$check" ] && echo true || echo false
  1067. }
  1068.  
  1069.  
  1070. is_builtin_pkg (){                # return true if $1 is a builtin pkg, else false FUNCLIST
  1071.  
  1072.  # if $1 is an exact match of a pkg short name,
  1073.  # long name or file name in woof-installed-packages,
  1074.  # we will return true, else we return false
  1075.  
  1076.  # exit if no valid input
  1077.  [ ! "$1"  ] && exit 1
  1078.  
  1079.  local pkg_builtin=''
  1080.  local pkg_name_only=''
  1081.  
  1082.  pkg_builtin="$(cut -f1,2,8 -d '|' ${REPO_DIR}/woof-installed-packages 2>/dev/null \
  1083.     | sed -e 's@^@|@' -e 's@$@|@' \
  1084.     | grep -m1 "|$1|")"
  1085.  
  1086.  # try again.. if user gave only partial name (geany-1.27), the above would fail
  1087.  if [ "$pkg_builtin" = '' ];then
  1088.    pkg_name_only=$(get_pkg_name_only "$1")
  1089.  
  1090.    # so search for pkg_name* AND pkg_name_only (exact match, should be in field 2)
  1091.    if [ "$pkg_name_only" != '' ];then
  1092.      pkg_builtin="$(cut -f1,2,8 -d '|' ${REPO_DIR}/woof-installed-packages 2>/dev/null \
  1093.         | sed -e 's@^@|@' -e 's@$@|@' \
  1094.         | grep "|$1" \
  1095.         | grep -m1 "|$pkg_name_only|")"
  1096.    fi
  1097.  fi
  1098.  
  1099.  # print result
  1100.  [ "$pkg_builtin" != '' ] && echo true || echo false
  1101. }
  1102.  
  1103.  
  1104. is_devx_pkg (){                   # return true if $1 is a pkg in the devx, else false FUNCLIST
  1105.  
  1106.  # if $1 is an exact match of a pkg short name,
  1107.  # long name or file name in devx-only-installed-packages,
  1108.  # we will return true, else we return false
  1109.  
  1110.  # exit if no valid input
  1111.  [ ! "$1"  ] && exit 1
  1112.  
  1113.  local devx_pkg=''
  1114.  local pkg_name_only=''
  1115.  
  1116.  devx_pkg="$(cut -f1,2,8 -d '|' ${REPO_DIR}/devx-only-installed-packages 2>/dev/null\
  1117.     | sed -e 's@^@|@' -e 's@$@|@' \
  1118.     | grep -m1 "|$1|")"
  1119.  
  1120.  # try again.. if user gave only partial name (geany-1.27), the above would fail
  1121.  if [ "$devx_pkg" = '' ];then
  1122.    pkg_name_only=$(get_pkg_name_only "$1")
  1123.  
  1124.    # so search for pkg_name* AND |pkg_name_only| (exact match, should be in field 2)
  1125.    if [ "$pkg_name_only" != '' ];then
  1126.      devx_pkg="$(cut -f1,2,8 -d '|' ${REPO_DIR}/devx-only-installed-packages 2>/dev/null\
  1127.         | sed -e 's@^@|@' -e 's@$@|@' \
  1128.         | grep "|$1" \
  1129.         | grep -m1 "|$pkg_name_only|")"
  1130.    fi
  1131.  fi
  1132.  
  1133.  # print result
  1134.  [ "$devx_pkg" != '' ] && echo true || echo false
  1135. }
  1136.  
  1137.  
  1138. is_usr_pkg (){                    # return true if $1 is a user installed pkg, else false FUNCLIST
  1139.  
  1140.  # if $1 is an exact match of a pkg short name,
  1141.  # long name or file name in user-installed-packages,
  1142.  # we will return true, else we return false
  1143.  
  1144.  # exit if no valid input
  1145.  [ ! "$1"  ] && exit 1
  1146.  
  1147.  local usr_pkg=''
  1148.  local pkg_name_only=''
  1149.  
  1150.  usr_pkg="$(cut -f1,2,8 -d '|' ${REPO_DIR}/user-installed-packages 2>/dev/null \
  1151.     | sed -e 's@^@|@' -e 's@$@|@' \
  1152.     | grep -m1 "|$1|")"
  1153.  
  1154.  # try again.. if user gave only partial name (geany-1.27), the above would fail
  1155.  if [ "$usr_pkg" = '' ];then
  1156.    pkg_name_only=$(get_pkg_name_only "$1")
  1157.  
  1158.    # so search for pkg_name* AND |pkg_name_only| (exact match, should be in field 2)
  1159.    if [ "$pkg_name_only" != '' ];then
  1160.      usr_pkg="$(cut -f1,2,8 -d '|' ${REPO_DIR}/user-installed-packages 2>/dev/null \
  1161.         | sed -e 's@^@|@' -e 's@$@|@' \
  1162.         | grep "|$1" \
  1163.         | grep -m1 "|$pkg_name_only|")"
  1164.    fi
  1165.  fi
  1166.  
  1167.  # print result
  1168.  [ "$usr_pkg" != '' ] && echo true || echo false
  1169. }
  1170.  
  1171.  
  1172. is_repo_pkg (){                   # return true if $1 is a pkg in a supported repo, else false FUNCLIST
  1173.  
  1174.  # if $1 is an exact match of a pkg short name,
  1175.  # long name or file name in Packages-*-
  1176.  # we will return true, else we return false
  1177.  
  1178.  # exit if no valid input
  1179.  [ ! "$1"  ] && exit 1
  1180.  
  1181.  local repo_pkg=''
  1182.  local pkg_name_only=''
  1183.  local all_supported_repofiles
  1184.  
  1185.  # all repo files, full paths
  1186.  all_supported_repofiles=$(repo_file_list | sed "s#^#${REPO_DB_FILE_DIR}/#" | tr '\n' ' ')
  1187.  
  1188.  # search all repo files for the $1
  1189.  repo_pkg="$(cut -f1,2,8 -d '|' $all_supported_repofiles 2>/dev/null\
  1190.     | sed -e 's/ //g' -e 's@^@|@' -e 's@$@|@' \
  1191.     | grep -m1 "|${1}|")"
  1192.  
  1193.  # search all repo files for the $1*
  1194.  [ "$repo_pkg" = '' ] \
  1195.    && repo_pkg="$(cut -f1,2,8 -d '|' $all_supported_repofiles 2>/dev/null\
  1196.     | sed -e 's/ //g' -e 's@^@|@' -e 's@$@|@' \
  1197.     | grep -m1 "|${1}-\?_\?[0-9.a-zA-Z]*|")"
  1198.  
  1199.  # try again.. if user gave only partial name (geany-1.27), the above would fail
  1200.  if [ "$repo_pkg" = '' ];then
  1201.    pkg_name_only=$(get_pkg_name_only "$1")
  1202.  
  1203.    # so search for pkg_name* AND |pkg_name_only| (exact match, should be in field 2)
  1204.    if [ "$pkg_name_only" != '' ];then
  1205.      repo_pkg="$(cut -f1,2,8 -d '|' $all_supported_repofiles 2>/dev/null\
  1206.         | sed -e 's@^@|@' -e 's@$@|@' \
  1207.         | grep "|$1" \
  1208.         | grep -m1 "|$pkg_name_only|")"
  1209.    fi
  1210.  fi
  1211.  
  1212.  # print result
  1213.  [ "$repo_pkg" != '' ] && echo true || echo false
  1214. }
  1215.  
  1216.  
  1217. is_current_repo_pkg(){            # takes $PKGNAME ($1), returns true or false FUNCLIST
  1218.  
  1219.  # exit if no valid input
  1220.  [ ! "$1"  ] && exit 1
  1221.  
  1222.  # get current repo ($REPOFILE)
  1223.  . "${PKGRC}"
  1224.  
  1225.  local pkg_in_repo
  1226.  
  1227.  # check if given pkg ($1) is in the current repo
  1228.  pkg_in_repo="$(LANG=C cut -f1,2,7,8 -d'|' ${REPO_DB_FILE_DIR}/$REPOFILE 2>/dev/null \
  1229.     | sed -e "s/^/|/" \
  1230.           -e "s/$/|/" \
  1231.     | grep -m1 "|$1|")"
  1232.  
  1233.  # print msg
  1234.  [ "$pkg_in_repo" != '' ] && echo true || echo false
  1235. }
  1236.  
  1237.  
  1238. is_local_pkg(){                   # returns true if $1 is local package file FUNCLIST
  1239.  
  1240.  # exit if no valid input
  1241.  [ ! "$1"  ] && exit 1
  1242.  
  1243.  . "${PKGRC}"
  1244.  
  1245.  # if $1 is a local file with a supported package extension
  1246.  # then we return true, else, we return false
  1247.  local is_local_pkg=false
  1248.  
  1249.  # first, check we have a local file
  1250.  if [ -f "$1" ];then
  1251.  
  1252.    # file *probably* has an extension, lets try to get it..
  1253.    # the func get_pkg_ext() will return empty if not a valid package extension
  1254.    file_ext=$(get_pkg_ext "$1")
  1255.  
  1256.    # if not empty, it must be a local file, with valid pkg ext (a local pkg)
  1257.    [ "$file_ext" != '' ] && is_local_pkg=true
  1258.  
  1259.  elif [ -f "$CURDIR/$1" ];then
  1260.  
  1261.    file_ext=$(get_pkg_ext "$CURDIR/$1")
  1262.    [ "$file_ext" != '' ] && is_local_pkg=true
  1263.  
  1264.  elif [ -f "$WORKDIR/$1" ];then
  1265.  
  1266.    file_ext=$(get_pkg_ext "$CURDIR/$1")
  1267.    [ "$file_ext" != '' ] && is_local_pkg=true
  1268.  
  1269.  fi
  1270.  
  1271.  # print output
  1272.  echo $is_local_pkg
  1273. }
  1274.  
  1275.  
  1276.  
  1277. # RC file funcs
  1278.  
  1279. set_workdir(){                    # set new WORKDIR in rc file location FUNCLIST
  1280.  
  1281.  # get latest rc file values
  1282.  . ${PKGRC}
  1283.  
  1284.  # make sure $1 was given and doesn't already exist
  1285.  [ ! "$1"  ] && print_usage workdir && exit 1
  1286.  [ -e "$1" ] && echo "Error: directory already exists. Choose a new one." && exit 1
  1287.  
  1288.  # create the dir
  1289.  mkdir -p "$1" 2>/dev/null
  1290.  
  1291.  # if dir not created, exit with error
  1292.  [ $? -eq 1  ] && echo "Error: Could not create $1" && exit 1
  1293.  [ ! -d "$1" ] && echo "Error: Could not create directory:  $1" && exit 1
  1294.  
  1295.  # dir was created, so lets copy our pkgs in there
  1296.  list_downloaded_pkgs | while read pkg_file
  1297.  do
  1298.    cp -v "$WORKDIR/$pkg_name" "$1/"
  1299.  done
  1300.  
  1301.  # if copying everything to new dir failed
  1302.  if [ $? -eq 1 ]; then
  1303.    # print msg
  1304.    echo -e "${yellow}Warning:${endcolour}Could not copy packages from $WORKDIR to $1.."
  1305.    echo "You should copy all packages in $WORKDIR into $1."
  1306.  fi
  1307.  
  1308.  # update the RC file
  1309.  WORKDIR="$1"
  1310.  set_config
  1311.  echo "Success. Work directory updated."
  1312.  exit 0
  1313. }
  1314.  
  1315.  
  1316. set_pkg_scope(){                  # one|all set search for pkgs in current repo or all FUNCLIST
  1317.  
  1318.  # get old values, any we dont set here are not overwritten
  1319.  . ${PKGRC}
  1320.  
  1321.  [ "$1" != "" ] && PKGSCOPE="$1" || PKGSCOPE="$PKGSCOPE"
  1322.  [ "$PKG_DEBUG" = "true" ] && echo "set_pkg_scope(): PKGSCOPE=$PKGSCOPE" >&2
  1323.  
  1324.  case "$1" in
  1325.    all)
  1326.    # set pkg search to all
  1327.    PKGSEARCH="list_all_pkg_names"
  1328.    PKGSEARCHEXACT="$SELF -nea"
  1329.    echo -e "${green}Success:${endcolour} Find packages in all repos."
  1330.    ;;
  1331.    one)
  1332.    # set pkg search to current only
  1333.    PKGSEARCH="list_pkg_names"
  1334.    PKGSEARCHEXACT="$SELF -ne"
  1335.    echo -e "${green}Success:${endcolour} Find packages in current repo ($REPONAME) only."
  1336.    ;;
  1337.    *)
  1338.    PKGSCOPE="one"
  1339.    # set pkg search to current only
  1340.    PKGSEARCH="list_pkg_names"
  1341.    PKGSEARCHEXACT="$SELF -ne"
  1342.    echo "Find packages in current repo ($REPONAME) only."
  1343.    ;;
  1344.  esac
  1345.  
  1346.  set_config #170213
  1347. }
  1348.  
  1349.  
  1350. set_dep_scope(){                  # all|one set search for deps in current repo or all FUNCLIST
  1351.  
  1352.   # get RC file values, so any we dont set here are not overwritten
  1353.   . ${PKGRC}
  1354.  
  1355.  # make sure we have a valid value
  1356.  [ "$1" != "" ] && DEPSCOPE="$1" || DEPSCOPE="$DEPSCOPE"
  1357.  
  1358.  case "$1" in
  1359.    all)
  1360.    # set pkg search to all
  1361.    DEPSEARCH="list_all_pkg_names"
  1362.    DEPSEARCHEXACT="$SELF -nea"
  1363.    echo -e "${green}Success:${endcolour} Find dependencies in all repos."
  1364.    ;;
  1365.    one)
  1366.    # set pkg search to one
  1367.    DEPSEARCH="list_pkg_names"
  1368.    DEPSEARCHEXACT="$SELF -ne"
  1369.    echo -e "${green}Success:${endcolour} Find dependencies in current repo ($REPONAME) only."
  1370.    ;;
  1371.  esac
  1372.  
  1373.  set_config
  1374. }
  1375.  
  1376.  
  1377. set_recursive_deps(){             # yes|no search for deps of deps or not FUNCLIST
  1378.  [ "`echo $1 | grep -E "^yes\$|^no\$"`" = "" ] && print_usage recursive-dep-check && exit 1
  1379.  
  1380.  # get old values, any we dont set here are not overwritten
  1381.  . ${PKGRC}
  1382.  
  1383.  # make sure we have a valid value
  1384.  RDCHECK="$1"
  1385.  [ "$RDCHECK" = "yes" -o "$RDCHECK" = "no" ] || RDCHECK=no
  1386.  
  1387.  set_config
  1388.  
  1389.  # print final msg
  1390.  if [ "$1" = "yes" ]; then
  1391.    echo -e "${green}Success:${endcolour} 'Recursive dependency checking' enabled."
  1392.  else
  1393.    echo -e "${green}Success:${endcolour} 'Recursive dependency checking' disabled"
  1394.  fi
  1395. }
  1396.  
  1397.  
  1398. set_bleeding_edge(){              # yes|no get latest pkgs, ignore fall backs FUNCLIST
  1399.  [ "`echo $1 | grep -E "^yes\$|^no\$"`" = "" ] && echo usage bleeding-edge && exit 1
  1400.  
  1401.  #get old values, any we dont set here are not overwritten
  1402.  . ${PKGRC}
  1403.  
  1404.  # make sure we have a valid value
  1405.  BLEDGE="$1"
  1406.  [ "$BLEDGE" = "yes" -o "$BLEDGE" = "no" ] || BLEDGE=no
  1407.  
  1408.  set_config
  1409.  
  1410.  # print final msg
  1411.  if [ "$1" = "yes" ]; then
  1412.    echo -e "${green}Success:${endcolour} 'Bleeding edge' package search enabled."
  1413.  else
  1414.    echo -e "${green}Success:${endcolour} 'Bleeding edge' package search disabled."
  1415.  fi
  1416. }
  1417.  
  1418.  
  1419. set_autoclean(){                  # yes|no auto delete installed packages from WORKDIR FUNCLIST
  1420.  [ "`echo $1 | grep -E "^yes\$|^no\$"`" = "" ] && print_usage autoclean && exit 1
  1421.  
  1422.  #get old values, any we dont set here are not overwritten
  1423.  . ${PKGRC}
  1424.  
  1425.  # make sure we have a valid value
  1426.  AUTOCLEAN="$1"
  1427.  [ "$AUTOCLEAN" = "yes" -o "$AUTOCLEAN" = "no" ] || AUTOCLEAN=no
  1428.  
  1429.  set_config
  1430.  
  1431.  # print final msg
  1432.  if [ "$1" = "yes" ]; then
  1433.    echo -e "${green}Success:${endcolour} Auto-remove installed packages ENABLED."
  1434.  else
  1435.    echo -e "${green}Success:${endcolour} Auto-remove installed packages DISABLED."
  1436.  fi
  1437. }
  1438.  
  1439.  
  1440. set_config(){                     # update all options in config file FUNCLIST
  1441.  echo "WORKDIR=$WORKDIR"                    > ${PKGRC}
  1442.  echo "REPONAME=$REPONAME"                 >> ${PKGRC}
  1443.  echo "EX=$EX"                             >> ${PKGRC}
  1444.  echo "REPOFILE=$REPOFILE"                 >> ${PKGRC}
  1445.  echo "REPOURL1=$REPOURL1"                 >> ${PKGRC}
  1446.  echo "REPOURL2=$REPOURL2"                 >> ${PKGRC}
  1447.  #140213 add all urls
  1448.  echo "REPOURL3=$REPOURL3"                 >> ${PKGRC}
  1449.  echo "REPOURL4=$REPOURL4"                 >> ${PKGRC}
  1450.  # seach settings
  1451.  echo "PKGSEARCH=\"$PKGSEARCH\""           >> ${PKGRC}
  1452.  echo "PKGSEARCHEXACT=\"$PKGSEARCHEXACT\"" >> ${PKGRC}
  1453.  echo "DEPSEARCH=\"$DEPSEARCH\""           >> ${PKGRC}
  1454.  echo "DEPSEARCHEXACT=\"$DEPSEARCHEXACT\"" >> ${PKGRC}
  1455.  # multiple repo settings
  1456.  echo "REPOFALLBACKS=\"$REPOFALLBACKS\""   >> ${PKGRC}
  1457.  echo "PKGSCOPE=\"$PKGSCOPE\""             >> ${PKGRC}
  1458.  echo "DEPSCOPE=\"$DEPSCOPE\""             >> ${PKGRC}
  1459.  echo "BLEDGE=\"$BLEDGE\""                 >> ${PKGRC}
  1460.  echo "RDCHECK=\"$RDCHECK\""               >> ${PKGRC} #150813
  1461.  echo "AUTOCLEAN=\"$AUTOCLEAN\""           >> ${PKGRC}
  1462.  echo "BUILDTOOL=$BUILDTOOL"               >> ${PKGRC}
  1463. }
  1464.  
  1465.  
  1466. show_config(){                    # show config settings from ~/.pkg/pkgrc FUNCLIST
  1467.  . ${PKGRC}
  1468.  
  1469.  # set default values
  1470.  PKGSCOPETXT="Search for packages in all repos."
  1471.  DEPSCOPETXT="Search for dependencies in all repos."
  1472.  FALLBACKTXT="Accessing other repos in this order:"
  1473.  FALLBACKTXT="$FALLBACKTXT\n`repo_list | grep -v "^$REPONAME" | tr '\n' ' ' | sed -e "s/ /, /g" -e "s/, $//" | fold -w 54 -s`"
  1474.  RDCHECKTXT="Recursive dependency search is NOT enabled."
  1475.  
  1476.  # update with values from PKGRC file
  1477.  [ "$PKGSCOPE" = "one" ] && PKGSCOPETXT="Search for packages in current repo only."
  1478.  [ "$DEPSCOPE" = "one" ] && DEPSCOPETXT="Search for dependencies in current repo only."
  1479.  [ "$BLEDGE"   = "yes" ] && FALLBACKTXT="Bleeding-edge enabled - searching all repos, for the latest packages versions.."
  1480.  [ "$RDCHECK"  = "yes" ] && RDCHECKTXT="Recursive dependency search is enabled."
  1481.  
  1482.  # print current Pkg config
  1483.  echo "==========================="
  1484.  echo "$APPNAME $APPVER"
  1485.  echo "==========================="
  1486.  echo "Config file:  $PKGRC"
  1487.  echo "Packages dir: ${WORKDIR}/"
  1488.  echo "Autoclean:    $AUTOCLEAN"
  1489.  echo
  1490.  echo "Search settings:"
  1491.  #echo "- $HIDEINSTALLEDTXT"
  1492.  echo "- $PKGSCOPETXT"
  1493.  echo "- $DEPSCOPETXT"
  1494.  echo "- $RDCHECKTXT"
  1495.  echo
  1496.  echo "Package Compiling backend:"
  1497.  echo "- $BUILDTOOL"
  1498.  echo
  1499.  echo "Repo details:"
  1500.  echo "- Current Repo: $REPONAME"
  1501.  echo "- Package type: $EX"
  1502.  echo "- Packages:     $(cat "$REPO_DB_FILE_DIR/${REPOFILE}" | wc -l)"
  1503.  echo "- Mirror 1:     `echo $REPOURL1 | cut -f1-3 -d'/' `"
  1504.  [ "$REPOURL2" != "" ] && echo "- Mirror 2:     `echo $REPOURL2 | cut -f1-3 -d'/' `"
  1505.  [ "$REPOURL3" != "" ] && echo "- Mirror 3:     `echo $REPOURL3 | cut -f1-3 -d'/' `"
  1506.  [ "$REPOURL4" != "" ] && echo "- Mirror 4:     `echo $REPOURL4 | cut -f1-3 -d'/' `"
  1507.  echo
  1508.  echo -e "$FALLBACKTXT"
  1509. }
  1510.  
  1511.  
  1512. # repo and source funcs
  1513.  
  1514. get_repo_info(){                  # return details of given repo name ($1) FUNCLIST
  1515.  [ ! "$1" -o "$1" = "-" ] && print_usage repo-info && exit 1
  1516.  
  1517.  REPOLINE="`list_all_sources $1 | sort -u | uniq`" #170214 always get latest info, not info from rcfile
  1518.  
  1519.  # on first run, this might be needed to set the repo correctly
  1520.  [ "$REPOLINE" = '' ] && REPOLINE="`list_all_sources noarch`"
  1521.  
  1522.  REPONAME="`echo $REPOLINE | cut -f1 -d'|'`"
  1523.        EX="`echo $REPOLINE | cut -f2 -d'|'`"
  1524.  REPOFILE="`echo $REPOLINE | cut -f3 -d'|'`"
  1525.  REPOURL1="`echo $REPOLINE | cut -f4 -d'|'`"
  1526.  REPOURL2="`echo $REPOLINE | cut -f5 -d'|'`"
  1527.  REPOURL3="`echo $REPOLINE | cut -f6 -d'|'`"
  1528.  REPOURL4="`echo $REPOLINE | cut -f7 -d'|'`"
  1529.  REPOFALLBACKS="`echo "$REPOLINE"| cut -f8 -d'|'`"
  1530. }
  1531.  
  1532.  
  1533. set_current_repo(){               # set the current repo to use, give repo name as $1 FUNCLIST
  1534.  
  1535.  # print usage if no valid options
  1536.  [ ! "$1" -o "$1" = "-" -o "$1" = "-h" -o "$1" = "--help" ] && print_usage repo && exit 1
  1537.  
  1538.  # get repo details from rc file
  1539.  . ${PKGRC}
  1540.  
  1541.  # remove the default mirror tmp file, we're changing repo and mirrors
  1542.  rm $TMPDIR/CURREPOURL 2>/dev/null
  1543.  
  1544.  # remove old repo files list (used in list_source_files)
  1545.  rm -f ${TMPDIR}/source_files 2>/dev/null
  1546.  
  1547.  # if not updating the scopes or bleeding-edge,leave them as set in rcfile
  1548.  [ "$PKGSCOPE" = "" ] && PKGSCOPE="$PKGSCOPE"
  1549.  [ "$DEPSCOPE" = "" ] && DEPSCOPE="$DEPSCOPE"
  1550.  [ "$BLEDGE" = "" ]   && BLEDGE="$BLEDGE"
  1551.  
  1552.  # if nothing was found in rcfile, we need to set to default
  1553.  [ "`echo $PKGSCOPE | grep -E "^one\$|^all\$"`" = "" ] && PKGSCOPE="one"
  1554.  [ "`echo $DEPSCOPE | grep -E "^one\$|^all\$"`" = "" ] && DEPSCOPE="one"
  1555.  [ "$BLEDGE" = "" ] && BLEDGE="no"
  1556.  
  1557.  # check if $1 is valid repo
  1558.  if [ "$(LANG=C list_sources "$1")" != "" ]; then
  1559.    # set repo details
  1560.    LANG=C get_repo_info "$1"
  1561.    LANG=C set_config #170213
  1562.    LANG=C update_sources 1>/dev/null #update the order of sources to match new fallback list of new current repo
  1563.    LANG=C print_repo_info "$1" #output msg, repo info
  1564.  else
  1565.    # not a valid repo, print message
  1566.    echo "The name '$1' is not a valid repo name. These are:"
  1567.    LANG=C repo_list #250613
  1568.  fi
  1569. }
  1570.  
  1571.  
  1572. repo_list(){                      # list names of all avail repos [optionally matching $1] FUNCLIST
  1573.  
  1574.  # we need to list these repos in the order defined in the RC file
  1575.  # so Pkg can 'fall back' to other repos in that order
  1576.  
  1577.  # get current config
  1578.  . ${PKGRC}
  1579.  
  1580.  # set vars for this func
  1581.  local list=''
  1582.  local repo_file=''
  1583.  local repo_names=`cut -f1 -d'|' ${HOME}/.pkg/sources`
  1584.  local current_fallback_list=`grep "^$REPONAME|" ${HOME}/.pkg/sources | cut -f8 -d'|' | grep -v "^\$"`
  1585.  
  1586.  # get current repo fallback list order.. note if repo not listed in fallback list, it will be appended after
  1587.  for line in $current_fallback_list
  1588.  do
  1589.    # get the repo file
  1590.    repo_file="`grep "^$line|" ${HOME}/.pkg/sources | cut -f3 -d'|'`"
  1591.    # add it to the list
  1592.    [ "$repo_file" != "" ] && list="$list$line "
  1593.  done
  1594.  
  1595.  # now add all other avail repos to the end of fallback list
  1596.  for repo_name in $repo_names
  1597.  do
  1598.    #if not blank, not already in the repo list and not the current repo ($REPONAME from rc file) then add to list
  1599.    [ "$repo_name" != "" -a "`echo "$list" | grep "$repo_name "`" = ""  -a "$repo_name" != "$REPONAME" ] && list="$list$repo_name "
  1600.  done
  1601.  
  1602.  # list the current repo first
  1603.  echo $REPONAME
  1604.  # then the other repos
  1605.  echo "$list" | tr ' ' '\n' | grep -v "^\$"
  1606. }
  1607.  
  1608.  
  1609. repo_file_list(){                 # list available repo files, $1 optional FUNCLIST
  1610.  
  1611.  # we need to list these repos in the order defined in the RC file
  1612.  # so Pkg can 'fall back' to other repos in that order
  1613.  
  1614.  # get current config
  1615.  . ${PKGRC}
  1616.  
  1617.  # set vars for this func
  1618.  local list=''
  1619.  local repo_file=''
  1620.  local repo_files=`cut -f3 -d'|' ${HOME}/.pkg/sources`
  1621.  local current_fallback_list=`grep "^$REPONAME|" ${HOME}/.pkg/sources | cut -f8 -d'|' | grep -v "^\$"`
  1622.  
  1623.  # get current repo fallback list order.. note if repo not listed in fallback list, it will be appended after
  1624.  for line in $current_fallback_list
  1625.  do
  1626.    # get the repo file
  1627.    repo_file="`grep "^$line|" ${HOME}/.pkg/sources | cut -f3 -d'|'`"
  1628.    # add it to the list
  1629.    [ "$repo_file" != "" ] && list="$list$repo_file "
  1630.  done
  1631.  
  1632.  # now add all other avail repos to the end of fallback list
  1633.  for repo_file in $repo_files
  1634.  do
  1635.    #if not blank, not already in the repo list and not the current repo ($REPONAME from rc file) then add to list
  1636.    [ "$repo_file" != "" -a "`echo "$list" | grep "$repo_file "`" = ""  -a "$repo_file" != "$REPOFILE" ] && list="$list$repo_file "
  1637.  done
  1638.  
  1639.  # list the current repo first
  1640.  echo $REPOFILE
  1641.  # then the other repos
  1642.  echo "$list" | tr ' ' '\n' | grep -v "^\$"
  1643. }
  1644. declare -ga REPO_DB_PATHS
  1645. #mk_repo_file_list_arry(){
  1646.  
  1647.  #REPO_DB_PATHS=()
  1648.  #declare -A REPO_DB_PATHS
  1649.  
  1650. #Can't put the loop in the loop in a pipeline if you want to assign array values:
  1651. #https://stackoverflow.com/questions/13091700/why-is-my-array-gone-after-exiting-loop
  1652. # repo_file_list |
  1653. while read REPO_FILE_NAME; do
  1654.    #echo "REPO_FILE_NAME=$REPO_FILE_NAME"
  1655.    RF_FILE_PATH="${ALL_REPO_DB_PATHS[$REPO_FILE_NAME]}"
  1656.    if [ -e "$RF_FILE_PATH" ]; then
  1657.      REPO_DB_PATHS+=( "$RF_FILE_PATH" )
  1658.    fi
  1659. done < <( repo_file_list )
  1660. #echo "REPO_DB_PATHS=${ALL_REPO_DB_PATHS[@]}"
  1661. #echo "REPO_DB_PATHS_keys=${!ALL_REPO_DB_PATHS[@]}"  
  1662. #}
  1663. #mk_repo_file_list_arry
  1664.  
  1665. dir2repo(){                       # create a native-Puppy repo from a dir of packages FUNCLIST
  1666.  
  1667.  # exit if not a valid directory
  1668.  if [ ! -d "$1" ]; then
  1669.    print_usage dir2repo && exit 1
  1670.  fi
  1671.  
  1672.  local filename=''
  1673.  local filepath=''
  1674.  local fileext=''
  1675.  local prevext=''
  1676.  local repo_name
  1677.  
  1678.  # get the repo directory
  1679.  local repo_dir="$(realpath "$1")"
  1680.  
  1681.  # create a temp repo_file name
  1682.  local repo_file="${repo_dir}/Packages-$DISTRO_BINARY_COMPAT-$DISTRO_COMPAT_VERSION"
  1683.  
  1684.  # remove any old files
  1685.  rm "${repo_dir}/install" "${repo_dir}/Packages-"* "$repo_file" &>/dev/null
  1686.  
  1687.  echo
  1688.  echo "Creating repo from contents of: $repo_dir"
  1689.  echo
  1690.  
  1691.  # exit if we encounter multiple file extensions
  1692.  for file in $(find "$repo_dir" -maxdepth 5 -type f)
  1693.  do
  1694.    filename=$(basename $file)
  1695.    fileext="$(get_pkg_ext $filename)"
  1696.    if [ "$fileext" != "$prevext" ] && [ "$prevext" != "" ]; then
  1697.      echo "All packages must have the same extension."
  1698.      echo "Extensions '$fileext' and '$prevext' don't match."
  1699.      echo "Package '$filename' needs to be converted to $prevext or removed."
  1700.      exit 1
  1701.    fi
  1702.    prevext="$fileext"
  1703.  done
  1704.  
  1705.  
  1706.  # get the packages in $repo_dir
  1707.  local package_list="$(find "$repo_dir" -maxdepth 5 -type f | grep -v ^Packages | grep -v "^install$")"
  1708.  
  1709.  if [ "$package_list" = "" ]; then
  1710.    echo "No packages found in ${repo_dir}!"
  1711.    exit 1
  1712.  fi
  1713.  
  1714.  # for each file in the repo dir
  1715.  for file in $package_list
  1716.  do
  1717.    # skip if not a recognised package type
  1718.    [ "$(is_local_pkg "$file")" != true ] && continue
  1719.  
  1720.    # get the package info
  1721.    filename=$(basename $file)
  1722.    fileext="$(get_pkg_ext $filename)"
  1723.    filepath="$(realpath $(dirname $file))"
  1724.    pathname="$(dirname ${filepath})"
  1725.    repopath="$(echo $filepath | sed -e "s#${repo_dir}##" -e "s/^\///")"
  1726.    pkgname=$(basename $filename .$fileext)
  1727.    pkgnameonly="$(get_pkg_name_only $pkgname)"
  1728.    pkgversion="$(get_pkg_version $pkgname)"
  1729.    pkgcat="$(grep -m1 "|$pkgnameonly|" "$REPO_DB_FILE_DIR"/Packages-* | cut -f5 -d'|')"
  1730.    pkgsize="$(du "$file" 2>/dev/null | cut -f1)K"
  1731.    [ "$pkgsize" = "" ] && pkgsize="$(grep -m1 "|$pkgnameonly|" "$REPO_DB_FILE_DIR"/Packages-* | cut -f6 -d'|')"
  1732.    pkgdeps="$(grep -m1 "|$pkgnameonly|" "$REPO_DB_FILE_DIR"/Packages-* | cut -f9 -d'|')"
  1733.    # dont include deps if we're adding a combined package + plus deps (they have $CP_SUFFIX in the name)
  1734.     [ "$(echo "$filename" | grep "$CP_SUFFIX")" != "" ] && pkgdeps=''
  1735.     pkgdesc="$(grep -m1 "|$pkgnameonly|" "$REPO_DB_FILE_DIR"/Packages-* | cut -f10 -d'|')"
  1736.     pkgdistro="$DISTRO_BINARY_COMPAT"
  1737.     pkgdistroversion="$DISTRO_COMPAT_VERSION"
  1738.  
  1739.     # if we don't have the required package info, dont add it to the repo file
  1740.     [ "$pkgname" = "" -o "$pkgnameonly" = "" -o "$filename" = "" ] && continue
  1741.  
  1742.     # add this package to the repo file
  1743.     echo "$pkgname|$pkgnameonly|$pkgversion||${pkgcat:-BuildingBlock}|$pkgsize|$repopath|$filename|$pkgdeps|${pkgdesc:-No description}|$pkgdistro|$pkgdistroversion|" >> "$repo_file"
  1744.   done
  1745.  
  1746.   if [ ! -f "$repo_file" ]; then
  1747.     echo "Error: file '$repo_file' not created."
  1748.     exit 1
  1749.   fi
  1750.  
  1751.   echo "Step 1 of 3: CHOOSE A REPO NAME"
  1752.   echo "(something like 'distroversion-username-repo', such as 'bionic-bob-main' or 'stretch-sc0ttman-games')"
  1753.   echo
  1754.   bash -c 'read -e -r -p "Type a repo name and hit ENTER: " repo_name; echo "$repo_name" > /tmp/pkg/repo_name'
  1755.   repo_name="$(cat /tmp/pkg/repo_name)"
  1756.  
  1757.   # rename the repo file to the new name
  1758.   mv "$repo_file"   "${repo_dir}/Packages-${DISTRO_BINARY_COMPAT:-puppy}-$repo_name"
  1759.   repo_file="${repo_dir}/Packages-${DISTRO_BINARY_COMPAT:-puppy}-$repo_name"
  1760.  
  1761.   # build repo entry
  1762.   local repo_entry="$repo_name|$fileext|$repo_file|$repo_url||||$repo_fallbacks"
  1763.  
  1764.   # get the URL where the repo file and packages will live
  1765.   echo
  1766.   echo "Step 2 of 3: ADD THE REPO URL"
  1767.   echo "(the full URL where you will upload your repo file and packages)"
  1768.   echo
  1769.   bash -c 'read -e -r -i "http://" -p "Type a repo URL and hit ENTER: " mirror1; echo "$mirror1" > /tmp/pkg/mirror1'
  1770.   mirror1="$(cat /tmp/pkg/mirror1)"
  1771.  
  1772.   # get fallback repos list
  1773.   echo
  1774.   echo "Step 3 of 3: ADD FALLBACK REPOS"
  1775.   echo "(the other repos to fall back to when looking for dependencies)"
  1776.   echo
  1777.   bash -c 'read  -e -r -i "noarch common32" -p "List fallback repos (separated by a space) and hit ENTER: " fallback_repos; echo "$fallback_repos" > /tmp/pkg/fallback_repos'
  1778.   fallback_repos="$(cat /tmp/pkg/fallback_repos)"
  1779.  
  1780.   # remove the tmp files which store user input, they're no longer needed
  1781.   rm /tmp/pkg/repo_name /tmp/pkg/mirror1 /tmp/pkg/fallback_repos
  1782.  
  1783.   # add a trailing slash to the URL
  1784.   if [ "$(echo "${mirror1}" | grep "/$")" = "" ]; then
  1785.     mirror1="${mirror1}/"
  1786.   fi
  1787.  
  1788.   # create repo installer file in $repo_dir
  1789.   echo "REPONAME=$repo_name
  1790. EX=$fileext
  1791. REPOFILE=$(basename $repo_file)
  1792. URL1=${mirror1}
  1793. URL2=''
  1794. URL3=''
  1795. URL4=''
  1796. FALLBACKS='$fallback_repos'" > "${repo_dir}/install"
  1797.  
  1798.   # print final message
  1799.   echo
  1800.   echo -e "${green}Success${endcolour}: Repo ${yellow}$repo_name${endcolour} created."
  1801.   echo
  1802.   echo -e "You should upload everything in ${lightblue}$repo_dir${endcolour} to:"
  1803.   echo
  1804.   echo "  $mirror1"
  1805.   echo
  1806.   echo "You (and anyone else) can then install the repo using:"
  1807.   echo
  1808.   echo "  pkg add-repo ${mirror1}"
  1809.   echo
  1810.   echo -e "NOTE: You can edit the ${yellow}install${endcolour} and ${yellow}$(basename $repo_file)${endcolour}"
  1811.   echo "files in a text editor, before you upload your new repo."
  1812.   echo
  1813. }
  1814.  
  1815. check_https_cert(){
  1816.   if [ "$HTTPS_CHECK" = 'none' ] || [ -z "$HTTPS_CHECK" ]; then
  1817.     echo --no-check-certificate
  1818.   elif [ ! "$HTTPS_CHECK" = all ]; then
  1819.     local repo_name="$1"
  1820.     local repo_file_url="$2"
  1821.     local check_cert=-1
  1822.     #TODO add further certificate checking logic here.
  1823.     #TODO: First check for a config file
  1824.     #Then use hardcoded defaults:
  1825.     if [ $check_cert -eq -1 ]; then
  1826.       case "$repo_file_url" in
  1827.       https://*tor*)
  1828.         check_cert=1 ;;
  1829.       https://*nodesource*)
  1830.         check_cert=1 ;;
  1831.       *)
  1832.         check_cert=0 ;; #TODO: have some config variable for the default checkcert value
  1833.       esac
  1834.     fi
  1835.     if [ check_cert -eq 0 ]; then
  1836.       echo --no-check-certificate
  1837.     fi
  1838.   fi
  1839.    
  1840. }
  1841. add_pkg_repo() {                  # add a Pkg-created repo, called by add_repo() FUNCLIST
  1842.   [ ! "$1" ] && return 1
  1843.   mkdir -p /tmp/pkg/
  1844.   # this is probably a Pkg created Puppy repo, get info from 'install' file
  1845.   rm /tmp/pkg/repo_info &>/dev/null
  1846.  
  1847.   # check if the 'install' file we need exists
  1848.   wget --no-check-certificate -O "/tmp/pkg/repo_info" -4 "$1" &>/dev/null
  1849.   if [ ! -f /tmp/pkg/repo_info ]; then
  1850.     echo "Error: Repo installer file not downloaded!"
  1851.     return 1
  1852.   elif [ "$(grep -m1 "^REPONAME" /tmp/pkg/repo_info 2>/dev/null)" = "" ]; then
  1853.     echo "Error: invalid repo installer file"
  1854.     return 1
  1855.   fi
  1856.  
  1857.   # build a valid repo entry for the ~/.pkg/sources* files
  1858.   # (must have some fallbacks, strip quotes, newlines, no trailing pipes)
  1859.   local repo_entry="$(cat /tmp/pkg/repo_info \
  1860.    | sed -e "s/FALLBACKS=''/FALLBACKS='noarch common32'/" -e 's/.*=//g' \
  1861.    | tr -d '"' \
  1862.    | tr -d "'" \
  1863.    | tr '\n' '|' \
  1864.    | head -c -1)"
  1865.  
  1866.   if [ "$repo_entry" = "" ]; then
  1867.     echo "Error: repo entry not created."
  1868.     return 1
  1869.   fi
  1870.  
  1871.   # get repo info
  1872.   local repo_name="$(echo "$repo_entry" | cut -f1 -d'|')"
  1873.   local repo_filename="$(echo "$repo_entry" | cut -f3 -d'|')"
  1874.   local repo_file_url="$(echo ${1} | sed 's#/install$##')/${repo_filename}"
  1875.   local repo_check_cert=$(check_https_cert "$repo_name" "$repo_file_url")
  1876.   # download the repo file
  1877.  
  1878.   wget $repo_check_cert -O "/tmp/pkg/$repo_filename" -4 "$repo_file_url" &>/dev/null
  1879.   if [ -z /tmp/pkg/$repo_filename ] || [ ! -f /tmp/pkg/$repo_filename ]; then
  1880.     echo "Error: Repo file $repo_filename not downloaded!"
  1881.     return 1
  1882.   fi
  1883.  
  1884.   # install the repo file
  1885.   mv /tmp/pkg/$repo_filename "$REPO_DB_FILE_DIR/$repo_filename"
  1886.   RETVAL=$?
  1887.  
  1888.   # add repo entry to ~/.pkg/sources
  1889.   echo
  1890.   add_source "${repo_entry}"
  1891.   echo
  1892.  
  1893.   # refresh list of available repos
  1894.   update_sources
  1895.   echo
  1896.   echo "Repo info:"
  1897.   print_repo_info $repo_name
  1898.   echo
  1899.  
  1900.   if [ "$(cat ~/.pkg/sources 2>/dev/null | grep -m1 "^$repo_name|")" != "" ]; then
  1901.     echo "Success! Repo added and available to use."
  1902.     echo
  1903.     echo "To use this repo, simply type the following and hit ENTER:"
  1904.     echo "  pkg repo $repo_name"
  1905.     echo
  1906.  
  1907.     # register as a user installed repo, so we can remove it later (using rm_repo)
  1908.     touch ~/.pkg/sources-user
  1909.     echo "$repo_entry" >> ~/.pkg/sources-user
  1910.     local user_repos="$(sort -u ~/.pkg/sources-user | uniq)"
  1911.     echo "$user_repos" > ~/.pkg/sources-user
  1912.  
  1913.   fi
  1914.  
  1915.   return $RETVAL
  1916. }
  1917.  
  1918. add_repo(){                       # add Pkg/Ubuntu/Debian/Slackware third-party repos FUNCLIST
  1919.  
  1920.   local slack_repo_url
  1921.  
  1922.   if [ ! "$1" -o "$1" = "-" -o "$1" = "-h" -o "$1" = "--help" ]; then
  1923.     print_usage add_repo && exit 1
  1924.   fi
  1925.  
  1926.   # work out which kind of repo we are processing
  1927.   case "$1" in
  1928.     'http'*'/PACKAGES.TXT.gz'|'http'*'/PACKAGES.TXT')
  1929.       # strip trailing /PACKAGES.TXT[.gz]
  1930.       slack_repo_url="${1//PACKAGES.TXT.gz/}"
  1931.       slack_repo_url="${slack_repo_url//\/.gz/}"
  1932.       slack_repo_url="${slack_repo_url//.gz\//}"
  1933.       slack_repo_url="${slack_repo_url//PACKAGES.TXT/}"
  1934.       # add trailing slash, if needed
  1935.       if [ "$(echo $slack_repo_url | grep '/$')" = '' ]; then
  1936.         slack_repo_url="${slack_repo_url}/"
  1937.       fi
  1938.       slack2pup "$@"
  1939.       retval=$?
  1940.       if [ $retval -eq 0 ]; then
  1941.         # register repo, so we know to update it in update_sources()
  1942.         touch ~/.pkg/sources-user
  1943.         echo "$repo_entry" >> ~/.pkg/sources-user
  1944.         local user_repos="$(sort -u ~/.pkg/sources-user | uniq)"
  1945.         echo "$user_repos" > ~/.pkg/sources-user
  1946.  
  1947.         # register in a slack supported file too
  1948.         mkdir -p /etc/slackpkg/
  1949.         touch /etc/slackpkg/mirrors
  1950.         echo "$slack_repo_url" >> /etc/slackpkg/mirrors
  1951.         local slack_repos="$(sort -u /etc/slackpkg/mirrors | uniq)"
  1952.         echo "$slack_repos" > /etc/slackpkg/mirrors
  1953.       fi
  1954.       ;;
  1955.  
  1956.     'http'*'/install')
  1957.       add_pkg_repo "$1"
  1958.       ;;
  1959.  
  1960.     'ppa:'*|'http://'*|'https://'*)
  1961.       wget --quiet --timeout=2 --no-parent --spider "${1}install"
  1962.       REPLY=$?
  1963.       if [ "$REPLY" = 0 ]; then
  1964.         add_pkg_repo "${1}install"
  1965.         exit $?
  1966.       fi
  1967.  
  1968.       ppa2pup "$@"
  1969.       retval=$?
  1970.       if [ $retval -eq 0 ]; then
  1971.         # register repo, so we know to update it in update_sources()
  1972.         touch ~/.pkg/sources-user
  1973.         echo "$repo_entry" >> ~/.pkg/sources-user
  1974.         local user_repos="$(sort -u ~/.pkg/sources-user | uniq)"
  1975.         echo "$user_repos" > ~/.pkg/sources-user
  1976.  
  1977.         # register in an apt-supported file too
  1978.         mkdir -p /etc/apt/
  1979.         touch /etc/apt/sources.list
  1980.         echo "deb $@" >> /etc/apt/sources.list
  1981.         local deb_repos="$(sort -u /etc/apt/sources.list | uniq)"
  1982.         echo "$deb_repos" > /etc/apt/sources.list
  1983.       fi
  1984.       ;;
  1985.  
  1986.     *)
  1987.       print_usage add_repo && exit 1
  1988.       ;;
  1989.  
  1990.   esac
  1991.  
  1992. }
  1993.  
  1994.  
  1995. rm_repo(){                        # remove an installed repo by name ($1) FUNCLIST
  1996.   if [ ! "$1" -o "$1" = "-" -o "$1" = "-h" -o "$1" = "--help" ]; then
  1997.     print_usage rm_repo
  1998.     exit 1
  1999.   fi
  2000.  
  2001.   # $1 must be a valid, installed repo name
  2002.   if [ "$(repo_list | grep -m1 "^${1}$")" = "" ]; then
  2003.     print_usage rm-repo
  2004.     exit 1
  2005.   fi
  2006.  
  2007.   # get the repo file and URL of the given repo
  2008.   local repo_file="$(grep "^$1" ~/.pkg/sources-all | cut -f3 -d'|' )"
  2009.   local repo_url="$(grep "^$1" ~/.pkg/sources-all | cut -f4 -d'|')"
  2010.  
  2011.   # dont allow removal of 'built in' repos, only user-added repos
  2012.   local matching_user_added_repos="$(grep -m1 -E "\|$repo_name\||$repo_url|${repo_name//*-/}" ~/.pkg/sources-user /etc/apt/sources.list /etc/slackpkg/mirrors 2>/dev/null)"
  2013.   if [ "${matching_user_added_repos}" = "" ]; then
  2014.     echo "You can only remove the repositories that you installed yourself."
  2015.     echo -e "Use the command  ${bold}pkg repo-list${endcolour}  to see which repos are installed."
  2016.     exit 1
  2017.   fi
  2018.  
  2019.   # dont allow removal of current repo
  2020.   if [ "$REPONAME" = "$1" ]; then
  2021.     echo "Can't remove current repo."
  2022.     echo -e "Switch to another repo first using:  ${bold}pkg repo REPONAME${endcolour}"
  2023.     exit 1
  2024.   fi
  2025.  
  2026.   # remove the repo file
  2027.   echo "Removing '$1'.. Please wait.."
  2028.   [ -f "$REPO_DB_FILE_DIR/$repo_file" ] && rm "$REPO_DB_FILE_DIR/$repo_file"
  2029.  
  2030.   # remove from sources
  2031.   grep -v "^$1|" ~/.pkg/sources > /tmp/pkg/sources
  2032.   grep -v "^$1|" ~/.pkg/sources-all > /tmp/pkg/sources-all
  2033.   [ -f /tmp/pkg/sources-all ] && mv /tmp/pkg/sources-all ~/.pkg/sources-all
  2034.   [ -f /tmp/pkg/sources     ] && mv /tmp/pkg/sources ~/.pkg/sources
  2035.  
  2036.   # remove from third-party lists
  2037.   grep -v  "|${repo_url}|"  ~/.pkg/sources-user > /tmp/pkg/sources-user
  2038.   grep -vE "$repo_url|${repo_name//*-/}" /etc/apt/sources.list > /tmp/pkg/sources.list
  2039.   grep -v  "$repo_url" /etc/slackpkg/mirrors > /tmp/pkg/mirrors
  2040.   [ -f /tmp/pkg/sources-user ] && mv /tmp/pkg/sources-user ~/.pkg/sources-user
  2041.   [ -f /tmp/pkg/sources.list ] && mv /tmp/pkg/sources.list /etc/apt/sources.list
  2042.   [ -f /tmp/pkg/mirrors ] && mv /tmp/pkg/mirrors /etc/slackpkg/mirrors
  2043.  
  2044.   if [ "$(repo_list | grep -m1 "^${1}$")" = "" ]; then
  2045.     update_sources &>/dev/null
  2046.     echo -e "${green}Success${endcolour}: Repo removed."
  2047.   fi
  2048.  
  2049. }
  2050.  
  2051.  
  2052. add_source(){                     # add a new repo to your repo 'sources' list FUNCLIST
  2053.   [ ! "$1" \
  2054.     -o "$1" = "-" \
  2055.     -o "$1" = "-h" \
  2056.     -o "$1" = "--help" \
  2057.     -o "`echo "$1" |  grep '|'`" = "" \
  2058.     -o "`echo "$1" | cut -f2 -d'|'`" = "" \
  2059.     -o "`echo "$1" | cut -f4 -d'|'`" = "" \
  2060.     -o "`echo "$1" | cut -f8 -d'|'`" = "" ] && \
  2061.     print_usage add_source && exit 1
  2062.  
  2063.   # get repo file to add to sources
  2064.   REPOTOADD="`echo $1 | cut -f1 -d'|'`"
  2065.   REPOFILETOADD="`echo $1 | cut -f3 -d'|'`"
  2066.  
  2067.   # do checks before adding repo (dont add duplicate, make sure file exists, etc)
  2068.  
  2069.   # check if repo name already in sources-all
  2070.   #if [ "`grep "^$REPOTOADD\$" ~/.pkg/sources-all`" != "" ] || \
  2071.   #   [ "`repo_list | grep "^$REPOTOADD\$"`" != "" ]; then
  2072.   #  echo "Repo with the name '$REPOTOADD' already in the list"
  2073.   #fi
  2074.  
  2075.   # check if repo filename already  exists in sources-all
  2076.   #if [ "`repo_file_list | grep -m1 "^$REPOFILETOADD\$"`" != "" ]; then
  2077.   #  echo "Repo with database file $REPO_DIR/'$REPOFILETOADD' already in the list"
  2078.   #fi
  2079.  
  2080.   # check if the repo file exists in $REPO_DIR
  2081.   #if [ ! -f "`find $REPO_DIR/ -name "$REPOFILETOADD"`" ]; then
  2082.   #  echo "The repo database file '$REPO_DIR/$REPOFILETOADD' not found."
  2083.   #fi
  2084.  
  2085.   # add the repo to sources-all, if not already there
  2086.   if [ "$(grep -m1 "^$REPOTOADD|" ${HOME}/.pkg/sources-all)" = "" ]; then
  2087.     # all good, so add repo entry to sources-all
  2088.     echo "$1" >> ${HOME}/.pkg/sources-all
  2089.   fi
  2090.  
  2091.   # update users repo sources to get the new repo
  2092.   update_sources 1>/dev/null || { echo "Could not update repo sources."; exit 2; }
  2093.  
  2094.   # print msg
  2095.   echo "Repo '$REPOTOADD' added successfully."
  2096. }
  2097.  
  2098.  
  2099. update_sources(){                 # create the list of available repos FUNCLIST
  2100.  
  2101.   # get current repo values and Pkg settings
  2102.   . ${PKGRC}
  2103.  
  2104.   #list current repo first in sources
  2105.   get_repo_info "${REPONAME:-noarch}"
  2106.  
  2107.  
  2108.   # only add the current repo to the list of available sources if it exists
  2109.   if [ "$(find "$REPO_DB_FILE_DIR" -iname "$REPOFILE" )" != '' ]; then
  2110.     echo "$REPONAME|$EX|$REPOFILE|$REPOURL1|$REPOURL2|$REPOURL3|$REPOURL4|$REPOFALLBACKS" > ${HOME}/.pkg/sources
  2111.   #elif [ "$(find "$(realpath "/var/packages/")" -iname "$REPOFILE" )" != '' ]; then
  2112.   #  echo "$REPONAME|$EX|$REPOFILE|$REPOURL1|$REPOURL2|$REPOURL3|$REPOURL4|$REPOFALLBACKS" > ${HOME}/.pkg/sources
  2113.   fi
  2114.  
  2115.  
  2116.   # get repos in order of fallbacks, pkg will then 'fall back' to each repo in that order
  2117.   FALLBACKS="`grep -m1 "^${REPONAME}|" ${HOME}/.pkg/sources-all 2>/dev/null | grep -v ^'#' | cut -f8 -d'|'`"
  2118.  
  2119.   # for each repo in fallback list
  2120.   for FBACK in $REPOFALLBACKS; do
  2121.     # dont add current repo, its already added
  2122.     [ "$FBACK" = "$REPONAME" ] && continue
  2123.  
  2124.     # check if repo is supported (has entries in sources-all)
  2125.     LINE="`grep -m1 "^$FBACK|" ${HOME}/.pkg/sources-all 2>/dev/null | grep -v ^'#' | grep -v "^${REPONAME}|"`"
  2126.     [ "$LINE" = "" ] && continue
  2127.  
  2128.     # if repo is valid, add to users list of in-use repos (only valid repos from sources-all => sources)
  2129.     if [ -f "$REPO_DB_FILE_DIR/`echo "$LINE" | cut -f3 -d'|'`" ]; then
  2130.  
  2131.       if [ "$(grep -m1 "^$FBACK|" ${HOME}/.pkg/sources)" = "" ]; then
  2132.         echo "Adding repo: `echo "$LINE"|cut -f1 -d'|'`.."
  2133.         echo "$LINE" >> ${HOME}/.pkg/sources
  2134.       fi
  2135.     fi
  2136.   done
  2137.  
  2138.   cleaned_repo_list="`cat ${HOME}/.pkg/sources-all 2>/dev/null | grep -v ^'#'| grep -v ^$`"
  2139.  
  2140.   # now add any other repos to the list (repos that are installed, but not added from fallback list)
  2141.   echo "$cleaned_repo_list" | uniq | while read repo_entry
  2142.   do
  2143.     # dont add current repo, its already added
  2144.     [ "`echo "$repo_entry" | cut -f1 -d'|'`" = "$REPONAME" ] && echo "Adding repo: $REPONAME.." && continue
  2145.  
  2146.     # get the repo name
  2147.     repo_name="`echo "$repo_entry"|cut -f1 -d'|'`"
  2148.  
  2149.     # build the repo file (full path)
  2150.     repo_file="$REPO_DB_FILE_DIR"/`echo "$repo_entry" | cut -f3 -d'|'`
  2151.  
  2152.     # set a flag true if repo already in repo, false if not
  2153.     already_in_repo=false
  2154.     [ "`grep -m1 "^$repo_entry" ${HOME}/.pkg/sources 2>/dev/null`" != "" ] && already_in_repo=true
  2155.  
  2156.     if [ -f "$repo_file" -a "$already_in_repo" = false ]; then
  2157.       echo "Adding repo: $repo_name.."
  2158.       echo "$repo_entry" >> ${HOME}/.pkg/sources
  2159.     fi
  2160.   done
  2161.  
  2162.   # finished, print message
  2163.   [ -f ${HOME}/.pkg/sources ] && echo "Sources updated." && . ${PKGRC}
  2164. }
  2165.  
  2166. parse_sources_list_opts(){
  2167.     local line="${1/#deb /}" #Remove
  2168.    
  2169.     #stripped="$(echo $line | sed -e '^[[^\[]*\]*$//g')"
  2170.    
  2171.     #local opt_sect="$(sed -r 's|^(\[[^\[]*\]*).*$|/1|')"
  2172.     #opt_sect={$opt_sect:1:-1}
  2173.    
  2174.       if [ ${line:0:1} = "[" ]; then
  2175.         local ind=`expr index "$line" "]"`
  2176.         local ind2=$((ind - 1))
  2177.         local line=${line:1:$ind2}
  2178.       fi
  2179.    
  2180.     set -- $line
  2181.     for opt in "$@"; do
  2182.       opt="$(echo $opt | tr "-" "_")"
  2183.       echo "export SOURCES_LIST_OPT__$opt"
  2184.     done   
  2185. }
  2186. update_repo(){                    # update the current repo from a file stored online FUNCLIST
  2187.  
  2188.   # check internet, net connection required
  2189.   NET="`check_net`"; [ $NET -eq 1 ] && echo "You need an internet connection" && exit 1
  2190.  
  2191.   # remove the repo update tmp file
  2192.   rm -f $TMPDIR/update_repo_results 2>/dev/null
  2193.  
  2194.   echo "Updating system repositories, please wait.."
  2195.   echo
  2196.  
  2197.   # use petget for now .. not ideal, petget wont accept $1 and only do that repo,
  2198.   # also petget uses loads of other files pkg doesnt have/need (in $REPO_DIR)
  2199.   # also, we're limited to updating only the repos that petget supports, not all the ones Pkg supports..
  2200.   # .. on the plus side petget code is way faster than mine
  2201.   mkdir -p /var/local/petget/
  2202.   chmod 777 /var/local/petget/
  2203.   echo 'false' > /var/local/petget/db_verbose
  2204.  
  2205.   # now call petget 0setup.. the ENV options prevent popup windows, and need for user input
  2206.   #DISPLAY='' SETUPCALLEDFROM=ppm /usr/local/petget/0setup &>$TMPDIR/update_repo_results
  2207.  
  2208.   # if the repos updated ok
  2209.   if [ $? -eq 0 ]; then
  2210.     [ "`which logger`" != '' ] && logger "$0 Repo files updated by $APP $APPVER"
  2211.     echo -e "Repo files updated:"
  2212.     grep ^Processing $TMPDIR/update_repo_results | cut -f2 -d' '
  2213.     # remove the repo update tmp file
  2214.     rm -f $TMPDIR/update_repo_results 2>/dev/null
  2215.   else
  2216.     # repo did not update ok
  2217.     error "Repos NOT updated."
  2218.     cat $TMPDIR/update_repo_results | tail -20
  2219.     exit 2
  2220.   fi
  2221.  
  2222.   # update Pkg created, user-added (third-party) repos
  2223.   if [ -f ~/.pkg/sources-user ] || [ -f /etc/apt/sources.list ] || [ -f /etc/slackpkg/mirrors ]; then
  2224.     echo
  2225.     echo "Updating third-party repos.. This may take a while.."
  2226.     echo
  2227.   fi
  2228.  
  2229.   # update Pkg created repos
  2230.   if [ -f ~/.pkg/sources-user ]; then
  2231.     pkg_repos="$(sort -u ~/.pkg/sources-user | grep -v ^$ | uniq | cut -f1 -d'|')"
  2232.     for pkg_repo in $pkg_repos
  2233.     do
  2234.       local pkg_repo_url="$(cat ~/.pkg/sources-user | grep -m1 "^$pkg_repo|" | cut -f4 -d'|')"
  2235.       echo "Processing:  $pkg_repo_url"
  2236.  
  2237.       ANSWER=y
  2238.       if [ "$ASK" = true ]; then
  2239.         bash -c 'read -r -N 1 -p "Update repo $pkg_repo? [y/n] " ANSWER; echo $ANSWER > /tmp/pkg/ANSWER'
  2240.         echo
  2241.         ANSWER="$(cat /tmp/pkg/ANSWER)"
  2242.       fi
  2243.  
  2244.       if [ "$ANSWER" = 'y' ] || [ "$ANSWER" = 'Y' ]; then
  2245.         echo "Please wait..."
  2246.         add_pkg_repo ${pkg_repo_url}install 1>/dev/null && echo -e "${green}Success${endcolour}: Updated repo '$pkg_repo'"
  2247.       fi
  2248.     done
  2249.   fi
  2250.  
  2251.   # update third-party non-native repos.. These repos comes 'from source',
  2252.   # and are not in native Puppy format - they need to be downloaded, converted
  2253.   # into the Puppy format, then installed... so this will be slow..
  2254.   if [ -f /etc/apt/sources.list ] || [ -f /etc/slackpkg/mirrors ]; then
  2255.  
  2256.     # update third-party Ubuntu/Debian repos
  2257.     # search inside /etc/apt/sources.list /etc/apt/sources.list.d/*.list
  2258.     # ...look for lines starting with 'deb ', ignore others
  2259.     # ...(use grep -h, to remove the preppended filenames if grepping multiple files)
  2260.     # ...remove any arch stuff from the entries, ppa2pup will handle that,
  2261.     # ...convert spaces to | chars, so we can process each line as a whole later
  2262.     #local apt_sources_list="$(grep -h '^deb ' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null \
  2263.     #  | sed \
  2264.     #    -e "s/^deb //g" \
  2265.     #    -e "s/^tor+//g" \
  2266.     #    -e 's/\[arch=[a-z,0-9].*\] //g' \
  2267.     #    -e 's/ /|/g'\
  2268.     #)"
  2269.     local apt_sources_list="$(grep -h '^deb ' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null \
  2270.      | sed \
  2271.      -e "s/^deb //g" \
  2272.      -e "s/^tor+//g" \
  2273.      -e 's/ /|/g'\
  2274.    )"
  2275.     # for each repo in $apt_sources_list, use `ppa2pup` to update the repo
  2276.     for line in $apt_sources_list
  2277.     do
  2278.       [ "$line" = "" ] && continue
  2279.       [ "$line" = "\n" ] && continue
  2280.       if [ ${line:0:1} = "[" ]; then
  2281.         local ind=`expr index "$line" "]"`
  2282.         local ind2=$((ind - 1))
  2283.         local line_opts=${line:1:$ind2}
  2284.       else
  2285.         local line_opts=''
  2286.       fi
  2287.    
  2288.       local ppa_repo_url="$(echo ${line//|/ } | cut -f1 -d" ")"
  2289.       local ppa_repo_name=$(cat ~/.pkg/sources-all | grep -m1 $ppa_repo_url | cut -f1 -d'|')
  2290.  
  2291.       # if a PPA repo, get a user-friendly repo name from the /etc/sources.list entry
  2292.       if [ "$ppa_repo_name" = "" ]; then
  2293.         ppa_repo_name="$(echo ${line} | cut -f3 -d'|')-$(echo $line | cut -f2 -d':' | cut -f1 -d '/' | tr -d '-' | tr -d '_')"
  2294.       fi
  2295.  
  2296.       echo
  2297.       echo "Processing:  ${line//|/ }"
  2298.  
  2299.       ANSWER=y
  2300.       if [ "$ASK" = true ]; then
  2301.         # ask user to update repo
  2302.         bash -c 'read -r -N 1 -p "Update repo $ppa_repo_name? [y/n] " ANSWER; echo $ANSWER > /tmp/pkg/ANSWER'
  2303.         echo
  2304.         ANSWER="$(cat /tmp/pkg/ANSWER)"
  2305.       fi
  2306.  
  2307.       if [ "$ANSWER" = 'y' ] || [ "$ANSWER" = 'Y' ]; then
  2308.         echo "Please wait..."
  2309.         (
  2310.           if [ ! -z "$line_opts" ]; then
  2311.            eval parse_sources_list_opts "$line_opts"          
  2312.           fi
  2313.           ppa2pup ${line//|/ } 1>/dev/null && echo -e "${green}Success${endcolour}: Updated repo '$ppa_repo_name'"
  2314.         )
  2315.         retval=$?
  2316.       fi
  2317.     done
  2318.  
  2319.     # update third-party Slackware repos
  2320.     if [ -f /etc/slackpkg/mirrors ]; then
  2321.       local slack_repos="$(sort -u /etc/slackpkg/mirrors | uniq)"
  2322.       local slack_repo_url=""
  2323.  
  2324.       for slack_repo in $slack_repos
  2325.       do
  2326.         slack_repo_url="${slack_repo//PACKAGES.TXT.gz/}"
  2327.         slack_repo_url="${slack_repo_url//\/.gz/}"
  2328.         slack_repo_url="${slack_repo_url//.gz\//}"
  2329.         slack_repo_url="${slack_repo_url//PACKAGES.TXT/}"
  2330.  
  2331.         # add trailing slash, if needed
  2332.         if [ "$(echo $slack_repo_url | grep '/$')" = '' ]; then
  2333.           slack_repo_url="${slack_repo_url}/"
  2334.         fi
  2335.  
  2336.         local slack_repo_name=$(cat ~/.pkg/sources-all | grep -m1 $slack_repo_url | cut -f1 -d'|')
  2337.  
  2338.         echo
  2339.         echo "Processing:  $slack_repo_url"
  2340.  
  2341.         ANSWER=y
  2342.         if [ "$ASK" = true ]; then
  2343.           # ask user to update repo
  2344.           bash -c 'read -r -N 1 -p "Update repo '"$slack_repo_name"'? [y/n] " ANSWER; echo $ANSWER > /tmp/pkg/ANSWER'
  2345.           echo
  2346.           ANSWER="$(cat /tmp/pkg/ANSWER)"
  2347.         fi
  2348.  
  2349.         if [ "$ANSWER" = 'y' ] || [ "$ANSWER" = 'Y' ]; then
  2350.           echo "Please wait..."
  2351.           slack2pup "${slack_repo_url}/PACKAGES.TXT" $slack_repo_name 1>/dev/null && echo -e "${green}Success${endcolour}: Updated repo '$slack_repo_name'"
  2352.         fi
  2353.       done
  2354.     fi
  2355.   fi
  2356.  
  2357.   echo
  2358.   exit 0
  2359.  
  2360. }
  2361.  
  2362.  
  2363. list_sources(){                   # return available (matching) repos (~/.pkg/sources) FUNCLIST
  2364.   grep -m1 "^$1" ${HOME}/.pkg/sources ${HOME}/.pkg/sources-user | cut -f2-99 -d':' | uniq
  2365. }
  2366.  
  2367.  
  2368. list_all_sources(){               # return all (or matching) repos (~/.pkg/sources-all) FUNCLIST
  2369.   grep -m1 "^$1" ${HOME}/.pkg/sources-all ${HOME}/.pkg/sources-user | cut -f2-99 -d':' | uniq
  2370. }
  2371.  
  2372.  
  2373. convert_repofile(){               # convert repo files formats (pre-woof/post-woof) FUNCLIST
  2374.  
  2375.   # get the file name (FILENAME) and full path (FILE)
  2376.   FILENAME="`basename "$1"`"
  2377.   FILE="${CURDIR}/${FILENAME}"
  2378.  
  2379.   # check for valid options
  2380.   [ ! -f "$FILE" ] && print_usage repo-convert && exit 1
  2381.  
  2382.   # dont replace repo unless -f was given
  2383.   [ "$FORCE" != true -a -f "$REPO_DB_FILE_DIR/${FILENAME}" ] && echo "File '$REPO_DB_FILE_DIR/$FILENAME' already exists."  && exit 1
  2384.  
  2385.   # remove tmp files
  2386.   rm $TMPDIR/$FILENAME &>/dev/null
  2387.   rm $TMPDIR/${FILENAME}_subdirs &>/dev/null
  2388.  
  2389.   # check repo file format (woof or pre-woof)
  2390.   if [ -f "$FILE" -a "`cat "$FILE" | head -1 | grep -m1 '^"'`" = "" ]; then #if is a new format  #'
  2391.  
  2392.     # convert woof repo file to pre-woof repo file.. takes ages..
  2393.     echo "Converting '${FILE}' to pre-woof format.. This might take a while.."
  2394.     cat "$FILE" | while read LINE
  2395.     do
  2396.       PKGNAME="`echo $LINE| cut -f1 -d'|'`"
  2397.       PKGNAME1=''
  2398.       # we need to get the package name, try lots of different extensions
  2399.       [ "`echo $PKGNAME1 | grep ".deb\$"`" != "" ]  && PKGNAME1="`echo $LINE| cut -f8 -d'|'`"
  2400.       [ "`echo $PKGNAME1 | grep ".deb\$"`" != "" ]  && PKGNAME="`basename $PKGNAME1 .deb`"
  2401.       [ "`echo $PKGNAME1 | grep ".rpm\$"`" != "" ]  && PKGNAME="`basename $PKGNAME1 .rpm`"
  2402.       [ "`echo $PKGNAME1 | grep ".txz\$"`" != "" ]  && PKGNAME="`basename $PKGNAME1 .txz`"
  2403.       [ "`echo $PKGNAME1 | grep ".tgz\$"`" != "" ]  && PKGNAME="`basename $PKGNAME1 .tgz`"
  2404.       [ "`echo $PKGNAME1 | grep ".tar.xz\$"`" != "" ] && PKGNAME="`basename $PKGNAME1 .tar.xz`"
  2405.       [ "`echo $PKGNAME1 | grep ".tar.gz\$"`" != "" ] && PKGNAME="`basename $PKGNAME1 .tar.gz`"
  2406.       # get size
  2407.       SIZE=" `echo $LINE| cut -f6 -d'|'`"
  2408.       # get category
  2409.       CAT="`echo $LINE| cut -f5 -d'|'`"
  2410.       #150813 remove extra categories .. example 'Setup;Installation' .. remove 'Installation'
  2411.       [ "`echo "$CAT" | grep ';'`" != "" ] && CAT="`echo "$CAT" | cut -f1 -d';'`"
  2412.       # get sub dir in repo
  2413.       SUBDIR="`echo $LINE| cut -f7 -d'|'`" #150213
  2414.       # get deps
  2415.       DEPS=" `echo $LINE| cut -f9 -d'|'| grep '+'`"
  2416.       # get desc
  2417.       DESC="`echo $LINE| cut -f10 -d'|'`"
  2418.       # add repo entry to tmp file
  2419.       [ "$PKGNAME" != "" ] && echo "\"$PKGNAME\" \"$PKGNAME: ${DESC//:/,}\" off \"$CAT$DEPS$SIZE\" /" | sed -e 's/  / /g' -e 's/,,/,/g' -e 's/, ,/,/g' | sort --field-separator='-' -k1,1d -k2gr -k3gr -k4gr | uniq >> $TMPDIR/$FILENAME
  2420.       #150213 now do subdirs... slow..
  2421.       [ "`echo $SUBDIR | grep "/"`" != "" -a "`echo $SUBDIR | grep -i pet_packages`" = "" ] && echo "$PKGNAME|/$SUBDIR" >> $TMPDIR/${FILENAME}_subdirs
  2422.     done
  2423.     # done making a pre-woof repo file, print message
  2424.     [ -f $TMPDIR/$FILENAME ] && echo "Finished converting $FILENAME to pre-woof format." || exit 1
  2425.  
  2426.   else
  2427.  
  2428.     # convert pre-woof repo file to woof repo file.. takes ages..
  2429.     echo "Converting '$FILENAME' to a 'woof' compatible repo file. This could take a while..."
  2430.  
  2431.     # parse a pre-woof repo file
  2432.     cat "$FILE" | while read LINE
  2433.     do
  2434.       PKGNAME='' PKGNAMEONLY='' PKGVER='' SIZE='' CAT='' DEPS='' BUILD='' SUBDIR='' PKGEXT='.pet' DESC=''
  2435.  
  2436.       PKGNAME="`echo "$LINE" | cut -d'"' -f2`" #'micro
  2437.       [ "`echo $PKGNAME | grep ".deb\$"`" != "" ]     && PKGEXT='.deb'    && PKGNAME="`basename $PKGNAME1 .deb`"
  2438.       [ "`echo $PKGNAME | grep ".pet\$"`" != "" ]     && PKGEXT='.pet'    && PKGNAME="`basename $PKGNAME1 .pet`"
  2439.       [ "`echo $PKGNAME | grep ".rpm\$"`" != "" ]     && PKGEXT='.rpm'    && PKGNAME="`basename $PKGNAME1 .rpm`"
  2440.       [ "`echo $PKGNAME | grep ".tcz\$"`" != "" ]     && PKGEXT='.tcz'    && PKGNAME="`basename $PKGNAME1 .tcz`"
  2441.       [ "`echo $PKGNAME | grep ".tgz\$"`" != "" ]     && PKGEXT='.tgz'    && PKGNAME="`basename $PKGNAME1 .tgz`"
  2442.       [ "`echo $PKGNAME | grep ".txz\$"`" != "" ]     && PKGEXT='.txz'    && PKGNAME="`basename $PKGNAME1 .txz`"
  2443.       [ "`echo $PKGNAME | grep ".tar.gz\$"`" != "" ]    && PKGEXT='.tar.gz'   && PKGNAME="`basename $PKGNAME1 .tar.gz`"
  2444.       [ "`echo $PKGNAME | grep ".tar.xz\$"`" != "" ]    && PKGEXT='.tar.xz'   && PKGNAME="`basename $PKGNAME1 .tar.xz`"
  2445.       [ "`echo $PKGNAME | grep ".pkg.tar.gz\$"`" != "" ]  && PKGEXT='.pkg.tar.gz' && PKGNAME="`basename $PKGNAME1 .pkg.tar.gz`"
  2446.       [ "`echo $PKGNAME | grep ".pkg.tar.xz\$"`" != "" ]  && PKGEXT='.pkg.tar.xz' && PKGNAME="`basename $PKGNAME1 .pkg.tar.xz`"
  2447.  
  2448.       # get pkg name only .. without versions or suffix
  2449.       PKGNAME_ONLY="`echo "${PKGNAME}" | sed -e 's/-[0-9.]*/-/g' -e 's/-$//'`"
  2450.       # if that didnt work, use the old method
  2451.       if [ "$PKGNAME_ONLY" = '' -o "$PKGNAME_ONLY" = "$PKGNAME" ]; then
  2452.         # get pkg name without version
  2453.         PKGNAMEONLY="`echo $PKGNAME | cut -d'-' -f1`"
  2454.       fi
  2455.       PKGNAME_ONLY="${PKGNAME_ONLY//-*/}"
  2456.  
  2457.       # get pkg version
  2458.       PKGVER="`LANG=C echo "$LINE" | sed -e 's/^[^0-9]*-//g' | cut -f1 -d'_' | cut -f1 -d'-'`"
  2459.       # get pkg size
  2460.       SIZE="`echo $LINE| cut -d'"' -f6 | cut -d' ' -f3`" #'micro
  2461.       SIZE=${SIZE## }
  2462.       SIZE=${SIZE%% }
  2463.       # must check again if pkg had no deps
  2464.       [ "$SIZE" = "" ] && SIZE=" `echo $LINE| cut -d'"' -f6|cut -d' ' -f2`"
  2465.       # get pkg category
  2466.       CAT="`echo $LINE | cut -d'"' -f6 | cut -d' ' -f1`"
  2467.       # remove extra categories
  2468.       [ "`echo "$CAT" | grep ';'`" != "" ] && CAT="`echo "$CAT" | cut -f1 -d';'`"
  2469.       # get pkg deps
  2470.       DEPS="`echo "$LINE" | cut -d'"' -f6 | cut -d' ' -f2 | grep ^'+'`"
  2471.       DESC="`echo "$LINE" | cut -f10 -d'|'`" #'micro
  2472.       # build the woof compatible repo file
  2473.       [ "$PKGNAME" != "" ] && echo "$PKGNAME|$PKGNAMEONLY|$VER|$BUILD|$CAT|$SIZE|$SUBDIR|${PKGNAME}${PKGEXT}|$DEPS|$DESC|$DISTRO_BINARY_COMPAT|$DISTRO_COMPAT_VERSION||" >> $TMPDIR/$FILENAME
  2474.     done
  2475.     #done making a woof repo file, print message
  2476.     [ -f $TMPDIR/$FILENAME ] && echo "Finished converting $FILENAME to woof format." || exit 1
  2477.   fi
  2478.  
  2479.   # if we are updating the repo, we dont wanna install the converted file straight over
  2480.   # the actual repo file, we wanna parse it for new pkgs and add only those
  2481.   if [ "$2" != "for_repo_update" ]; then
  2482.     # we converted a repo that we actually wanna install, so install it
  2483.     mv $TMPDIR/$FILENAME "$REPO_DB_FILE_DIR/$FILENAME"
  2484.     mv $TMPDIR/${FILENAME}_subdirs "$REPO_DB_FILE_DIR/${FILENAME}_subdirs" 2>/dev/null
  2485.     echo "Repo file '$REPO_DB_FILE_DIR/$FILENAME' created."
  2486.     update_sources #210613 update the list of sources after adding the newly converted repo
  2487.     #exit 0
  2488.   else
  2489.     mv "$TMPDIR/$FILENAME" "$REPO_DB_FILE_DIR/$FILENAME"
  2490.   fi
  2491. }
  2492.  
  2493.  
  2494. print_repo_info(){                # get repo settings, return current repo name FUNCLIST
  2495.  
  2496.   # get latest repo info (from sources file), or from PKGRC if that fails
  2497.   [ "$1" ] && get_repo_info "$1" || . ${PKGRC}
  2498.  
  2499.   local pkg_count=$(cat "$REPO_DB_FILE_DIR/${REPOFILE}" | wc -l)
  2500.  
  2501.   # output the repo info
  2502.   echo "- Repo:          $REPONAME"
  2503.   echo "- Repo file:     $REPOFILE"
  2504.   echo "- Package Type:  $EX"
  2505.   echo "- Packages:      $pkg_count"
  2506.   echo "- URL Mirror 1:  `echo $REPOURL1 | cut -f1-3 -d'/'`"
  2507.   [ "$REPOURL2" != "" ] && echo "- URL Mirror 2:  `echo $REPOURL2  | cut -f1-3 -d'/'`"
  2508.   [ "$REPOURL3" != "" ] && echo "- URL Mirror 3:  `echo $REPOURL3  | cut -f1-3 -d'/'`"
  2509.   [ "$REPOURL4" != "" ] && echo "- URL Mirror 4:  `echo $REPOURL4  | cut -f1-3 -d'/'`"
  2510.   echo
  2511.   echo "- Fall back to:`echo $REPOFALLBACKS | fold -w 50 -s | sed -e "s/ /, /g" -e "s/^/  /g"`"
  2512. }
  2513.  
  2514.  
  2515.  
  2516. # pkg search funcs
  2517.  
  2518. hide_blacklisted_pkgs_from_search_results(){ # hide BUILTIN and installed pkgs from searches NOLIST
  2519.  
  2520.   # dont hide package if using --force (the user may want to force a re-install of already installed pkg, for example)
  2521.   [ "$FORCE" = true ] && return 0
  2522.  
  2523.   # get pkg names (generic names, no versions) of all blacklisted packages in pipe delimited list
  2524.   blacklisted_pkgs_list="$(echo $PKG_NAME_IGNORE | sed -e 's/ /|/g')"
  2525.  
  2526.   # remove blacklisted packages from search list
  2527.   cat $TMPDIR/pkglist | grep -v -E "'$blacklisted_pkgs_list'" > $TMPDIR/pkglist_without_blacklisted
  2528.   mv $TMPDIR/pkglist_without_blacklisted $TMPDIR/pkglist
  2529.  
  2530.   # clean up tmp files
  2531.   rm $TMPDIR/pkglist_* &>/dev/null
  2532. }
  2533.  
  2534.  
  2535. hide_installed_pkgs_from_search_results(){  # hide BUILTIN and installed pkgs from searches NOLIST
  2536.  
  2537.   # dont hide package if using --force (the user may want to force a re-install of already installed pkg, for example)
  2538.   [ "$FORCE" = true ] && return 0
  2539.  
  2540.   # reset tmp file
  2541.   rm $TMPDIR/pkglist_inst &>/dev/null
  2542.  
  2543.   # get pkg names (generic names, no versions) of all installed pkgs (builtins, devx and user installed)
  2544.   inst_pkg_list="`cut -f1 -d'|' "$USER_INST_PKGS_FILE" \
  2545.    "$DEVX_INST_PKGS_FILE" \
  2546.    "$WOOF_INST_PKGS_FILE" 2>/dev/null \
  2547.    | grep -v ^$ \
  2548.    | tr '\n' '|' \
  2549.    | sed -e "s/||/|/g" \
  2550.    | sed -e "s/|\$//g"`"
  2551.  
  2552.   # remove woof and user installed packages from search list
  2553.   cat $TMPDIR/pkglist | grep -v -E "'$inst_pkg_list'" > $TMPDIR/pkglist_without_inst
  2554.   mv $TMPDIR/pkglist_without_inst $TMPDIR/pkglist
  2555.  
  2556.   # clean up tmp files
  2557.   rm $TMPDIR/pkglist_* &>/dev/null
  2558. }
  2559.  
  2560.  
  2561. list_pkg_names(){                 # list pkg names in current repo only ($1 is optional filter) FUNCLIST
  2562.   #TODO add option to return more than just package names.
  2563.   local MATCH_FLEXIBILITY
  2564.   if [ ! -z "$2" ]; then
  2565.      MATCH_FLEXIBILITY="$2"
  2566.   else
  2567.     if [ ! -z "$PKG_MATCH_FLEXIBILITY" ]; then
  2568.        MATCH_FLEXIBILITY="$PKG_MATCH_FLEXIBILITY"
  2569.     else
  2570.       MATCH_FLEXIBILITY=3
  2571.     fi
  2572.   fi
  2573.   # remove any previous searches
  2574.   rm $TMPDIR/pkglist* 2>/dev/null
  2575.  
  2576.   # get current repo ($REPOFILE)
  2577.   . ${PKGRC}
  2578.  
  2579.   # create the search results
  2580.   for MF in 0 1 2 3 4; do
  2581.     if [ $MF -eq 0 ]; then
  2582.       cut -f1 -d'|' "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | grep "^${1}|" > $TMPDIR/pkglist
  2583.     elif [ $MF -eq 1 ]; then
  2584.       cut -f1 -d'|' "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | grep -F "|${1}|" > $TMPDIR/pkglist
  2585.     elif [ $MF -eq 2 ]; then
  2586.      cut -f1 -d'|' "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | grep "^${1}\(-\|_\)" > $TMPDIR/pkglist
  2587.     elif [ $MF -ge 3 ]; then    
  2588.       cut -f1 -d'|' "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | grep "^$1" > $TMPDIR/pkglist
  2589.     fi
  2590.     # filter out builtin and user installed packages
  2591.     if [ $HIDE_INSTALLED = true ]; then
  2592.       hide_installed_pkgs_from_search_results
  2593.     fi
  2594.  
  2595.     hide_blacklisted_pkgs_from_search_results
  2596.     if [ $MF -ge 1 ]; then
  2597.     # support pkg name aliases in finding packages
  2598.       if [ $NO_ALIASES = false ]; then
  2599.         local ALIAS_LIST
  2600.         local ALIAS
  2601.         local ALIAS_RES
  2602.         # if we have some results to parse
  2603.         if [ "$1" != "" -a -f $TMPDIR/pkg_aliases ]; then
  2604.           # get the list of aliases
  2605.           ALIAS_LIST="`grep -m1 "$1" $TMPDIR/pkg_aliases  2>/dev/null | tr ',' ' '`";
  2606.           # for each alias
  2607.           echo $ALIAS_LIST | while read ALIAS
  2608.           do
  2609.             [ "$ALIAS" = '' ] && continue
  2610.             # get the match from the current repo (if any)
  2611.             if [ $MF -eq 1 ]; then
  2612.               ALIAS_RES="$(LANG=C cut -f1 -d'|' "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | grep "|$ALIAS|")"
  2613.             elif [ $MF -eq 2 ]; then
  2614.               ALIAS_RES="$(LANG=C cut -f1 -d'|' "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | grep "^${ALIAS}\(-\|_\)")"
  2615.            elif [ $MF -ge 3 ]; then    
  2616.              ALIAS_RES="$(LANG=C cut -f1 -d'|' "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | grep "^${ALIAS}")"
  2617.            fi          
  2618.            [ ! "$ALIAS_RES" ] && ALIAS_RES="`LANG=C cut -f2 -d'|' "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | grep "^$ALIAS"`" #'
  2619.            # if the repo match was found in the search results
  2620.            if [ "$ALIAS_RES" != "" ]; then
  2621.              # add the alias results to the search results
  2622.              echo "$ALIAS_RES" >> $TMPDIR/pkglist
  2623.            fi
  2624.          done # for each alias
  2625.          # sort and clean the search results
  2626.          LANG=C cat $TMPDIR/pkglist | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq > $TMPDIR/pkglist1
  2627.          # replace the original search results
  2628.          mv $TMPDIR/pkglist1 $TMPDIR/pkglist
  2629.        fi
  2630.      fi
  2631.    fi
  2632.    mapfile -n 2 < $TMPDIR/pkg_aliases #https://stackoverflow.com/questions/25947072/how-to-check-that-a-file-has-more-than-1-line-in-a-bash-conditional
  2633.    if ((${#MAPFILE[@]}>1)); then
  2634.       #echo "This file has more than 1 line."
  2635.       break
  2636.    fi
  2637.    [ $MF -ge $MATCH_FLEXIBILITY ] && break
  2638.  done
  2639.  # return the search results
  2640.  [ -s $TMPDIR/pkglist ] && cat $TMPDIR/pkglist 2>/dev/null
  2641.  
  2642.  # clean up
  2643.  [ ! -f $TMPDIR/pkglist ] && exit 1
  2644.  rm $TMPDIR/pkglist* 2>/dev/null
  2645.  
  2646. }
  2647.  
  2648.  
  2649. list_all_pkg_names(){             # list pkg names in any repo  ($1 is optional filter)FUNCLIST
  2650.  
  2651.  # remove any previous search results
  2652.  rm $TMPDIR/pkglist &>/dev/null
  2653.  
  2654.  # if bleeding edge disabled, output the list repo by repo, in the fallback order, current repo first (that order is set in update_sources)
  2655.  repo_file_list | while read repo_file
  2656.  do
  2657.    cut -f1 -d'|' "$REPO_DB_FILE_DIR/${repo_file}" 2>/dev/null | grep "^$1" | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr >> $TMPDIR/pkglist
  2658.  done
  2659.  
  2660.  # if bleeding edge enabled, re-order the whole list, so Pkg returns the most recent pgk versions from ANY repos
  2661.  if [ "$BLEDGE" = "yes" ]; then
  2662.    LANG=C cat $TMPDIR/pkglist  2>/dev/null | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr > $TMPDIR/pkglist1
  2663.    mv $TMPDIR/pkglist1 $TMPDIR/pkglist
  2664.  fi
  2665.  
  2666.  # filter out built-in and user installed packages
  2667.  if [ $HIDE_INSTALLED = true ]; then
  2668.    hide_installed_pkgs_from_search_results
  2669.  fi
  2670.  
  2671.  hide_blacklisted_pkgs_from_search_results
  2672.  
  2673.  # support pkg name aliases in finding packages
  2674.  if [ $NO_ALIASES = false ]; then
  2675.    local ALIAS_LIST
  2676.    local ALIAS
  2677.    local ALIAS_RES
  2678.    # if we have some results to parse
  2679.    if [ "$1" != "" -a -f $TMPDIR/pkg_aliases ]; then
  2680.      # get the list of aliases
  2681.      ALIAS_LIST="`grep -m1 "$1" $TMPDIR/pkg_aliases  2>/dev/null | tr ',' ' '`";
  2682.      # for each repo
  2683.      LANG=C repo_file_list | while read RF
  2684.      do
  2685.        # and for each alias
  2686.        for ALIAS in $ALIAS_LIST; do
  2687.          # get the match from the current repo (if any)
  2688.          ALIAS_RES="`LANG=C cut -f1 -d'|' "$REPO_DB_FILE_DIR/${RF}" 2>/dev/null | grep -m1 "^$ALIAS"`"
  2689.          [ ! "$ALIAS_RES" ] && ALIAS_RES="`LANG=C cut -f2 -d'|' "$REPO_DB_FILE_DIR/${RF}" 2>/dev/null | grep -m1 "^$ALIAS"`"
  2690.          # if the repo match was found in the search results
  2691.          if [ "$ALIAS_RES" != "" ]; then
  2692.            # add the alias results to the search results
  2693.            echo "$ALIAS_RES" >> $TMPDIR/pkglist
  2694.          fi
  2695.        done # for each alias
  2696.      done # for each repo
  2697.    fi
  2698.  fi
  2699.  
  2700.  # return the search results
  2701.  [ -s $TMPDIR/pkglist ] && LANG=C cat $TMPDIR/pkglist | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq
  2702.  
  2703.  # clean up
  2704.  [ ! -f $TMPDIR/pkglist ] && exit 1
  2705.  rm $TMPDIR/pkglist &>/dev/null
  2706. }
  2707.  
  2708.  
  2709. list_build_scripts(){             # list available build scripts  ($1 is optional filter)FUNCLIST
  2710.  
  2711.  # get settings from RC file
  2712.  . ${PKGRC}
  2713.  
  2714.  # make sure PetBuild is ready
  2715.  [ "$BUILDTOOL" = "petbuild" ] && prepare_petbuild
  2716.  
  2717.  # if no option given, just sort the results
  2718.  [ "$1" != "" ] && FILTER="grep -i $1" || FILTER="sort"
  2719.  
  2720.  # different build tools have their build scripts in different places.
  2721.  # check which BUILDTOOL we are using and list its build scripts
  2722.  case $BUILDTOOL in
  2723.    petbuild)
  2724.      echo "$(LANG=C ls -R -1 /usr/share/petbuild/ 2>/dev/null | grep -v ^'/' | grep -v ^$ | grep '.petbuild' | sed -e "s/.petbuild$//g" | sort | $FILTER)" | grep -v ^$
  2725.    ;;
  2726.    buildpet)
  2727.      echo "$(LANG=C ls -R -1 /usr/share/buildpet/ 2>/dev/null  | grep -v ^'/' | grep -v ^$ | grep -v 'buildpet.profile' | sed -e "s/.bp$//g" | sort | $FILTER)" | grep -v ^$
  2728.    ;;
  2729.    src2pkg)
  2730.      echo "Use: src2pkg FILE|URL or src2pkg -h"
  2731.    ;;
  2732.    sbopkg)
  2733.      sbopkg
  2734.    ;;
  2735.    *)
  2736.      echo "No build system configured. Set BUILDTOOL in $PKGRC."
  2737.      echo "Supported build tools: petbuild, buildpet, src2pkg, sbopkg"
  2738.    ;;
  2739.  esac
  2740. }
  2741.  
  2742.  
  2743. list_downloaded_pkgs(){           # list packages downloaded in WORKDIR ($1 is optional filter) FUNCLIST
  2744.  
  2745.  . ${PKGRC}
  2746.  
  2747.  cd "$WORKDIR" || { error "Cant cd into $WORKDIR"; exit 3; }
  2748.  
  2749.  local pkg
  2750.  
  2751.  # if no pkg given, list all
  2752.  if [ ! "$1" ]; then
  2753.  
  2754.    # get all pkgs in WORKDIR
  2755.    find "${WORKDIR}" -maxdepth 1 -type f | sed -e "s#${WORKDIR}##g" -e "s#^/##g" | grep -v ^'.pkg' | sort \
  2756.       | while read pkg # but go through each and only print it if a valid pkg
  2757.       do
  2758.         [ "`is_local_pkg "$pkg"`" = true ] && echo $pkg || continue
  2759.       done
  2760.  
  2761.     return 0
  2762.  
  2763.   else
  2764.  
  2765.     # $1 might be a pkg, or list of pkgs, loop through them
  2766.     for x in $1; do
  2767.       if [ "$x" != "-" ]; then
  2768.  
  2769.         # print the list of packages matching $x
  2770.         find "${WORKDIR}" -maxdepth 1 -type f | sed -e "s#${WORKDIR}##g" -e "s#^/##g" | grep -v ^'.pkg'|grep ^"$x" | sort \
  2771.           | while read pkg # but go through each and only print it if a valid pkg
  2772.           do
  2773.             [ "`is_local_pkg "$pkg"`" = true ] && echo $pkg || continue
  2774.           done
  2775.  
  2776.         return 0
  2777.       fi
  2778.     done
  2779.   fi
  2780.  
  2781.   cd -
  2782. }
  2783.  
  2784.  
  2785. list_installed_pkgs(){            # list user installed packages ($1 is optional filter) FUNCLIST
  2786.  
  2787.   local user_pkgs_list=''
  2788.   local builtins_list=''
  2789.   local devx_pkgs_list=''
  2790.  
  2791.   user_pkgs_list="$USER_INST_PKGS_FILE"
  2792.  
  2793.   if [ "$HIDE_BUILTINS" != true -a -f "$DEVX_INST_PKGS_FILE" ]; then
  2794.     devx_pkgs_list="$DEVX_INST_PKGS_FILE"
  2795.   fi
  2796.  
  2797.   [ "$HIDE_BUILTINS" != true ] && builtins_list="$WOOF_INST_PKGS_FILE"
  2798.  
  2799.   # search current repo file only .. $1 is the optional pkg filter.. take field1, remove empty lines, remove dashes, remove Pkg from pkg list too
  2800.   if [ ! "$1" ]; then
  2801.     cut -f1 -d'|' $user_pkgs_list $devx_pkgs_list $builtins_list | grep -vE '^\$|^pkg\-'
  2802.     return 0
  2803.   else
  2804.     cut -f1 -d'|' $user_pkgs_list $devx_pkgs_list $builtins_list | grep "^$1" | grep -vE '^\$|^pkg\-'
  2805.     return 0
  2806.   fi
  2807. }
  2808.  
  2809.  
  2810. list_builtin_pkgs(){              # lists builtin packages FUNCLIST
  2811.  
  2812.   local builtins_repo
  2813.  
  2814.   builtins_repo="$WOOF_INST_PKGS_FILE"
  2815.  
  2816.   # search builtins (woof-installed) repo file only .. $1 is the optional pkg filter.. take field1, remove empty lines, remove dashes, remove Pkg from pkg list too
  2817.   cut -f1 -d'|' $builtins_repo | grep "^$1" | grep -v ^\$  | grep -v "\-$" | grep -v ^pkg\- | sort
  2818. }
  2819.  
  2820.  
  2821. search_pkgs(){                    # given $1, searches current repo, shows name and desc columns FUNCLIST
  2822.  
  2823.   local name
  2824.   local desc
  2825.   local descfull
  2826.   local search
  2827.  
  2828.   # convert "foo[ -_]bar" into "foo.*bar"
  2829.   search=`echo "$1" | sed -e 's| |.*|g' -e 's|-|.*|g' -e 's|_|.*|g'`
  2830.  
  2831.   # convert repo file to nice columnised output, use cat|grep so empty searches return all results
  2832.   cat "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | grep -i "$search" | while read repo_line;
  2833.   do
  2834.     name=`echo  "$repo_line"|cut -f2  -d'|'`
  2835.     desc="`echo "$repo_line"|cut -f10 -d'|' | head -c 57`"
  2836.     descfull="`echo "$repo_line"|cut -f10 -d'|'`"
  2837.     [ "$desc" != "$descfull" ] && desc="${desc}.."
  2838.     printf "%-20s" "$name" " ${desc:-No description}"
  2839.     echo
  2840.   done
  2841. }
  2842.  
  2843.  
  2844. search_fast(){                    # given $1, searches current repo, show pkg names only, case sensitive FUNCLIST
  2845.   local RES
  2846.   local search
  2847.  
  2848.   # convert "foo[ -_]bar" into "foo.*bar"
  2849.   search=`echo "$1" | sed -e 's| |.*|g' -e 's|-|.*|g' -e 's|_|.*|g'`
  2850.  
  2851.   # get matching pkgs
  2852.   RES="`LANG=C grep "$search" "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | cut -f1 -d'|'`"
  2853.   # error if no result
  2854.   [ ! "$RES" ] && exit 1
  2855.  
  2856.   # put results into file
  2857.   echo "$RES" > $TMPDIR/pkglist
  2858.  
  2859.   # hide built-in, devx and user installed packages
  2860.   if [ $HIDE_INSTALLED = true ]; then
  2861.     hide_installed_pkgs_from_search_results
  2862.   fi
  2863.  
  2864.   hide_blacklisted_pkgs_from_search_results
  2865.  
  2866.   # now build the search results
  2867.   RES="`grep -v "^\$" $TMPDIR/pkglist 2>/dev/null | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq`"
  2868.   rm $TMPDIR/pkglist &>/dev/null
  2869.  
  2870.   #finshed making search results, print results
  2871.   [ "$RES" = "" ] && exit 1
  2872.   echo "$RES"
  2873. }
  2874.  
  2875.  
  2876. search_all_pkgs(){                # given $1, search all repos, show name and desc columns FUNCLIST
  2877.  
  2878.   local name
  2879.   local desc
  2880.   local descfull
  2881.   local search
  2882.  
  2883.   # convert "foo[ -_]bar" into "foo.*bar"
  2884.   search=`echo "$1" | sed -e 's| |.*|g' -e 's|-|.*|g' -e 's|_|.*|g'`
  2885.  
  2886.   for repo_file in `repo_file_list`
  2887.   do #090817
  2888.     # convert repo file to nice columnised output
  2889.     cat "$REPO_DB_FILE_DIR/${repo_file}" 2>/dev/null | grep -i "$search" | while read repo_line;
  2890.     do
  2891.     # get details from repo
  2892.     name=`echo  "$repo_line"|cut -f2  -d'|'`
  2893.     desc="`echo "$repo_line"|cut -f10 -d'|' | head -c 57`"
  2894.     descfull="`echo "$repo_line"|cut -f10 -d'|'`"
  2895.     [ "$desc" != "$descfull" ] && desc="${desc}.."
  2896.     # remove spaces and comments (slackware-extra repo has some dodgy entries.. dogy names, descriptions with comments, etc)
  2897.     name="${name// /}"
  2898.     name="${name//#/}"
  2899.     [ "$name" = '' ] && continue
  2900.     # print columnised output
  2901.     printf "%-20s" "$name" " ${desc:-No description}"
  2902.     echo
  2903.     done
  2904.   done
  2905.  
  2906. }
  2907.  
  2908.  
  2909. search_all_fast(){                # given $1, search all repos, show pkg names only, case sensitive FUNCLIST
  2910.   local RES
  2911.   local search
  2912.  
  2913.   # convert "foo[ -_]bar" into "foo.*bar"
  2914.   search=`echo "$1" | sed -e 's| |.*|g' -e 's|-|.*|g' -e 's|_|.*|g'`
  2915.  
  2916.   #if bleeding edge enabled, combine into 1 list, so
  2917.   # that `pkg -g` etc return most recent from ALL repos
  2918.   rm $TMPDIR/pkglist &>/dev/null
  2919.   if [ "$BLEDGE" = "yes" ]; then
  2920.     RES=''
  2921.     for RF in `repo_file_list`
  2922.     do #090817
  2923.       cat "$REPO_DB_FILE_DIR/${RF}" 2>/dev/null | grep -i "$search" | cut -f1 -d'|' | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq | sed -e '/^$/d' >> $TMPDIR/pkglist
  2924.     done
  2925.  
  2926.   else
  2927.     # if bleeding edge disabled, output the list repo by repo, in #
  2928.     # the fallback order, current repo first (that order is set in update_sources)
  2929.     for RF in `repo_file_list`
  2930.     do #090817
  2931.       cat "$REPO_DB_FILE_DIR/${RF}" 2>/dev/null | grep -i "$search"| cut -f1 -d'|' | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq | sed -e '/^$/d' >> $TMPDIR/pkglist
  2932.     done
  2933.  
  2934.   fi
  2935.  
  2936.   #100817 hide built-in, devx and user installed packages
  2937.   if [ $HIDE_INSTALLED = true ]; then
  2938.     hide_installed_pkgs_from_search_results
  2939.   fi
  2940.  
  2941.   hide_blacklisted_pkgs_from_search_results
  2942.  
  2943.   # now build the search results
  2944.   RES="`grep -v "^\$" $TMPDIR/pkglist 2>/dev/null | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq`"
  2945.   rm $TMPDIR/pkglist &>/dev/null
  2946.  
  2947.   #finshed making search results, print results
  2948.   [ "$RES" = "" ] && exit 1
  2949.  
  2950.   echo "$RES"
  2951. }
  2952.  
  2953.  
  2954. which_repo(){                     # list repo of given package ($1) FUNCLIST
  2955.  
  2956.   # if no valid options, quit
  2957.   [ ! "$1" -o "$1" = "-" -o "$1" = " " ] && print_usage which-repo && exit 1
  2958.  
  2959.   . ${PKGRC}
  2960.  
  2961.   local PKGNAME=''
  2962.   local PKGNAME_ONLY=''
  2963.   local pkg_ext=''
  2964.   local repo_file=''
  2965.   local repo_name=''
  2966.   local pkg_list=''
  2967.  
  2968.   PKGNAME=`get_pkg_name "$1"`
  2969.   PKGNAME_ONLY=`get_pkg_name_only "$1"`
  2970.   pkg_ext=`get_pkg_ext "$PKGNAME"`
  2971.  
  2972.   repo_file="$(grep -l "|$PKGNAME.$pkg_ext|" "$REPO_DB_FILE_DIR"/Packages-*)"
  2973.   if [ "$repo_file" != '' ]; then
  2974.     # get the repo name
  2975.     repo_name="`grep -m1 "$(basename $repo_file)|" ${HOME}/.pkg/sources | cut -f1 -d'|'`"
  2976.     pkg_list="$PKGNAME $repo_name"
  2977.   else
  2978.  
  2979.     # for each repo
  2980.     for repo_file in `repo_file_list`
  2981.     do
  2982.       # get the repo name
  2983.       repo_name="`grep -m1 "$repo_file|" ${HOME}/.pkg/sources | cut -f1 -d'|'`"
  2984.       # create the entries, example: "vlc slacko14.2"
  2985.       # (each line shows a matching package, then a SPACE, then the name of repo the package lives in)
  2986.       pkg_list="$pkg_list`LANG=C grep "$PKGNAME" "$REPO_DB_FILE_DIR/${repo_file}" 2>/dev/null | grep -m1 "^$PKGNAME|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2987.       [ "$pkg_list" = '' ] && pkg_list="$pkg_list`LANG=C grep "$PKGNAME" "$REPO_DB_FILE_DIR/${repo_file}" 2>/dev/null | grep -m1 "|$PKGNAME.$EX|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2988.       [ "$pkg_list" = '' ] && pkg_list="$pkg_list`LANG=C grep "$PKGNAME" "$REPO_DB_FILE_DIR/${repo_file}" 2>/dev/null | grep -m1 "|$1.$EX|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2989.       [ "$pkg_list" = '' ] && pkg_list="$pkg_list`LANG=C grep "$PKGNAME" "$REPO_DB_FILE_DIR/${repo_file}" 2>/dev/null | grep -m1 "|$PKGNAME_ONLY|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2990.       [ "$pkg_list" = '' ] && pkg_list="$pkg_list`LANG=C grep "$PKGNAME" "$REPO_DB_FILE_DIR/${repo_file}" 2>/dev/null | grep -m1 "^$PKGNAME_ONLY|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2991.       [ "$pkg_list" = '' ] && pkg_list="$pkg_list`LANG=C grep "$PKGNAME" "$REPO_DB_FILE_DIR/${repo_file}" 2>/dev/null | grep -m1 "|$1|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2992.       [ "$pkg_list" = '' ] && pkg_list="$pkg_list`LANG=C grep "$PKGNAME" "$REPO_DB_FILE_DIR/${repo_file}" 2>/dev/null | grep -m1 "^$1|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2993.     done
  2994.   fi
  2995.  
  2996.   # show results
  2997.   LANG=C echo -e "$pkg_list" | sed -e '/^$/d' | uniq
  2998. }
  2999.  
  3000.  
  3001. which_pkg(){                      # find out which pkg FILE ($1) comes from FUNCLIST
  3002.  
  3003.   # exit if no valid opts
  3004.   [ ! "$1" -o "$1" = '-' -o "$1" = '' ] && print_usage which && exit 1
  3005.  
  3006.   local PKGNAME=''
  3007.   local PKG_FILE_LIST=''
  3008.   local FILENAME=''
  3009.   local FILENAME_ONLY=''
  3010.   local DIRNAME=''
  3011.   local builtins_without_busybox="$(find "$BUILTIN_FILE_LIST_DIR"/* -maxdepth 1 -type f | grep -v "/busybox")"
  3012.  
  3013.   # get user input
  3014.   FILENAME="`basename "$1"`"
  3015.   FILENAME_ONLY="$FILENAME"
  3016.   DIRNAME="`dirname "$1"`"
  3017.  
  3018.   # if we don't have a file, the user probably gave a command,
  3019.   # so lets check see
  3020.   if [ ! -f "$1" ]; then
  3021.     # if we get a command, then that is our FILENAME
  3022.     cmd=`which "$1"`
  3023.     # if $cmd found, set FILENAME to $cmd (FILENAME now includes full path)
  3024.     [ "$cmd" != '' ] && FILENAME=$cmd && FILENAME_ONLY=`basename "$FILENAME"`
  3025.   fi
  3026.  
  3027.   # dont use relative paths
  3028.   [ "`echo "$DIRNAME" | grep "^\."`" != '' ] && DIRNAME=''
  3029.  
  3030.   # try user installed pkgs contents of $REPO_DIR/*.files
  3031.  
  3032.   # try '$dir/$file', returns filename (no path) of matching *.files
  3033.   [ "$PKG_FILE_LIST" = '' ] && PKG_FILE_LIST="`grep -l "$DIRNAME/$FILENAME\$"  "$PACKAGE_FILE_LIST_DIR"/*.files 2>/dev/null| sed "s#$REPO_DIR/##g" 2>/dev/null`"
  3034.  
  3035.   # try '$dir/$file_*' if needed
  3036.   [ "$PKG_FILE_LIST" = '' ] && PKG_FILE_LIST="`grep -l "$DIRNAME/${FILENAME}_" "$PACKAGE_FILE_LIST_DIR"/*.files 2>/dev/null| sed "s#$REPO_DIR/##g" 2>/dev/null`"
  3037.  
  3038.   # try '$dir/$file-*' if needed
  3039.   [ "$PKG_FILE_LIST" = '' ] && PKG_FILE_LIST="`grep -l "$DIRNAME/${FILENAME}\-" "$PACKAGE_FILE_LIST_DIR"/*.files 2>/dev/null| sed "s#$REPO_DIR/##g" 2>/dev/null`"
  3040.  
  3041.   # if we found a package, set the package name to the name of the *.files file that we got
  3042.   [ "$PKG_FILE_LIST" != '' ] && PKGNAME="`basename "$PKG_FILE_LIST" .files`"
  3043.  
  3044.   # maybe we got nothing from *.files.. check builtins/$1* for ' $FILENAME_ONLY',
  3045.   # returns PKGNAME of the matching builtin
  3046.  
  3047.   # if needed, search inside builtin file lists (EXCEPT busybox) for ' $FILENAME_ONLY'
  3048.   [ "$PKG_FILE_LIST" = '' ] && [ "$PKGNAME" = "" ] \
  3049.     && PKGNAME="$(basename `grep -l "^ $FILENAME_ONLY\$" $builtins_without_busybox | sed "s#$BUILTIN_FILE_LIST_DIR/##g"` 2>/dev/null)"
  3050.  
  3051.   # if that didn't work, search inside builtin file lists of busybox for ' $FILENAME_ONLY'
  3052.   [ "$PKG_FILE_LIST" = '' ] && [ "$PKGNAME" = "" ] \
  3053.     && PKGNAME="$(basename `grep -l "^ $FILENAME_ONLY\$" "$BUILTIN_FILE_LIST_DIR/busybox" | sed "s#$BUILTIN_FILE_LIST_DIR/##g"` 2>/dev/null)"
  3054.  
  3055.   # apps in ADRV sfs dont have their files listed in their $REPO_DIR/builtin_files/$pkgname.. so..
  3056.  
  3057.  
  3058.   # now check pkg names (not pkg contents) of builtins (EXCEPT busybox) for '$file'.. cos maybe we didnt find the file inside the file lists
  3059.   [ "$PKGNAME" = "" ] \
  3060.     && PKGNAME="`echo "$builtins_without_busybox" | grep -m1 "^${FILENAME_ONLY}\$" | sed "s#$BUILTIN_FILE_LIST_DIR/##g" 2>/dev/null`"
  3061.  
  3062.   # now look for '$file' in user installed repo list
  3063.   [ "$PKGNAME" = "" ] \
  3064.     && PKGNAME="`cut -f1,2,8 -d'|' "$USER_INST_PKGS_FILE" 2>/dev/null | sed -e "s/^/|/" -e "s/\$/|/" 2>/dev/null| grep -m1 "|$FILENAME_ONLY|" | cut -f2 -d'|'`"
  3065.  
  3066.   # also look for '$file' in user/woof/devx installed repo list
  3067.   [ "$PKGNAME" = "" ] \
  3068.     && PKGNAME="$(cut -f1,2,8 -d'|' "${SPEC_DB_PATHS[@]}" 2>/dev/null | sed -e "s/^/|/" -e "s/\$/|/" 2>/dev/null | grep -m1 "|$FILENAME_ONLY|" | cut -f2 -d'|')"
  3069.  
  3070.  
  3071.   # we searched for an exact match in builtins, user installed, woof installed and devx pkgs, now try some fuzzier matches
  3072.  
  3073.   # try /$file_* in builtins
  3074.   [ "$PKGNAME" = "" ] \
  3075.     && PKGNAME="$(basename `grep -l " ${FILENAME_ONLY}_" "$PKGS_BUILTIN_DIR"/* | sed "s#$PKGS_BUILTIN_DIR/##g"` 2>/dev/null)"
  3076.  
  3077.   # try $file-* in builtins
  3078.   [ "$PKGNAME" = "" ] \
  3079.     && PKGNAME="$(basename `grep -l " ${FILENAME_ONLY}-" "$PKGS_BUILTIN_DIR"/* | sed "s#$REPO_DIR/builtin_files/##g"` 2>/dev/null)"
  3080.  
  3081.   # try $file.* in builtins
  3082.   [ "$PKGNAME" = "" ] \
  3083.     && PKGNAME="$(basename `grep -l " ${FILENAME_ONLY}\." "$PKGS_BUILTIN_DIR"/* | sed "s#$PKGS_BUILTIN_DIR/##g"` 2>/dev/null)"
  3084.  
  3085.   # try *file in builtin pkgs
  3086.   [ "$PKGNAME" = "" ] \
  3087.     && PKGNAME="$(basename `grep -l "${FILENAME_ONLY}"\$ "$PKGS_BUILTIN_DIR"/* | sed "s#$PKGS_BUILTIN_DIR/##g"` 2>/dev/null)"
  3088.  
  3089.  
  3090.   # if we still didnt find it, look in user installed package lists, for FILENAME
  3091.   [ "$PKGNAME" = "" ] \
  3092.     && PKGNAME=`cut -f1,2,8 -d '|' "$USER_INST_PKGS_FILE" \
  3093.       | sed -e "s/^/|/" -e "s/\$/|/" 2>/dev/null \
  3094.       | grep -m1 "|${FILENAME_ONLY}|" \
  3095.       | cut -f2 -d'|'` 2>/dev/null
  3096.  
  3097.   # if we still didnt find it, look in all installed package lists, for FILENAME
  3098.   [ "$PKGNAME" = "" ] \
  3099.     && PKGNAME=`cut -f1,2,8 -d '|' "${SPEC_DB_PATHS[@]}" \
  3100.       | sed -e "s/^/|/" -e "s/\$/|/" 2>/dev/null \
  3101.       | grep -m1 "|${FILENAME_ONLY}|" \
  3102.       | cut -f2 -d'|'` 2>/dev/null
  3103.  
  3104.   # if we still didnt find it, look in all installed package lists, for *FILENAME
  3105.   [ "$PKGNAME" = "" ] \
  3106.     && PKGNAME=`cut -f1,2,8 -d '|' "${SPEC_DB_PATHS[@]}" \
  3107.       | sed -e "s/^/|/" -e "s/\$/|/" 2>/dev/null \
  3108.       | grep -m1 "${FILENAME_ONLY}|" \
  3109.       | cut -f2 -d'|'` 2>/dev/null
  3110.  
  3111.   # if we still didnt find it, look in all installed package lists, for FILENAME*
  3112.   [ "$PKGNAME" = "" ] \
  3113.     && PKGNAME=`cut -f1,2,8 -d '|' "${SPEC_DB_PATHS[@]}" \
  3114.       | sed -e "s/^/|/" -e "s/\$/|/" \
  3115.       | grep -m1 "|${FILENAME_ONLY}" \
  3116.       | cut -f2 -d'|'` 2>/dev/null
  3117.  
  3118.  
  3119.   # clean up
  3120.   [ "$PKGNAME" = '.files' ] && PKGNAME=''
  3121.   PKGNAME="`echo "$PKGNAME" | head -1`"
  3122.  
  3123.   # now print pkgs which contain the given file ($1)
  3124.   if [ "$PKGNAME" != "" ]; then
  3125.     echo "$PKGNAME"
  3126.     return 0
  3127.   else
  3128.     echo "File '$FILENAME' not found in any installed or built-in pkgs."
  3129.     exit 1
  3130.   fi
  3131. }
  3132.  
  3133.  
  3134.  
  3135. # pkg info funcs
  3136.  
  3137. pkg_contents(){                   # list package ($1) contents FUNCLIST
  3138.  
  3139.   # if no valid options, quit
  3140.   [ ! "$1" -o "$1" = "-" -o "$1" = " " ] && print_usage contents && exit 1
  3141.  
  3142.   # get settings
  3143.   . ${PKGRC}
  3144.  
  3145.   local PKGFILE
  3146.   local ext
  3147.   local PKGNAME
  3148.   local PKGNAME_ONLY
  3149.   local pkg_is_local_file
  3150.   local pkg_is_builtin
  3151.   local pkg_is_installed
  3152.   local PKGFLIST=''
  3153.  
  3154.   # get pkg extension
  3155.   ext=`get_pkg_ext "$1"`
  3156.  
  3157.   # get pkg name (includes version), but no extension or path
  3158.   PKGNAME="$(basename "$1" .$ext)"
  3159.   PKGNAME=`get_pkg_name "$PKGNAME"`
  3160.  
  3161.   # get pkg name without version
  3162.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  3163.  
  3164.   pkg_is_local_file=`is_local_pkg "$1"`
  3165.   pkg_is_builtin=`is_builtin_pkg "$PKGNAME_ONLY"`
  3166.   pkg_is_installed=`is_installed_pkg "$PKGNAME_ONLY"`
  3167.  
  3168.   # now we check various ways to find out the contents of
  3169.   # a pkg (either by *.files, user installed, woof, builtin, etc)
  3170.  
  3171.   [ "$pkg_is_local_file" = true ] && PKGFILE="$1"
  3172.  
  3173.   # try a file in the current dir if needed
  3174.   [ ! -f "$PKGFILE" ] && PKGFILE="${CURDIR}/${PKGNAME}.$ext"
  3175.   # check $WORKDIR if needed
  3176.   [ ! -f "$PKGFILE" ] && PKGFILE="${WORKDIR}/${PKGNAME}.$ext"
  3177.  
  3178.   # if the pkg is a local file
  3179.   if [ -f "$PKGFILE" ]; then
  3180.     #list contents based on extension
  3181.     case "$ext" in
  3182.     sfs)
  3183.       unsquashfs -l "$PKGFILE"  | cut -f2-99 -d'/'| sed -e 's#^#/#g' 2>/dev/null | grep -v -E 'unsquashfs:|/squashfs-root|) to write' | grep -v "^/$"
  3184.     ;;
  3185.     deb)
  3186.       dpkg-deb -c "$PKGFILE" 2>/dev/null | grep -v "^/$"
  3187.      ;;
  3188.     pet|tar|tar**tcz|*txz|*tgz|*xz|*gz)
  3189.       # remove leading pkg name from each line of file list
  3190.       # (shown if listing *some* pkg file contents)
  3191.       tar -tf "$PKGFILE" 2>/dev/null \
  3192.         | sed -e "s#^./${PKGNAME}/#/#g" -e "s#^${PKGNAME}/#/#g" -e "s#^./##g" 2>/dev/null \
  3193.         | grep -v ^$ 2>/dev/null 2>/dev/null \
  3194.         | grep -v "^/$"
  3195.     ;;
  3196.     rpm)
  3197.       busybox rpm -qlp "$PKGFILE" 2>/dev/null | grep -v "^/$"
  3198.     ;;
  3199.     esac
  3200.     exit $?
  3201.   fi
  3202.  
  3203.   # if we are here, the pkg is not a downloaded pkg, so we build a
  3204.   # list of files by checking  *.files, woof, builtins ..
  3205.  
  3206.   # check if we need to (and can) get our pkg contents from builtins/PKGNAME
  3207.   local  need_to_check_builtins=`echo "$PKG_FLIST" | grep -m1 'packages/builtin_files/'`
  3208.  
  3209.   # if the pkg is a builtin, we will re-format the output of LIST to match the others (full paths on each line)
  3210.   if [ "$need_to_check_builtins" != '' -a "$pkg_is_builtin" = true ]; then
  3211.  
  3212.     # reset the list of pkg files, we will re-build it
  3213.     rm $TMPDIR/pkg_file_list 2>/dev/null
  3214.  
  3215.     # first we get the package contents from $REPO_DIR/builtin_files/$PKGNAME_ONLY
  3216.     cat "$PKG_FLIST" 2>/dev/null | while read line
  3217.     do
  3218.       # parse it, so we get a full path to file on each line
  3219.       if [ "`echo "$line" | grep '^/'`" != '' ]; then
  3220.         # set the dir to use in our file list
  3221.         dir="$line"
  3222.       else
  3223.         # keep previous dir
  3224.         dir="$dir"
  3225.       fi
  3226.  
  3227.       # create our new line (a full file path) to $LIST
  3228.       line_to_add="$dir/`echo "$line" | cut -f2 -d' '`"
  3229.  
  3230.       # if only a dir, skip it
  3231.       [ "$line" = "$dir" ] && continue
  3232.  
  3233.       # if its already added, skip it
  3234.       [ "`grep -m1 "$line_to_add" ${TMPDIR}/pkg_file_list 2>/dev/null `" != '' ] && continue
  3235.  
  3236.       # if its not a file on the system (it should be), skip it
  3237.       [ ! -f "$line_to_add" ] && continue
  3238.  
  3239.       # all should be ok, add the line to our list
  3240.       echo "$line_to_add" >> $TMPDIR/pkg_file_list
  3241.  
  3242.     done
  3243.  
  3244.     # now get our pkg contents list, it might have been re-formatted (if a builtin)
  3245.     PKG_FLIST="`cat ${TMPDIR}/pkg_file_list 2>/dev/null`"
  3246.  
  3247.     # last resort, if we lost our file list or still dont have, try the basics again
  3248.     [ "$PKG_FLIST" = '' ] && PKG_FLIST="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${PKGNAME}"'*.files' 2>/dev/null)"
  3249.  
  3250.     # clean up the list a bit
  3251.     [ "$PKG_FLIST" != '' ] && PKG_FLIST="`echo "$PKG_FLIST" | grep -v ' ' | grep -v "^\$" | sort | uniq`"
  3252.  
  3253.     # and clean up the tmp files
  3254.     rm ${TMPDIR}/pkg_file_list 2>/dev/null
  3255.   fi
  3256.  
  3257.   # try PKGNAME_ONLY.files (exact match)
  3258.   PKG_FLIST="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${PKGNAME_ONLY}.files" 2>/dev/null)"
  3259.  
  3260.   # try finding PKGNAME.files (exact match)
  3261.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${PKGNAME}.files" 2>/dev/null)"
  3262.  
  3263.   # try the builtins files (exact match)
  3264.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$(find "$BUILTIN_FILE_LIST_DIR" -maxdepth 1 -type f -name "$PKGNAME_ONLY")"
  3265.  
  3266.   # try PKGNAME (exact match) in user installed pkgs
  3267.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$REPO_DIR/`cut -f1 -d'|' $USER_INST_PKGS_FILE | grep -m1 ^"$PKGNAME\$" 2>/dev/null`.files"
  3268.  
  3269.   # try PKGNAME (exact match) in all installed pkgs
  3270.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$REPO_DIR/`cut -f1 -d'|' "${SPEC_DB_PATHS[@]" | grep -m1 ^"$PKGNAME\$" 2>/dev/null`.files"
  3271.  
  3272.   # try PKGNAME_ONLY (exact match) in all installed pkgs
  3273.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$REPO_DIR/`cut -f2 -d'|' "${SPEC_DB_PATHS[@]" | grep -m1 ^"$PKGNAME_ONLY\$" 2>/dev/null`.files"
  3274.  
  3275.  
  3276.   # no exact matches found, try fuzzy searches..
  3277.  
  3278.   # try PKGNAME*.files
  3279.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "/${PKGNAME}"'*.files'  2>/dev/null)"
  3280.  
  3281.   # try finding *PKGNAME.files
  3282.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name '*'"${PKGNAME}.files" 2>/dev/null)"
  3283.  
  3284.   # try ^PKGNAME_ONLY[_-] in user installed pkgs
  3285.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$REPO_DIR/`cut -f1 -d'|' $USER_INST_PKGS_FILE | grep -Em1 "^$PKGNAME_ONLY[_-]" 2>/dev/null`.files"
  3286.  
  3287.   # try PKGNAME_ONLY* in user installed pkgs only
  3288.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$REPO_DIR/`cut -f1 -d'|' $USER_INST_PKGS_FILE | grep -m1 ^"$PKGNAME_ONLY" 2>/dev/null`.files"
  3289.  
  3290.   # try PKGNAME_ONLY-* (any installed pkgs)
  3291.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$REPO_DIR/`cut -f1 -d'|' "${SPEC_DB_PATHS[@]" | grep -Em1 ^"${PKGNAME_ONLY}[_-]" 2>/dev/null`.files"
  3292.  
  3293.   # try PKGNAME_ONLY* (any installed pkgs)
  3294.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$REPO_DIR/`cut -f1 -d'|' "${SPEC_DB_PATHS[@]" | grep -m1 ^"$PKGNAME_ONLY" 2>/dev/null`.files"
  3295.  
  3296.   # try PKGNAME_ONLY*.files
  3297.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$(find "$REPO_DIR" -maxdepth 1 -type f -name "/${PKGNAME_ONLY}"'*.files'  2>/dev/null)"
  3298.  
  3299.  
  3300.  
  3301.   #if we found a list of files
  3302.   if [ "$PKG_FLIST" != "" ]; then
  3303.  
  3304.     print_cmd=echo
  3305.     # PKG_FLIST just might contain a path to the pkg contents
  3306.     # themselves (a *.files, or a file in builtin_files/*).. so if
  3307.     # its a file path, we will cat it, if not, its the pkg contents
  3308.     # themselves, we echo it
  3309.     if [ "`echo "$PKG_FLIST" | grep -m1 '/builtin_files/'`" != '' -o "`echo "$PKG_FLIST" | grep -m1 "packages/$PKGNAME"`" != '' ]; then
  3310.       print_cmd=cat
  3311.     fi
  3312.  
  3313.     $print_cmd "$PKG_FLIST" | grep -v "^/$" 2>/dev/null
  3314.  
  3315.   else # if no files found
  3316.     if [ "$PKGNAME" != '' ]; then
  3317.       INST_CHECK="`is_installed_pkg $PKGNAME 2>/dev/null`"
  3318.       if [ "$INST_CHECK" != true -o "$pkg_is_local_file" = false ]; then
  3319.         error "Package must be installed, downloaded or built-in."
  3320.       else
  3321.         error "Could not get package contents, unable to get file list."
  3322.       fi
  3323.     else
  3324.       error "Could not get name of package."
  3325.     fi
  3326.   fi
  3327. }
  3328.  
  3329.  
  3330. pkg_entry(){                      # show pkg ($1) repo entry, each field on a new line FUNCLIST
  3331.  
  3332.   # exit if no valid opts
  3333.   [ ! "$1" -o "$1" = '-' ] && print_usage pkg-entry && exit 1
  3334.  
  3335.   local EX
  3336.   local PKGNAME
  3337.   local PKGNAME_ONLY
  3338.  
  3339.   # get pkg extension
  3340.   EX=`get_pkg_ext "$1"`
  3341.  
  3342.   # get pkg name with version, but no extension or path
  3343.   PKGNAME="$(basename "$1" .$EX)"
  3344.  
  3345.   # dont rely on the string the user gave us, try to get the exact match
  3346.   PKGNAME=`get_pkg_name "$PKGNAME"`
  3347.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  3348.  
  3349.   PKG_ENTRY="$(cat "$REPO_DB_FILE_DIR"/Packages-* | grep -m1 "|$PKGNAME|")"
  3350.   [ "$PKG_ENTRY" = '' ] && PKG_ENTRY="$(cat "$REPO_DB_FILE_DIR"/Packages-* | grep -m1 "^$PKGNAME|")"
  3351.   [ "$PKG_ENTRY" = '' ] && PKG_ENTRY="$(cat "$REPO_DB_FILE_DIR"/Packages-* | grep -m1 "|$PKGNAME_ONLY|")"
  3352.   if [ "$PKG_ENTRY" = '' ]; then
  3353.     echo "$1 not found in $REPOFILE"
  3354.     exit 1
  3355.   else
  3356.     echo "$PKG_ENTRY" | tr '|' '\n' | grep -v '^$'
  3357.   fi
  3358.   return 0
  3359. }
  3360.  
  3361.  
  3362. pkg_status(){                     # print package ($1) name, status, deps, etc FUNCLIST
  3363.  
  3364.   # exit if no valid options
  3365.   [ ! "$1" -o "$1" = "-" ] && print_usage pkg-status && exit 1
  3366.  
  3367.   # get current repo name
  3368.   . ${PKGRC}
  3369.  
  3370.   local EX
  3371.   local PKGNAME=''
  3372.   local PKGNAME_ONLY=''
  3373.   local PKGFILE=''
  3374.   local MSG=''
  3375.   local install_status=''
  3376.   local pkg_found=false
  3377.   local pkg_repo=''
  3378.   local pkg_repo_file=''
  3379.   local prev_repo=${REPONAME}
  3380.  
  3381.   # get pkg extension
  3382.   EX=`get_pkg_ext "$1"`
  3383.  
  3384.   # get pkg name with version, but no extension or path
  3385.   PKGNAME="$(basename "$1" .$EX)"
  3386.  
  3387.   # dont rely on the string the user gave us, try to get the exact match
  3388.   PKGNAME=`get_pkg_name "$PKGNAME"`
  3389.  
  3390.   # get pkg name without version
  3391.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  3392.  
  3393.   [ "$PKGNAME" = '' ] && error "Can't find $1" && exit 1
  3394.  
  3395.   # get install status of pkg (installed or not)
  3396.   [ "`is_installed_pkg $PKGNAME`" = true ] && install_status="installed" || install_status="not installed"
  3397.  
  3398.   # if pkg is installed, find out from where
  3399.   if [ "$install_status" = "installed" ]; then
  3400.  
  3401.     # check user installed pkgs for $PKGNAME
  3402.     pkg_found=`LANG=C is_usr_pkg "${PKGNAME}"`
  3403.     [ "$pkg_found" = true ] && install_status="installed (user)"
  3404.  
  3405.     # if pkg not found yet, check if pkg is from the devx
  3406.     if [ -f "$REPO_DIR/devx-only-installed-packages" -a "$pkg_found" = false ]; then
  3407.       pkg_found=`LANG=C is_devx_pkg "${PKGNAME}"`
  3408.       [ "$pkg_found" = true ] && install_status="installed (in devx)"
  3409.     fi
  3410.  
  3411.     # check builtins for PKGNAME_ONLY, if it exists, its a builtin pkg
  3412.     if [ -d "$BUILTIN_FILE_LIST_DIR/" -a "$pkg_found" = false ]; then
  3413.       pkg_found=`LANG=C is_builtin_pkg "${PKGNAME_ONLY}"`
  3414.       [ "$pkg_found" = true ] && install_status="installed (builtin)"
  3415.     fi
  3416.  
  3417.     # last gasp, if pkg not found yet, check if pkg if listed in layers-installed packages
  3418.     if [ -f "$REPO_DIR/layers-installed-packages" -a "$pkg_found" = false ]; then
  3419.       pkg_found=$(LANG=C grep -m1 "^${PKGNAME}|" "$LAYER_INST_PKGS_FILE")
  3420.  
  3421.       [ "$pkg_found" = '' ] && install_status="installed (layers)"
  3422.     fi
  3423.  
  3424.   fi
  3425.  
  3426.   # get the repo of this pkg, if needed
  3427.   [ "$pkg_repo" = '' ] && pkg_repo="`which_repo "$PKGNAME" 2>/dev/null | cut -f2 -d' '`"
  3428.  
  3429.   # if we got a repo name, get the repo file
  3430.   [ "$pkg_repo" != '' ] && pkg_repo_file="$(grep -m1 "^$pkg_repo|" ${HOME}/.pkg/sources | cut -f3 -d'|')"
  3431.  
  3432.   # if we have a repo to search for pkg info
  3433.   if [ "$pkg_repo_file" != "" ]; then
  3434.  
  3435.     # get package description from its repo entry
  3436.     pkg_desc="`LANG=C grep -m1 "|${PKGNAME_ONLY}|" "$REPO_DB_FILE_DIR/$pkg_repo_file" 2>/dev/null| grep -m1 "$PKGNAME" | cut -f10 -d'|' | head -1`"
  3437.  
  3438.     if [ "$pkg_desc" = '' ]; then
  3439.       pkg_desc="`LANG=C grep -m1 "|${PKGNAME_ONLY}|" "$REPO_DIR/Packages-"* 2>/dev/null| grep -m1 "${PKGNAME}|" | cut -f10 -d'|' | head -1`"
  3440.     fi
  3441.  
  3442.     # get size of pkg
  3443.     pkg_size=`LANG=C grep -m1 "|${PKGNAME_ONLY}|" "$REPO_DB_FILE_DIR/$pkg_repo_file" 2>/dev/null | cut -f6 -d'|' | head -1`
  3444.  
  3445.     # add K to end of pkg_size, if not there already
  3446.     [ "$pkg_size" != '' ] && pkg_size="${pkg_size}K" && pkg_size="${pkg_size//KK/K}"
  3447.  
  3448.     # if called with -PS, we get the full info (where we sort deps missing or installed)
  3449.     if [ "$FULL_PKG_STATUS" = true ]; then
  3450.  
  3451.       # if pkg has many deps, checking the deps might take a while, print a 'please wait' msg
  3452.       please_wait=false
  3453.       # count deps of pkg
  3454.       [ "`has_deps "$PKGNAME"`" = true ] && please_wait=true
  3455.       [ "$please_wait" = true ] && echo -ne "Please wait.. Gathering dependency information.\n"
  3456.  
  3457.       set_current_repo $pkg_repo 1>/dev/null
  3458.       # sort deps into 2 lists: missing deps and installed deps
  3459.       find_deps "$PKGNAME"
  3460.       set_current_repo $prev_repo 1>/dev/null
  3461.  
  3462.       # create a nicely formatted, coloured list of deps, green for installed, yellow for missing
  3463.       INSTALLED_DEPS_MSG="${green}`echo "${DEPS_INSTALLED}"|grep -v "^\$"| tr '\n' ','| sed -e "s#,\\$##" -e 's#,#, #g' 2>/dev/null| fold -w 50 -s | sed '2,10 s/^/                /g' 2>/dev/null`${endcolour}"
  3464.       MISSING_DEPS_MSG="${yellow}`echo "${DEPS_MISSING}"   |grep -v "^\$"| tr '\n' ','| sed -e "s#,\\$##" -e 's#,#, #g' 2>/dev/null| fold -w 50 -s | sed '2,10 s/^/                /g'`${endcolour}"
  3465.  
  3466.       DEPS_ENTRY="Installed deps: ${INSTALLED_DEPS_MSG:-None}
  3467. Missing deps:   ${MISSING_DEPS_MSG:-None}"
  3468.  
  3469.     else
  3470.  
  3471.       DEPS_ENTRY="Dependencies:   `list_deps "$PKGNAME" 2>/dev/null`"
  3472.  
  3473.     fi
  3474.  
  3475.     #250613 added desc
  3476.     MSG="
  3477. Name:           ${PKGNAME}
  3478. Description:    `echo ${pkg_desc:-No description} | fold -w 50 -s | sed '2,10 s/^/                /g'`
  3479. Size:           ${pkg_size:-Unknown}
  3480. Status:         ${install_status:-Unknown}
  3481. In Repo:        ${pkg_repo:-Not in any repos}
  3482. Repo file:      `basename ${pkg_repo_file:-Not in any repositories} 2>/dev/null`
  3483. $DEPS_ENTRY"
  3484.  
  3485.   fi
  3486.  
  3487.   # if not found in any repo, maybe a woof or alien (user-installed) package
  3488.   if [ "$MSG" = "" ]; then #if nothing found in any repo
  3489.  
  3490.     PKGFILE="`list_downloaded_pkgs | grep -m1 "^$PKGNAME"`"
  3491.  
  3492.     # if the pkg is a downloaded file (in WORKDIR)
  3493.     if [ -f "$WORKDIR/$PKGFILE" ]; then
  3494.  
  3495.       # get the file, and get its file size
  3496.       PKGFILE="$WORKDIR/${PKGFILE}"
  3497.       [ "$pkg_size" = '' ] && pkg_size="`du -s -k "$PKGFILE" | cut -f1`K"
  3498.       [ "$pkg_size" = '' ] && pkg_size="`grep -m1 "|${PKGNAME_ONLY}|" "${SPEC_DB_PATHS[@]" | cut -f6 -d'|' | head -1`"
  3499.  
  3500.  
  3501.       # create msg for downloaded pkgs
  3502.       MSG="Name:           ${PKGNAME}
  3503. Size:           ${pkg_size:-Unknown}
  3504. Status:         Downloaded
  3505. Note:           Downloaded package, not in any repo.
  3506. $DEPS_ENTRY"
  3507.  
  3508.     # else, search for exact match, then pkg-*, then pkg_*, then pkg* in installed pkgs
  3509.     elif [ "$(grep -m1 "|${PKGNAME_ONLY}|" "$USER_INST_PKGS_FILE" 2>/dev/null)"   \
  3510.       -o "$(grep -m1 "^${PKGNAME}|" "$USER_INST_PKGS_FILE" 2>/dev/null)"   \
  3511.       -o "`grep -m1 "^${PKGNAME}|" "${SPEC_DB_PATHS[@]" 2>/dev/null`"   \
  3512.       -o "`grep -m1 "^${PKGNAME}-" "${SPEC_DB_PATHS[@]" 2>/dev/null`" \
  3513.       -o "`grep -m1 "^${PKGNAME}_" "${SPEC_DB_PATHS[@]" 2>/dev/null`" \
  3514.       -o "`grep -m1 "^${PKGNAME}" "${SPEC_DB_PATHS[@]" 2>/dev/null`" ]; then
  3515.  
  3516.       # get deps and size from its entry in user-installed-packages
  3517.       DEPS_ENTRY="Dependencies:   `grep -m1 "^$PKGNAME" "$USER_INST_PKGS_FILE" | cut -f9 -d'|' | sed -e 's/,+/,/g' -e 's/^+//g' -e 's/,$//g' 2>/dev/null`"
  3518.       [ "$DEPS_ENTRY" = '' ] && DEPS_ENTRY="Dependencies:   `grep -m1 "^$PKGNAME" "${SPEC_DB_PATHS[@]" | cut -f9 -d'|' | sed -e 's/,+/,/' -e 's/^+//' -e 's/,$//' 2>/dev/null`"
  3519.       pkg_size=`LANG=C grep -m1 "|$PKGNAME_ONLY|" "${SPEC_DB_PATHS[@]" | cut -f6 -d'|' | head -1`
  3520.       # add K to end of pkg_size, if not there already
  3521.       [ "$pkg_size" != '' ] && pkg_size="${pkg_size}K" && pkg_size="${pkg_size//KK/K}"
  3522.  
  3523.       # create msg for user installed pkgs
  3524.       MSG="Name:      ${PKGNAME}
  3525. Size:           ${pkg_size:-Unknown}
  3526. Status:         ${install_status}
  3527. Repo:           Alien package, not in any repo.
  3528. $DEPS_ENTRY"
  3529.  
  3530.     else # pkg wasn't found, it's unknown to Pkg
  3531.       MSG="$PKGNAME not found.
  3532.  
  3533. Here are some packages in other repos:"
  3534.       PLIST="`which_repo ${PKGNAME}`"
  3535.       [ "$PLIST" != "" ] && MSG="$MSG
  3536. $PLIST"
  3537.       # create msg for alien pkgs
  3538.       MSG="$MSG
  3539.  
  3540. Name:           ${PKGNAME}
  3541. Status:         ${install_status:-Unknown}
  3542. Repo:           Alien package, not in $REPONAME repo.
  3543. $DEPS_ENTRY"
  3544.  
  3545.     fi
  3546.  
  3547.   fi
  3548.  
  3549.   # print message
  3550.   if [ "$MSG" != "" ]; then
  3551.     # now output the final msg
  3552.     echo -e "$MSG" | grep -v ^$
  3553.     menu_entry_msg "$PKGNAME"
  3554.     echo
  3555.   else
  3556.     not_found "${PKGNAME}"
  3557.     exit 1
  3558.   fi
  3559. }
  3560.  
  3561.  
  3562. # pkg creation funcs
  3563.  
  3564. prepare_petbuild(){               # get 01mickos petbuild system from Git, if needed FUNCLIST
  3565.  
  3566.   local gitcheck=`which git`
  3567.  
  3568.   # exit if no devx
  3569.   [ "`which gcc`" = '' ] \
  3570.     && echo "You need the devx SFS installed to compile packages." \
  3571.     && rm /tmp/pkg/petbuild_prepared 2>/dev/null \
  3572.     && exit 3
  3573.  
  3574.   # exit if no git
  3575.   [ "`$gitcheck`" = '' ] \
  3576.     && echo "You need git installed to auto-install petbuild." \
  3577.     && rm /tmp/pkg/petbuild_prepared 2>/dev/null \
  3578.     && exit 3
  3579.  
  3580.   if [ ! -d /usr/share/petbuild ]; then
  3581.     rm /tmp/pkg/petbuild_prepared 2>/dev/null && exit 1
  3582.   fi
  3583.  
  3584.   # if petbuild prepared flag not yet set
  3585.   if [ ! -f /tmp/pkg/petbuild_prepared ]; then
  3586.  
  3587.     # backup old petbuild if installed
  3588.     [ -d /usr/share/petbuild/ ] && mv /usr/share/petbuild/ /usr/share/petbuild.backup/ 2>/dev/null
  3589.  
  3590.     # remove the original petbuild dir (we have a backup)
  3591.     if [ -d /usr/share/petbuild.backup/ ]; then
  3592.       rm -rf /usr/share/petbuild 2>/dev/null
  3593.     fi
  3594.  
  3595.     # clone petbuild into /usr/share/petbuild
  3596.     git clone https://github.com/puppylinux-woof-CE/petbuilds.git /usr/share/petbuild &>/dev/null
  3597.  
  3598.     # if still no petbuild dir in /usr/share, tell user to install themselves
  3599.     if [ ! -d /usr/share/petbuild ]; then
  3600.       echo >&2
  3601.       echo "Run this command to download PetBuild:" >&2
  3602.       echo >&2
  3603.       echo -e " ${green}git clone https://github.com/puppylinux-woof-CE/petbuilds /usr/share/petbuild${endcolour}" >&2
  3604.       echo >&2
  3605.       echo "Alternatively, use a different build tool backend by changing BUILDTOOL= " >&2
  3606.       echo "to one of the options below, in the $PKGRC file:" >&2
  3607.       echo "petbuild, buildpet, sbopkg or src2pkg" >&2
  3608.       echo >&2
  3609.  
  3610.       exit 1
  3611.     fi
  3612.  
  3613.     # go into buildpet dir, check which Pup we have ($DISTRO_DB_SUBNAME)
  3614.     # and checkout the right branch for the running system
  3615.     cd /usr/share/petbuild
  3616.  
  3617.     # check the puppy running and checkout the relevant branch
  3618.     if [ "`echo $DISTRO_DB_SUBNAME | grep slacko`" != '' ]; then
  3619.       if [ "`echo $DISTRO_DB_SUBNAME | grep '14.2'`" != '' ]; then
  3620.         git checkout slacko_142
  3621.       else
  3622.         git checkout slacko_141
  3623.       fi
  3624.     elif [ "`echo $DISTRO_DB_SUBNAME | grep tahrpup`" ]; then
  3625.       git checkout tahrpup
  3626.     elif [ "`echo $DISTRO_DB_SUBNAME | grep stretch`" ]; then
  3627.       git checkout stretch || git checkout tahrpup
  3628.     elif [ "`echo $DISTRO_DB_SUBNAME | grep xenialpup`" ]; then
  3629.       git checkout xenialpup || git checkout tahrpup
  3630.     elif [ "$DISTRO_DB_SUBNAME" != '' ]; then
  3631.       git checkout tahrpup
  3632.     else
  3633.       echo "No pet builds available for your system ($DISTRO_DB_SUBNAME),"
  3634.       echo "using buildpet instead.."
  3635.       rm -rf /usr/share/petbuild 2>/dev/null
  3636.     fi
  3637.  
  3638.     # buildpet setup finished, create flag
  3639.     echo 'true' > /tmp/pkg/petbuild_prepared
  3640.  
  3641.     cd "$CURDIR"
  3642.   fi
  3643. }
  3644.  
  3645.  
  3646. pkg_build(){                      # build package ($1) from source (see BUILDTOOL in pkgrc) FUNCLIST
  3647.  
  3648.   # exit if devx not installed
  3649.   [ "`which gcc`" = "" ]   && echo "You need the devx SFS installed to compile." && exit 1
  3650.  
  3651.   # get the settins from RC file
  3652.   . ${PKGRC}
  3653.  
  3654.   # we do a different build method for each supported build tool
  3655.   case $BUILDTOOL in
  3656.  
  3657.     petbuild) # by 01micko
  3658.  
  3659.       # use git to install the latest petbuild
  3660.       prepare_petbuild
  3661.  
  3662.       # if petbuild not installed, quit
  3663.       [ ! -d /usr/share/petbuild ] && error "PetBuild not installed at /usr/share/petbuild." && exit 3
  3664.  
  3665.       # petbuild needs a package name, or exit
  3666.       [ ! "$1" -o "$1" = "-" ] && print_usage pkg-build && exit 1 #220613 #250613
  3667.  
  3668.       # get pkg extension
  3669.       EX=`get_pkg_ext "$1"`
  3670.  
  3671.       # get pkg name only, no extension or path
  3672.       PKGNAME="$(basename "$1" .$EX)"
  3673.  
  3674.       # get petbuild PKGNAME, then build using 01mickos petbuild
  3675.       BUILDSCRIPT="`find /usr/share/petbuild -iname ${PKGNAME}"*.petbuild"| grep -m1 "$PKGNAME"`"
  3676.  
  3677.       # if we got a build script
  3678.       if [ -f "$BUILDSCRIPT" ]; then
  3679.         cd `dirname "$BUILDSCRIPT"`
  3680.         # compile the pkg
  3681.         bash *.petbuild
  3682.         # after build, move any pets created to WORKDIR
  3683.         mv /usr/share/petbuild/0pets_out/*.pet "$WORKDIR" 2>/dev/null
  3684.         # back to prev dir (WORKDIR)
  3685.         cd -
  3686.       fi
  3687.  
  3688.       # if build script not found, exit
  3689.       [ ! -f "$BUILDSCRIPT" ] && echo "Build script for '$PKGNAME' not found in /usr/share/petbuild/" && exit 1
  3690.  
  3691.       # get the packages we just moved to WORKDIR
  3692.       PKGFILES="`find "$WORKDIR" -iname "$PKGNAME*.pet"`"
  3693.  
  3694.       # list any packages we just built
  3695.       [ "$PKGFILES" != '' ] && echo -e "${green}Success:${endcolour} Packages in $WORKDIR:" && echo "$PKGFILES"
  3696.     ;;
  3697.  
  3698.     buildpet) # by Tman/iguleder
  3699.  
  3700.       # exit if buildpet dir not found
  3701.       [ ! -d /usr/share/buildpet ] && error "buildpet not installed." && exit 1
  3702.  
  3703.       # buildpet expects a PKGNAME, or exit
  3704.       [ ! "$1" -o "$1" = "-" ] && print_usage pkg-build && exit 1
  3705.  
  3706.       # get pkg extension
  3707.       EX=`get_pkg_ext "$1"`
  3708.  
  3709.       # get pkg name (with version), no extension or path
  3710.       PKGNAME="$(basename "$1" .$EX)"
  3711.  
  3712.       # get the buildpet script of PKGNAME
  3713.       BUILDSCRIPT="`find /usr/share/buildpet/ -name $PKGNAME"*"`"
  3714.  
  3715.       # if build script not found, exit
  3716.       [ ! -f "$BUILDSCRIPT" ] && echo "Build script for '$PKGNAME' not found in /usr/share/buildpet/" && exit 1
  3717.  
  3718.       # now compile the package
  3719.       PKG_CONFIGURE="$PKG_CONFIGURE" PKG_CFLAGS="$PKG_CFLAGS" buildpet $BUILDSCRIPT
  3720.  
  3721.     ;;
  3722.  
  3723.     src2pkg)  # by amigo
  3724.  
  3725.       ### This code is a horribly simple wrapper to a great tool..
  3726.       ### Implement all src2pkg options here at some point..
  3727.       ##
  3728.       ### We should pass all options to src2pkg, and make it build a
  3729.       ### (petbuild|buildpet) buildscipt on successful compile
  3730.  
  3731.       # exit if src2pkg not installed
  3732.       [ "`which src2pkg`" = '' ] && error "src2pkg not installed." && error "Get it from http://distro.ibiblio.org/amigolinux/download/src2pkg/src2pkg-3.0-noarch-2.txz" && exit 3
  3733.  
  3734.       # exit if $1 not given or valid
  3735.       [ ! "$1" -o "$1" = "-" ]   && print_usage pkg-build && exit 1
  3736.  
  3737.       # show src2pkg help if user supplied any of the help options
  3738.       if [ "$1" = '-h'  -o "$1" = '-hh'  -o "$1" = '--help' -o "$1" = '--more-help' -o "$1" = '--list' ]; then
  3739.         src2pkg $1 | sed -e "s/  src2pkg /  $SELF -pb /g"
  3740.         echo -e "\n   See 'src2pkg' by amigo"
  3741.         exit 0
  3742.       fi
  3743.  
  3744.       # get pkg extension.. not really needed here
  3745.       EX=`get_pkg_ext "$1"`
  3746.  
  3747.       # use amigos src2pkg
  3748.       src2pkg $1
  3749.  
  3750.       # if src2pkg exited without error, copy any pets it made to WORKDIR
  3751.       [ $? -eq 0 ] && mv /tmp/*.pet "$WORKDIR" 2>/dev/null
  3752.  
  3753.     ;;
  3754.  
  3755.     sbopkg)   # slackware peeps
  3756.  
  3757.       # exit if not installed
  3758.       [ "`which sbopkg`" = '' ] && error "Sbopkg not installed." && exit 3
  3759.  
  3760.       # exit if no valid options
  3761.       [ ! "$1" -o "$1" = "-" ]  && print_usage pkg-build && exit 1
  3762.  
  3763.       sbopkg -b "$1"
  3764.  
  3765.       # move any built pkgs to WORKDIR.. need a better way to do this
  3766.       if [ "`find /tmp/* -iname $1*.t*`" != '' ]; then
  3767.         mv /tmp/*.tgz "$WORKDIR" 2>/dev/null
  3768.         mv /tmp/*.txz "$WORKDIR" 2>/dev/null
  3769.         mv /tmp/*.tar.xz "$WORKDIR" 2>/dev/null
  3770.         echo -e "DONE... The package should be in $WORKDIR."
  3771.       fi
  3772.  
  3773.     ;;
  3774.  
  3775.   *)
  3776.     # get pkg name only, no extension or path
  3777.     PKGNAME="$(basename "$1" .$EX)"
  3778.  
  3779.     echo "Cannot compile '$PKGNAME', no build system installed."
  3780.     echo
  3781.     echo "Please install one of the following:"
  3782.     echo " * petbuild by 01micko: http://murga-linux.com/puppy/viewtopic.php?t=96027"
  3783.     echo " * buildpet by Tman:    http://murga-linux.com/puppy/viewtopic.php?t=81056"
  3784.     echo " * src2pkg  by amigo:   http://distro.ibiblio.org/amigolinux/download/src2pkg/"
  3785.     echo " * sbopkg   by various: https://www.sbopkg.org/downloads.php"
  3786.     echo
  3787.     echo "Then set BUILDTOOL to either petbuild, buildpet, src2pkg or sbopkg "
  3788.     echo "in $PKGRC to enable building packages from source."
  3789.     echo
  3790.     echo "NOTE:"
  3791.     echo "01mickos petbuild should be installed to /usr/share/petbuild"
  3792.     echo "And Tmans buildpet should be installed to /usr/share/buildpet"
  3793.   ;;
  3794.   esac
  3795. }
  3796.  
  3797.  
  3798. pkg_repack(){                     # create package ($1) from its *.files list FUNCLIST
  3799.  
  3800.   # exit if no valid options
  3801.   [ ! "$1" -o "$1" = "-" ] && print_usage pkg-repack && exit 1
  3802.  
  3803.   local list=''
  3804.   local pkg_ext=''
  3805.   local PKGNAME=''
  3806.   local PKGNAME_ONLY=''
  3807.   local dir=''
  3808.   local build_number=''
  3809.  
  3810.   # get pkg extension
  3811.   pkg_ext=`get_pkg_ext "$1"`
  3812.  
  3813.   #get pkg name only, no extension or path
  3814.   PKGNAME="$(LANG=C basename "$1" .$pkg_ext)"
  3815.  
  3816.   # assume the file name from $1
  3817.   PKGFILE="${CURDIR}/${PKGNAME}.$pkg_ext"
  3818.  
  3819.   # don't rely on the given pkg name string, get the full name from repo if poss
  3820.   PKGNAME=`get_pkg_name "$PKGNAME"`
  3821.  
  3822.   # get pkg name without version
  3823.   PKGNAME_ONLY="`get_pkg_name_only "${PKGNAME}"`"
  3824.  
  3825.   # if the list is empty, pkg is not user installed or built in, cant show contents
  3826.   [ "`is_installed_pkg "$PKGNAME"`" = false ] && echo "$PKGNAME needs to be installed." && exit 1
  3827.  
  3828.   # if the package to built already exists, ask user to delete it
  3829.   [ -f "${PKGFILE}" ] && echo "$PKGNAME.$pkg_ext already exists in $CURDIR" && rm -f "${PKGFILE}" 2>/dev/null
  3830.  
  3831.   # if pkg exists, user didn't delete it
  3832.   [ -f "${PKGFILE}" ] && exit 0
  3833.  
  3834.   # get the build number, and increment by one
  3835.   build_number="`grep -m1 "^$PKGNAME|$PKGNAME_ONLY|" "$USER_INST_PKGS_FILE" "$WOOF_INST_PKGS_FILE" "$DEVX_INST_PKGS_FILE" "$REPO_DB_FILE_DIR"/Packages-* | cut -f4 -d'|'`"
  3836.   build_number=${build_number:-0}
  3837.   build_number="-$(($build_number + 1))"
  3838.  
  3839.   # process the file list, copy each file to our pkg folder
  3840.   pkg_contents "$PKGNAME_ONLY" | while read line
  3841.   do
  3842.     if [ -f "$line" ]; then
  3843.       linedir="$(dirname "${line}")"
  3844.       [ ! -d "${CURDIR}/$PKGNAME/${linedir}" ] && mkdir -p "${CURDIR}/$PKGNAME/${linedir}"
  3845.       cp -p -P "$line" "${CURDIR}/${PKGNAME}/${linedir}"
  3846.     fi
  3847.   done
  3848.  
  3849.   sync
  3850.  
  3851.   # make sure we populated a pkg folder, ready to package up
  3852.   [ ! -d "${CURDIR}/${PKGNAME}/" ] && error "No '$PKGNAME' directory in $CURDIR" && exit 5
  3853.  
  3854.   # pkg folder should be populated, now package it up and print final msg
  3855.   dir2pet "${CURDIR}/${PKGNAME}/" "$build_number"
  3856.   rm -rf "${CURDIR}/${PKGNAME}/" 2>/dev/null
  3857.   rm -rf "${CURDIR}/${PKGNAME}$build_number/" 2>/dev/null
  3858.   sync
  3859. }
  3860.  
  3861.  
  3862. pkg_unpack(){                     # extract/unpack $1 to current dir FUNCLIST
  3863.  
  3864.   # exit if not valid usage
  3865.   [ ! -f "$1" ] && print_usage unpack && exit 1
  3866.  
  3867.   local PKGNAME
  3868.   local PKGFILE
  3869.   local PKGEXT
  3870.   local comp
  3871.  
  3872.   # get pkg details
  3873.   PKGFILE="$1"
  3874.   PKGNAME=`get_pkg_name "$PKGFILE"`
  3875.   PKGEXT=`get_pkg_ext "$PKGFILE"`
  3876.  
  3877.   # exit if we dont have enough info
  3878.   [ "$PKGEXT" = '' ]  && error "Cant get package extension" && exit 1
  3879.   [ ! -f "$PKGFILE" ] && error "Cant find $1" && exit 6
  3880.   [ "$PKGNAME" = '' ] && error "Cant get PKGNAME" && exit 3
  3881.  
  3882.   # support overriding the output dir with $2
  3883.   if [ "$2" != "" ]; then
  3884.     PKGNAME="$2"
  3885.   fi
  3886.  
  3887.   # determine compression utility
  3888.   case $PKGEXT in
  3889.  
  3890.     deb)
  3891.       mkdir "$PKGNAME" 2>/dev/null
  3892.       rm -rf "$PKGNAME"/* 2>/dev/null
  3893.       dpkg-deb -x "$PKGFILE" "$PKGNAME"    # extracts main pkg contents
  3894.       result=$?
  3895.       dpkg-deb -e "$PKGFILE" "$PKGNAME"/DEBIAN # extracts deb control files
  3896.       [ $result -eq 0 ] && echo -e "${green}Extracted${endcolour}: ${magenta}`basename $PKGFILE`${endcolour}" || error -e "${red}Error${endcolour}: Cannot extract $PKGFILE"
  3897.       return $result
  3898.       ;;
  3899.  
  3900.     rpm)
  3901.       mkdir "${PKGNAME}" 2>/dev/null
  3902.       rm -rf "$PKGNAME"/* 2>/dev/null
  3903.       # create dir called $PKGNAME, cd into it, extract the rpm in there
  3904.       cp "$PKGFILE" "$PKGNAME/"
  3905.       cd "$PKGNAME" 1>/dev/null
  3906.       # now extract the rpm contents into current dir
  3907.       exploderpm -x "$PKGFILE" &>/dev/null
  3908.       result=$?
  3909.       # clean up and leave
  3910.       rm -f *.rpm 2>/dev/null
  3911.       cd - 1>/dev/null
  3912.       # final msg
  3913.       [ $result -eq 0 ] && echo -e "${green}Success${endcolour}: File ${magenta}`basename $PKGFILE`${endcolour} extracted." || error "Cannot extract $PKGFILE"
  3914.       return $result
  3915.       ;;
  3916.  
  3917.     sfs)
  3918.  
  3919.       mkdir "/${PKGNAME}" 2>/dev/null
  3920.       rm -rf "$PKGNAME"/* 2>/dev/null
  3921.  
  3922.       # make mnt dir, mount sfs
  3923.       mkdir "/mnt/${PKGNAME}" 2>/dev/null
  3924.       mount -t squashfs "$PKGFILE" /mnt/"$PKGNAME" -o loop 1>/dev/null || { error "Could not mount $PKGFILE"; exit 3; }
  3925.       # copy sfs contents into ./$PKGNAME
  3926.       cp -a -Rf --remove-destination /mnt/"$PKGNAME" "$PKGNAME" || { error "Failed copying files from /mnt/$PKGNAME to ./$PKGNAME"; exit 4; }
  3927.       result=$?
  3928.       # clean up
  3929.       umount "/mnt/${PKGNAME}"
  3930.       rmdir "/mnt/${PKGNAME}"
  3931.       # final msg
  3932.       [ $result -eq 0 ] && echo -e "${green}Success${endcolour}: File ${magenta}`basename $PKGFILE`${endcolour} extracted." || { error "Cannot extract $PKGFILE"; exit 7; }
  3933.       return $result
  3934.       ;;
  3935.     pet)  file -b "$PKGFILE" | grep -i -q "^xz" && comp=xz || comp=gzip ;;
  3936.     tgz)  comp=gzip ;;
  3937.     gz)   comp=gzip ;;
  3938.     tbz)  comp=bzip2 ;;
  3939.     bz2)  comp=bzip2 ;;
  3940.     tlz)  comp=lzma ;;
  3941.     lzma) comp=lzma ;;
  3942.     txz)  comp=xz ;;
  3943.     xz)   comp=xz ;;
  3944.   esac
  3945.   sync
  3946.  
  3947.   # if pkg is extractable with tar, then unpack
  3948.   case $PKGEXT in
  3949.   pet|tgz|gz|tbz|bz2|tlz|lzma|txz|xz)
  3950.     ( umask 000 ; cat "$PKGFILE" | $comp -dc 2>/dev/null | tar -xf - 2> /dev/null && echo -e "${green}Success${endcolour}: File ${magenta}`basename $PKGFILE`${endcolour} extracted." || error "Cannot extract $PKGFILE" )
  3951.     ;;
  3952.   esac
  3953.  
  3954.   # return
  3955.   return ${result:-0}
  3956.  
  3957. }
  3958.  
  3959.  
  3960. pkg_combine(){                    # combine a pkg ($1) and deps into one single pkg FUNCLIST
  3961.  
  3962.   # exit if no valid options
  3963.   [ ! "$1" -o "$1" = "-" ] && print_usage pkg-combine && exit 1
  3964.  
  3965.   . ${PKGRC}
  3966.  
  3967.   local EX=''
  3968.   local PKGNAME=''
  3969.   local PKGNAME_ONLY=''
  3970.   local PKG_FILENAME=''
  3971.   local PKG_FILE=''
  3972.   local ALL_DEPS=''
  3973.   local PKG_DEPLIST=''
  3974.   local SUFFIX="${CP_SUFFIX}"
  3975.   local BUILD_DIR=$TMPDIR/build_pkg
  3976.   local SFS_FILE
  3977.   local please_wait=false
  3978.   local PREVDIR="$CURDIR"
  3979.   local ask_opt=$ASK
  3980.   local force_opt=$FORCE
  3981.  
  3982.   cd "$WORKDIR"
  3983.  
  3984.   # get pkg extension
  3985.   EX=`get_pkg_ext "$1"`
  3986.  
  3987.   # get reliable package names
  3988.   PKGNAME="$(basename "$1" .$EX)"
  3989.   PKGNAME=`get_pkg_name "$PKGNAME"`
  3990.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  3991.  
  3992.   PKG_FILE="${WORKDIR}/${PKGNAME}.$EX" #the file to install
  3993.  
  3994.   # we want to include all deps, except builtins, by default
  3995.   # we dont set HIDE_BUILTINS here, we leave that up to user
  3996.   # when user gives -F, HIDE_BUILTINS= false, and builtin
  3997.   # pkgs will be included in the package created
  3998.   FORCE=true
  3999.   HIDE_USER_PKGS=false
  4000.  
  4001.   # get full pkg filename (inc name-ver.ext).. try repos first
  4002.   PKG_FILENAME="`cut -f8 -d'|' "$REPO_DB_FILE_DIR"/Packages-* | grep -m1 "^$PKGNAME"`"
  4003.   # then WORKDIR if needed (dont include the combined pkgs)
  4004.   [ "$PKG_FILENAME" = "" ] && PKG_FILENAME="`ls -1 "$WORKDIR" | grep -v "${SUFFIX}" | grep -v ".sfs\$" | grep -m1 "^$PKGNAME"`"
  4005.  
  4006.   # just in case its empty, revert back to the earlier PKGNAME value
  4007.   [ "$PKG_FILENAME" = "" ] && PKG_FILENAME="$PKGNAME"
  4008.  
  4009.   [ "$PKG_FILENAME" = "" ] && error "Cant find $1" && exit 1
  4010.  
  4011.   # update PKGFILE with new pkg filename (PKG)
  4012.   PKG_FILE="${WORKDIR}/${PKG_FILENAME}"
  4013.  
  4014.   # check if the desired file has already been built
  4015.   [ "$COMBINE2SFS" = true ] && final_ext=sfs || final_ext=pet
  4016.   [ -f "${WORKDIR}/${PKGNAME}-${SUFFIX}.$final_ext" ] && echo -e "File ${magenta}${WORKDIR}/${PKGNAME}-${SUFFIX}.${final_ext}${endcolour} already exists." && continue
  4017.  
  4018.   # get list of deps for PKGNAME, if no deps, exit
  4019.   PKG_DEPLIST="`FORCE=$force_opt list_deps $PKGNAME_ONLY`"
  4020.   [ "$PKG_DEPLIST" = "" ] && echo "No need to combine, $PKGNAME has no dependencies." && exit 1
  4021.  
  4022.   # if exact match of pkgname is found in a repo list
  4023.   if [ "`is_repo_pkg "$PKGNAME"`" = true ]; then
  4024.  
  4025.     echo "Please wait.. Gathering dependency information."
  4026.  
  4027.     # get the deps of the pkg, but dont install
  4028.     FORCE=$force_opt find_deps "$PKGNAME"
  4029.     ALL_DEPS="`cat $TMPDIR/deps_installed $TMPDIR/deps_missing 2>/dev/null | sort | uniq`"
  4030.  
  4031.     # download the needed package
  4032.     if [ ! -f "$PKG_FILE" -o "$FORCE" = true ]; then
  4033.       ASK=false pkg_download "$PKGNAME"
  4034.     fi
  4035.  
  4036.     # make work dirs
  4037.     mkdir -p "$BUILD_DIR/"
  4038.     mkdir -p "$BUILD_DIR/${PKGNAME}-${SUFFIX}"
  4039.  
  4040.     if [ -f "$PKG_FILE" ]; then
  4041.       # copy the main pkg to the tmp dir
  4042.       # and remove the downloade file, we no longer need it
  4043.       cp "$PKG_FILE" "$BUILD_DIR/" 2>/dev/null && rm "$PKG_FILE"
  4044.     else
  4045.        error "Could not add the main package '$PKGNAME_ONLY'"
  4046.     fi
  4047.  
  4048.  
  4049.     # make a cleaned list to go over (sanity check)
  4050.     ALL_DEPS_LIST="`echo "$ALL_DEPS" | tr ',' '\n' | grep -v "^\$" | sort | uniq`"
  4051.  
  4052.     # go through each dep listed
  4053.     echo "$ALL_DEPS_LIST" | while read LINE
  4054.     do
  4055.  
  4056.       [ "$LINE" = "" -o "$LINE" = "-" -o "$LINE" = " " -o "$LINE" = "," -o "$LINE" = ", " ] && continue
  4057.  
  4058.       # only include builtins if HIDE_BUILTINS=true
  4059.       [ "`is_builtin_pkg "$LINE"`" = true -a "$HIDE_BUILTINS" = true ] && continue
  4060.  
  4061.       # only include devx pkgs if user gave the -f option
  4062.       [ "`is_devx_pkg "$LINE"`" = true -a "$force_opt" = false ] && continue
  4063.  
  4064.       # download the matching package(s)
  4065.       [ "`list_downloaded_pkgs "$LINE"`" = "" ] && ASK=false pkg_download "$LINE"
  4066.  
  4067.       # get the downloaded file
  4068.       PKGDEP="`find "$WORKDIR" -maxdepth 1 -type f -name "${LINE}*" | grep -v ".sfs\$" | grep -v "${SUFFIX}" | head -1`"
  4069.  
  4070.       # re-try download in other repos if needed
  4071.       if [ ! -f "$PKGDEP" ]; then
  4072.         PKGREPO=''; PKGREPO="`LANG=C which_repo $LINE | cut -f2 -d' ' | head -1`"
  4073.         [ "$PKGREPO" != "" ] && ASK=false pkg_download "$LINE"
  4074.       fi
  4075.  
  4076.       #if downloaded
  4077.       if [ -f "$PKGDEP" -o "$FORCE" = true ]; then
  4078.  
  4079.         # copy dep to the tmp dir, with the main pkg
  4080.         if [ ! -f "$BUILD_DIR/$PKGDEP" ]; then
  4081.           cp "$PKGDEP" "$BUILD_DIR/$(basename $PKGDEP)" && rm "$PKGDEP"
  4082.         else
  4083.           error "Cannot copy $PKGDEP to $BUILD_DIR/$(basename $PKGDEP)"
  4084.         fi
  4085.  
  4086.       else # dep not found, may be missing, or in another repo
  4087.         echo -e "${yellow}Warning:${endcolour} $LINE not downloaded to $WORKDIR.. Cannot add $LINE.."
  4088.         continue
  4089.       fi
  4090.     done
  4091.  
  4092.     # we should now be ready to make our combined pkg
  4093.     cd "$BUILD_DIR"
  4094.  
  4095.     PARENTPKG=${PKGNAME}
  4096.  
  4097.     # for all pkgs in the tmp dir (nto including any 'combined' pkgs)
  4098.     TMP_PKGS="`find "$BUILD_DIR/" -maxdepth 1 -type f -name "*" | grep -v $SUFFIX | grep -v ^$ | grep -v ' ' | sort | uniq`"
  4099.  
  4100.     local count=1
  4101.  
  4102.     for i in $TMP_PKGS
  4103.     do
  4104.  
  4105.       # skip if not a valid pkg file
  4106.       [ ! "$i" -o "$i" = ' ' -o "$i" = '' -o ! -f "$i" ] && continue
  4107.  
  4108.       # dont include the combined pkgs
  4109.       [ "`echo "$i" | grep -m1 "${SUFFIX}"`" != "" ] && continue
  4110.  
  4111.       # get the extensions of each file .. they might be from different repos, so cant use $EX
  4112.       base="`basename "$i" 2>/dev/null`"
  4113.       FILE_EXT=`get_pkg_ext "$base"`
  4114.  
  4115.       [ ! "$base" -o "$base" = ' ' -o "$base" = '' ] && continue
  4116.  
  4117.       # get name without version for this pkg ($i)
  4118.       name_only=`get_pkg_name_only "$i"`
  4119.  
  4120.       [ ! "$name_only" -o "$name_only" = ' ' -o "$name_only" = '' ] && continue
  4121.  
  4122.       CONFIRM=y
  4123.       if [ "$ask_opt" = true ]; then
  4124.         echo -n "Add package: $name_only  ($FILE_EXT)  (y/N):  "
  4125.         read -n 1 CONFIRM </dev/tty
  4126.         echo
  4127.       else
  4128.         echo "Adding package: $name_only  ($FILE_EXT)"
  4129.       fi
  4130.  
  4131.       # skip pkg if user wants to skip it
  4132.       if [ "$CONFIRM" != 'y' ]; then
  4133.         rm "$i"
  4134.         continue
  4135.       fi
  4136.  
  4137.       # add each file
  4138.       case $FILE_EXT in
  4139.       pet)
  4140.  
  4141.         # convert and extract
  4142.         pkg_unpack "${i}" 1>/dev/null
  4143.         sync
  4144.  
  4145.         # copy extracted contents (only the stuff inside the extracted folders)
  4146.         if [ ! -d "${i/.pet/}/" ]; then
  4147.           error "Dir '${i/.pet/}/' does not exist"
  4148.         fi
  4149.         cp -a --preserve=all -fr -L "${i/.pet/}/"* "${PARENTPKG}-${SUFFIX}/"
  4150.  
  4151.         if [ -f "${i/.pet/}/pinstall.sh" ]; then
  4152.           mv "${i/.pet/}/pinstall.sh" "${PARENTPKG}-${SUFFIX}/pinstall_${count}.sh"
  4153.         fi
  4154.  
  4155.         # now remove the pet and extract folder
  4156.         rm "${i}"
  4157.         rm -rf "${i/.pet/}/"
  4158.         sync
  4159.         ;;
  4160.  
  4161.       deb)
  4162.  
  4163.         #cp "$i" "$BUILD_DIR/${PARENTPKG}-${SUFFIX}"
  4164.         pkg_unpack "$i" 2>$TMPDIR/$SELF-cp-errlog
  4165.         dpkg-deb -e "$i" "$BUILD_DIR/${PARENTPKG}-${SUFFIX}/DEBIAN"
  4166.  
  4167.         # .deb package names often have extra stuff at the end of the file name
  4168.         # that does not exist in the root folder name, inside the package.. so
  4169.         # we need to strip off that extra stuff to get the correct unpacked
  4170.         # package dir
  4171.         local pkg_dir="${i/.deb/}/"
  4172.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/_all/}"
  4173.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/_i386/}"
  4174.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/_amd64/}"
  4175.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/_x64/}"
  4176.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/~dfsg-[0-9]/}"
  4177.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/~dsfg-[0-9]/}"
  4178.  
  4179.         if [ ! -d "$pkg_dir" ]; then
  4180.           needle="$(get_pkg_name "$i")"
  4181.           pkg_dir="$(find "${BUILD_DIR}" -type d -iname "$needle" | head -1)"
  4182.         fi
  4183.  
  4184.         if [ ! -d "$pkg_dir" ]; then
  4185.           error "Dir '$pkg_dir' does not exist!"
  4186.         fi
  4187.  
  4188.         # copy extracted contents (only the stuff inside the extracted folders)
  4189.         cp -a --preserve=all -fr -L "${pkg_dir}/"* "${PARENTPKG}-${SUFFIX}/"
  4190.  
  4191.         if [ -f "${pkg_dir}DEBIAN/postinst" ]; then
  4192.           mv "${pkg_dir}DEBIAN/postinst" "${PARENTPKG}-${SUFFIX}/DEBIAN/postinst_${count}"
  4193.         fi
  4194.  
  4195.         if [ -f "${pkg_dir}DEBIAN/preinst" ]; then
  4196.           mv "${pkg_dir}DEBIAN/preinst" "${PARENTPKG}-${SUFFIX}/DEBIAN/preinst_${count}"
  4197.         fi
  4198.  
  4199.         # now remove the deb and extracted folder
  4200.         rm "$i"
  4201.         rm -rf "$pkg_dir"
  4202.         sync
  4203.         ;;
  4204.  
  4205.       *tgz|*txz|*tar.gz|*tar.xz|*xz|*gz)
  4206.  
  4207.         pkg_unpack "$i" 1>/dev/null
  4208.  
  4209.         # now copy $BUILD_DIR/$PKGNAME/* into our output (combined pkg) dir
  4210.         cp -a --preserve=all -fr -L "${name_only}/"* "${PARENTPKG}-${SUFFIX}/" || error "Dir '${name_only}/' does not exist!"
  4211.  
  4212.         if [ -f "${PARENTPKG}-${SUFFIX}/install/doinst.sh" ]; then
  4213.           mv "${PARENTPKG}-${SUFFIX}/install/doinst.sh" "${PARENTPKG}-${SUFFIX}/install/doinst_${count}.sh"
  4214.         fi
  4215.  
  4216.         # remove the package and folder we just extracted
  4217.         rm "$i"
  4218.         rm -rf "${i/.*/}/"
  4219.         sync
  4220.         ;;
  4221.  
  4222.       rpm)
  4223.         pkg_unpack "$i" 1>/dev/null
  4224.  
  4225.         # now copy $BUILD_DIR/$PKGNAME/* into our output (combined pkg) dir
  4226.         cp -a --preserve=all -fr -L "${name_only}/"* "${PARENTPKG}-${SUFFIX}/"
  4227.  
  4228.         if [ -f "${PARENTPKG}-${SUFFIX}/install/doinst.sh" ]; then
  4229.           mv "${PARENTPKG}-${SUFFIX}/install/doinst.sh" "${PARENTPKG}-${SUFFIX}/install/doinst_${count}.sh"
  4230.         fi
  4231.  
  4232.         # remove the package and folder we just extracted
  4233.         rm "$i"
  4234.         rm -rf "${i/.rpm/}/"
  4235.         ;;
  4236.  
  4237.       esac
  4238.       count=$(($count + 1))
  4239.     done
  4240.  
  4241.     # we have now unpacked the package, lets combine the pre/post install scripts
  4242.     # into one pinstall.sh script
  4243.  
  4244.     local pinstall_file="$BUILD_DIR/${PARENTPKG}-${SUFFIX}/pinstall.sh"
  4245.  
  4246.     # append this pinstall/postinst/doinst script to a new pinstall.sh
  4247.     echo '#!/bin/sh' > "$pinstall_file"
  4248.  
  4249.     # concat the post install scripts (for puppy, slackware, arch, debian/ubuntu, etc) into 'pinstall.sh'
  4250.     for i in ${PARENTPKG}-${SUFFIX}/pinstall_* ${PARENTPKG}-${SUFFIX}/install/doinst_* ${PARENTPKG}-${SUFFIX}/DEBIAN/postinst_*
  4251.     do
  4252.       [ -z ${PARENTPKG}-${SUFFIX}/${i} ] && continue
  4253.       cd ${PARENTPKG}-${SUFFIX}/ 2>/dev/null
  4254.  
  4255.       files=`find . -type f -iname "$(basename $i)"`
  4256.  
  4257.       for file in $files
  4258.       do
  4259.         if [ -f "$file" ]; then
  4260.           echo "Appending ${file} to $pinstall_file"
  4261.           cat ${file} | grep -vE '^#!|^exit|exit 0|exit 1' >> "$pinstall_file"
  4262.           echo '' >> "$pinstall_file"
  4263.         fi
  4264.       done
  4265.       # we have combined the pre/post install stuff in $i into a new, combined file,
  4266.       # so remove the original
  4267.       rm -f ${PARENTPKG}-${SUFFIX}/${i}
  4268.     done
  4269.  
  4270.     chmod +x $BUILD_DIR/${PARENTPKG}-${SUFFIX}/pinstall.sh 2>/dev/null
  4271.  
  4272.     #xmessage "$(cat $BUILD_DIR/${PARENTPKG}-${SUFFIX}/pinstall.sh)"
  4273.  
  4274.     # remove any debian/ubuntu/slackware pre/post install stuff left in the package
  4275.     rm -rf $BUILD_DIR/${PARENTPKG}-${SUFFIX}/pinstall_*
  4276.     rm -rf $BUILD_DIR/${PARENTPKG}-${SUFFIX}/install
  4277.     rm -rf $BUILD_DIR/${PARENTPKG}-${SUFFIX}/DEBIAN
  4278.  
  4279.     # fix the symlinks to lib dirs - the linux-gnu-* dirs are symlinks in puppy,
  4280.     # so make sure we dont replace them with dirs (or programs won't load)
  4281.     for libdir in i386-linux-gnu i486-linux-gnu i586-linux-gnu i686-linux-gnu amd64-linux-gnu
  4282.     do
  4283.       if [ -d $BUILD_DIR/${PARENTPKG}-${SUFFIX}/usr/lib/${libdir}/ ]; then
  4284.         mv -n -u $BUILD_DIR/${PARENTPKG}-${SUFFIX}/usr/lib/${libdir}/* $BUILD_DIR/${PARENTPKG}-${SUFFIX}/usr/lib/
  4285.         cd $BUILD_DIR/${PARENTPKG}-${SUFFIX}/usr/lib
  4286.         rmdir ${libdir}/
  4287.         ln -s ${libdir}/ .
  4288.         cd -
  4289.       fi
  4290.     done
  4291.  
  4292.  
  4293.     # now we are ready to build our combined pkg
  4294.     [ "`pwd`" != "$BUILD_DIR" ] && cd "$BUILD_DIR/"
  4295.  
  4296.  
  4297.     # if not building SFS, build a .pet
  4298.     if [ "$COMBINE2SFS" = false ]; then #240613
  4299.       echo "Building PET package.. Please wait.."
  4300.  
  4301.       # build pet package... start with $removing PKGNAME-${SUFFIX}.pet.specs
  4302.       rm ${PKGNAME}-${SUFFIX}/*.specs 2>/dev/null
  4303.  
  4304.       # get compression type
  4305.       file -b "${PKGNAME}-${SUFFIX}" | grep -i -q "^xz" && TAREXT=xz || TAREXT=gz
  4306.  
  4307.       # tar up the folder
  4308.       tar -c -f ${PKGNAME}-${SUFFIX}.tar ${PKGNAME}-${SUFFIX} &>/dev/null
  4309.       sync
  4310.  
  4311.       case $TAREXT in
  4312.         xz)xz -z -9 -e ${PKGNAME}-${SUFFIX}.tar ;;
  4313.         gz)gzip --best ${PKGNAME}-${SUFFIX}.tar ;;
  4314.       esac
  4315.  
  4316.       TARBALL="${PKGNAME}-${SUFFIX}.tar.$TAREXT"
  4317.       FULLSIZE="`stat --format=%s ${TARBALL}`"
  4318.       MD5SUM="`md5sum $TARBALL | cut -f 1 -d ' '`"
  4319.       echo -n "$MD5SUM" >> $TARBALL
  4320.       sync
  4321.  
  4322.       mv -f $TARBALL ${PKGNAME}-${SUFFIX}.pet
  4323.       sync
  4324.  
  4325.       if [ $? -eq 1 ]; then
  4326.         echo "Cannot create PET file ${PKGNAME}-${SUFFIX} from dir."
  4327.         error "Please check `pwd`"
  4328.       fi
  4329.  
  4330.       # move our new PET to $WORKDIR
  4331.       mv ${PKGNAME}-${SUFFIX}.pet $WORKDIR/${PKGNAME}-${SUFFIX}.pet
  4332.       # get the size
  4333.       s=`LANG=C du -m "$WORKDIR/${PKGNAME}-${SUFFIX}.pet" | sed "s/\s.*//"`
  4334.  
  4335.     else #240613  build a .sfs
  4336.       echo "Building SFS package.. Please wait.."
  4337.  
  4338.       dir2sfs "${PKGNAME}-${SUFFIX}" 1>/dev/null
  4339.  
  4340.       # get the full file name, it may (or not) have been appended with '-DISTRO_VERSION'
  4341.       SFS_FILE=`find . -maxdepth 1 -type f -name "*${PKGNAME}-${SUFFIX}*.sfs" | head -1`
  4342.  
  4343.       s=`LANG=C du -m "${SFS_FILE}" | sed "s/\s.*//"`
  4344.       MD5SUM=`md5sum "$SFS_FILE" | cut -f1 -d ' '`
  4345.       mv "$SFS_FILE" "$WORKDIR/"
  4346.     fi
  4347.  
  4348.     # done, go back to work dir from WORKDIR/build_pkg
  4349.     cd "$WORKDIR"
  4350.     rm -R  "$BUILD_DIR/" 2>/dev/null
  4351.  
  4352.     # create install command
  4353.     if [ "$COMBINE2SFS" = false ]; then
  4354.       PKGEXT=pet
  4355.       PKGCMD="Install command: $SELF install ${WORKDIR}/${PKGNAME}-${SUFFIX}"
  4356.     else
  4357.       PKGEXT=sfs
  4358.       PKGCMD="Install command: sfs_loadr --cli -q \"${WORKDIR}/$(basename $SFS_FILE)\""
  4359.     fi
  4360.  
  4361.     #226013 updated output
  4362.     echo -e "Created package ${magenta}${PKGNAME}-${SUFFIX}.${PKGEXT}${endcolour} (${s}MB)"
  4363.     echo "The md5 checksum: $MD5SUM"
  4364.     echo "$PKGCMD"
  4365.  
  4366.   else # no exact PKGNAME match in repo
  4367.     not_found "${PKGNAME}"
  4368.     exit 1
  4369.   fi
  4370.  
  4371.   cd "$PREVDIR"
  4372. }
  4373.  
  4374.  
  4375. merge_pkg(){                      # merge the given comma-separated packages FUNCLIST
  4376.   # make sure at least 2 packages exist
  4377.  
  4378.   [ ! "$1" ] && print_usage merge && exit 1
  4379.   [ "$(echo "$1" |grep ',')" = "" ] && print_usage merge && exit 1
  4380.  
  4381.   cd "$WORKDIR" 1>/dev/null
  4382.  
  4383.   local PKG_LIST="$(echo "${1}" | tr ',' '\n' | sort -u | uniq)"
  4384.   local SUFFIX=''
  4385.  
  4386.   if [ "$2" = "--with-deps" ]; then
  4387.     SUFFIX="-WITHDEPS"
  4388.     echo "Gathering dependencies to include.."
  4389.     echo
  4390.     # add deps of pkgs to list of pkgs to merge
  4391.     for package in $PKG_LIST
  4392.     do
  4393.       PKG_LIST="$PKG_LIST $(pkg le $package)"
  4394.     done
  4395.     PKG_LIST="$(echo "$PKG_LIST" | tr ' ' '\n' | sort -u | uniq)"
  4396.   fi
  4397.  
  4398.   echo "Merging the following packages:"
  4399.   echo
  4400.   echo "${PKG_LIST}" | sed 's/^/  /g'
  4401.   echo
  4402.  
  4403.   # get a new package name for the new, merged package
  4404.   local PKGNAME="`get_pkg_name "$(basename "${1//,*/}")" 2>/dev/null`"
  4405.   bash -c 'read -e -r -p "Enter a package name and hit ENTER: " -i "${PKGNAME}-MERGED${SUFFIX}" NEWPKGNAME; echo $NEWPKGNAME > /tmp/pkg/NEWPKGNAME'
  4406.   echo
  4407.   NEWPKGNAME="$(cat /tmp/pkg/NEWPKGNAME)"
  4408.  
  4409.   if [ "$NEWPKGNAME" = "" ]; then
  4410.     echo "You must give a new package name!"
  4411.     return 1
  4412.   fi
  4413.  
  4414.   rm -rf "${NEWPKGNAME}/" &>/dev/null
  4415.   mkdir "${NEWPKGNAME}"
  4416.  
  4417.   # remove any old folders
  4418.   rm -rf ${WORKDIR}${NEWPKGNAME}/ &>/dev/null
  4419.  
  4420.   # for each package to merge
  4421.   for package in $PKG_LIST
  4422.   do
  4423.  
  4424.     # get the package details
  4425.     local PKGNAME="`get_pkg_name "$(basename "$package")" 2>/dev/null`"
  4426.     local PKGNAME_ONLY="`get_pkg_name_only "$PKGNAME" 2>/dev/null`"
  4427.     local PKGEXT="`get_pkg_ext "$PKGNAME"`"
  4428.     local PKGFILE=''
  4429.  
  4430.     [ "$PKGNAME" = "" ] && continue
  4431.     [ "$PKGNAME" = " " ] && continue
  4432.     [ "$PKGNAME" = "-" ] && continue
  4433.  
  4434.     # download it
  4435.     pkg_download "$PKGNAME"
  4436.  
  4437.     retval=$?
  4438.  
  4439.     if [ $retval -eq 1 ]; then
  4440.       echo -e "${yellow}Warning${endcolour}: package '$PKGNAME' not downloaded."
  4441.       continue
  4442.     fi
  4443.  
  4444.     # get the downloaded file
  4445.     PKGFILE="$(find $WORKDIR -maxdepth 1 -type f -name "${PKGNAME}*${PKGEXT}")"
  4446.  
  4447.     [ ! -f "$PKGFILE" ] && \
  4448.       PKGFILE="$(find $WORKDIR -maxdepth 1 -type f -name "${PKGNAME_ONLY}_*${PKGEXT}")"
  4449.  
  4450.     [ ! -f "$PKGFILE" ] && \
  4451.       PKGFILE="$(find $WORKDIR -maxdepth 1 -type f -name "${PKGNAME_ONLY}-*${PKGEXT}")"
  4452.  
  4453.     if [ ! -f "$PKGFILE" ]; then
  4454.       echo -e "${red}Error${endcolour}: could not find downloaded package '$PKGFILE'"
  4455.       return 1
  4456.     fi
  4457.  
  4458.     # unpack the downloaded file
  4459.     pkg_unpack "$PKGFILE"
  4460.     rm "${PKGFILE}"
  4461.  
  4462.     # move the unpacked files into our new package dir
  4463.     PKGDIR="$(find $WORKDIR -maxdepth 1 -type d -name "${PKGNAME}*" | grep -v ${NEWPKGNAME})"
  4464.  
  4465.     if [ ! -d "$PKGDIR" ]; then
  4466.       echo -e "${red}Error${endcolour}: could not find extracted package dir '$PKGDIR'"
  4467.       return 1
  4468.     fi
  4469.  
  4470.     cp -R "$PKGDIR"/* "$NEWPKGNAME"
  4471.     rm -rf "${PKGDIR}/"
  4472.   done
  4473.  
  4474.   if [ ! -d "${WORKDIR}/$NEWPKGNAME" ]; then
  4475.     echo -e "${red}Error${endcolour}: Dir ${lightblue}${NEWPKGNAME}${endcolour} NOT created!"
  4476.     return 1
  4477.   fi
  4478.  
  4479.   echo
  4480.   echo "Package contents:"
  4481.   find "${NEWPKGNAME}/"
  4482.   echo '--------------------------------'
  4483.   echo
  4484.  
  4485.   # create the merged PET package
  4486.   dir2pet "${NEWPKGNAME}/" || error "Could not create $NEWPKGNAME} PET file"
  4487.  
  4488.   rm -rf "./${NEWPKGNAME}/" &>/dev/null
  4489.   rm -rf "${WORKDIR}/${NEWPKGNAME}/" &>/dev/null
  4490.  
  4491.   # get the filename of the new PET package
  4492.   NEWPKGFILENAME="$(find . -maxdepth 1 -type f -name "${NEWPKGNAME}*.pet")"
  4493.  
  4494.   # output messages
  4495.   if [ ! -f "$NEWPKGFILENAME" ]; then
  4496.     echo
  4497.     echo -e "${red}Error${endcolour}: Package ${yellow}${NEWPKGFILENAME}${endcolour} NOT created!"
  4498.     return 1
  4499.   fi
  4500.  
  4501.   return 0
  4502. }
  4503.  
  4504.  
  4505. split_pkg(){                      # split package ($1) into dev, doc, nls packages FUNCLIST
  4506.  
  4507.   # make sure the package exists
  4508.   [ ! "$1" -o ! -f "$1" ] && print_usage split && exit 1
  4509.  
  4510.   local PKGNAME="`get_pkg_name "$(basename "$1")" 2>/dev/null`"
  4511.   local PKGNAME_ONLY="`get_pkg_name_only "$PKGNAME" 2>/dev/null`"
  4512.   local PKGEXT="pet"
  4513.  
  4514.   # get package extension
  4515.   PKGEXT="`get_pkg_ext "$1"`"
  4516.  
  4517.   pkg_unpack "$1" &>/dev/null
  4518.  
  4519.   # get the package directory we just extracted
  4520.   local PKG_PATH="$(find $(dirname "$PKGNAME") -maxdepth 1 -type d -iname "$PKGNAME" | head -1)"
  4521.  
  4522.   # get the full path to the package dir (dir with contents of pkg to be split)
  4523.   PKG_PATH="$(realpath "$PKG_PATH")"
  4524.  
  4525.   # make sure we have the right package dir
  4526.   [ ! -d "$PKG_PATH" ] && PKG_PATH="$(realpath "$PKGNAME")"
  4527.  
  4528.   # get the base name of the package directory
  4529.   local PKG_DIR_NAME=$(basename "$PKG_PATH")
  4530.  
  4531.   # get the package name
  4532.   local PKG_NAME="$PKGNAME_ONLY"
  4533.  
  4534.   # get the parent directory of the package
  4535.   local PARENT_DIR="$(dirname "$PKG_PATH")"
  4536.  
  4537.   # set the correct package naming style
  4538.   local dev_suffix='_DEV'
  4539.   local doc_suffix='_DOC'
  4540.   local nls_suffix='_NLS'
  4541.   if [ "${PKGEXT:-pet}" = "deb" ]; then
  4542.     dev_suffix='-dev'
  4543.     doc_suffix='-doc'
  4544.     nls_suffix='-nls'
  4545.   fi
  4546.  
  4547.   # get the sub-package names
  4548.   local EXE_PKG="$PKGNAME"
  4549.   local DEV_PKG="$(echo "$PKG_DIR_NAME" | sed -e "s/^${PKGNAME_ONLY}/${PKGNAME_ONLY}${dev_suffix}/")"
  4550.   local DOC_PKG="$(echo "$PKG_DIR_NAME" | sed -e "s/^${PKGNAME_ONLY}/${PKGNAME_ONLY}${doc_suffix}/")"
  4551.   local NLS_PKG="$(echo "$PKG_DIR_NAME" | sed -e "s/^${PKGNAME_ONLY}/${PKGNAME_ONLY}${nls_suffix}/")"
  4552.  
  4553.   # remove the target package dirs if they already exist
  4554.   [ -d "$DEV_PKG" ] && rm -rf "$DEV_PKG"
  4555.   [ -d "$DOC_PKG" ] && rm -rf "$DOC_PKG"
  4556.   [ -d "$NLS_PKG" ] && rm -rf "$NLS_PKG"
  4557.   # now create them fresh and empty
  4558.   mkdir -p "$DEV_PKG"
  4559.   mkdir -p "$DOC_PKG"
  4560.   mkdir -p "$NLS_PKG"
  4561.  
  4562.   # make sure the package directory begins with the given package name
  4563.   case $(basename "$PKG_PATH") in
  4564.     $PKG_NAME*)
  4565.         ;;
  4566.     *)
  4567.         echo "Error: $(basename "$PKG_PATH") must match $PKG_NAME"
  4568.         print_usage split
  4569.         exit 1
  4570.         ;;
  4571.   esac
  4572.  
  4573.   cd "$PKG_PATH" &>/dev/null || { echo "Not found: $PKG_PATH"; return 1; }
  4574.  
  4575.   for i in $(find -mindepth 1)
  4576.   do
  4577.     # if the file no longer exists, skip this iteration
  4578.     [ ! -e "$i" ] && continue
  4579.  
  4580.     local FILE_NAME=$(basename "$i")
  4581.  
  4582.     case "$FILE_NAME" in
  4583.         *.la|*.a|*.o|*.prl|pkgconfig|include|*.m4|*.h|*.c|*.cpp)
  4584.             PKGNAME="$DEV_PKG"
  4585.             ;;
  4586.         gdb)
  4587.             [ -d "$i" ] && PKGNAME="$DEV_PKG"
  4588.             ;;
  4589.         dir)
  4590.             [ -f "$i" ] && PKGNAME="$DOC_PKG"
  4591.             ;;
  4592.         doc|*-doc|gtk-doc|Help|HELP|readme.*|README.*|ABOUT|about.txt|ABOUT.TXT|readme|README|manual|MANUAL|faq|FAQ|todo|TODO|examples|EXAMPLES|LessTif|man-html|E-docs)
  4593.             PKGNAME="$DOC_PKG"
  4594.             ;;
  4595.         help)
  4596.             [ -d "$i" ] && PKGNAME="$DOC_PKG"
  4597.             ;;
  4598.         man|info) # if it's a directory named "man" or "info", move to doc
  4599.             [ -d "$i" ] && PKGNAME="$DOC_PKG"
  4600.              ;;
  4601.         locale|locales|lang|strings) # if it's a directory named "locale", move to nls
  4602.             [ -d "$i" ] && PKGNAME="$NLS_PKG"
  4603.             ;;
  4604.         i18n|nls)
  4605.             PKGNAME="$NLS_PKG"
  4606.             ;;
  4607.         system.profile-*|*.strings|normal.awt-*) # AbiWord stores its locale information in those files
  4608.             [ "$PKGNAME_ONLY" = "abiword" ] && PKGNAME="$NLS_PKG"
  4609.             ;;
  4610.         *)
  4611.             PKGNAME="$EXE_PKG"
  4612.             ;;
  4613.     esac
  4614.  
  4615.     # verbosity, output the redirection for each redirected file
  4616.     case "$PKGNAME" in
  4617.         $DEV_PKG|$DOC_PKG|$NLS_PKG)
  4618.             #local SUFFIX="$(echo $SUFFIX | tr [:lower:] [:upper:])"
  4619.  
  4620.             #echo "$FILE_NAME -> $PKG"
  4621.  
  4622.             # detect the sub_directory inside the package
  4623.             local SUB_DIR="${i%/$FILE_NAME}"
  4624.             SUB_DIR="${SUB_DIR:2}"
  4625.  
  4626.             # create the directory under the sub-package directory
  4627.             mkdir -p "$PARENT_DIR/$PKGNAME/$SUB_DIR"
  4628.  
  4629.             # move the file to the sub-package
  4630.             mv "$i" "$PARENT_DIR/$PKGNAME/$SUB_DIR"
  4631.  
  4632.             # get rid of empty directories in the EXE package
  4633.             rmdir "$SUB_DIR" &>/dev/null
  4634.             ;;
  4635.     esac
  4636.  
  4637.   done
  4638.  
  4639.   # if the EXE package is empty, remove it
  4640.   is_empty="$(find "$PKG_PATH" -maxdepth 2 -type f 2>/dev/null)"
  4641.   [ -z "$is_empty" ] && rm -rf "$PKG_PATH"
  4642.  
  4643.   # go back to where we started (where the main package lives)
  4644.   [ -d "$CURDIR" ] && cd "$CURDIR" &>/dev/null
  4645.  
  4646.   # build each package from the package dir
  4647.   for dir in "$EXE_PKG" "$DEV_PKG" "$DOC_PKG" "$NLS_PKG"
  4648.   do
  4649.     if [ -d "$dir" ]; then
  4650.       dir2${PKGEXT//./} "$dir" && rm -rf "$dir"
  4651.     fi
  4652.   done
  4653.  
  4654.   # get a list of the new packages we created
  4655.   new_pkgs="$(find . -maxdepth 1 -type f -name "${PKGNAME}*.$PKGEXT"
  4656.  find . -maxdepth 1 -type f -name "${DEV_PKG}*.$PKGEXT"
  4657.  find . -maxdepth 1 -type f -name "${DOC_PKG}*.$PKGEXT"
  4658.  find . -maxdepth 1 -type f -name "${NLS_PKG}*.$PKGEXT")"
  4659.  
  4660.   if [ "$new_pkgs" != "" ]; then
  4661.     echo
  4662.     echo -e "${green}Success${endcolour}: Package split into these new files:"
  4663.     echo "$new_pkgs" | sed 's/^.\// /'
  4664.   fi
  4665.  
  4666.   # cleanup
  4667.   [ -d "$DEV_PKG" ] && rm -rf "$DEV_PKG"
  4668.   [ -d "$DOC_PKG" ] && rm -rf "$DOC_PKG"
  4669.   [ -d "$NLS_PKG" ] && rm -rf "$NLS_PKG"
  4670.  
  4671.   return 0
  4672. }
  4673.  
  4674.  
  4675.  
  4676. # main pkg funcs
  4677.  
  4678. pkg_download(){                   # download a pkg ($1) to WORKDIR FUNCLIST
  4679.  
  4680.   # exit if no valid options
  4681.   [ ! "$1" ] || [ "$1" = "-" ] && print_usage download && exit 1
  4682.  
  4683.   . "${PKGRC}"
  4684.  
  4685.   local pkg_ext=''        # extension of pkg, empty if not a supported pkg ext
  4686.   local PKG_FNAME_GUESS  
  4687.   local PKGNAME=''        # the pkgname with version
  4688.   local PKG_FILENAME=''   # package file name (field 8 of repo, with extension)
  4689.   local PKGFILE=''        # full path to package file
  4690.   #Create a new global variable rather than using PKGFILE, so that we minimize the amount of places that global variables are used.
  4691.   pkg_download_rtn='' #See also: https://gitlab.com/sc0ttj/Pkg/merge_requests/13/diffs?commit_id=140125efbe963755de53da77a740db5b01dc567b
  4692.  
  4693.   local NET=''            # 1 or 0 if net connection available
  4694.   local curr_repo=''      # name of current repo in the loop
  4695.   local curr_repo_ext=''  # pkg ext for the current repo in the loop
  4696.   local curr_repo_url=''  # mirror that is used to download the pkg
  4697.   local pkg_in_repo=''    # true if pkg found in ANY repo, else false
  4698.  
  4699.   # the file with our repo URL info
  4700.   sources_file="${HOME}/.pkg/sources"
  4701.  
  4702.   # set download options
  4703.   [ "$FORCE" = true  ] && CONTINUE='' || CONTINUE='-c'
  4704.   [ "$ASK"  != true  ] && QTAG=''
  4705.  
  4706.   # get pkg extension
  4707.   pkg_ext=$(get_pkg_ext "$1")
  4708.  
  4709.   # get pkg name with version (if given!), no extension or path
  4710.   PKGNAME="$(basename "$1" .$pkg_ext)"
  4711.   # get full package name from given pkg name string .. vlc -> vlc-1.2.3-blah
  4712.   PKGNAME=$(get_pkg_name "$PKGNAME")
  4713.  
  4714.   # the file to save to.. not reliable till we checked the repos
  4715.   PKGFILE="${WORKDIR}/${PKGNAME}.$pkg_ext"
  4716.    [ "$PKG_DEBUG" = "true" ] && echo "pkg_download(): get_pkg_filename \"$PKGNAME\"" >&2    
  4717.   [ ! -f "$PKGFILE" ] && PKGFILE="${WORKDIR}/$(get_pkg_filename "$PKGNAME")"
  4718.  [ "$PKG_DEBUG" = "true" ] && echo "pkg_download(): exited: get_pkg_filename \"$PKGNAME\"" >&2    
  4719.  
  4720.   #PKG_FNAME_GUESS="$PKGFILE"
  4721.  
  4722.   PKG_FILENAME="$(basename $PKGFILE)"
  4723.  
  4724.   if [ -z "${PKG_FILENAME}" ];then
  4725.     error "Cant find the package file name needed to download"
  4726.     return 1
  4727.   fi
  4728.  
  4729.   # skip if downloaded already, print msg
  4730.   [ -f "$PKGFILE" ] && [ "$FORCE" = false ] && DONE=true && echo -e "Already downloaded ${magenta}${PKG_FILENAME}${endcolour}" && return 0
  4731.  
  4732.   #if pkg not yet downloaded, or we are downloading all, or we are forcing downloads
  4733.   if [ ! -f "$PKGFILE" ] || [ "$NO_INSTALL" = true ] || [ "$FORCE" = true ];then
  4734.  
  4735.     # mark this pkg as not yet downloaded
  4736.     DONE=false
  4737.  [ "$PKG_DEBUG" = "true" ] && set +x
  4738.  [ "$PKG_DEBUG" = "true" ] && echo "pkg_download(): get_pkg_name_only "$PKGNAME"" >&2    
  4739.     PKGNAME_ONLY=$(get_pkg_name_only "$PKGNAME")
  4740.  
  4741.     # exit if no internet connection
  4742.     #NET="`check_net`"; [ $NET -eq 1 ] && echo "You need an internet connection" && exit 2
  4743.  
  4744.     # for each repo, starting with current repo
  4745.     for repo_file in `repo_file_list` #TODO: make sure this is safe if spaces are in the path.
  4746.     do
  4747.  
  4748.       # get name of current repo in loop
  4749.       prev_repo="$curr_repo"
  4750.       curr_repo_info="$(LANG=C grep -m1 "|$repo_file|" "$sources_file" 2>/dev/null | cut -f1,2 -d'|')"
  4751.       curr_repo="${curr_repo_info/|*/}"
  4752.       curr_repo_ext="${curr_repo_info/*|/}"
  4753.  
  4754.       # check if $repo_file contains the given pkg
  4755.       pkg_in_this_repo="$(LANG=C cut -f1,2,7,8 -d'|' $REPO_DB_FILE_DIR/$repo_file 2>/dev/null | grep -m1 "^${PKGNAME}|")"
  4756.       [ "$pkg_in_this_repo" = '' ] && pkg_in_this_repo="$(LANG=C cut -f1,2,7,8 -d'|' $REPO_DB_FILE_DIR/$repo_file 2>/dev/null | grep -m1 "|${PKG_FILENAME}\$")"
  4757.       [ "$pkg_in_this_repo" = '' ] && pkg_in_this_repo="$(LANG=C cut -f1,2,7,8 -d'|' $REPO_DB_FILE_DIR/$repo_file 2>/dev/null | grep -m1 "|${PKGNAME_ONLY}|")"
  4758.  
  4759.       # skip this repo if no exact match of package file found
  4760.       if [ -z "$pkg_in_this_repo" ];then
  4761.         continue
  4762.       fi
  4763.  
  4764.       # skip pings and download if already downloaded.. unless forcing download
  4765.       if [ -f "$PKG_FILENAME" ]; then
  4766.          if check_SHA256 "${WORKDIR}/${PKG_FILENAME}" "$REPO_DB_FILE_DIR/$repo_file"; then
  4767.             if [ "$FORCE" != true ]; then
  4768.               DONE=true
  4769.               echo -e "Already downloaded ${magenta}${PKG_FILENAME}${endcolour}"
  4770.               break
  4771.             fi
  4772.             pkg_download_rtn="$PKG_FILENAME"
  4773.             continue
  4774.           else
  4775.             #error "Failed md5sum check '${WORKDIR}/${PKG_FILENAME}'."
  4776.             echo "Warning: failed checksum of old download '${WORKDIR}/${PKG_FILENAME}'."
  4777.             echo "removing: ${WORKDIR}/${PKG_FILENAME}"
  4778.  
  4779.             # remove the download
  4780.             rm "${WORKDIR}/${PKG_FILENAME}" &>/dev/null
  4781.  
  4782.           fi      
  4783.       fi  
  4784.       # ask user to download
  4785.       echo -en "Download ${magenta}${PKGNAME_ONLY}${endcolour} from ${lightblue}${curr_repo}${endcolour} repo$QTAG:  "
  4786.       [ "$ASK" = true ] && read -n 1 CONFIRM </dev/tty || CONFIRM=y
  4787.       [ "$CONFIRM" != 'y' ] && echo
  4788.  
  4789.       # if user answered yes, we will now download the pkgs
  4790.       if [ "$CONFIRM" != "y" ];then #25073
  4791.         DONE=true
  4792.         break
  4793.       fi
  4794.  
  4795.       # only ask once
  4796.       ASK=false
  4797.  
  4798.       echo # start a new line
  4799.  
  4800.       # get the subdir (from repo line) that the package lives in
  4801.       sub_dir="$(echo "$pkg_in_this_repo" | cut -f3 -d'|' | sed 's/^\.//')"
  4802.  
  4803.       if [ "$sub_dir" = "." ];then
  4804.         sub_dir=''
  4805.       fi
  4806.  
  4807.       # make a space separated list of all our repo URLs ..
  4808.       # we also add a final /, fixing any double slashes
  4809.       curr_repo_url_list="$(LANG=C grep -m1 "|$repo_file|" $sources_file 2>/dev/null \
  4810.        | cut -f4,5,6,7 -d'|' \
  4811.        | tr '|' '\n'         \
  4812.        | sed                 \
  4813.          -e 's#\$#/#g'       \
  4814.          -e 's#//\$#/#g'     \
  4815.        | tr '\n' ' '         )"
  4816.  
  4817.       # update the ext to that of current repo
  4818.       pkg_ext="$curr_repo_ext"
  4819.  
  4820.       # get the best repo mirror
  4821.       #if [ "$prev_repo" != "$current_repo"  ] && [ -f $TMPDIR/curr_repo_url ] || [ ! -f $TMPDIR/curr_repo_url ];then
  4822.         #echo "Checking '$curr_repo' repo mirrors..."
  4823.         for URL in $curr_repo_url_list
  4824.         do
  4825.           [ -z "$URL" ] && continue
  4826.           [ "$URL" = '/' ] && continue
  4827.           [ "$URL" = '' ] && continue
  4828.           # add some system values into the repo URL (arch, distro version, etc.. from DISTRO_SPECS)
  4829.           URL="$(echo "$URL"| sed -e 's@${DBIN_ARCH}@'$DBIN_ARCH'@g')"
  4830.           URL="$(echo "$URL"| sed -e 's@${DISTRO_COMPAT_VERSION}@'$DISTRO_COMPAT_VERSION'@g')"
  4831.           URL="$(echo "$URL"| sed -e 's@${DDB_COMP}@'$DDB_COMP'@g')"
  4832.           URL="$(echo "$URL"| sed -e 's@${SLACKWARE}@'$SLACKWARE'@g')"
  4833.           URL="$(echo "$URL"| sed -e 's@${SLACKARCH}@'$SLACKARCH'@g')"
  4834.           URL="$(echo "$URL"| sed -e 's@${lxDISTRO_COMPAT_VERSION}@'$lxDISTRO_COMPAT_VERSION'@g')"
  4835.           #ping -W2 -c1 -q $(echo  "${URL}" | awk -F/ '{print $3}') &>/dev/null
  4836.           wget --quiet --timeout=2 --no-parent --spider "${URL}" &>/dev/null && \
  4837.           {
  4838.             # set the current URL
  4839.             curr_repo_url="$URL"
  4840.             if [ ! -z "$curr_repo_url" ];then
  4841.               echo "$curr_repo_url" > $TMPDIR/curr_repo_url
  4842.              
  4843.               #break
  4844.             fi
  4845.           }
  4846.           if [ ! -z "$curr_repo_url" ] || [ "$PKG_NO_SPIDER" = true ]; then
  4847.             # lets build our DOWNLOAD_URL: set it to the working repo mirror we found above
  4848.             DOWNLOAD_URL="${curr_repo_url}"
  4849.      
  4850.             # now add the subdir to DOWNLOAD_URL .. sub_dir may be empty, but we add a trailing '/' anyway
  4851.             DOWNLOAD_URL="${DOWNLOAD_URL}${sub_dir}/"
  4852.      
  4853.             # add some system values into the repo URL (arch, distro version, etc.. from DISTRO_SPECS)
  4854.             DOWNLOAD_URL="$(echo "$DOWNLOAD_URL"| sed -e 's@${DBIN_ARCH}@'$DBIN_ARCH'@g')"
  4855.             DOWNLOAD_URL="$(echo "$DOWNLOAD_URL"| sed -e 's@${DISTRO_COMPAT_VERSION}@'$DISTRO_COMPAT_VERSION'@g')"
  4856.             DOWNLOAD_URL="$(echo "$DOWNLOAD_URL"| sed -e 's@${DDB_COMP}@'$DDB_COMP'@g')"
  4857.             DOWNLOAD_URL="$(echo "$DOWNLOAD_URL"| sed -e 's@${SLACKWARE}@'$SLACKWARE'@g')"
  4858.             DOWNLOAD_URL="$(echo "$DOWNLOAD_URL"| sed -e 's@${SLACKARCH}@'$SLACKARCH'@g')"
  4859.             DOWNLOAD_URL="$(echo "$DOWNLOAD_URL"| sed -e 's@${lxDISTRO_COMPAT_VERSION}@'$lxDISTRO_COMPAT_VERSION'@g')"
  4860.      
  4861.             # add our package to the URL
  4862.             DOWNLOAD_URL="${DOWNLOAD_URL}${PKG_FILENAME}"
  4863.      
  4864.             # remove any double forward slashes (the main URL or subdir may have ended in slashes, it may not, we added our own to be sure)
  4865.             DOWNLOAD_URL="$(echo "$DOWNLOAD_URL"| sed -e "s@//$PKG_FILENAME@/$PKG_FILENAME@g")"
  4866.      
  4867.             # if sub_dir not empty, lets clean that bit too
  4868.             [ "$sub_dir" != '' ] && DOWNLOAD_URL="$(echo "$DOWNLOAD_URL" \
  4869.              | sed \
  4870.                -e "s@//${sub_dir}@/${sub_dir}@g" \
  4871.                -e "s@${sub_dir}//@${sub_dir}/@g")"
  4872.      
  4873.             # exit if URL is not found (if we get a 404 back)
  4874.             if [ -z "$DOWNLOAD_URL" ]; then
  4875.               error "Package URL not found   $DOWNLOAD_URL"
  4876.               return 8
  4877.             else
  4878.               wget -S --spider "$DOWNLOAD_URL" &>/dev/null || \
  4879.               {
  4880.                 error "Package URL not found   $DOWNLOAD_URL"
  4881.                 return 8
  4882.               }
  4883.             fi
  4884.      
  4885.             # we may be using multiple URLs, so each time we change URL,
  4886.             #  remember the new one
  4887.             if [ "$OLDURL" != "$DOWNLOAD_URL" ];then
  4888.               OLDURL="$DOWNLOAD_URL";
  4889.               echo -e "URL: ${lightblue}${DOWNLOAD_URL}${endcolour}";
  4890.             fi
  4891.      
  4892.             # if --force, remove the package if it already exists
  4893.             [ "$FORCE" = true ] && rm -f "${PKGFILE}" &>/dev/null
  4894.      
  4895.             # BEGIN DOWNLOAD file here..
  4896.      
  4897.             # print DOWNLOADING msg
  4898.             echo -en "Downloading ${magenta}${PKG_FILENAME}${endcolour}. Please wait:     "
  4899.      
  4900.             # if called as 'gpkg', give a pop GUI (uses Xdialog) showing download progress..
  4901.             # can be used by X apps to easily start (and show!) downloads
  4902.             if [ "$SELF" = "gpkg" ];then
  4903.      
  4904.               download_progress "$DOWNLOAD_URL" "${PKGFILE}" 2>/dev/null
  4905.      
  4906.             else # if called as 'pkg', dont use Xdialog, output to terminal
  4907.      
  4908.               if [ "$QUIET" = true ];then
  4909.                 echo
  4910.                 echo -n "Downloading now..."
  4911.                 LANG=C wget \
  4912.                   --no-check-certificate \
  4913.                   --progress=dot -O "${PKGFILE}" \
  4914.                   -4 $CONTINUE "$DOWNLOAD_URL" &>/dev/null
  4915.               else
  4916.                 # START DOWNLOAD, show percentage as we go
  4917.                 LANG=C wget \
  4918.                   --no-check-certificate \
  4919.                   --progress=dot -O "${PKGFILE}" \
  4920.                   -4 $CONTINUE "$DOWNLOAD_URL" 2>&1 \
  4921.                   | grep --line-buffered "%" \
  4922.                   | sed -u -e "s#\.##g" \
  4923.                   | awk '{printf("\b\b\b\b%4s", $2)}' #220613
  4924.               fi
  4925.                     # if file downloaded ok
  4926.                 if [ -f "${PKGFILE}" ];then
  4927.                   echo -e "${green}Downloaded:${endcolour} ${PKGFILE}"
  4928.                   if check_SHA256 "${WORKDIR}/${PKG_FILENAME}" "$REPO_DB_FILE_DIR/$repo_file"; then
  4929.                     pkg_download_rtn="$PKG_FILENAME"
  4930.                     DONE=true
  4931.                     break 2            
  4932.                   else              
  4933.                     echo "Failed md5sum check '${WORKDIR}/${PKG_FILENAME}'."
  4934.  
  4935.                     # remove the download
  4936.                     rm "${WORKDIR}/${PKG_FILENAME}" &>/dev/null                      
  4937.                     DONE=true
  4938.                     break 2
  4939.                   fi        
  4940.                 else # file NOT downloaded ok
  4941.          
  4942.                   error "Failed to download '${PKGFILE}'."
  4943.                   echo "Check '$DOWNLOAD_URL'"
  4944.    
  4945.                   # remove the page we tried to get pkg from (if exists)
  4946.                   rm "${WORKDIR}/index.html" &>/dev/null
  4947.                 fi
  4948.             fi        
  4949.           fi
  4950.         done
  4951.       #else
  4952.       # curr_repo_url="`cat $TMPDIR/curr_repo_url`"
  4953.       #fi
  4954.  
  4955.       # exit if URL is not found or empty
  4956.       if [ -z "$curr_repo_url" ]; then
  4957.         error "Package URL not found"
  4958.         return 8
  4959.       fi
  4960.  
  4961.  
  4962.  
  4963.       # clean up output
  4964.       [ "$QUIET" != true ] && echo -ne "\b\b\b\b"
  4965.       echo
  4966.  
  4967.       # ln -s "${PKG_FILENAME}" "$PKG_FNAME_GUESS" 2>/dev/null #TODO use relative symbolic links + symbolic link cleanup.
  4968.  
  4969.  
  4970.  
  4971.     done #done while read list of repo files
  4972.     if [ -f "${PKGFILE}" ];then
  4973.       echo -e "${green}Downloaded:${endcolour} ${PKGFILE}"
  4974.       DONE=true
  4975.  
  4976.     else # file NOT downloaded ok
  4977.  
  4978.       error "Failed to download '${PKGFILE}'."
  4979.       echo "Check '$DOWNLOAD_URL'"
  4980.  
  4981.       # remove the page we tried to get pkg from (if exists)
  4982.       rm "${WORKDIR}/index.html" &>/dev/null
  4983.       DONE=true
  4984.       exit 6
  4985.     fi
  4986.   fi
  4987. }
  4988.  
  4989.  
  4990. pkg_install(){                    # install downloaded package ($1) FUNCLIST
  4991.  
  4992.   . ${PKGRC}
  4993.  
  4994.   # exit if no valid option
  4995.   [ ! "$1" -o "$1" = "-" ] && print_usage install && exit 1
  4996.  
  4997.   # skip if not installing pkgs
  4998.   [ "$NO_INSTALL" = true ] && continue
  4999.  
  5000.   local pkg_ext
  5001.   local PKGNAME
  5002.   local PKGNAME_ONLY
  5003.   local PKGFILE
  5004.   local PREVDIR="$CURDIR"
  5005.   local petspecs
  5006.  
  5007.   # get pkg extension
  5008.   pkg_ext=`get_pkg_ext "$1"`
  5009.   # get pkg name only, no extension or path
  5010.   PKGNAME="$(basename "$1" .$pkg_ext)"
  5011.   # exit if no valid option
  5012.   [ "$PKGNAME" = '' ] && print_usage install && exit 1
  5013.   # get name without version
  5014.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  5015.  
  5016.   [ "`is_installed_pkg "$PKGNAME"`" = true -a "$FORCE" = false ] && echo "Already installed: $PKGNAME" && return 1
  5017.   [ "`is_blacklisted_pkg "$PKGNAME_ONLY"`" = true ] && echo "Blacklisted package: $PKGNAME" && return 1
  5018.  
  5019.   if [ -f "$1" ]; then
  5020.     PKGFILE="$1"
  5021.     CURDIR="$(dirname "$(realpath "$1")")"
  5022.    PKGNAME=`basename "$1" .$pkg_ext`
  5023.  else
  5024.    # get the real file name (inc path) from the given PKGFILE and pkg_ext (which may be empty)
  5025.    PKGFILE=`find "$CURDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*${pkg_ext}" | head -1`
  5026.    [ ! -f "$PKGFILE" ] && PKGFILE=`find "$CURDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*${EX}" | grep -m1 $EX`
  5027.    [ -f "$PKGFILE" ] && PKGNAME=`basename "$1" .$pkg_ext`
  5028.  fi
  5029.  
  5030.  # maybe the file is not in the current dir, but in WORKDIR, so lets look there too
  5031.  if [ ! -f "$PKGFILE" ]; then
  5032.    PKGFILE=`find "$WORKDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*${pkg_ext}" | head -1`
  5033.    [ ! -f "$PKGFILE" ] && PKGFILE=`find "$WORKDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*${EX}" | grep -m1 $EX`
  5034.  
  5035.    # if we found the file in WORKDIR, make that our CURDIR
  5036.    if [ -f "$PKGFILE" ]; then
  5037.      PKGNAME=`basename "$PKGFILE" .$pkg_ext`
  5038.      CURDIR="$WORKDIR"
  5039.      cd "$WORKDIR"
  5040.    else
  5041.      # if we still didn't find it, do a broader check
  5042.      PKGFILE=`find "$WORKDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*" | head -1`
  5043.      if [ -f "$PKGFILE" ]; then
  5044.        PKGNAME=`basename "$PKGFILE" .$pkg_ext`
  5045.        CURDIR="$WORKDIR"
  5046.        cd "$WORKDIR"
  5047.      fi
  5048.    fi
  5049.  fi
  5050.  
  5051.  # if the file exists, or using --force
  5052.  if [ -f "$PKGFILE" -o "$FORCE" = true ]; then
  5053.  
  5054.    [ ! -f "$PKGFILE" ] && echo "The file $1 was not found." && return 1
  5055.  
  5056.    # get the extension of this newly found pkg (which we should have found by now)
  5057.    pkg_ext=`get_pkg_ext "$PKGFILE"`
  5058.    # get extension again, we may have been given only a pkg name, find its extension from repo files
  5059.    [ "$pkg_ext" = "" ] && pkg_ext=`get_pkg_ext "$PKGNAME"`
  5060.  
  5061.    [ "$pkg_ext" = '' ] && echo "Not installing $PKGFILE." && error "Invalid file extension ($pkg_ext)." && return 1
  5062.  
  5063.    # remove any previous PET/pkg stuff lying around from previous installs
  5064.    rm -f /pet.specs /pinstall.sh /puninstall.sh /install/doinst.sh
  5065.  
  5066.    # if pkg is not yet installed, or we are force re-installing
  5067.  
  5068.    # get filename from download file
  5069.    PKGNAME=`basename "$PKGFILE" .$pkg_ext`
  5070.  
  5071.    # ask/inform user before install
  5072.    echo -n "Install package ${PKGNAME}$QTAG:  "
  5073.    [ "$ASK" = true ] && read -n 1 CONFIRM </dev/tty || CONFIRM=y
  5074.    [ "$ASK" = true ] && echo -ne "\b\b\n"
  5075.  
  5076.    # if user answered yes, we will now download the pkgs
  5077.    if [ "$CONFIRM" = "y" ]; then
  5078.  
  5079.      # print new line if we didnt take any user input on tty
  5080.      [ "$ASK" != true ] && echo
  5081.  
  5082.      # only ask once
  5083.      ASK=false
  5084.  
  5085.      #if [ "`pkg_contents "$PKGFILE" 2>/dev/null`" = '' ]; then
  5086.      # error "$PKGFILE not a valid package."
  5087.      # exit 1
  5088.      #fi
  5089.  
  5090.      # fallback to repo pkg ext if none found
  5091.      [ "$pkg_ext" = "" ] && pkg_ext="$EX"
  5092.  
  5093.      #remove the old error log
  5094.      rm $TMPDIR/$SELF-cp-errlog 2>/dev/null
  5095.  
  5096.      # extract the archive file into a $CURDIR/$PKGNAME folder
  5097.      case "$pkg_ext" in
  5098.  
  5099.        pet)
  5100.          PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.pet"`"
  5101.          #determine the compression, extend test to 'XZ'
  5102.          file -b "${PKGFILE}" | grep -i -q "^xz" && TAREXT=xz || TAREXT=gz
  5103.          [ "$TAREXT" = 'xz' ] && taropts='-xJf' || taropts='-zxf'
  5104.  
  5105.          # convert to tar extractable archive
  5106.          pet2tgz "${PKGFILE}" 1>/dev/null
  5107.  
  5108.          # now extract the file
  5109.          tar $taropts "${CURDIR}/${PKGNAME}.tar.${TAREXT}" 2> $TMPDIR/$SELF-cp-errlog
  5110.  
  5111.          # some old types need this extra step
  5112.          if [ -f "${CURDIR}/${PKGNAME}.tar.$TAREXT" ]; then
  5113.            cd "${CURDIR}/${PKGNAME}/" 1>/dev/null
  5114.            tar --absolute-names $tarops "./${PKGNAME}.tar.$TAREXT" ${DIRECTSAVEPATH}/ 2> $TMPDIR/$SELF-cp-errlog #260713
  5115.            sync
  5116.            rm -f  "./${PKGNAME}.tar.$TAREXT" 1>/dev/null
  5117.            cd - 1>/dev/null
  5118.          fi
  5119.  
  5120.          # save the uninstall script for later
  5121.          if [ -f "${CURDIR}/${PKGNAME}/puninstall.sh" ]; then
  5122.            mv "${CURDIR}/${PKGNAME}/puninstall.sh" "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.remove"
  5123.          fi
  5124.  
  5125.          # create a tgz
  5126.          tgz2pet "${CURDIR}/${PKGNAME}.tar.$TAREXT" 1>/dev/null
  5127.          rm "${CURDIR}/${PKGNAME}.tar.$TAREXT" 2>/dev/null
  5128.        ;;
  5129.  
  5130.        sfs)
  5131.          PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.sfs"`"
  5132.          [ ! -f "$PKGFILE" ] && PKGFILE="`find ${WORKDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.sfs"`"
  5133.          echo sfs_loadr -q --cli +"$PKGFILE" #2>/dev/null
  5134.          sfs_loadr -q --cli +"$PKGFILE" #2>/dev/null
  5135.          rm -f $PACKAGE_FILE_LIST_DIR/${PKGNAME}.files 2>/dev/null
  5136.          # create $REPO_DIR/$PKGNAME.files
  5137.          pkg_contents "$PKGFILE" | while read line
  5138.          do
  5139.            [ -f "$line" ] && echo "$line" >> $PACKAGE_FILE_LIST_DIR/${PKGNAME}.files
  5140.          done
  5141.          # exit, we dont need to unpack and copy
  5142.          return 0
  5143.        ;;
  5144.  
  5145.        deb)
  5146.  
  5147.          rmdir "${CURDIR}/${PKGNAME}" &>/dev/null
  5148.          mkdir -p "${CURDIR}/${PKGNAME}"
  5149.          [ ! -f "$PKGFILE" ] && PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.deb"`"
  5150.          cp "${PKGFILE}" "${CURDIR}/${PKGNAME}/${PKGNAME}.deb" || exit 4
  5151.          cd "${CURDIR}/${PKGNAME}/"
  5152.  
  5153.          if [ -f "${CURDIR}/${PKGNAME}/${PKGNAME}.deb" ]; then
  5154.            dpkg-deb --contents "${CURDIR}/${PKGNAME}/${PKGNAME}.deb" \
  5155.              | grep -v '/$' \
  5156.              | tr -s ' ' \
  5157.              | cut -f6 -d' ' \
  5158.              | sed -e 's/^.//g' 2>/dev/null \
  5159.              | grep -v '^$' > "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  5160.          fi
  5161.  
  5162.                     # woofce: NO_MULTIARCH_SYMLINK=1 (DISTRO_SPECS)
  5163.                     if [ -z "$NO_MULTIARCH_SYMLINK" ] ; then
  5164.            if [ -f "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" ]; then
  5165.              pkg_has_archdir="$(grep -m1 "$DISTRO_ARCHDIR" "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files")"
  5166.            fi
  5167.  
  5168.                         # Workaround to avoid overwriting the $DISTRO_ARCHDIR symlink.
  5169.                         if [ "$DISTRO_ARCHDIR" != "" -a -f "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" -a "$pkg_has_archdir" != "" ]; then
  5170.                           mkdir -p /tmp/$PKGNAME
  5171.                           rm -rf /tmp/$PKGNAME/*
  5172.                           dpkg-deb -x ${CURDIR}/${PKGNAME}.deb /tmp/$PKGNAME/ 2> $TMPDIR/$SELF-cp-errlog
  5173.                           #set -x
  5174.                           while read f
  5175.                           do
  5176.                               xpath="$(echo "$f" |  cut  -f 4-30 -d "/" | sed "s/$DISTRO_ARCHDIR\///")"
  5177.                               mkdir -p ${DIRECTSAVEPATH}/$(dirname "$xpath")
  5178.                               cp -a "$f" ${DIRECTSAVEPATH}/$(dirname "$xpath")/
  5179.                           done < <(find "/tmp/$PKGNAME" \( -type f -o -type l \))
  5180.                           #set +x
  5181.                           rm -rf /tmp/$PKGNAME &>/dev/null
  5182.                         else
  5183.                           dpkg-deb -x ${CURDIR}/${PKGNAME}.deb ${DIRECTSAVEPATH}/ 2> $TMPDIR/$SELF-cp-errlog
  5184.                         fi
  5185.                     else
  5186.                         dpkg-deb -x ${CURDIR}/${PKGNAME}.deb ${DIRECTSAVEPATH}/ 2> $TMPDIR/$SELF-cp-errlog
  5187.                     fi
  5188.  
  5189.          if [ "`cat $TMPDIR/$SELF-cp-errlog | grep 'tar: Exiting with failure status due to previous errors'`" != "" ]; then
  5190.            error "Failed to unpack $PKGNAME.deb"
  5191.            exit 1
  5192.          fi
  5193.          [ -d /DEBIAN ] && rm -rf /DEBIAN #130112 precaution.
  5194.          dpkg-deb -e "${CURDIR}/${PKGNAME}.deb" /DEBIAN #130112 extracts deb control files to dir /DEBIAN. may have a post-install script, see below.
  5195.          rm "${PKGNAME}.deb"
  5196.  
  5197.          cd - 1>/dev/null
  5198.        ;;
  5199.  
  5200.        *tbz|*tar.bz2)
  5201.          rmdir "${CURDIR}/${PKGNAME}" &>/dev/null
  5202.          mkdir -p "${CURDIR}/${PKGNAME}"
  5203.          cp "${CURDIR}/${PKGNAME}.$pkg_ext" "${CURDIR}/${PKGNAME}/${PKGNAME}.$pkg_ext" || exit 4
  5204.          cd "${CURDIR}/${PKGNAME}/"
  5205.  
  5206.          ( umask 000 ; cat "${PKGNAME}.$pkg_ext" | bzip2 -dc | tar -xf - 2> $TMPDIR/$SELF-cp-errlog )
  5207.  
  5208.          rm "${PKGNAME}.$pkg_ext"
  5209.          cd - 1>/dev/null
  5210.        ;;
  5211.  
  5212.        *tlz|*tar.lzma)
  5213.          rmdir "${CURDIR}/${PKGNAME}" &>/dev/null
  5214.          mkdir -p "${CURDIR}/${PKGNAME}"
  5215.          PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.${pkg_ext}"`"
  5216.          cp "$PKGFILE" "${CURDIR}/${PKGNAME}/`basename $PKGFILE`" || exit 4
  5217.          cd "${CURDIR}/${PKGNAME}/"
  5218.  
  5219.          ( umask 000 ; cat "${PKGNAME}.$pkg_ext" | lzma -dc | tar -xf - 2> $TMPDIR/$SELF-cp-errlog )
  5220.  
  5221.          rm "${PKGNAME}.$pkg_ext"
  5222.          cd - 1>/dev/null
  5223.        ;;
  5224.  
  5225.        *tgz|*txz|*tar.gz|*tar.xz|*xz|*gz)
  5226.          rmdir "${CURDIR}/${PKGNAME}" &>/dev/null
  5227.          mkdir -p "${CURDIR}/${PKGNAME}"
  5228.          PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.${pkg_ext}"`"
  5229.          cp "$PKGFILE" "${CURDIR}/${PKGNAME}/`basename $PKGFILE`" || exit 4
  5230.          cd "${CURDIR}/${PKGNAME}/"
  5231.  
  5232.          file -b "${PKGNAME}.$pkg_ext" | grep -i -q "^xz" && TAREXT=xz || TAREXT=gz
  5233.          [ "$TAREXT" = 'xz' ] && taropts='-xJf' || taropts='-zxf'
  5234.  
  5235.          tar $taropts "${PKGNAME}.$pkg_ext" 2> $TMPDIR/$SELF-cp-errlog
  5236.          rm "${PKGNAME}.$pkg_ext"
  5237.          cd - 1>/dev/null
  5238.        ;;
  5239.  
  5240.        rpm)
  5241.          PKGNAME="`basename ${PKGFILE} .rpm`"
  5242.          busybox rpm -qp "$PKGFILE" > /dev/null 2>&1
  5243.          [ $? -ne 0 ] && exit 1
  5244.          PFILES="`busybox rpm -qpl $PKGFILE`"
  5245.          [ $? -ne 0 ] && exit 1
  5246.          echo "$PFILES" > "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  5247.          pkg_unpack "$PKGFILE" 1>/dev/null
  5248.        ;;
  5249.  
  5250.      esac
  5251.  
  5252.      # now extract pkg contents to /
  5253.      [ ! -d "${PKGNAME}/" ] && error "Cannot enter directory '${PKGNAME}/', it doesn't exist." && exit 4
  5254.      cd "${PKGNAME}/" 1>/dev/null
  5255.  
  5256.      cp -a --remove-destination * ${DIRECTSAVEPATH}/ 2> $TMPDIR/$SELF-cp-errlog
  5257.  
  5258.      #source is a directory, target is a symlink...
  5259.      if [ -s $TMPDIR/$SELF-cp-errlog ]; then
  5260.        cat $TMPDIR/$SELF-cp-errlog  2>/dev/null | grep -E 'Cannot create symlink to|cannot overwrite non-directory' | cut -f 1 -d "'" | cut -f 2 -d '`' |
  5261.        while read ONEDIRSYMLINK
  5262.        do
  5263.          #adding that extra trailing / does the trick...
  5264.          cp -a --remove-destination ${ONEDIRSYMLINK}/* /${ONEDIRSYMLINK}/ 2> $TMPDIR/$SELF-cp-errlog #260713
  5265.        done
  5266.      fi
  5267.      sync
  5268.      cd .. 1>/dev/null
  5269.  
  5270.      if [ ! -f "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" ] || \
  5271.         [ -z "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" ]; then
  5272.        # add ${HOME}/.packages/${PKGNAME}.files, need to find regular files and links separately... then images..
  5273.        find "./${PKGNAME}/" -mount -mindepth 2 -type f | sed -e "s/\.\/${PKGNAME}//g" -e 's/\/root0\//\/root\//g' > "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  5274.        find "./${PKGNAME}/" -mount -mindepth 2 -type l | sed -e "s/\.\/${PKGNAME}//g" -e 's/\/root0\//\/root\//g' >> "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  5275.  
  5276.        for ONEIMAGEFILE in `ls -1 /*[0-9].xpm 2>/dev/null`
  5277.        do
  5278.         BASEONEIMAGE="`basename $ONEIMAGEFILE`"
  5279.         echo "/usr/local/lib/X11/pixmaps/$BASEONEIMAGE" >> "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  5280.        done
  5281.        for ONEIMAGEFILE in `ls -1 /*[0-9].png 2>/dev/null`
  5282.        do
  5283.         BASEONEIMAGE="`basename $ONEIMAGEFILE`"
  5284.         echo "/usr/local/lib/X11/pixmaps/$BASEONEIMAGE" >> "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  5285.        done
  5286.        for ONEIMAGEFILE in `ls -1 /*[^0-9].xpm 2>/dev/null`
  5287.        do
  5288.         BASEONEIMAGE="`basename $ONEIMAGEFILE`"
  5289.         echo "/usr/local/lib/X11/mini-icons/$BASEONEIMAGE" >> "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  5290.        done
  5291.        for ONEIMAGEFILE in `ls -1 /*[^0-9].png 2>/dev/null` #v2.16
  5292.        do
  5293.         BASEONEIMAGE="`basename $ONEIMAGEFILE`"
  5294.         echo "/usr/local/lib/X11/mini-icons/$BASEONEIMAGE" >> "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  5295.        done
  5296.      fi
  5297.  
  5298.      # move the *.files to $REPO_DIR
  5299.      FILES=''
  5300.      FILES="`find "$CURDIR" -mount -maxdepth 1 -iname "*.files" | head -1`"
  5301.      [ -f "`echo $FILES`" ] && mv "$FILES" "$PACKAGE_FILE_LIST_DIR/" 2>/dev/null #260713
  5302.  
  5303.      #pkgname.files may need to be fixed...
  5304.      cat "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" | grep -v '/$' | sed -e 's%^\\.%%' -e 's%^%/%' -e 's%^//%/%' 2>/dev/null > "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files2"
  5305.      mv "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files2" "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  5306.  
  5307.      sort -u "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" > "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files2"
  5308.      mv "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files2" "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  5309.  
  5310.      # some pets add images and icons at / so move them
  5311.      mv /{*.xpm,*.png} /usr/share/pixmaps/ 2>/dev/null
  5312.      mv /*.ico /usr/share/pixmaps/ 2>/dev/null
  5313.  
  5314.      # run the post install scripts (for puppy, slackware, arch, debian/ubuntu, etc)
  5315.      for i in /pinstall.sh /install/doinst.sh /DEBIAN/postinst
  5316.      do
  5317.        [ -z /${i} -o ! -e /${i} ] && continue
  5318.        chmod +x /${i}
  5319.        cd /
  5320.        nohup sh ${i} &>/dev/null
  5321.        sleep 0.2
  5322.        rm -f ${i}
  5323.        cd ${CURDIR}/
  5324.      done
  5325.      rm -rf /install
  5326.      rm -rf /DEBIAN
  5327.  
  5328.      #130314 run arch linux pkg post-install script...
  5329.      if [ -f /.INSTALL -a /usr/local/petget/ArchRunDotInstalls ]; then #precaution. see 3builddistro, script created by noryb009.
  5330.        #this code is taken from below...
  5331.        dlPATTERN='|'"`echo -n "${PKGNAME}" | sed -e 's%\\-%\\\\-%'`"'|'
  5332.        archVER="`cat $TMPDIR/petget_missing_dbentries-Packages-* 2>/dev/null | grep "$dlPATTERN" | head -n 1 | cut -f 3 -d '|'`"
  5333.        if [ "$archVER" ]; then #precaution.
  5334.          cd /
  5335.          mv -f .INSTALL .INSTALL1-${archVER}
  5336.          cp -a /usr/local/petget/ArchRunDotInstalls ArchRunDotInstalls
  5337.          LANG=$LANG_USER ./ArchRunDotInstalls
  5338.          rm -f ArchRunDotInstalls
  5339.          rm -f .INSTALL*
  5340.          cd ${CURDIR}/
  5341.        fi
  5342.      fi
  5343.  
  5344.      PKGCAT=''
  5345.      PKGSIZE=''
  5346.      PKGDESC=''
  5347.      DEPLIST=''
  5348.  
  5349.      # clean up pet installs
  5350.      if [ "$pkg_ext" = "pet" ]; then
  5351.        cd /
  5352.  
  5353.        # get pkg info from pet.spec before we delete it, because the PET may not be a repo pkg
  5354.        petspecs="`cat /pet.specs 2>/dev/null`"
  5355.  
  5356.        if [ "$petspecs" != '' ]; then
  5357.          # now get pkg info
  5358.          PKGCAT="`echo $petspecs|  cut -f5  -d'|'`"
  5359.          PKGSIZE="`echo $petspecs| cut -f6  -d'|'`"
  5360.          PKGDESC="`echo $petspecs| cut -f10 -d'|'`"
  5361.          DEPSLIST="`echo $petspecs|cut -f9  -d'|'`"
  5362.        fi
  5363.  
  5364.        cd ${CURDIR}/
  5365.      fi
  5366.  
  5367.      # cleanup a bit
  5368.      rm -R "${PKGNAME}/" 2>/dev/null || echo "${yellow}Warning:${endcolour} Cannot remove directory '${PKGNAME}/'." #150213
  5369.  
  5370.      #120102 install may have overwritten a symlink-to-dir...
  5371.      #tar defaults to not following symlinks, for both dirs and files, but i want to follow symlinks
  5372.      #for dirs but not for files. so, fix here... (note, dir entries in .files have / on end)
  5373.      cat "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" | grep '[a-zA-Z0-9]/$' | sed -e 's%/$%%' | grep -v '^/mnt' |
  5374.      while read ONESPEC
  5375.      do
  5376.       if [ -d "${DIRECTSAVEPATH}${ONESPEC}" ]; then
  5377.        if [ ! -h "${DIRECTSAVEPATH}${ONESPEC}" ]; then
  5378.         DIRLINK=""
  5379.         if [ -h "/initrd${PUP_LAYER}${ONESPEC}" ]; then #120107
  5380.          DIRLINK="`readlink -m "/initrd${PUP_LAYER}${ONESPEC}" | sed -e "s%/initrd${PUP_LAYER}%%"`" #PUP_LAYER: see /etc/rc.d/PUPSTATE. 120107
  5381.          xDIRLINK="`readlink "/initrd${PUP_LAYER}${ONESPEC}"`" #120107
  5382.         fi
  5383.         if [ ! "$DIRLINK" ]; then
  5384.          if [ -h "/initrd${SAVE_LAYER}${ONESPEC}" ]; then #120107
  5385.           DIRLINK="`readlink -m "/initrd${SAVE_LAYER}${ONESPEC}" | sed -e "s%/initrd${SAVE_LAYER}%%"`" #SAVE_LAYER: see /etc/rc.d/PUPSTATE. 120107
  5386.           xDIRLINK="`readlink "/initrd${SAVE_LAYER}${ONESPEC}"`" #120107
  5387.          fi
  5388.         fi
  5389.         if [ "$DIRLINK" ]; then
  5390.          if [ -d "$DIRLINK"  ]; then
  5391.           if [ "$DIRLINK" != "${ONESPEC}" ]; then #precaution.
  5392.            mkdir -p "${DIRECTSAVEPATH}${DIRLINK}" #120107
  5393.            cp -a -f --remove-destination ${DIRECTSAVEPATH}"${ONESPEC}"/* "${DIRECTSAVEPATH}${DIRLINK}/" #ha! fails if put double-quotes around entire expression.
  5394.            rm -rf "${DIRECTSAVEPATH}${ONESPEC}"
  5395.            if [ "$DIRECTSAVEPATH" = "" ]; then
  5396.             ln -s "$xDIRLINK" "${ONESPEC}"
  5397.            else
  5398.             DSOPATH="`dirname "${DIRECTSAVEPATH}${ONESPEC}"`"
  5399.             DSOBASE="`basename "${DIRECTSAVEPATH}${ONESPEC}"`"
  5400.             rm -f "${DSOPATH}/.wh.${DSOBASE}" #allow underlying symlink to become visible on top.
  5401.            fi
  5402.           fi
  5403.          fi
  5404.         fi
  5405.        fi
  5406.       fi
  5407.      done
  5408.  
  5409.             # woofce: NO_MULTIARCH_SYMLINK=1 (DISTRO_SPECS)
  5410.             if [ -z "$NO_MULTIARCH_SYMLINK" ] ; then
  5411.        #121217 it seems that this problem is occurring in other modes (13 reported)...
  5412.        #121123 having a problem with multiarch symlinks in full-installation...
  5413.        #it seems that the symlink is getting replaced by a directory.
  5414.        if [ "$DISTRO_ARCHDIR" ]; then #in /etc/rc.d/DISTRO_SPECS. 130112 change test from DISTRO_ARCHDIR. 130114 revert DISTRO_ARCHDIR_SYMLINKS==yes.
  5415.          if [ -d /usr/lib/${DISTRO_ARCHDIR} ]; then
  5416.            if [ ! -h /usr/lib/${DISTRO_ARCHDIR} ]; then
  5417.              cp -a -f --remove-destination /usr/lib/${DISTRO_ARCHDIR}/* /usr/lib/
  5418.              sync
  5419.              rm -r -f /usr/lib/${DISTRO_ARCHDIR}
  5420.              ln -s ./ /usr/lib/${DISTRO_ARCHDIR}
  5421.            fi
  5422.          fi
  5423.          if [ -d /lib/${DISTRO_ARCHDIR} ]; then
  5424.            if [ ! -h /lib/${DISTRO_ARCHDIR} ]; then
  5425.              cp -a -f --remove-destination /lib/${DISTRO_ARCHDIR}/* /lib/
  5426.              sync
  5427.              rm -r -f /lib/${DISTRO_ARCHDIR}
  5428.              ln -s ./ /lib/${DISTRO_ARCHDIR}
  5429.            fi
  5430.          fi
  5431.          if [ -d /usr/bin/${DISTRO_ARCHDIR} ]; then
  5432.            if [ ! -h /usr/bin/${DISTRO_ARCHDIR} ]; then
  5433.              cp -a -f --remove-destination /usr/bin/${DISTRO_ARCHDIR}/* /usr/bin/
  5434.              sync
  5435.              rm -r -f /usr/bin/${DISTRO_ARCHDIR}
  5436.              ln -s ./ /usr/bin/${DISTRO_ARCHDIR}
  5437.            fi
  5438.          fi
  5439.        fi
  5440.      fi
  5441.  
  5442.      #flush unionfs cache, so files in pup_save layer will appear "on top"...
  5443.      if [ "$DIRECTSAVEPATH" != "" ]; then
  5444.       #but first, clean out any bad whiteout files...
  5445.       # 22sep10 shinobar: bugfix was not working clean out whiteout files
  5446.       find /initrd/pup_rw -mount -type f -name .wh.\*  -printf '/%P\n'|
  5447.       while read ONEWHITEOUT
  5448.       do
  5449.        ONEWHITEOUTFILE="`basename "$ONEWHITEOUT"`"
  5450.        ONEWHITEOUTPATH="`dirname "$ONEWHITEOUT"`"
  5451.        if [ "$ONEWHITEOUTFILE" = ".wh.__dir_opaque" ]; then
  5452.         [ "$(grep "$ONEWHITEOUTPATH" "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files")" != "" ] && rm -f "/initrd/pup_rw/$ONEWHITEOUT"
  5453.         continue
  5454.        fi
  5455.        ONEPATTERN="`echo -n "$ONEWHITEOUT" | sed -e 's%/\\.wh\\.%/%'`"'/*' ;#echo "$ONEPATTERN" >&2
  5456.        [ "$(grep -x "$ONEPATTERN" "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files")" != "" ] && rm -f "/initrd/pup_rw/$ONEWHITEOUT"
  5457.       done
  5458.       #111229 /usr/local/petget/removepreview.sh when uninstalling a pkg, may have copied a file from sfs-layer to top, check...
  5459.       cat "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" |
  5460.       while read ONESPEC
  5461.       do
  5462.        [ "$ONESPEC" = "" ] && continue #precaution.
  5463.        if [ ! -d "$ONESPEC" ]; then
  5464.         [ -e "/initrd/pup_rw${ONESPEC}" ] && rm -f "/initrd/pup_rw${ONESPEC}"
  5465.        fi
  5466.       done
  5467.       #now re-evaluate all the layers...
  5468.       busybox mount -t aufs -o remount,udba=reval unionfs / #remount with faster evaluation mode.
  5469.       [ $? -ne 0 ] && logger -s -t "pkg" "Failed to remount aufs / with udba=reval"
  5470.       sync
  5471.      fi
  5472.  
  5473.      # get pkg size, category and description
  5474.      [ "$PKGCAT" = ''  ] && PKGCAT=`LANG=C  grep -m1 "|${PKGNAME_ONLY}|" "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | cut -f5 -d'|' | head -1`
  5475.      [ "$PKGCAT" = ''  ] && PKGCAT=`LANG=C  grep -m1 "^${PKGNAME}|" "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | cut -f5 -d'|' | head -1`
  5476.      [ "$PKGDESC" = '' ] && PKGDESC=`LANG=C grep -m1 "|${PKGNAME_ONLY}|" "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null| cut -f10 -d'|' | head -1`
  5477.      [ "$PKGDESC" = '' ] && PKGDESC=`LANG=C grep -m1 "^${PKGNAME}|" "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null| cut -f10 -d'|' | head -1`
  5478.      [ "$PKGSIZE" = '' ] && PKGSIZE=`LANG=C du -s -k ${CURDIR}/${PKG} 2>/dev/null | cut -f1`
  5479.      # get pkg version
  5480.      PKGVER="`LANG=C  echo "$PKGNAME" | sed -e 's/^[^0-9]*-//g'`"
  5481.      PKGARCH="`LANG=C echo "$PKGNAME" | cut -f3 -d'-' | grep -E 'i[3-9]|x[8-9]|noarch'`"
  5482.  
  5483.      # last check for pkg info
  5484.      [ "$DEPSLIST" = '' ] && DEPSLIST="`grep -m1 "^${PKGNAME}|" "$REPO_DB_FILE_DIR"/Packages-* 2>/dev/null | cut -f9 -d'|' | grep '+' | head -1`"
  5485.      [ "$PKGDESC" = ''  ] && PKGDESC="No description"
  5486.  
  5487.      #080917 - build woof compatible repo line .. #090817
  5488.      if [ "`cut -f1 -d'|' "$USER_INST_PKGS_FILE" 2>/dev/null | grep -m1 ^${PKGNAME}`" = '' ]; then
  5489.  
  5490.        # get the package db entry to add to user-installed-packages...
  5491.        # first, get it from petspecs if possible, or from the pkgs repo, or make one
  5492.        if [ -f /pet.specs ]; then
  5493.          DB_ENTRY="`cat /pet.specs | head -n 1`"
  5494.        elif [ "`grep -m1 "^${PKGNAME}|" "$REPO_DB_FILE_DIR/$REPOFILE"`" != "" ]; then
  5495.          DB_ENTRY="`grep -m1 "^${PKGNAME}|" "$REPO_DB_FILE_DIR/$REPOFILE"`"
  5496.        else
  5497.          DB_ENTRY="${PKGNAME}|${PKGNAME_ONLY}|${PKGVER}|${BUILD}|${PKGCAT}|${PKGSIZE}|${PKGDIR}|${PKGNAME}.${pkg_ext}|${DEPSLIST}|${PKGDESC}|${DISTRO_BINARY_COMPAT}|${DISTRO_COMPAT_VERSION}"
  5498.        fi
  5499.  
  5500.        # Fix for Debian executables showing as shared libs in ROX.
  5501.        if [ "$DISTRO_FILE_PREFIX" = "stretch" -o "$DISTRO_FILE_PREFIX" = "ascii" ]; then
  5502.          if [ "$(ps aux | grep ROX |grep -v grep)" ]; then # Other managers are OK
  5503.            if [ "$(which elfedit)" ]; then
  5504.              grep -E '/bin/|/sbin/' "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" |
  5505.              while read FLINE
  5506.              do
  5507.                [ "$(file "$FLINE" | grep -i 'shared object')" ] && elfedit --input-type=dyn --output-type=exec $FLINE
  5508.              done
  5509.            else
  5510.              echo -e "${yellow}Warning${endcolour} Recent Debian executables show as shared libraries in ROX,"
  5511.              echo "which causes ROX to fail to open or execute them. To fix that during package "
  5512.              echo "installation you should install elfutils or have devx loaded."
  5513.            fi
  5514.          fi
  5515.        fi
  5516.  
  5517.        # now add the db entry to user-installed-packages
  5518.        echo "$DB_ENTRY" >> "$USER_INST_PKGS_FILE" #130913
  5519.  
  5520.      fi
  5521.  
  5522.      # now delete the petspecs file(s), we already added our PKG info to installed-packages
  5523.      rm /pet.specs &>/dev/null
  5524.      rm /*.pet.specs &>/dev/null
  5525.  
  5526.      # make sure /tmp has correct permission, a pkg may have overwritten it
  5527.      ls -dl /tmp | grep -q '^drwxrwxrwt' || chmod 1777 /tmp #130305 rerwin.
  5528.  
  5529.      #090817
  5530.      if [ "$(is_installed_pkg $PKGNAME)" = true -a "$(cat "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files")" != '' ]; then
  5531.        echo -e "${green}Installed:${endcolour} $PKGNAME"
  5532.        # show if pkg has menu entry
  5533.        menu_entry_msg "$PKGNAME_ONLY"
  5534.        #080413 do fixmenus, if menu entry found #200713 moved here
  5535.        if [ "$(grep -m1 '/usr/share/applications' "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" 2>/dev/null | grep desktop$)" != "" ]; then
  5536.          [ ! -f /tmp/pkg/update_menus_busy ] && update_menus &
  5537.        fi
  5538.        [ "`which logger`" != '' ] && logger "$0 Package $PKGNAME installed by $APP $APPVER"
  5539.      else
  5540.        error "$PKGNAME may not have installed correctly."
  5541.      fi
  5542.  
  5543.      #100817 clean up user-installed-packages (remove duplicates and empty lines
  5544.      cat "$USER_INST_PKGS_FILE" | grep -v "^\$" | uniq > "$REPO_DIR/user-installed-packages_clean"
  5545.      mv "$REPO_DIR/user-installed-packages_clean" "$USER_INST_PKGS_FILE"
  5546.  
  5547.      # puppy specific fixes for installed package
  5548.      postinstall_hacks "$PKGNAME" "$PKGNAME_ONLY" &
  5549.  
  5550.      #100622 slackware 13.1: just in case any got through, remove c-shell scripts...
  5551.      rm -f /etc/profile.d/*.csh* 2>/dev/null
  5552.  
  5553.      #120523 precise puppy needs this... (refer also rc.update and 3builddistro)
  5554.      if [ "`grep '/usr/share/glib-2.0/schemas' "$PACKAGE_FILE_LIST_DIR/${PKGNAME}*.files" 2>/dev/null`" != "" ]; then
  5555.        [ -e /usr/bin/glib-compile-schemas ] && /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas &>/dev/null
  5556.      fi
  5557.  
  5558.      if [ "`grep '/usr/lib/gio/modules' "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" 2>/dev/null`" != "" ]; then
  5559.        [ -e /usr/bin/gio-querymodules ] && /usr/bin/gio-querymodules /usr/lib/gio/modules &>/dev/null
  5560.      fi
  5561.  
  5562.      sync
  5563.  
  5564.    fi #if CONFIRM = y #100213,0.9  moved down to here, fixes install, and output msgs
  5565.  
  5566.  else # file doesn't exists, user must download it first
  5567.  
  5568.    if [ "$PKGNAME" != '' ]; then
  5569.  
  5570.      echo "Package '$PKGNAME' not yet downloaded."
  5571.  
  5572.      matching_local_pkgs="`list_downloaded_pkgs $PKGNAME`"
  5573.  
  5574.      # if no matching pkgs downloaded
  5575.      if [ "$matching_local_pkgs" != "" ]; then
  5576.        echo "You could install one of the following packages with \`$SELF -i PKGNAME\`:"
  5577.        echo "`list_downloaded_pkgs $(basename "$PKGNAME" .$pkg_ext 2>/dev/null) 2>/dev/null`"
  5578.  
  5579.      # else if not downloaded, and in the current repo, list the matching repo pkgs
  5580.      elif [ "$matching_local_pkgs" = "" -a "`grep -m1 "^${PKGNAME}|" "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null`" != "" ]; then
  5581.        echo "You must first download one of the following packages with \`$SELF -d PKGNAME\`:"
  5582.        echo "`$PKGSEARCH $(basename $PKGNAME .$pkg_ext)`"
  5583.  
  5584.      else
  5585.        not_found "${PKGNAME}"
  5586.      fi
  5587.  
  5588.    else # PKGNAME is empty
  5589.  
  5590.      echo "Package could not be identified."
  5591.  
  5592.    fi
  5593.  
  5594.    exit 1
  5595.  
  5596.  fi
  5597.  
  5598.  # go back to the dir we started in
  5599.  cd "$PREVDIR"
  5600.  
  5601. }
  5602.  
  5603.  
  5604. postinstall_hacks(){              # fix pkgs after installation
  5605.  local PKGNAME="$1"
  5606.  local PKGNAME_ONLY="$2"
  5607.  
  5608.  # remove %u, %U, %f (etc) from Exec lines
  5609.  DESKTOPFILE="`find /usr/share/applications -iname "${PKGNAME_ONLY}*.desktop"`"
  5610.  [ -f "$DESKTOPFILE" ] && \
  5611.    sed -i 's/ %u//' $DESKTOPFILE && \
  5612.    sed -i 's/ %U//' $DESKTOPFILE && \
  5613.    sed -i 's/ %f//' $DESKTOPFILE && \
  5614.    sed -i 's/ %F//' $DESKTOPFILE
  5615.  
  5616.  case $PKGNAME in
  5617.   0ad-*|0ad_*)
  5618.    bbe -e 's/geteuid/getppid/' /usr/games/pyrogenesis > /usr/games/pyrogenesis1 2>/dev/null
  5619.    mv /usr/games/pyrogenesis1 /usr/games/pyrogenesis
  5620.    chmod 755 /usr/games/pyrogenesis
  5621.   ;;
  5622.   openclonk-*|openclonk_*)
  5623.    bbe -e 's/geteuid/getppid/' /usr/games/openclonk > /usr/games/openclonk1 2>/dev/null
  5624.    mv /usr/games/openclonk1 /usr/games/openclonk
  5625.    chmod 755 /usr/games/openclonk
  5626.   ;;
  5627.   vlc_*|vlc-*)
  5628.    VLCDESKTOP="`find /usr/share/applications -mindepth 1 -maxdepth 1 -iname 'vlc*.desktop'`"
  5629.    [ -f $VLCDESKTOP ] && sed -i 's/ --started-from-file %U//' $VLCDESKTOP
  5630.    [ -f $VLCDESKTOP ] && sed -i 's/ --started-from-file//' $VLCDESKTOP
  5631.  
  5632.    #120907 vlc in debian/ubuntu configured to not run as root (it is a pre-compile configure option to enable running as root).
  5633.    #this hack will fix it...
  5634.    #note, this code is also in FIXUPHACK in 'vlc' template.
  5635.    if [ -f /usr/bin/bbe ]; then #bbe is a sed-like utility for binary files.
  5636.     if [ -f /usr/bin/vlc  ]; then
  5637.       bbe -e 's/geteuid/getppid/' /usr/bin/vlc > /tmp/vlc-temp1
  5638.       mv -f /tmp/vlc-temp1 /usr/bin/vlc
  5639.       chmod 755 /usr/bin/vlc
  5640.     fi
  5641.    fi
  5642.   ;;
  5643.   google-chrome-*) #130221 pemasu. 130224 pemasu: limit cache size...
  5644.    if [ -f /usr/bin/bbe ]; then #bbe is a sed-like utility for binary files.
  5645.     if [ -f /opt/google/chrome/chrome  ]; then
  5646.    bbe -e 's/geteuid/getppid/' /opt/google/chrome/chrome > /tmp/chrome-temp1
  5647.    mv -f /tmp/chrome-temp1 /opt/google/chrome/chrome
  5648.    chmod 755 /opt/google/chrome/chrome
  5649.    [ -e /usr/bin/google-chrome ] && rm -f /usr/bin/google-chrome
  5650.    echo '#!/bin/sh
  5651.  exec /opt/google/chrome/google-chrome --user-data-dir=/root/.config/chrome --disk-cache-size=10000000 --media-cache-size=10000000 "$@"' > /usr/bin/google-chrome
  5652.    chmod 755 /usr/bin/google-chrome
  5653.    ln -s google-chrome /usr/bin/chrome
  5654.    ln -s /opt/google/chrome/product_logo_48.png /usr/share/pixmaps/google-chrome.png
  5655.    ln -s /opt/google/chrome/product_logo_48.png /usr/share/pixmaps/chrome.png
  5656.    CHROMEDESKTOP="`find /usr/share/applications -mindepth 1 -maxdepth 1 -iname '*chrome*.desktop'`"
  5657.    if [ "$CHROMEDESKTOP" = "" ]; then #precaution.
  5658.     echo '[Desktop Entry]
  5659. Encoding=UTF-8
  5660. Version=1.0
  5661. Name=Google Chrome web browser
  5662. GenericName=Google Chrome
  5663. Comment=Google Chrome web browser
  5664. Exec=google-chrome
  5665. Terminal=false
  5666. Type=Application
  5667. Icon=google-chrome.png
  5668. Categories=WebBrowser;' > /usr/share/applications/google-chrome.desktop
  5669.    fi
  5670.     fi
  5671.    fi
  5672.   ;;
  5673.  chromium*) #130221 pemasu. 130224 pemasu: limit cache size...
  5674.    if [ -f /usr/bin/bbe ]; then #bbe is a sed-like utility for binary files.
  5675.     if [ -f /usr/lib/chromium/chromium  ]; then
  5676.    bbe -e 's/geteuid/getppid/' /usr/lib/chromium/chromium > /tmp/chrome-temp1
  5677.    mv -f /tmp/chrome-temp1 /usr/lib/chromium/chromium
  5678.    chmod 755 /usr/lib/chromium/chromium
  5679.    [ -e /usr/bin/chromium ] && rm -f /usr/bin/chromium
  5680.    echo '#!/bin/sh
  5681.  exec /usr/lib/chromium/chromium --user-data-dir=/root/.config/chrome --disk-cache-size=10000000 --media-cache-size=10000000 --audio-buffer-size=2048 "$@"' > /usr/bin/chromium
  5682.    chmod 755 /usr/bin/chromium
  5683.    ln -s /usr/share/icons/hicolor/48x48/apps/chromium.png /usr/share/pixmaps/chromium.png
  5684.    CHROMEDESKTOP="`find /usr/share/applications -mindepth 1 -maxdepth 1 -iname '*chromium-br*.desktop'`"
  5685.    if [ "$CHROMEDESKTOP" = "" ]; then #precaution.
  5686.     echo '[Desktop Entry]
  5687. Encoding=UTF-8
  5688. Version=1.0
  5689. Name=Chromium web browser
  5690. GenericName=Chromium
  5691. Comment=Chromium web browser
  5692. Exec=chromium
  5693. Terminal=false
  5694. Type=Application
  5695. Icon=chromium.png
  5696. Categories=WebBrowser;' > /usr/share/applications/chromium.desktop
  5697.    fi
  5698.     fi
  5699.    fi
  5700.   ;;
  5701.   jwm_theme_*)
  5702.    #120924 DejaVu font no good for non-Latin languages...
  5703.    #see also langpack_* pinstall.sh (template is in /usr/share/doc/langpack-template/pinstall.sh, read by momanager).
  5704.    LANGUSER="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
  5705.    case $LANGUSER in
  5706.     zh*|ja*|ko*) #chinese, japanese, korean
  5707.    sed -i -e 's%DejaVu Sans%Sans%' /etc/xdg/templates/_root_*
  5708.    sed -i -e 's%DejaVu Sans%Sans%' /root/.jwm/themes/*-jwmrc
  5709.    sed -i -e 's%DejaVu Sans%Sans%' /root/.jwm/jwmrc-theme
  5710.     ;;
  5711.    esac
  5712.    #130326 font size fix for 96 dpi...
  5713.    if [ "$PKGNAME_ONLY" ]; then
  5714.     JWMTHEMEFILE="$(grep '^/root/\.jwm/themes/.*-jwmrc$' "$PACKAGE_FILE_LIST_DIR/${PKGNAME_ONLY}.files" | head -n 1)"
  5715.     [ "$JWMTHEMEFILE" ] && hackfontsize "JWMTHEMES='${JWMTHEMEFILE}'"
  5716.    fi
  5717.   ;;
  5718.   openbox*)
  5719.    #120924 DejaVu font no good for non-Latin languages...
  5720.    #see also langpack_* pinstall.sh (template is in /usr/share/doc/langpack-template/pinstall.sh, read by momanager).
  5721.    LANGUSER="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
  5722.    case $LANGUSER in
  5723.     zh*|ja*|ko*) #chinese, japanese, korean
  5724.    sed -i -e 's%DejaVu Sans%Sans%' /etc/xdg/openbox/*.xml
  5725.    sed -i -e 's%DejaVu Sans%Sans%' /root/.config/openbox/*.xml
  5726.     ;;
  5727.    esac
  5728.   ;;
  5729.   gtk_theme_*)
  5730.    #120924 DejaVu font no good for non-Latin languages...
  5731.    #see also langpack_* pinstall.sh (template is in /usr/share/doc/langpack-template/pinstall.sh, read by momanager).
  5732.    LANGUSER="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
  5733.    case $LANGUSER in
  5734.     zh*|ja*|ko*) #chinese, japanese, korean
  5735.    GTKRCFILE="$(find /usr/share/themes -type f -name gtkrc | tr '\n' ' ')"
  5736.    for ONEGTKRC in $GTKRCFILE
  5737.    do
  5738.     sed -i -e 's%DejaVu Sans%Sans%' $ONEGTKRC
  5739.    done
  5740.     ;;
  5741.    esac
  5742.    #130326 font size fix for 96 dpi...
  5743.    if [ "$PKGNAME_ONLY" ]; then
  5744.     GTKTHEMEFILE="$(grep '^/usr/share/themes/.*/gtk-2\.0/gtkrc$' "$PACKAGE_FILE_LIST_DIR/${PKGNAME_ONLY}.files" | head -n 1)"
  5745.     [ "$GTKTHEMEFILE" ] && hackfontsize "GTKRCS='${GTKTHEMEFILE}'"
  5746.    fi
  5747.   ;;
  5748.   seamonkey*|firefox*)
  5749.    #120924 DejaVu font no good for non-Latin languages...
  5750.    #see also langpack_* pinstall.sh (template is in /usr/share/doc/langpack-template/pinstall.sh, read by momanager).
  5751.    LANGUSER="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
  5752.    case $LANGUSER in
  5753.     zh*|ja*|ko*) #chinese, japanese, korean
  5754.    MOZFILE="$(find /root/.mozilla -type f -name prefs.js -o -name '*.css' | tr '\n' ' ')"
  5755.    for ONEMOZ in $MOZFILE
  5756.    do
  5757.     sed -i -e 's%DejaVu Sans%Sans%' $ONEMOZ
  5758.    done
  5759.     ;;
  5760.    esac
  5761.   ;;
  5762.   mc_*) #121206 midnight commander
  5763.    #in ubuntu, won't run from the menu. this fixes it...
  5764.    [ -f /usr/share/applications/mc.desktop ] && sed -i -e 's%^Exec=.*%Exec=TERM=xterm mc%' /usr/share/applications/mc.desktop
  5765.   ;;
  5766.   xsane*) #130122
  5767.    #xsane puts up a warning msg at startup if running as root, remove it...
  5768.    #this code is also in file FIXUPHACK in xsane template (in Woof).
  5769.    #WARNING: this may only work for x86 binary.
  5770.    if [ -f /usr/bin/bbe ]; then #bbe is a sed-like utility for binary files.
  5771.     if [ -f /usr/bin/xsane  ]; then
  5772.    bbe -e 's/\x6b\x00getuid/\x6b\x00getpid/' /usr/bin/xsane > /tmp/xsane-temp1
  5773.    mv -f /tmp/xsane-temp1 /usr/bin/xsane
  5774.    chmod 755 /usr/bin/xsane
  5775.     fi
  5776.    fi
  5777.   ;;
  5778.   kompozer*) #130507
  5779.    [ -f /usr/bin/kompozer ] && [ -d /usr/lib/kompozer ] && sed -i -e 's%^moz_libdir=%export MOZILLA_FIVE_HOME="/usr/lib/kompozer" #BK\nmoz_libdir=%' /usr/bin/kompozer
  5780.   ;;
  5781.  esac
  5782. }
  5783.  
  5784.  
  5785. choose_pkg(){                     # given partial name ($1), choose from a list of matching packages FUNCLIST
  5786.  
  5787.  
  5788.  # exit if no valid options
  5789.  [ ! "$1" ] || [ "$1" = "" ] || [ "$1" = "-" ] && print_usage get && exit 1
  5790.  
  5791.  # get $REPONAME and $EX
  5792.  . "${PKGRC}"
  5793.  
  5794.  local REPOEX    # extension of pkgs in the current repo $REPONAME (from rc file)
  5795.  local PKGEX     # pkg extension we get from $1, defaults to $REPOEXT is empty
  5796.  local PKGNAME   # the name of the pkg, we get this from $1
  5797.  local PKGNAME_ONLY  # the pkg name without version, we get this from PKGNAME
  5798.  local PKGS      # the list of PKGS returned matching $1/$PKGNAME
  5799.  local INT=1     # used to provide numbered lists
  5800.  local sort      # set to either sort or sort -r
  5801.  
  5802.  REPOEX=$EX
  5803.  # get pkg extension
  5804.  PKGEX=$(get_pkg_ext "$1")
  5805.  
  5806.  # if no extension, set to extension of current repo
  5807.  [ "$PKGEX" = '' ] && PKGEX=$REPOEX
  5808.  
  5809.  # get pkg name with version, no extension or path
  5810.  PKGNAME="$(basename "$1" .$PKGEX)"
  5811.  
  5812.  # get the full pkg name, to compare against repo pkgs we find
  5813.  PKGNAME_FULL="$(get_pkg_name "$PKGNAME")"
  5814.  
  5815.  # get pkg name only .. without version or suffix
  5816.  PKGNAME_ONLY=$(get_pkg_name_only "$PKGNAME")
  5817.  
  5818.  [ "$PKG_DEBUG" = "true" ] && echo "choose_pkg(): PKGNAME=$PKGNAME" >&2
  5819.  [ "$PKG_DEBUG" = "true" ] && echo "choose_pkg(): PKGNAME_ONLY=$PKGNAME_ONLY" >&2
  5820.  [ "$PKG_DEBUG" = "true" ] && echo "choose_pkg(): PKGNAME_FULL=$PKGNAME_FULL" >&2  
  5821.  #[ "$PKG_DEBUG" = "true" ] && set -x
  5822.  
  5823.  # remove any previous user choice lists
  5824.  rm $TMPDIR/PKGLIST &>/dev/null
  5825.  rm $TMPDIR/USRPKGLIST &>/dev/null
  5826.  
  5827.  # get all pkgs that match the given pkgname
  5828.  # returns full pkg names (field1 of repo, pkgname with ver but no extension) each on a new line
  5829.  PKGS="$($PKGSEARCH "${PKGNAME}")"
  5830.  [ "$PKG_DEBUG" = "true" ] && echo "choose_pkg(): PKGS=$PKGS" >&2  
  5831.  if [ "$FORCE" = false ] && [ "$(is_installed_pkg "$PKGNAME_FULL")" = true ] && [ "$HIDE_INSTALLED" = true ];then
  5832.    # remove it from choices
  5833.    [ "$PKG_DEBUG" = "true" ] && set -x
  5834.    PKGS="$(echo "$PKGS" | grep -v ^$PKGNAME_FULL\$)"
  5835.    [ "$PKG_DEBUG" = "true" ] &&  set +x
  5836.  fi
  5837. [ "$PKG_DEBUG" = "true" ] && set -x
  5838.  # extra steps for ubuntu and debian repos, if multiple choices returned.. remove non-compatible archs, remove dev and dbg pkgs...
  5839.  if [ "$PKGEX" = "deb" ] && [ "$(echo "$PKGS" | wc -l)" != "1" ];then
  5840.  
  5841.    ARCH="$(uname -m)"
  5842.    #remove x64 pkgs from choices if not using an x64 cpu
  5843.    [ "$ARCH" != "x86_64" ] && PKG="$(echo "$PKGS" | grep -v -E 'amd64|x86_64')"
  5844.  
  5845.    # set any pkgs matching current arch to top of list:
  5846.    # first pkg found (newest) added to top, then the next, etc,
  5847.    for LINE in $(echo "$PKGS"  | sort -r)
  5848.    do
  5849.      # if not searching for -dev or -dgb pkg, move it to bottom of the list
  5850.      if [ "$(is_blacklisted_pkg "$(get_pkg_name_only "$LINE")")" = false ] && \
  5851.         [ "$(echo "$PKGNAME" | grep -E "\-dbg_|\-dev_")" = "" ] && \
  5852.         [ "$(echo "$LINE" | grep -E "\-dbg_|\-dev_")" != "" ]
  5853.      then
  5854.        PKGS="$(echo "$PKGS" | grep -v  "$LINE")"
  5855.        PKGS="$PKGS
  5856. $LINE"
  5857.      fi
  5858.      # if pkg is for current cpu arch, move it to top of the list
  5859.      if [ "$(echo "$LINE" | grep -m1 "$ARCH")" != "" ];then
  5860.        PKGS="$(echo "$PKGS" | grep -v  "$LINE")"
  5861.        PKGS="$LINE
  5862. $PKGS"
  5863.      fi
  5864.    done
  5865.    #remove debug and dev pkgs
  5866.    #PKGS="`echo "$PKGS" | grep -v "\-dbg_"`"
  5867.    #PKGS="`echo "$PKGS" | grep -v "\-dev_"`"
  5868.  fi
  5869.  
  5870.  # get the user to choose which packages they want to install
  5871.  if [ "$ASK" = true ] && [ "$PKGS" != "" ];then
  5872.    echo "Please choose the package number. For the first package,"
  5873.    echo "enter '1', without quotes. To install multiple packages,"
  5874.    echo "enter the numbers, separated by a comma. Example:  1,3,4"
  5875.    echo
  5876.  fi
  5877.  
  5878.  # if using ubuntu/debian packages, put pkg_* before pkg-* .. else dont
  5879.  [ "$PKGEX" = 'deb' ] && sort='sort -r' || sort='sort'
  5880.  
  5881.  # go through each actual pkg that matches the pkgname search, make it a numbered list
  5882.  echo "$PKGS" | $sort -u | while read LINE
  5883.  do
  5884.    if [ "$LINE" != "" ] && [ "$LINE" != "," ] && [ "$LINE" != " " ];then
  5885.      [ "$ASK" = true ] && echo "${INT}. $LINE"
  5886.      echo "${INT}. $LINE" >> $TMPDIR/PKGLIST
  5887.      INT=$(($INT + 1))
  5888.    fi
  5889.  done
  5890.  
  5891.  # if pkg list was made
  5892.  if [ -f $TMPDIR/PKGLIST ] && [ -s "$TMPDIR/PKGLIST" ];then
  5893.  
  5894.    # set to first pkg only as default
  5895.    if [ "$ASK" = false ];then
  5896.      USRPKGLIST="$(echo "$PKGS" | $sort | head -1)"
  5897.      echo "$USRPKGLIST" > $TMPDIR/USRPKGLIST
  5898.    fi
  5899.  
  5900.    # user can now input which actual pkgs to get, chosen by number
  5901.    if [ "$ASK" = true ];then
  5902.      # only ask once
  5903.      ASK=false
  5904.  
  5905.      echo
  5906.      echo "Give the numbers of the packages you want to install,"
  5907.      echo -n "separated by a comma, or hit ENTER only to skip: "
  5908.      read USRPKGLIST1 </dev/tty
  5909.      echo
  5910.  
  5911.      # if user chose nothing (hit ENTER only), just skip
  5912.      [ "$USRPKGLIST1" = '' ] && exit 0
  5913.  
  5914.      # split the results into newlines, create the list of chosen pkgs (used by other funcs)
  5915.      echo "${USRPKGLIST1}" | tr ',' '\n' | while read LINE
  5916.      do
  5917.        # set chosen pkg choice(s)
  5918.        grep "^$LINE. " "$TMPDIR/PKGLIST" 2>/dev/null | cut -f2 -d' ' >> $TMPDIR/USRPKGLIST
  5919.      done
  5920.    fi
  5921.  
  5922.    # remove temp file.. but keep $TMPDIR/USRPKGLIST, it contains our users choices, and is used by pkg_get() and get_deps()
  5923.    rm $TMPDIR/PKGLIST &>/dev/null
  5924.  fi
  5925.  set +x
  5926.  [ "$PKG_DEBUG" = "true" ] && echo "finished: choose_pkg()" >&2
  5927. }
  5928.  
  5929.  
  5930.  
  5931. pkg_get(){                        # find, download and install $1 and its deps FUNCLIST
  5932.  
  5933.  # The function `choose_pkg` is run just before this one. It gives us $TMPDIR/USRPKGLIST,
  5934.  # which contains a list of packages the user wants to install or download.
  5935.  # In this func, we will go through the list and download/install the package, as well
  5936.  # as its dependencies (depending on what the user chose to do).
  5937.  
  5938.  . ${PKGRC}
  5939.  
  5940.  # exit if no valid options
  5941.  [ ! "$1" -o "$1" = "" -o "$1" = "-" ] && print_usage get && exit 1
  5942.  
  5943.  local PREVDIR="$CURDIR"
  5944.  local pkg_ext=`get_pkg_ext "$1"`; pkg_ext="${pkg_ext:-$EX}" # fall back to repo extension
  5945.  local PKGNAME="`get_pkg_name $(basename "$1" .$pkg_ext)`"
  5946.  local PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  5947.  local PKGLIST="${PKGNAME}"
  5948.  local pkg_builtin=''
  5949.  
  5950.  [ "$PKG_DEBUG" = "true" ] && echo "pkg_get(): PKGNAME=$PKGNAME" >&2
  5951.  [ "$PKG_DEBUG" = "true" ] && echo "pkg_get(): PKGNAME_ONLY=$PKGNAME_ONLY" >&2
  5952.  [ "$PKG_DEBUG" = "true" ] && set -x
  5953.  
  5954.  # dont ask to download or install pkg or deps, as choose_pkg() already asked
  5955.  ASK=false
  5956.  
  5957.  # exit if no valid pkg name
  5958.  [ ! "$PKGNAME" -o "$PKGNAME" = "" -o "$PKGNAME" = "-" ] && print_usage get && exit 1
  5959.  
  5960.  # we want to download all pkgs to same place
  5961.  cd "$WORKDIR"
  5962.  CURDIR="$WORKDIR"
  5963.  
  5964.  # use the list of pkgs user has chosen to install, or the given PKGNAME
  5965.  if [ -f $TMPDIR/USRPKGLIST ] && [ -s "$TMPDIR/USRPKGLIST" ];then
  5966.    PKGLIST="$(grep -v "^\$" "$TMPDIR/USRPKGLIST")"
  5967.  fi
  5968.  
  5969.  if [ -z "$PKGLIST" ]; then
  5970.    echo "No packages to get, exiting."
  5971.    exit 1
  5972.  fi
  5973.  
  5974.  # list the pkgs and ask to download (and maybe install)
  5975.  echo "$PKGLIST" | while read pkg
  5976.  do
  5977.  
  5978.    [ "$pkg" = '' -o ! "$pkg" ] && continue
  5979.  
  5980.    local pkg_name=$(get_pkg_name "$pkg" 2>/dev/null)
  5981.    [ "$pkg_name" = '' -o ! "$pkg_name" ] && continue
  5982.  
  5983.    local pkg_name_only=`get_pkg_name_only "$pkg" 2>/dev/null`
  5984.    [ "`is_blacklisted_pkg "$pkg_name_only"`" = true ] && continue
  5985.  
  5986.    local pkg_already_done=`grep -m1 "^$pkg_name_only\$" $TMPDIR/PKGSDONE 2>/dev/null`
  5987.    [ "$pkg_already_done" != '' ] && continue
  5988.  
  5989.    local pkg_in_repo=`is_repo_pkg $pkg_name_only`
  5990.    local PKGFILE=''
  5991.  
  5992.    # dont even try to download if no matches found
  5993.    if [ "$pkg_in_repo" = true -a "$pkg_name_only" != "" ]; then
  5994.  
  5995.      local pkg_is_builtin=`is_builtin_pkg "$pkg_name_only"`
  5996.      [ "$PKG_DEBUG" = "true" ] && echo "pkg_get(): pkg_is_builtin=${pkg_is_builtin}" >&2      
  5997.      local pkg_is_in_devx=`is_devx_pkg "$pkg_name_only"`
  5998.      [ "$PKG_DEBUG" = "true" ] && echo "pkg_get(): pkg_is_in_devx=${pkg_is_in_devx}" >&2        
  5999.      [ "$PKG_DEBUG" = "true" ] && set -x
  6000.  
  6001.      # skip getting pkg if its a builtin, unless HIDE_BUILTINS=false
  6002.      if [ "$pkg_is_builtin" = true -a "${HIDE_BUILTINS}" = true ]; then
  6003.        echo "Skipping $pkg_name_only (already built-in).."
  6004.        continue
  6005.      fi
  6006.  
  6007.      # skip getting pkg if its in the devx, unless HIDE_BUILTINS=false
  6008.      if [ "$pkg_is_in_devx" = true -a "${HIDE_BUILTINS}" = true ]; then
  6009.        echo "Skipping $pkg_name_only (already in devx).."
  6010.        continue
  6011.      fi
  6012.  
  6013.      # if we are intending to install the pkg
  6014.      if [ "$NO_INSTALL" = false -a "$FORCE" = false ]; then
  6015.        # skip if package is already installed, unless --force given
  6016.        if [ "$FORCE" = false -a "`is_installed_pkg $pkg_name`" = true ]; then
  6017.          echo -e "Package ${magenta}${pkg_name_only}${endcolour} already installed."
  6018.          echo -e "Use the -f option to force installation: $SELF add $pkg_name_only -f"
  6019.          continue
  6020.        fi
  6021.      fi
  6022.  
  6023.      # get deps early (if any)
  6024.      [ "$PKG_DEBUG" = "true" ] && set +x
  6025.      [ "$PKG_DEBUG" = "true" ] && echo "pkg_get(): list_deps \"$pkg_name\""
  6026.      list_deps "$pkg_name" --rev-logic > ${TMPDIR}/${pkg_name}_dep_list #&
  6027.      [ "$PKG_DEBUG" = "true" ] && echo "pkg_get(): exited: list_deps \"$pkg_name\""
  6028.      # DOWNLOAD PKG
  6029.      [ "$PKG_DEBUG" = "true" ] && echo "pkg_get(): pkg_download \"$pkg_name\""      
  6030.      pkg_download "$pkg_name"
  6031.      [ "$PKG_DEBUG" = "true" ] && echo "pkg_get(): exited: pkg_download \"$pkg_name\""  
  6032.      [ "$PKG_DEBUG" = "true" ] && set -x      
  6033.      PKGFILE="$pkg_download_rtn"
  6034.      if [ ! -f "$PKGFILE" ]; then
  6035.        echo "warning: global rtn failed pkg_name=${pkg_name} PKGFILE=${PKGFILE}"
  6036.        echo "trying slower method"
  6037.        PKGFILE="$(find "$CURDIR" -maxdepth 1 -type f -name "$(get_pkg_filename "$pkg_name")")"
  6038.      fi
  6039.  
  6040.      if [ -f "${PKGFILE}" ]; then
  6041.  
  6042.        # check if we install or not
  6043.        if [ "${NO_INSTALL}" = false ]; then
  6044.            pkg_install "${PKGFILE}"
  6045.        fi
  6046.  
  6047.         ## if pkg was installed, or Pkg is simply downloading all deps
  6048.         #if [ "$(is_installed_pkg "$pkg_name")" = true ] || [ "${NO_INSTALL}" = true ];then
  6049.    
  6050.           # get the dependencies for this package
  6051.           echo "pkg_get(): get_deps ${pkg_name}"
  6052.           [ "$(has_deps $pkg_name)" = true ] && get_deps "${pkg_name}"
  6053.    
  6054.         #fi
  6055.  
  6056.      else # PKGFILE not a file
  6057.        echo "Can't find ${PKGNAME} or not a valid pkg.."
  6058.      fi
  6059.      [ "$PKG_DEBUG" = "true" ] && set +x
  6060.    else # no matches in repo found
  6061.      echo "Cannot find ${PKGNAME}.."
  6062.    fi
  6063.  
  6064.    # done with this pkg, add it to done list, will be skipped it seen again, until loop is finished
  6065.    echo "$pkg_name_only" >> $TMPDIR/PKGSDONE
  6066.  done
  6067. }
  6068.  
  6069.  
  6070. pkg_update(){                     # update installed pkgs, $1 is optional filter FUNCLIST
  6071.  
  6072.  # exit if no valid options
  6073.  #[ ! "$1" -o "$1" = "-" ] && print_usage pkg-update && exit 1
  6074.  
  6075.  # get rc file settings
  6076.  . ${PKGRC}
  6077.  
  6078.  local PKGNAME=''
  6079.  local PKGLIST=''
  6080.  local pkg_ext=$EX
  6081.  local BUILTINS=''
  6082.  local separator=''
  6083.  
  6084.  if [ "$1" != '' ]; then
  6085.  
  6086.    PKGNAME=$(basename "$1")
  6087.    PKGNAME=`get_pkg_name "$PKGNAME"`
  6088.    PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  6089.  
  6090.    # get installed packages
  6091.    PKGLIST="`HIDE_BUILTINS=false list_installed_pkgs "$PKGNAME_ONLY"`"
  6092.  
  6093.    #is_builtin=`is_builtin_pkg "$PKGNAME"`
  6094.  
  6095.    # dont update builtins unless -F was given
  6096.    #if [ "$is_builtin" = true -a "$HIDE_BUILTINS" = true ]; then
  6097.    #  echo -e "Package ${magenta}${PKGNAME}${endcolour} is built in, not updating."
  6098.    #  #echo -e "Use `$SELF -F --pkg-update $PKGNAME` to update it anyway."
  6099.    #  exit 0
  6100.    #fi
  6101.  else
  6102.    # get installed packages
  6103.    PKGLIST="`HIDE_BUILTINS=false list_installed_pkgs`"
  6104.  fi
  6105.  
  6106.  # iterate over the list
  6107.  echo "$PKGLIST"  | grep -v ^$ | sort -u | while read installed_pkg; do
  6108.  
  6109.    [ ! "$installed_pkg" -o "$installed_pkg" = '' ] && continue
  6110.  
  6111.    # if this pkg ($installed_pkg) is not from a known repo, we cant compare its version to anything else, skip it
  6112.    #[ "`is_repo_pkg "$installed_pkg"`" = false ] && echo -e "Package ${magenta}$installed_pkg${endcolour} not found in any repos." && continue
  6113.  
  6114.    local PNAME=''
  6115.    local latest_repo_pkg=''
  6116.    local ASKREPLY='y'
  6117.    # get pkg name (without version) and version of current PKG
  6118.  
  6119.    PNAME=`get_pkg_name_only $installed_pkg`
  6120.  
  6121.    [ "$PNAME" = '' ] && continue
  6122.  
  6123.    case $DISTRO_BINARY_COMPAT in
  6124.      ubuntu|trisquel|debian|devuan)
  6125.        separator='_'
  6126.        ;;
  6127.      *)
  6128.        separator='-'
  6129.        ;;
  6130.    esac
  6131.  
  6132.    latest_repo_pkg="`grep -m1 "^${PNAME}${separator}" "${ALL_REPO_DB_PATHS[@]}" |cut -f1 -d'|' | head -1 | cut -f2 -d':'`"
  6133.  
  6134.    # skip if we didn't find the right package
  6135.    [ "`echo "$latest_repo_pkg" | grep "^${PNAME}${separator}"`" = '' ] && continue
  6136.  
  6137.    latest_version=`get_pkg_version "$latest_repo_pkg"`
  6138.    installed_version=`get_pkg_version "$installed_pkg"`
  6139.  
  6140.    # get latest versions
  6141.  
  6142.    # check pkg version against installed version
  6143.    if [ "$latest_version" != "" -a "$installed_version" != "" ]; then
  6144.      vercmp $latest_version gt $installed_version 2>/dev/null
  6145.      RESULT=$?
  6146.  
  6147.      if [ "$RESULT" = 0 ]; then #newer version available
  6148.  
  6149.        if [ "$ASK" = true ]; then
  6150.          echo "Do you want to update to ${PNAME}${separator}${latest_version}? [y/N]   "
  6151.          read -n 1 ASKREPLY </dev/tty
  6152.          [ "$ASK" = true ] && echo -ne "\b\b\n"
  6153.        fi
  6154.  
  6155.        if [ "$ASK" = false -o "$ASKREPLY" = "y" ]; then
  6156.          echo -e "${yellow}Update${endcolour}: ${magenta}$PNAME${endcolour} from ${bold}$installed_version${endcolour} to ${bold}$latest_version${endcolour}"
  6157.          cd "$WORKDIR"
  6158.          ASK=$ASK FORCE=$FORCE pkg_get "$latest_repo_pkg"
  6159.          cd "$CURDIR"
  6160.        fi
  6161.  
  6162.      else #280613 inform user if no update found
  6163.        echo -e "${green}Latest${endcolour}: $PNAME${separator}$installed_version"
  6164.      fi #end if vercmp XX gt YY = 0
  6165.  
  6166.    elif [ "$PNAME" = '' ]; then
  6167.      error "${installed_pkg} not found."
  6168.    else #280613
  6169.      error "$installed_pkg package versions could not be compared: ${latest_version:-unknown latest} => ${installed_version:-unknown installed version}"
  6170.      #return 1
  6171.    fi #end if PKGVER != ""
  6172.  done
  6173. }
  6174.  
  6175.  
  6176. pkg_uninstall(){                  # remove an installed package ($1) FUNCLIST
  6177.  
  6178.  # quit if no valid options
  6179.  [ ! "$1" -o "$1" = "-" ] && print_usage uninstall && exit 1
  6180.  
  6181.  local PKGNAME
  6182.  local PKGNAME_ONLY
  6183.  local PKGFILE
  6184.  local pkg_ext
  6185.  
  6186.  # get pkg extension
  6187.  pkg_ext=`get_pkg_ext "$1"`
  6188.  
  6189.  #get pkg name with version, but no extension or path
  6190.  PKGNAME="$(basename "$1" .$pkg_ext)"
  6191.  PKGNAME="$(basename "$PKGNAME" .pet)"
  6192.  PKGNAME="$(basename "$PKGNAME" .deb)"
  6193.  PKGNAME="$(basename "$PKGNAME" .sfs)"
  6194.  PKGNAME="$(basename "$PKGNAME" .tar.gz)"
  6195.  PKGNAME="$(basename "$PKGNAME" .tar.xz)"
  6196.  PKGNAME="$(basename "$PKGNAME" .tgz)"
  6197.  PKGNAME="$(basename "$PKGNAME" .txz)"
  6198.  PKGNAME=`get_pkg_name "$PKGNAME"`
  6199.  
  6200.  # get pkg name only .. without versions or suffix
  6201.  PKGNAME_ONLY="`get_pkg_name_only "$PKGNAME"`"
  6202.  
  6203.  local pkg_is_builtin=`is_builtin_pkg "$PKGNAME_ONLY"`
  6204.  local is_installed=`is_installed_pkg "$PKGNAME"`
  6205.  
  6206.  # skip pkg if its a builtin
  6207.  [ "$pkg_is_builtin" = true ] && return 1
  6208.  
  6209.  local pkg_is_sfs_file="`sfs_loadr -q -i | grep -v ^sfs_loadr | grep ^$PKGNAME`"
  6210.  
  6211.  # if pkg is SFS, "uninstall" it here
  6212.  if [ "$pkg_is_sfs_file" != "" -o "$pkg_ext" = "sfs" ]; then
  6213.    PKGNAME="$(sfs_loadr -q -i | grep -v ^sfs_loadr | grep ^$PKGNAME | head -1)"
  6214.    sfs_loadr -q --cli -"${CURDIR}/${PKGNAME}" 2>/dev/null
  6215.    is_installed=true # we want to continue
  6216.  fi
  6217.  
  6218.  
  6219.  if [ "$is_installed" = true -o "$FORCE" = true ]; then #250713
  6220.  
  6221.    # get the list of files to be deleted, exact match
  6222.    PKGFILE="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "$PKGNAME.files")"
  6223.  
  6224.    # if no exact match, search for pkgname*
  6225.    [ ! -f "$PKGFILE" ] && PKGFILE="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${PKGNAME}"'*.files')"
  6226.    [ ! -f "$PKGFILE" ] && PKGFILE="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${PKGNAME_ONLY}_"'*.files')"
  6227.    [ ! -f "$PKGFILE" ] && PKGFILE="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${PKGNAME_ONLY}-"'*.files')"
  6228.    [ ! -f "$PKGFILE" ] && PKGFILE="${PKGNAME}.files"
  6229.    [ ! -f "$PKGFILE" ] && PKGFILE="${PKGNAME}.sfs.files"
  6230.    [ ! -f "$PKGFILE" ] && PKGFILE="${PKGNAME}-${CP_SUFFIX}.sfs.files"
  6231.  
  6232.    #if PKG.files not found, then remove it from alien packages list
  6233.    if [ ! -f "$PKGFILE" ]; then
  6234.  
  6235.      #error "'${PKGFILE}' not found.. Cleaning up.."
  6236.  
  6237.      # backup the original list of user installed pkgs
  6238.      cp "$USER_INST_PKGS_FILE" $TMPDIR/user-installed-packages.backup
  6239.  
  6240.      # remove $PKGNAME from list of installed pkgs
  6241.      cat "$USER_INST_PKGS_FILE" 2>/dev/null | grep -v "^${PKGNAME}" > "$REPO_DIR/user-installed-packages.updated"
  6242.  
  6243.      # if we created a new file ok
  6244.      if [ -f "$REPO_DIR/user-installed-packages.updated" ]; then
  6245.        # replace the user-installed-packages file with our new one
  6246.        mv "$REPO_DIR/user-installed-packages.updated" "$USER_INST_PKGS_FILE" 2>/dev/null
  6247.      fi
  6248.  
  6249.      # clean up user-installed-packages (remove duplicates and empty lines)
  6250.      cat "$USER_INST_PKGS_FILE" | grep -v "^\$" | uniq > "$REPO_DIR/user-installed-packages.clean"
  6251.      mv "$REPO_DIR/user-installed-packages.clean" "$USER_INST_PKGS_FILE"
  6252.  
  6253.      # no *.files to process, so if not forcing full uninstall, we can exit here
  6254.      [ "$FORCE" = false ] && return 1
  6255.    fi
  6256.  
  6257.    # if we are here, we have a $REPO_DIR/***.files to work with (or using --force)
  6258.  
  6259.    # get pkgs that depend on $PKGNAME
  6260.    [ "$FORCE" = false ] && dependents="`list_dependents "$PKGNAME"`" || dependents=''
  6261.  
  6262.    # if we have dependents, we should not uninstall and just exit, unless --force was given
  6263.    if [ "$dependents" != "" -a "`echo "$dependents" | grep 'not installed'`" = '' -a "$FORCE" != true ]; then
  6264.  
  6265.      # inform user of dependent pkgs
  6266.      echo -e "${yellow}Warning${endcolour}: $PKGNAME_ONLY is needed by:  "
  6267.      echo -e "${magenta}$dependents${endcolour}"
  6268.      echo "Uninstall the packages above first, or use:"
  6269.      echo -e "${bold}pkg --force uninstall $PKGNAME${endcolour}."
  6270.      echo
  6271.      return 1
  6272.  
  6273.    fi
  6274.  
  6275.    # ask/inform user before uninstall
  6276.    echo -n "Uninstall the package ${PKGNAME}$QTAG:  "
  6277.    [ "$ASK" = true ] && read -n 1 CONFIRM </dev/tty || CONFIRM=y
  6278.    [ "$ASK" = true ] && echo -ne "\b\b\n"
  6279.  
  6280.    # if user answered yes, we will now uninstall the pkgs
  6281.    if [ "$CONFIRM" = "y" ]; then
  6282.  
  6283.      # print new line if we didnt take any user input on tty
  6284.      [ "$ASK" != true ] && echo
  6285.  
  6286.      # execute uninstall script.
  6287.      if [ -x "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.remove" ]; then
  6288.        "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.remove" &>/dev/null
  6289.        rm -f "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.remove" &>/dev/null
  6290.      fi
  6291.  
  6292.      # if we have no pkg file (listing of pkg contents), we cant cat/grep it
  6293.      if [ ! -f "$PKGFILE" ]; then
  6294.        echo "Not found: $REPO_DIR/$PKGNAME.files"
  6295.        return 1
  6296.      fi
  6297.  
  6298.      # check if has menu entry
  6299.      [ "`cat "$PKGFILE" | grep -m1 ".desktop\$"`" != "" ] && HASMENUENTRY=true || HASMENUENTRY=false
  6300.  
  6301.      # remove files listed in *.files
  6302.      cat "$PKGFILE" | while read LINE
  6303.      do
  6304.        # some symlinks may not get removed. '-e' will not work if symlink
  6305.        # is pointing to a non-existent file. So, check for symlink...
  6306.        REMFILE=""
  6307.        [ -h "$LINE" ] && REMFILE="yes"
  6308.        [ -e "$LINE" ] && REMFILE="yes"
  6309.        if [ "$REMFILE" = "yes" ]; then
  6310.          if [ ! -d "$LINE" ]; then
  6311.            if [ -e "/initrd/pup_ro2$LINE" ]; then
  6312.              # deleting the file on the top layer places a ".wh" whiteout file, that hides the original file.
  6313.              # what we want is to remove the installed file, and restore the original pristine file...
  6314.              cp -af "/initrd/pup_ro2${LINE}" "$LINE"
  6315.            else
  6316.              rm -f "$LINE" &>/dev/null
  6317.            fi
  6318.            #delete empty dirs...
  6319.            DELDIR="`dirname "$LINE" 2>/dev/null`"
  6320.            [ "`ls -1 "$DELDIR"`" = "" ] && rmdir "$DELDIR" &>/dev/null
  6321.          fi
  6322.        fi
  6323.      done
  6324.  
  6325.      # go through again and remove any empty dirs...
  6326.      cat "$PKGFILE" 2>/dev/null  | while read LINE
  6327.      do
  6328.        DELDIR="`dirname "$LINE" 2>/dev/null`"
  6329.        [ -d "$DELDIR" ] && [ "`ls -1 "$DELDIR"`" = "" ] && rmdir "$DELDIR"
  6330.        #check one level up... but do not delete top dir, like /opt...
  6331.        DELLEVELS=`echo -n "$DELDIR" | sed -e 's/[^/]//g' | wc -c | sed -e 's/ //g'`
  6332.        if [ $DELLEVELS -gt 2 ]; then
  6333.          DELDIR="`dirname "$DELDIR" 2>/dev/null`"
  6334.          [ -d "$DELDIR" ] && [ "`ls -1 "$DELDIR"`" = "" ] && rmdir $DELDIR
  6335.        fi
  6336.      done
  6337.  
  6338.      # remove $PKGNAME from user-installed-packages
  6339.      NEWUSERPKGS="$(grep -v "^${PKGNAME}" "$USER_INST_PKGS_FILE")"
  6340.      [ "$NEWUSERPKGS" != "" ] && echo "$NEWUSERPKGS" > "$USER_INST_PKGS_FILE"
  6341.  
  6342.      # clean up user-installed-packages (remove duplicates and empty lines)
  6343.      cat "$USER_INST_PKGS_FILE" | grep -v "^\$" | uniq > "$REPO_DIR/user-installed-packages_clean"
  6344.      mv "$REPO_DIR/user-installed-packages_clean" "$USER_INST_PKGS_FILE"
  6345.  
  6346.      # remove $REPO_DIR/$PKGNAME.files
  6347.      rm $PKGFILE ${PKGFILE} 2>/dev/null
  6348.  
  6349.      # do fixmenus, if menu entry found
  6350.      if [ "$HASMENUENTRY" = true ]; then
  6351.        [ ! -f /tmp/pkg/update_menus_busy ] && update_menus &
  6352.      fi
  6353.  
  6354.      # UNINSTALL DONE .. print message
  6355.      echo -e "${green}Uninstalled:${endcolour} $PKGNAME"
  6356.  
  6357.      # log uninstall with the system logs
  6358.      [ "`which logger`" != '' ] && logger "$0 Package $PKGNAME uninstalled by $APP $APPVER"
  6359.  
  6360.    fi #end if $CONFIRM=yes
  6361.  
  6362.  else # $PKGNAME is not installed
  6363.  
  6364.    # if any installed pkg matches $PKGNAME
  6365.    if [ "`list_installed_pkgs $PKGNAME`" != "" ]; then #290613
  6366.      # list the matching pkgs
  6367.      echo "These installed packages match '$PKGNAME':"
  6368.      echo "`list_installed_pkgs $PKGNAME`"
  6369.    fi
  6370.  
  6371.    return 1
  6372.  
  6373.  fi # endif installed or not
  6374. }
  6375.  
  6376.  
  6377. pkg_remove(){                     # remove pkg ($1) and its leftover deps FUNC_LIST
  6378.  
  6379.  # quit if no valid options
  6380.  [ ! "$1" -o "$1" = "-" ] && print_usage remove && exit 1
  6381.  
  6382.  local PKGNAME
  6383.  local PKGNAME_ONLY
  6384.  local PKGFILE
  6385.  local pkg_ext
  6386.  
  6387.  # get pkg extension
  6388.  pkg_ext=`get_pkg_ext "$1"`
  6389.  
  6390.  #get pkg name with version, but no extension or path
  6391.  PKGNAME="$(basename "$1" .$pkg_ext)"
  6392.  PKGNAME=`get_pkg_name "$PKGNAME"`
  6393.  
  6394.  # get pkg name only .. without versions or suffix
  6395.  PKGNAME_ONLY="`get_pkg_name_only "$PKGNAME"`"
  6396.  
  6397.  if [ "`is_installed_pkg "$PKGNAME"`" = false ]; then
  6398.    echo "Package '$1' not installed."
  6399.    return 1
  6400.  fi
  6401.  
  6402.  pkg_uninstall "$PKGNAME"
  6403.  
  6404.  # now we will remove any left over dependencies
  6405.  
  6406.  file="`find $TMPDIR -iname "${1}*_dep_list"`"
  6407.  
  6408.  if [ -f "$file" ]; then
  6409.  
  6410.    # if we have a deps list file, go over it three times, uninstalling all deps
  6411.    # that have no packages that depend on them ... fugly solution, but works ok..
  6412.  
  6413.    for x in 1 2 3 4
  6414.    do
  6415.      cat "$file" | tr ' ' '\n' | while read user_installed_dep
  6416.      do
  6417.        [ "$user_installed_dep" = '' ] && continue
  6418.        ASK=false pkg_uninstall $user_installed_dep &>/dev/null && \
  6419.          echo -e "${green}Uninstalled:${endcolour} $(get_pkg_name $user_installed_dep)"
  6420.      done
  6421.    done
  6422.  
  6423.  fi
  6424.  
  6425.  return 0
  6426. }
  6427.  
  6428.  
  6429. clean_pkgs(){                     # delete downloaded pkg files of installed pkgs FUNCLIST
  6430.  local pkg_to_rm
  6431.  [ "$ASK" = true ] && ASKOPT='--ask'
  6432.  # list all (matching) installed pkgs
  6433.  list_installed_pkgs | while read line;
  6434.  do
  6435.    # get all pkgs except the combined (user-created) pkgs
  6436.    pkg_to_rm="`list_downloaded_pkgs $line | grep -v $CP_SUFFIX`"
  6437.    # if it has a downloaded pkg in $WORKDIR, (offer to) delete it
  6438.    [ "$pkg_to_rm" != '' ] && rm -v "${WORKDIR}/$pkg_to_rm";
  6439.  done
  6440. }
  6441.  
  6442.  
  6443. # dependency funcs
  6444.  
  6445. get_deps_entry(){                 # $1 is PKGNAME, returns dep entry from repo db
  6446.  [ "$1" = '' -o "$1" = "-" ] && print_usage list-deps && exit 1
  6447.  
  6448.  # get current $REPOFILE, $DEPSEARCH
  6449.  #. ${PKGRC}
  6450.  
  6451.  local PKGNAME="$(get_pkg_name "$1")"
  6452.  #local PKGNAME_GUESS="$1"
  6453.  local pkg_ext=''
  6454.  local repo_files=''
  6455.  local deps_list=''
  6456.  local deps=''
  6457.  local repo_of_pkg=''
  6458.  local repo_file_of_pkg=''
  6459.  
  6460.  # get pkg extension
  6461.  pkg_ext=`get_pkg_ext "$1"`
  6462.  [ "$PKG_DEBUG" = "true" ] && echo "get_deps_entry(): PKGNAME=$PKGNAME" >&2
  6463.  [ "$PKG_DEBUG" = "true" ] && set -x
  6464.  
  6465.  # get repo file to search from RC file
  6466.  repo_file_of_pkg="$REPOFILE"
  6467.  
  6468.  # if searching dependencies in all repos
  6469.  if [ "$DEPSEARCH" = "list_all_pkg_names" ]; then
  6470.  
  6471.    # get the repo that $PKGNAME lives in
  6472.    repo_of_pkg=`which_repo "$PKGNAME" | cut -f2 -d' ' | head -1`
  6473.    # then get the repo file for that repo.. that is where this pkg lists its deps
  6474.    repo_file_of_pkg=`grep -m1 ^"$repo_of_pkg" ~/.pkg/sources-all | cut -f3 -d'|'`
  6475.  
  6476.  fi
  6477.  
  6478.  # add the full path to the repo file
  6479.  repo_file_of_pkg="$REPO_DB_FILE_DIR/$repo_file_of_pkg"
  6480.  
  6481.  
  6482.  # search for deps entry in repo file.. look for ^pkgname|
  6483.  deps_list="$(LANG=C grep -m1 "^${PKGNAME}|" $repo_file_of_pkg 2>/dev/null)"
  6484.  
  6485.  # try again if needed.. look for |pkgname.$pkg_ext|
  6486.  [ -z "$deps_list" ] && [ "$PKGNAME" != '' ] && deps_list="$(LANG=C grep -m1 "|${PKGNAME}.$pkg_ext|" $repo_file_of_pkg 2>/dev/null)"
  6487.  
  6488.  # try again if needed.. look for |pkgname|
  6489.  [ -z "$deps_list" ] && [ "$PKGNAME" != '' ] && deps_list="$(LANG=C grep -m1 "|${PKGNAME}|" $repo_file_of_pkg 2>/dev/null)"
  6490.  
  6491.  # try again if needed.. look for |pkgfilename|
  6492.  [ -z "$deps_list" ] && [ "$PKGNAME" != '' ] && deps_list="$(LANG=C grep -m1 "|$(get_pkg_filename ${PKGNAME})|" $repo_file_of_pkg 2>/dev/null | head -1 | grep -m1 ^$PKGNAME)"
  6493.  
  6494.  # if no deps, exit with error
  6495.  [ -z "$deps_list" ] && return 1
  6496.  
  6497.  if [ ! -z "$deps_list" ];then
  6498.    echo "$deps_list"  \
  6499.      | cut -f9 -d'|'  \
  6500.      | grep '+'       \
  6501.      | sed            \
  6502.        -e "s/:any//g" \
  6503.        -e 's/&[geql][eqt][^,]*//g' \
  6504.        -e 's/,$//' \
  6505.      | grep -vE '/ge|/le'
  6506.  fi
  6507.  [ "$PKG_DEBUG" = "true" ] && set +x
  6508.  #[ "$PKG_DEBUG" = "true" ] && read -p "Press enter to continue"
  6509. }
  6510. list_all_installed_pkgs_old(){        # list inc builtins if HIDE_BUILTINS=false
  6511.  # reset list of installed pkgs
  6512.  echo -n '' > $TMPDIR/installed_pkgs
  6513.  
  6514.  #if [ "${HIDE_INSTALLED}" = true -a "$FORCE" = false ]; then
  6515.  #  # add user installed pkgs to list of pkgs to remove from final output
  6516.    cut -f2 -d'|' "$USER_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6517.  #fi
  6518.  
  6519.  #if [ "${HIDE_BUILTINS}" = true ]; then
  6520.  #  # add builtins to list of pkgs to remove from final output
  6521.    cut -f2 -d'|' "$WOOF_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6522.  #fi
  6523.  
  6524.  #if [ -f "$DEVX_INST_PKGS_FILE"  ]; then
  6525.  #  # add devx pkgs to list of pkgs to remove from final output
  6526.    cut -f2 -d'|' "$DEVX_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6527.  #fi
  6528.  
  6529.  #if [ -f "$LAYER_INST_PKGS_FILE" ]; then
  6530.  #  # add layers list of pkgs to remove from final output
  6531.    cut -f2 -d'|' "$LAYER_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6532.  #fi
  6533.  
  6534.  if [ -f $TMPDIR/installed_pkgs -a ! -z $TMPDIR/installed_pkgs ]; then
  6535.    sort -u $TMPDIR/installed_pkgs | grep -v ^$ > $TMPDIR/installed_pkgs__sorted
  6536.    mv $TMPDIR/installed_pkgs__sorted $TMPDIR/installed_pkgs
  6537.  fi
  6538. }
  6539.  
  6540. list_all_installed_pkgs(){        # list inc builtins if HIDE_BUILTINS=false
  6541.  # reset list of installed pkgs
  6542.  [ "$PKG_DEBUG" = "true" ] && echo "entering: list_all_installed_pkgs()" >&2
  6543.  [ "$PKG_DEBUG" = "true" ] && set -x
  6544.  echo -n '' > $TMPDIR/installed_pkgs
  6545.  
  6546.  if [ "${HIDE_INSTALLED}" != true -o -z "${HIDE_INSTALLED}" ] && [ "$FORCE" != false ]; then
  6547.  #  # add user installed pkgs to list of pkgs to remove from final output
  6548.    cut -f2 -d'|' "$USER_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6549.  fi
  6550.  
  6551.  if [ "${HIDE_BUILTINS}" != true -o -z "${HIDE_INSTALLED}" ]; then
  6552.  #  # add builtins to list of pkgs to remove from final output
  6553.    cut -f2 -d'|' "$WOOF_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6554.  fi
  6555.  
  6556.  if [ -f "$DEVX_INST_PKGS_FILE"  ]; then
  6557.  #  # add devx pkgs to list of pkgs to remove from final output
  6558.    cut -f2 -d'|' "$DEVX_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6559.  fi
  6560.  
  6561.  if [ -f "$LAYER_INST_PKGS_FILE" ]; then
  6562.  #  # add layers list of pkgs to remove from final output
  6563.    cut -f2 -d'|' "$LAYER_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6564.  fi
  6565.  
  6566.  if [ -f $TMPDIR/installed_pkgs -a ! -z $TMPDIR/installed_pkgs ]; then
  6567.    sort -u $TMPDIR/installed_pkgs | grep -v ^$ > $TMPDIR/installed_pkgs__sorted
  6568.    mv $TMPDIR/installed_pkgs__sorted $TMPDIR/installed_pkgs
  6569.  fi
  6570.  ln -s $TMPDIR/installed_pkgs $TMPDIR/installed_pkgs_finished
  6571.  [ "$PKG_DEBUG" = "true" ] && set +x
  6572.  [ "$PKG_DEBUG" = "true" ] && echo "exiting: list_all_installed_pkgs()" >&2
  6573. }
  6574.  
  6575. list_all_installed_pkgs_rev_logic(){        # list inc builtins if HIDE_BUILTINS=false
  6576.  # reset list of installed pkgs
  6577.  [ "$PKG_DEBUG" = "true" ] && echo "entering: list_all_installed_pkgs_rev_logic()" >&2
  6578.  [ "$PKG_DEBUG" = "true" ] && set -x
  6579.  echo -n '' > $TMPDIR/installed_pkgs
  6580.  
  6581.  if [ "${HIDE_INSTALLED}" = true -o -z "${HIDE_INSTALLED}" ] && [ "$FORCE" != true ]; then
  6582.  #  # add user installed pkgs to list of pkgs to remove from final output
  6583.    cut -f2 -d'|' "$USER_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6584.  fi
  6585.  
  6586.  if [ "${HIDE_BUILTINS}" = true -o -z "${HIDE_BUILTINS}" ]; then
  6587.  #  # add builtins to list of pkgs to remove from final output
  6588.    cut -f2 -d'|' "$WOOF_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6589.  fi
  6590.  
  6591.  if [ -f "$DEVX_INST_PKGS_FILE"  ]; then
  6592.  #  # add devx pkgs to list of pkgs to remove from final output
  6593.    cut -f2 -d'|' "$DEVX_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6594.  fi
  6595.  
  6596.  if [ -f "$LAYER_INST_PKGS_FILE" ]; then
  6597.  #  # add layers list of pkgs to remove from final output
  6598.    cut -f2 -d'|' "$LAYER_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6599.  fi
  6600.  
  6601.  if [ -f $TMPDIR/installed_pkgs -a ! -z $TMPDIR/installed_pkgs ]; then
  6602.    sort -u $TMPDIR/installed_pkgs | grep -v ^$ > $TMPDIR/installed_pkgs__sorted
  6603.    mv $TMPDIR/installed_pkgs__sorted $TMPDIR/installed_pkgs
  6604.  fi
  6605.  ln -s $TMPDIR/installed_pkgs $TMPDIR/installed_pkgs_finished
  6606.  [ "$PKG_DEBUG" = "true" ] && set +x
  6607.  [ "$PKG_DEBUG" = "true" ] && echo "exiting: list_all_installed_pkgs_rev_logic()" >&2
  6608. }
  6609.  
  6610. has_deps(){                       # return true if $1 has deps, else false
  6611.  [ "$1" = '' -o "$1" = "-" -o ! "$1" ] && echo false
  6612.  [ "`get_deps_entry $1`" != '' ] && echo true || echo false
  6613. }
  6614.  
  6615.  
  6616. list_deps(){                      # list all deps of PKG ($1), space separated on one line FUNCLIST
  6617.   [ "$PKG_DEBUG" = "true" ] && echo "entering list_deps()" >&2
  6618.  [ "$PKG_DEBUG" = "true" ] && set -x
  6619.  list_deps_count=$(( list_deps_count + 1 ))
  6620.  [ "$1" = '' ] || [ "$1" = "-" ] && print_usage list-deps && exit 1
  6621.  if [ -z "$2" ] || [ $2 != "--rev-logic" ]; then
  6622.    list_all_installed_pkgs_fn="list_all_installed_pkgs"
  6623.  else
  6624.    list_all_installed_pkgs_fn="list_all_installed_pkgs_rev_logic"
  6625.  fi
  6626.  echo true > ${TMPDIR}/list_deps_busy
  6627.  
  6628.  # get current $REPOFILE, $DEPSEARCH
  6629.  #. ${PKGRC}
  6630. [ "$PKG_DEBUG" = "true" ] && set +x
  6631.  local PKGNAME=''
  6632.  local PKGNAME_ONLY=''
  6633.  local pkg_ext=''
  6634.  local repo_files=''
  6635.  local deps_list=''
  6636.  local deps=''
  6637.  local repo_of_pkg=''
  6638.  local repo_file_of_pkg=''
  6639.  dep_id=$((dep_id +1))
  6640.  
  6641.  
  6642.  # get pkg extension
  6643.  pkg_ext=$(get_pkg_ext "$1")
  6644.  
  6645.  #pkg name only ,no path, no extension
  6646.  PKGNAME=$(get_pkg_name "$1")
  6647.  PKGNAME_ONLY=$(get_pkg_name_only "$PKGNAME")
  6648.  echo true > /tmp/pkg/list_deps_busy_"$PKGNAME"  
  6649.  echo true > ${TMPDIR}/list_deps_busy_"$PKGNAME_ONLY"
  6650.  echo true > ${TMPDIR}/list_deps_busy_"$PKGNAME"    
  6651.  local ldepID="$PKGNAME_ONLY"
  6652.  [ "$PKG_DEBUG" = "true" ] && echo "list_deps(): PKGNAME=$PKGNAME" >&2
  6653.  [ "$PKG_DEBUG" = "true" ] && echo "list_deps(): PKGNAME_ONLY=$PKGNAME_ONLY" >&2  
  6654.  [ "$PKG_DEBUG" = "true" ] && set -x
  6655.  
  6656.  
  6657.  # get the deps of the pkg, note, in the repo deps are NOT listed by proper pkg names, .. they are +dbus,+glib,+SDL
  6658.  # search for deps entry in repo file.. look for |pkgname.ext|
  6659.  deps_list="$(LANG=C get_deps_entry "$PKGNAME")"
  6660.  [ "$PKG_DEBUG" = "true" ] && echo "list_deps(): deps_list=$deps_list" >&2  
  6661.  [ "$PKG_DEBUG" = "true" ] && set -x
  6662.  # if no deps, exit with error
  6663.  [ "$deps_list" = "" ] && rm ${TMPDIR}/list_deps_busy 2>/dev/null && return 1
  6664.  
  6665.  # remove the '+' from the start of each dep
  6666.  deps="${deps_list/+/}"           # remove first + at start of line
  6667.  deps="${deps//,+/ }"             # remove others .. DEPS will now be just 'dep1 dep2 dep3'
  6668.  deps="${deps//  / }"             # remove double spaces
  6669.  deps="${deps//.pet/}"            # remove extensions (creeping in from somewhere!)
  6670.  deps="${deps//.${EX:-noextension}/}"      # remove extensions (creeping in from somewhere!)
  6671.  deps="${deps//.${pkg_ext:-noextension}/}" # remove extensions (creeping in from somewhere!)
  6672.  
  6673.  
  6674.  if [ "$deps" != '' ] && [ "$deps" != ' ' ];then
  6675.  
  6676.    # put all deps of pkg in a tmp file
  6677.    echo "${deps// /,}" | tr ',' '\n' | grep -v ^$ | sort -u > ${TMPDIR}/all_deps_${ldepID}_0
  6678.  
  6679.    [ "$PKG_DEBUG" = "true" ] && set +x
  6680.    # create list of installed pkgs ($TMPDIR/installed_pkgs)
  6681.    $list_all_installed_pkgs_fn
  6682.    [ "$PKG_DEBUG" = "true" ] && set -x
  6683.    
  6684.    grep -vE "'$PKG_BLACKLIST_REGEX'" ${TMPDIR}/all_deps_${ldepID}_0 2>/dev/null | sort -u > ${TMPDIR}/all_deps_${ldepID}_1_without_blacklisted_packages
  6685.    mv ${TMPDIR}/all_deps_${ldepID}_1_without_blacklisted_packages ${TMPDIR}/all_deps_${ldepID}_0
  6686.  
  6687.    # remove any deps in installed pkgs list from all deps.. to leave only missing deps
  6688.    comm -23 ${TMPDIR}/all_deps_${ldepID}_0 ${TMPDIR}/installed_pkgs 2>/dev/null | grep -v ^$ | sort -u > ${TMPDIR}/all_deps_${ldepID}_1
  6689.  
  6690.    rm -f ${TMPDIR}/DEP_DONE 2>/dev/null
  6691.  
  6692.    if [ -f ${TMPDIR}/all_deps_${ldepID}_1 ] && [ -s ${TMPDIR}/all_deps_${ldepID}_1 ];then
  6693.      # recursive search deps of deps
  6694.      for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  6695.      do
  6696.        deps_list_file="${TMPDIR}/all_deps_${ldepID}_${i}"
  6697.  
  6698.        [ ! -f $deps_list_file ] && continue
  6699.  
  6700.        # remove all blacklisted packages from the list
  6701.        grep -vE "'$PKG_BLACKLIST_REGEX'" "$deps_list_file" > ${TMPDIR}/deps_list_file_without_blacklisted_pkgs
  6702.        mv ${TMPDIR}/deps_list_file_without_blacklisted_pkgs  $deps_list_file
  6703.  
  6704.        # remove done packages from the list
  6705.        if [ -s ${TMPDIR}/DEP_DONE ] || [ -f ${TMPDIR}/DEP_DONE ];then
  6706.          comm -23 $deps_list_file ${TMPDIR}/DEP_DONE 2>/dev/null | grep -v ^$ | sort -u > ${deps_list_file}_cleaned
  6707.          mv ${deps_list_file}_cleaned ${deps_list_file}
  6708.        fi
  6709.  
  6710.        if [ -s ${TMPDIR}/installed_pkgs ] || [ -f ${TMPDIR}/DEP_DONE ];then
  6711.          # remove any deps in installed pkgs list from all deps.. to leave only missing deps
  6712.          comm -23 $deps_list_file ${TMPDIR}/installed_pkgs 2>/dev/null | grep -v ^$ | sort -u > ${deps_list_file}_cleaned
  6713.          mv ${deps_list_file}_cleaned ${deps_list_file}
  6714.        fi
  6715.        [ ! -s "$deps_list_file" ] && break #Break if the file is empty
  6716.        next_deps_list_file="${TMPDIR}/all_deps_${ldepID}_$(($i + 1))"
  6717.  
  6718.        # for each dep in $deps, get their deps too
  6719.        for subdep in $(sort -u $deps_list_file | grep -v ^$ | sed -e "s/,$//g")
  6720.        do
  6721.          [ "$subdep" = '' ] || [ "$subdep" = ' ' ] && continue
  6722.  
  6723.          grep -q -m1 "$subdep" ${TMPDIR}/DEP_DONE 2>/dev/null && continue
  6724.  
  6725.          local subdeps_entry=''
  6726.          local subdeps=''
  6727.          local subdeps_list=''
  6728.  
  6729.          subdeps_entry="$(get_deps_entry "$subdep")"
  6730.          subdeps="${subdeps_entry/+/}"           # remove first + at start of line
  6731.          subdeps="${subdeps//,+/ }"              # remove others .. DEPS will now be just 'dep1 dep2 dep3'
  6732.          subdeps="${subdeps//  / }"              # remove double spaces
  6733.          subdeps="${subdeps//.pet/}"             # remove extensions (creeping in from somewhere!)
  6734.          subdeps="${subdeps//.${EX:-noextension}/}"       # remove extensions (creeping in from somewhere!)
  6735.          subdeps="${subdeps//.${pkg_ext:-noextension}/}"  # remove extensions (creeping in from somewhere!)
  6736.  
  6737.          if [ "$subdeps" != '' ] && [ "$subdeps" != ' ' ];then
  6738.            # remove everything after the + (if the + is followed by alphanumeric chars), then add to tmp files
  6739.            echo "$subdep" >> ${TMPDIR}/DEP_DONE
  6740.  
  6741.            # create the next deps list file to parse, containing the deps of this $subdep
  6742.            subdeps_list="$(echo "${subdeps}" | tr ' ' '\n' | grep -v ^$ | sort -u)"
  6743.            echo "$subdeps_list" >> $next_deps_list_file
  6744.          fi
  6745.            [ "$PKG_DEBUG" = "true" ] && set +x
  6746.            #[ "$PKG_DEBUG" = "true" ] && read -p "Press enter to continue"
  6747.            [ "$PKG_DEBUG" = "true" ] && set -x  
  6748.        done
  6749.  
  6750.      done
  6751.  
  6752.      # add all deps together, sorted, duplicated removed
  6753.      cat ${TMPDIR}/all_deps_${ldepID}_* 2>/dev/null | sort -u | grep -v ^$ > ${TMPDIR}/all_deps_${ldepID}_sorted
  6754.      mv ${TMPDIR}/all_deps_${ldepID}_sorted ${TMPDIR}/all_deps_${ldepID}
  6755.  
  6756.    fi
  6757.  
  6758.  fi
  6759.  
  6760.  [ "$PKG_DEBUG" = "true" ] && echo "list_deps: remove any deps in installed pkgs list from all deps.. to leave only missing deps" >&2
  6761.  [ "$PKG_DEBUG" = "true" ] && set -x  
  6762.  [ "$PKG_DEBUG" = "true" ] && echo "all_deps_${ldepID}=$(cat ${TMPDIR}/all_deps_${ldepID})" >&2
  6763.  # remove any deps in installed pkgs list from all deps.. to leave only missing deps
  6764.  #if [ "$FORCE" != "true" ]; then
  6765.    if [ -f ${TMPDIR}/installed_pkgs ] && [ -s ${TMPDIR}/installed_pkgs ] ;then
  6766.      comm -23 ${TMPDIR}/all_deps_${ldepID} ${TMPDIR}/installed_pkgs 2>/dev/null | sort -u | grep -v ^$ > ${TMPDIR}/missing_deps
  6767.    else #This should never happen
  6768.      echo "list_deps(): ${yellow}Warning:${endcolour} missing ${TMPDIR}/installed_pkgs" >&2
  6769.      cat ${TMPDIR}/all_deps_${ldepID} 2>/dev/null | sort -u | grep -v ^$ > ${TMPDIR}/missing_deps
  6770.    fi
  6771.  #fi
  6772.  # remove all blacklisted packages from the list
  6773.  grep -vE "'$PKG_BLACKLIST_REGEX'" ${TMPDIR}/missing_deps 2>/dev/null | sort -u > ${TMPDIR}/missing_deps_without_blacklisted_pkgs
  6774.  mv ${TMPDIR}/missing_deps_without_blacklisted_pkgs ${TMPDIR}/missing_deps
  6775.  
  6776.  #if [ ! -f ${TMPDIR}/missing_deps ] || [ ! -s ${TMPDIR}/missing_deps ];then
  6777.  #  rm ${TMPDIR}/list_deps_busy 2>/dev/null
  6778.  #  return 1
  6779.  #fi
  6780.  
  6781.  # get fixed deps list
  6782.  deps="$(LANG=C sort -u ${TMPDIR}/missing_deps 2>/dev/null | tr ' ' '\n' | while read dep
  6783.  do
  6784.    get_pkg_name_only "$dep"
  6785.  done)"
  6786.  
  6787.  [ "$deps" != "" ] && echo "$deps" | sed -e 's/^ //g' | tr ' ' '\n' | sort -u | tr '\n' ' '
  6788.  
  6789.  # clean up
  6790.  [ "$PKG_DEBUG" = "true" ] && set +x
  6791.  #[ "$PKG_DEBUG" = "true" ] && read -p "Press enter to continue"
  6792.  [ "$PKG_DEBUG" = "true" ] && set -x  
  6793.  #[ "$PKG_DEBUG" = "true" ] && read -p "Press enter to continue"
  6794.  
  6795.  
  6796.  rm ${TMPDIR}/missing_dep* ${TMPDIR}/installed_pkg* ${TMPDIR}/all_dep_${ldepID}* ${TMPDIR}/DEP_DONE ${TMPDIR}/list_deps_busy 2>/dev/null
  6797.  rm /${TMPDIR}/list_deps_busy_"$PKGNAME"  2>/dev/null
  6798.  rm > ${TMPDIR}/list_deps_busy_"$PKGNAME_ONLY" 2>/dev/null
  6799.  rm > ${TMPDIR}/list_deps_busy_"$PKGNAME"    2>/dev/null
  6800.  [ "$PKG_DEBUG" = "true" ] && set +x
  6801.  [ "$PKG_DEBUG" = "true" ] && echo "exiting list_deps()" >&2
  6802. }
  6803.  
  6804.  
  6805. find_deps(){                      # given a package name ($1), makes 2 lists: DEPS_INSTALLED and DEPS_MISSING FUNCLIST
  6806.  
  6807.  # This function takes 1 argument: PKGNAME
  6808.  #
  6809.  # PKGNAME should be the name of a package in one of your repos,
  6810.  # such as 'vlc' or 'hardinfo'.
  6811.  #
  6812.  # This function creates 2 files: $TMPDIR/deps_missing and $TMPDIR/deps_installed.
  6813.  # Each file is list the dependencies for PKGNAME, each dependency on a new line.
  6814.  # get_deps() can then go through those files and download/install the missing deps.
  6815.  #
  6816.  # If --force was given, all deps will be treated as missing.
  6817.  
  6818.  # get current repo ($REPOFILE)
  6819.  . ${PKGRC}
  6820.  
  6821.  local PKGNAME=''            # the pkg given ($1), may be full name, or generic name (no version), etc
  6822.  local PKGNAME_ONLY=''       # the pkg given ($1), without version (vlc,htop,etc)
  6823.  local pkg_ext=''            # the pkg extension, blank if not a valid extension or none given
  6824.  local repo_files=''         # the list of repo files to check, may be current only or all
  6825.  local deps_list=''          # deps of PKGNAME in comma-delimited format: dep1,dep2,dep3
  6826.  local deps_on_new_lines=''  # as above, but each dep on a new line
  6827.  local deps_missing=''       # comma separated list of missing deps
  6828.  local deps_installed=''     # comma separated list of deps already installed
  6829.  local dep=''                # dep name scraped from deps_list, usually the same as dep_name_only
  6830.  local dep_match=''          # used to find matches in the repo for $dep
  6831.  local dep_name_only=''      # short (generic) pkg name, no version (vlc,htop,etc)
  6832.  local dep_full_name=''      # pkg name with version (vlc-2.3.3-i686_s700, etc)
  6833.  local loading_indicator     # blank if only a few deps to parse, or .
  6834.  dep_id=$((dep_id +1))
  6835.  local ldepID="${dep_id}"
  6836.  
  6837.  # get pkg extension
  6838.  pkg_ext=`get_pkg_ext "$1"`
  6839.  
  6840.  # pkg name with version, but no path, no extension
  6841.  PKGNAME="$(basename "$1" .$pkg_ext)"
  6842.  
  6843.  # we can't rely on the user input, try to get the right pkg names
  6844.  PKGNAME=`get_pkg_name "$PKGNAME"`       # vlc -> 'vlc-2.3-blah_etc
  6845.  PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`   # vlc-2.3-blah_etc -> vlc
  6846.  
  6847.  # if PKGNAME still empty, we cant find its deps, move on
  6848.  [ -z "$PKGNAME" ] && return 1
  6849.  
  6850.  # didn't exit yet, so remove the old tmp dirs
  6851.  rm $TMPDIR/deps_installed &>/dev/null
  6852.  rm $TMPDIR/deps_missing   &>/dev/null
  6853.  rm $TMPDIR/deps_missing1  &>/dev/null
  6854.  
  6855.  # loop through all repo files, or current repo only, depending on PKGSEARCH in RC file
  6856.  # add the full path to the file(s) while we are getting the list of repo files
  6857.  [ "$DEPSEARCH" = "list_all_pkg_names" ] && repo_files="`repo_file_list | sed -e "s|^|$REPO_DIR/|g" | tr '\n' ' '`" || repo_files="$REPO_DB_FILE_DIR/$REPOFILE"
  6858.  [ "$PKG_DEBUG" = "true" ] && echo "find_deps(): PKGNAME=${PKGNAME}" >&2  
  6859.  [ "$PKG_DEBUG" = "true" ] && echo "find_deps(): PKGNAME_ONLY=${PKGNAME_ONLY}" >&2        
  6860.  [ "$PKG_DEBUG" = "true" ] && set -x
  6861.  # get the list of deps from the repo entry of this pkg
  6862.  deps_list="`LANG=C list_deps "$PKGNAME"`"
  6863.  
  6864.  # remove builtins from list unless HIDE_BUILTINS=false
  6865.  if [ "${HIDE_BUILTINS}" = true ]; then
  6866.    echo "$deps_list" | tr ' ' '\n'  | grep -v ^$ > ${TMPDIR}/all_deps_${ldepID}
  6867.    # add woof pkgs to list of pkgs to remove from final output
  6868.    cut -f2 -d'|' "$WOOF_INST_PKGS_FILE" | grep -v ^$ | sort | uniq >> $TMPDIR/installed_pkgs
  6869.    # remove any deps in installed pkgs list from all deps.. to leave only missing deps
  6870.  
  6871.    sort -u ${TMPDIR}/all_deps_${ldepID} > ${TMPDIR}/all_deps_${ldepID}_sorted
  6872.    mv ${TMPDIR}/all_deps_${ldepID}_sorted ${TMPDIR}/all_deps_${ldepID}
  6873.  
  6874.    sort -u ${TMPDIR}/installed_pkgs > ${TMPDIR}/installed_pkgs_sorted
  6875.    mv ${TMPDIR}/installed_pkgs_sorted ${TMPDIR}/installed_pkgs
  6876.  
  6877.    comm -23 ${TMPDIR}/all_deps_${ldepID} ${TMPDIR}/installed_pkgs | grep -v ^$ > ${TMPDIR}/missing_deps
  6878.    deps_list="`cat "${TMPDIR}/missing_deps" 2>/dev/null | grep -v ^$ | sort | tr '\n' ' ' `"
  6879.  fi
  6880.  
  6881.  # so now, $deps_list='dep1 dep2 dep3'
  6882.  
  6883.  # now get the deps list with each dep on its own line
  6884.  deps_on_new_lines="`echo "$deps_list" | tr ' ' '\n'`"
  6885.  
  6886.  # exit if no deps to parse
  6887.  [ "$deps_on_new_lines" = '' ] && return 1
  6888.  
  6889.  # set a loading bar (appending dots....) if we have numerous deps to search
  6890.  [ `echo "$deps_on_new_lines" | wc -l` -gt 4 ] && loading_indicator='.' || loading_indicator=''
  6891.  
  6892.  # now.. we go through ALL deps listed, and create a list for installed or not .. $dep will be 'gtkdialog3', for example
  6893.  [ "$PKG_DEBUG" = "true" ] && set +x
  6894.  echo "$deps_on_new_lines" | grep -v ^$ | while read dep
  6895.  do
  6896.    # append dots as we go, like a loading spinner
  6897.    [ "$loading_indicator" = '.' ] && echo -n "$loading_indicator"
  6898.    
  6899.    # $dep is currently
  6900.    dep_full_name=`get_pkg_name "$dep"`     #vlc-2.3.3-i586_s700
  6901.    dep_name_only=`get_pkg_name_only "$dep"`  #vlc
  6902.    [ "$PKG_DEBUG" = "true" ] && echo "find_deps(): dep=${dep}" >&2  
  6903.    [ "$PKG_DEBUG" = "true" ] && echo "find_deps(): dep_full_name=${dep_full_name}" >&2
  6904.    [ "$PKG_DEBUG" = "true" ] && echo "find_deps(): dep_name_only=${dep_name_only}" >&2
  6905.    [ "$PKG_DEBUG" = "true" ] && set -x
  6906.    # skip these non pkgs
  6907.    [ "`is_repo_pkg "$dep_name_only"`" = false ] && continue
  6908.  
  6909.    # if we added this dep to the list of PKGs already done, skip it
  6910.    [ "`grep -m1 "^$dep_name_only\$" $TMPDIR/PKGSDONE 2>/dev/null`" != '' ] && continue
  6911.  
  6912.    # lets check if the $dep is installed or not
  6913.  
  6914.    # if dep was found in a repo and not installed, add to missing deps list
  6915.    if [ "$FORCE"  = true -o "`is_installed_pkg "$dep_full_name"`" = false ]; then
  6916.      grep -m1 "^$dep_name_only\$" $TMPDIR/deps_missing 2>/dev/null || echo "$dep_name_only" | grep -v "^$PKGNAME" >> $TMPDIR/deps_missing
  6917.  
  6918.    else
  6919.      # else if the dep is already installed, add to installed deps list
  6920.      grep -m1 "^$dep_name_only\$" $TMPDIR/deps_installed 2>/dev/null || echo "$dep_name_only" | grep -v "^$PKGNAME" >> $TMPDIR/deps_installed
  6921.    fi
  6922.  
  6923.    # clean up deps_installed, remove duplicates, etc
  6924.    if [ -f $TMPDIR/deps_installed ]; then
  6925.      cat $TMPDIR/deps_installed 2>/dev/null| sort | uniq >> $TMPDIR/deps_installed1
  6926.      [ -f $TMPDIR/deps_installed1 ] && mv $TMPDIR/deps_installed1 $TMPDIR/deps_installed 2>/dev/null
  6927.    fi
  6928.    [ "$PKG_DEBUG" = "true" ] && set +x
  6929.  done # end of while $dep
  6930.  [ "$PKG_DEBUG" = "true" ] && set -x
  6931.  # make a comma separated list from newlines
  6932.  deps_installed="`cat $TMPDIR/deps_installed 2>/dev/null | sort | uniq | tr '\n' ',' | sed -e 's/,,/,/' -e 's/,$//' -e 's/^,//'`"
  6933.  deps_missing="`cat $TMPDIR/deps_missing 2>/dev/null | sort | uniq | tr '\n' ',' | sed -e 's/,,/,/' -e 's/,$//' -e 's/^,//'`"
  6934.  
  6935.  #120213, fixed, force all deps to be in the download list, if --force was given
  6936.  if [ "$FORCE" = true ]; then
  6937.    # dont skip installed deps (except builtins.. handled elsewhere)
  6938.    if [ "$deps_installed" != "" ]; then
  6939.      deps_missing="${deps_installed},${deps_missing}"
  6940.      deps_missing="`echo "${deps_missing//,,/}" | sed -e 's/,$//' -e 's/^,//'`"
  6941.    fi
  6942.  fi
  6943.  
  6944.  # later, get_deps() will use $DEPS_MISSING and $DEPS_INSTALLED
  6945.  DEPS_MISSING="$deps_missing"
  6946.  DEPS_INSTALLED="$deps_installed"
  6947.  
  6948.  # end appending dots... msg, by printing a new line
  6949.  [ "$loading_indicator" != '' ] && echo
  6950.  [ "$PKG_DEBUG" = "true" ] && set +x
  6951. }
  6952.  
  6953.  
  6954. get_deps(){                       # find, get and install the deps of pkgname ($1) FUNCLIST
  6955.  
  6956.  [ -z "$1" -o ! "$1" -o "$1" = "-" ] && print_usage deps && exit 1
  6957.  
  6958.  . ${PKGRC} #150813
  6959.  
  6960.  local EX
  6961.  local PKGNAME
  6962.  local DEPCONFIRM
  6963.  
  6964.  # get pkg extension
  6965.  local EX="$(get_pkg_ext "$1")"
  6966.  
  6967.  # get pkg name with version, but no path, no extension
  6968.  local PKGNAME_GUESS="$(LANG=C basename "$1" .$EX)"
  6969.  
  6970.  # don't rely on user input, get the name w version from repos
  6971.  local PKGNAME=`get_pkg_name "$PKGNAME_GUESS" 2>/dev/null`
  6972.  local PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME" 2>/dev/null`
  6973.  
  6974.  local pkg_is_builtin=`is_builtin_pkg "$PKGNAME_ONLY" 2>/dev/null`
  6975.  local pkg_already_done=`LANG=C grep -m1 "^$PKGNAME_ONLY\$" $TMPDIR/PKGSDONE 2>/dev/null`
  6976.  
  6977.  local pkg_is_blacklisted=`is_blacklisted_pkg "$PKG_NAME_ONLY" 2>/dev/null`
  6978.  
  6979.  [ "$PKG_DEBUG" = "true" ] && echo "get_deps(): PKGNAME=${PKGNAME}" >&2  
  6980.  [ "$PKG_DEBUG" = "true" ] && echo "get_deps(): PKGNAME_ONLY=${PKGNAME_ONLY}" >&2          
  6981.  [ "$PKG_DEBUG" = "true" ] && set -x
  6982.  
  6983.  # if pkg is builtin, skip it, we dont need to get its dependencies
  6984.  [ "$pkg_is_builtin" = true -a "$HIDE_BUILTINS" = true ] && continue
  6985.  
  6986.  # if pkg is blacklisted, also skip it
  6987.  [ "$pkg_is_blacklisted" = true ] && continue
  6988.  
  6989.  # skip if already processed, it should be listed in $TMPDIR/PKGSDONE
  6990.  [ "$pkg_already_done" != "" -a "$FORCE" = false ] && continue
  6991.  
  6992.  echo -n "Resolving dependencies.." # find deps will append ... as it goes
  6993.  
  6994.  # wait until list_deps() is finished (if it's running at all...)
  6995.  while [ -f "${TMPDIR}/list_deps_busy" ] || \
  6996.        [ -f "${TMPDIR}/list_deps_busy_${PKGNAME}" ] || \
  6997.        [ -f "${TMPDIR}/list_deps_busy_${PKGNAME_ONLY}" ];do #In a future release we'll try removing some of these test condtions
  6998.    echo -n '.'
  6999.    # ${PKGNAME}_dep_list is created in "pkg_get()"
  7000.    # the function "list_names()" instead puts PKGNAME at the end of the filename
  7001.    if [ -f "${TMPDIR}/${PKGNAME}_dep_list" ] ||
  7002.       [ -f "${TMPDIR}/${PKGNAME_ONLY}_dep_list" ]]; then
  7003.       break
  7004.    fi
  7005.    sleep 0.75
  7006.  done
  7007.  
  7008.  echo
  7009.  
  7010.  # if list_deps() created a file listing the deps, get it from the file created, else, run list deps to be sure
  7011.  [ -f ${TMPDIR}/${pkg_name}_dep_list ] && DEPS_MISSING="`cat ${TMPDIR}/${PKGNAME}_dep_list 2>/dev/null`" || DEPS_MISSING="`list_deps "$PKGNAME" 2>/dev/null`"
  7012.  
  7013.  # if we have missing deps, or are downloading them all regardless (INSTALLDEPS=false), or using --force
  7014.  if [ "$DEPS_MISSING" != "" -o "${NO_INSTALL}" = true -o "$FORCE" = true ]; then
  7015.  
  7016.    # ask to download (and maybe install) the deps
  7017.    DEPCONFIRM=y
  7018.    if [ "$ASK" = true ]; then
  7019.      echo "Missing deps: $DEPS_MISSING"
  7020.      echo -n "Download the missing dependencies$QTAG:  "
  7021.      read -n 1 DEPCONFIRM </dev/tty
  7022.      echo
  7023.      # skip if user chose --ask and didn't answer 'y'
  7024.      [ "$DEPCONFIRM" != "y" ] && return
  7025.    fi
  7026.  
  7027.    # if user answered yes, we will now download (and maybe install) the deps
  7028.    if [ "$DEPCONFIRM" = "y" -o "$FORCE" = true ]; then
  7029.  
  7030.      # only ask once
  7031.      ASK=false
  7032.  
  7033.      # make a list of the deps, each on a new line for each dep/newline
  7034.      WARNLIBS=''; SEP='';
  7035.  
  7036.      # if more than one missing dep, set separator to a comma
  7037.      [ ! -z "$DEPS_MISSING" -a "`echo "$DEPS_MISSING" | wc -l`" != "1" -a "`echo "$DEPS_MISSING" | wc -l`" != "0" ] && SEP=','
  7038.  
  7039.      # clean up our deps list, and make space separated only (no commas)
  7040.      DEPS_MISSING="`LANG=C echo "${DEPS_MISSING//,/ }" | grep -v '^,' | grep -v "^\$"`"
  7041.  
  7042.      # show deps info, if any available
  7043.      [ "${DEPS_MISSING}" != ""  ]   && echo "Dependencies to get: ${DEPS_MISSING//,/, }"
  7044.      [ "$FORCE" = false -a "${DEPS_INSTALLED}" != "" ] && echo "Dependencies installed: `LANG=C grep -v "^\$" $TMPDIR/deps_installed 2>/dev/null | head -1`"
  7045.  
  7046.      # for each missing dep  (or simply for each dep, if $FORCE is true)
  7047.      for DEP in $DEPS_MISSING
  7048.      do
  7049.  
  7050.        [ "$DEP" = "" -o "$DEP" = "${PKGNAME_ONLY}" -o "$DEP" = "${PKGNAME}" ] && continue #skip if the dep is the main package
  7051.        [ "$PKG_DEBUG" = "true" ] && set +x
  7052.        local DEPNAME=`get_pkg_name "$DEP" 2>/dev/null`
  7053.        local DEPNAME_ONLY=`get_pkg_name_only "$DEPNAME" 2>/dev/null`
  7054.        local DEPFILE=''
  7055.  
  7056.  
  7057.        [ "$PKG_DEBUG" = "true" ] && echo "get_deps(): PKGNAME=${PKGNAME}" >&2
  7058.        [ "$PKG_DEBUG" = "true" ] && echo "get_deps(): PKGNAME_ONLY=${PKGNAME_ONLY}" >&2      
  7059.        [ "$PKG_DEBUG" = "true" ] && set -x
  7060.        # skip if already processed, it should be listed in $TMPDIR/PKGSDONE
  7061.        local dep_already_done=`LANG=C grep -m1 "^$DEPNAME_ONLY\$" $TMPDIR/PKGSDONE 2>/dev/null`
  7062.        [ "$dep_already_done" != "" -a "$FORCE" = false ] && continue
  7063.  
  7064.        local dep_is_blacklisted=`is_blacklisted_pkg "$DEPNAME_ONLY" 2>/dev/null`
  7065.        local dep_is_builtin=`is_builtin_pkg "$DEPNAME_ONLY" 2>/dev/null`
  7066.        local dep_is_usr_pkg=`is_usr_pkg "$DEPNAME_ONLY" 2>/dev/null`
  7067.        local dep_is_in_devx=`is_devx_pkg "$DEPNAME_ONLY" 2>/dev/null`
  7068.  
  7069.        # skip getting pkg if its blacklisted
  7070.        if [ "$dep_is_blacklisted" = true ]; then
  7071.          echo "Skipping $DEPNAME_ONLY (blacklisted).."
  7072.          continue
  7073.        fi
  7074.  
  7075.        # skip getting pkg if its a builtin, unless HIDE_BUILTINS=false
  7076.        if [ "$dep_is_builtin" = true -a "${HIDE_BUILTINS}" = true ]; then
  7077.          echo "Skipping $DEPNAME_ONLY (already built-in).."
  7078.          continue
  7079.        fi
  7080.  
  7081.        # skip getting pkg if its user installed and not using --force
  7082.        if [ "$dep_is_usr_pkg" = true -a "${FORCE}" != true ]; then
  7083.          echo "Skipping $DEPNAME_ONLY (already installed).."
  7084.          continue
  7085.        fi
  7086.  
  7087.        # skip getting pkg if its in the devx, unless HIDE_BUILTINS=false
  7088.        if [ "$dep_is_in_devx" = true -a "${HIDE_BUILTINS}" = true ]; then
  7089.          echo "Skipping $DEPNAME_ONLY (already built-in).."
  7090.          continue
  7091.        fi
  7092.  
  7093.        #DOWNLOAD THE PKG
  7094.        [ "$PKG_DEBUG" = "true" ] && set +x
  7095.         [ "$PKG_DEBUG" = "true" ] && echo "get_deps(): pkg_download $DEPNAME" >&2
  7096.        pkg_download "$DEPNAME" 2>/dev/null
  7097.        [ "$PKG_DEBUG" = "true" ] && echo "get_deps(): exided pkg_download $DEPNAME" >&2
  7098.        [ "$PKG_DEBUG" = "true" ] && set -x
  7099.        # skip install unless NO_INSTALL=true
  7100.        if [ "${NO_INSTALL}" = false ]; then
  7101.  
  7102.          # get the actual file we just downloaded to WORKDIR
  7103.          #DEPFILE="`find "$WORKDIR" -maxdepth 1 -type f -name "$DEPNAME*" 2>/dev/null`"
  7104.          DEPFILE="$pkg_download_rtn"
  7105.          if [ ! -f "$DEPFILE" ]; then
  7106.            echo "Warning global return failed for DEPNAME=${DEPNAME}, DEPFILE=${DEPFILE}"
  7107.            echo "Trying slower method"
  7108.            DEPFILE="$(find "$WORKDIR" -maxdepth 1 -type f -name "$DEPNAME*" 2>/dev/null)"
  7109.          fi
  7110.  
  7111.          #INSTALL THE DEP, if it was downloaded
  7112.          [ "$(is_local_pkg "$DEPFILE" 2>/dev/null)" = true ] && pkg_install "$DEPFILE" 2>/dev/null
  7113.  
  7114.          # mark the pkg as done!
  7115.          echo "$DEPNAME_ONLY" >> $TMPDIR/PKGSDONE
  7116.  
  7117.        else # if not installing,
  7118.          # skip dep checking of the deps that dont get installed
  7119.          continue
  7120.        fi
  7121.  
  7122.  
  7123.        # we finished with this dep, mark it done,
  7124.        # .. so we can skip it if it appears again (in a recursive dep check loop for example)
  7125.        echo "$DEPNAME_ONLY" >> $TMPDIR/PKGSDONE #260713
  7126.  
  7127.        [ "$PKG_DEBUG" = "true" ] && set +x
  7128.      done #done for DEP in DEPS_MISSING
  7129.  
  7130.    fi #endif DEPCONFIM=y
  7131.  
  7132.  else
  7133.    echo "No missing dependencies."
  7134.  fi # endif DEPS_MISSING != ''
  7135.  
  7136.  # if some deps were missing (listed but not found), print msg
  7137.  [ "$INSTALLDEPS" = true ] && actioned=installed || actioned=downloaded
  7138.  [ "$WARNLIBS" != "" ]     && echo -e "${yellow}Warning:${endcolour} Not $actioned from repo: $WARNLIBS"
  7139.  #exit 0 #110913
  7140. }
  7141.  
  7142.  
  7143. pkg_ldd_msg(){                    # check given package ($1) for missing deps FUNCLIST
  7144.  
  7145.  # exit if no valid usage
  7146.  [ ! "$1" -o "$1" = "-" ] && print_usage deps-check && exit 1
  7147.  
  7148.  . ${PKGRC}
  7149.  
  7150.  local PKGNAME
  7151.  local FNDFILES
  7152.  local LIST
  7153.  local RES
  7154.  local MISSING
  7155.  
  7156.  # get pkg name
  7157.  PKGNAME=`get_pkg_name "$1"`
  7158.  rm $TMPDIR/pkg-$PKGNAME-MISSING.txt &>/dev/null
  7159.  
  7160.  [ "`is_installed_pkg "$PKGNAME"`" = false ] && print_usage deps-check && exit 1
  7161.  
  7162.  echo "Searching for missing dependencies.. Please wait."
  7163.  
  7164.  local list="$PKGNAME `list_deps $PKGNAME`"
  7165.  
  7166.  for pkg_name in $list
  7167.  do
  7168.    local pkg_name_only=`get_pkg_name_only "$pkg_name"`
  7169.    # get the *.files for this pkg
  7170.    FNDFILES="$(find $PACKAGE_FILE_LIST_DIR/ -iname "${pkg_name}.files" 2>/dev/null)"
  7171.    [ ! -f "$FNDFILES" ] && FNDFILES="$(find "$PACKAGE_FILE_LIST_DIR" -iname "${pkg_name}_*.files" 2>/dev/null)"
  7172.    [ ! -f "$FNDFILES" ] && FNDFILES="$(find "$PACKAGE_FILE_LIST_DIR" -iname "${pkg_name}-*.files" 2>/dev/null)"
  7173.    [ ! -f "$FNDFILES" ] && FNDFILES="$(find "PACKAGE_FILE_LIST_DIR" -iname "${pkg_name}*.files"  2>/dev/null)"
  7174.    [ ! -f "$FNDFILES" ] && continue
  7175.  
  7176.    # get list of ldd-able file
  7177.    cat "$FNDFILES" | grep -E '/lib/|/lib64/|/bin/|/games/|/sbin/' > ${TMPDIR}/ldd_file_list_${pkg_name}
  7178.    [ -z ${TMPDIR}/ldd_file_list_${pkg_name} ] && continue
  7179.  
  7180.    #loop through list
  7181.    for file in `cat ${TMPDIR}/ldd_file_list_${pkg_name}`
  7182.    do
  7183.      [ ! -x "$file" ] && continue
  7184.      RES="`ldd $file 2>/dev/null`"
  7185.      MISSING="`echo "$RES" | grep found`"
  7186.      [ "$MISSING" != "" -a "$MISSING" != " " ] && echo "  $file:
  7187.  $MISSING" >> $TMPDIR/${pkg_name}-MISSINGLIBS.txt
  7188.    done
  7189.  
  7190.    #print message
  7191.    if [ -f $TMPDIR/${pkg_name}-MISSINGLIBS.txt ]; then
  7192.      echo -e "${yellow}WARNING${endcolour}: ${magenta}${pkg_name_only}${endcolour} has missing dependencies: "
  7193.      echo -e "`cat $TMPDIR/${pkg_name}-MISSINGLIBS.txt`"
  7194.    else
  7195.      echo -e "${green}OK:${endcolour} ${pkg_name} has no missing dependencies."
  7196.    fi
  7197.    rm -f ${TMPDIR}/ldd_file_list* $TMPDIR/${pkg_name}-MISSINGLIBS.txt 2>/dev/null
  7198.  done
  7199. }
  7200.  
  7201.  
  7202. get_all_deps(){                   # try to install all missing deps FUNCLIST
  7203. echo "Checking $(list_installed_pkgs | wc -l) installed packages for missing dependencies. Please wait..."
  7204. rm $TMPDIR/pkg_get_all_deps 2>/dev/null
  7205.  
  7206. list_installed_pkgs | while read LINE; do
  7207.  DEPLIST="`LANG=C list_deps "$LINE"`"
  7208.  [ "$DEPLIST" != "" -a "$LINE" != "" ] && echo "$LINE|$DEPLIST" >> $TMPDIR/pkg_get_all_deps
  7209. done
  7210.  
  7211. [ ! -f $TMPDIR/pkg_get_all_deps ]   && echo "No missing dependencies." && exit 0
  7212. ASKOPT='';   [ "$ASK" = true ]   && ASKOPT='--ask ';
  7213. FORCEOPT=''; [ "$FORCE" = true ] && FORCEOPT='--force ';
  7214.  
  7215. cat $TMPDIR/pkg_get_all_deps 2>/dev/null | while read LINE
  7216. do
  7217.  [ "$LINE" = "" -o "`echo "$LINE" | grep -v '|'`" = "" ] && continue
  7218.  # get pkg gname from field 1
  7219.  PKGNAME="${LINE%%|*}"
  7220.  DEPS="${LINE##*|}"
  7221.  
  7222.  echo "Checking $PKGNAME..."
  7223.  
  7224.  for DEP in $DEPS;
  7225.  do
  7226.    # try to get the pkg name
  7227.    DEPPKG="`list_all_pkg_names $DEP- | head -1`" || \
  7228.    DEPPKG="`list_all_pkg_names $DEP_ | head -1`" || \
  7229.    DEPPKG="`list_all_pkg_names $DEP  | head -1`"
  7230.    # if dep not in any repos, skip it
  7231.    [ "$DEPPKG" = "" ] && continue
  7232.    # skip if the dep is already installed
  7233.    [ "`is_installed_pkg $DEPPKG`" = true ] && continue
  7234.    # ask to get dep
  7235.    echo -n "Get the missing package: ${DEPPKG}$QTAG:  "
  7236.    echo
  7237.    [ "$ASK" = true ] && read -n 1 CONFIRM </dev/tty || CONFIRM=y
  7238.    [ "$ASK" = true ] && echo -ne "\b\b\n"
  7239.    # if user answered yes, we will now download the pkgs
  7240.    if [ "$CONFIRM" = "y" ]; then
  7241.      NO_INSTALL=false pkg_get "$DEPPKG" # get the missing dep (and its deps)
  7242.    fi
  7243.  done
  7244. done
  7245. rm -f $TMPDIR/pkg_get_all_deps 2>/dev/null
  7246. }
  7247.  
  7248.  
  7249. list_dependents(){                # list user installed pkgs that depend on $1 FUNCLIST
  7250.  
  7251.  local PKGNAME=''
  7252.  local PKGNAME_ONLY=''
  7253.  
  7254.  if [ "$1" = '' ]; then
  7255.    print_usage what-needs
  7256.    exit 1
  7257.  fi
  7258.  
  7259.  # try to get correct pkg names
  7260.  PKGNAME=`get_pkg_name "$1"`
  7261.  PKGNAME_ONLY=`get_pkg_name_only "$1"`
  7262.  
  7263.  # if pkg is not installed, then nothing depends on it
  7264.  if [ "`is_installed_pkg "$PKGNAME"`" = false ]; then
  7265.    echo "Package $1 not installed, nothing depends on it."
  7266.    exit 1
  7267.  fi
  7268.  
  7269.  # list all pkgs (from all repos) with $PKGNAME_ONLY as a dep
  7270.  cut -f1,2,9 -d'|' "$REPO_DB_FILE_DIR"/Packages-* \
  7271.    | sed -e "s/:any//g" -e "s/&[geql][eqt][0-9a-z._-]*//g" 2>/dev/null \
  7272.    | grep -E "+${PKGNAME_ONLY},|+${PKGNAME_ONLY}\|" 2>/dev/null \
  7273.    | cut -f2 -d'|' 2>/dev/null \
  7274.    | grep -v "^\$" \
  7275.    | grep -v "^#" \
  7276.    | grep -v "^$PKGNAME_ONLY\$" \
  7277.    | sort -u \
  7278.    >> ${TMPDIR}/dependents_list
  7279.  
  7280.  if [ "$HIDE_BUILTINS" = false ]; then
  7281.    # list all builtin and devx packages with $PKGNAME_ONLY as a dependency..
  7282.    cut -f1,2,9 -d'|' "$WOOF_INST_PKGS_FILE"  "$DEVX_INST_PKGS_FILE" \
  7283.      "$DEVX_INST_PKGS_FILE" \
  7284.      | grep -E "+${PKGNAME_ONLY},|+${PKGNAME_ONLY}\|" \
  7285.      | cut -f2 -d'|' \
  7286.      | grep -v "^\$" \
  7287.      | grep -v "^#" \
  7288.      | grep -v "^$PKGNAME_ONLY\$" \
  7289.      | sort -u \
  7290.      >> ${TMPDIR}/dependents_list
  7291.  fi
  7292.  
  7293.  # remove duplicates
  7294.  sort -u ${TMPDIR}/dependents_list > ${TMPDIR}/dependents_list__sorted
  7295.  mv ${TMPDIR}/dependents_list__sorted ${TMPDIR}/dependents_list_tmp
  7296.  rm ${TMPDIR}/dependents_list
  7297.  
  7298.  # only keep the user installed pkgs in the list
  7299.  for pkg in `list_installed_pkgs | grep -v ^$PKGNAME`
  7300.  do
  7301.    [ "`grep -m1 "^$(get_pkg_name_only $pkg)\$" ${TMPDIR}/dependents_list_tmp`" != '' ] && echo "$pkg" >> ${TMPDIR}/dependents_list
  7302.  done
  7303.  
  7304.  # print the list if we have one
  7305.  [ -f ${TMPDIR}/dependents_list -a ! -z ${TMPDIR}/dependents_list ] && cat ${TMPDIR}/dependents_list
  7306.  rm ${TMPDIR}/dependents_lis* 2>/dev/null
  7307. }
  7308.  
  7309.  
  7310. # file conversion
  7311.  
  7312. deb2pet(){                        # $1 must be valid deb file or repo pkg FUNCLIST
  7313.  [ ! -f "$1" ] && print_usage deb2pet && exit 1
  7314.  
  7315.  local DEB="${CURDIR}/$(basename "${1}")"
  7316.  local DIRNAME="${CURDIR}"
  7317.  #create file name we work with
  7318.  [ -f "$1" ] && DEB="$1" && DIRNAME="`dirname "$1"`"
  7319.  
  7320.  #keep old file, download it if needed
  7321.  #[ -f "$(basename "$1" .deb)" ] || ASK=$ASK FORCE=$FORCE pkg_download "$(basename "$1" .deb)"
  7322.  
  7323.  # if the deb exists
  7324.  if [ -e "$DEB" ]; then
  7325.    for i in "$DEB" # for each deb given
  7326.    do
  7327.      #remove the extensions
  7328.      #example, will be something like FOLDR=$HOME/Desktop/atari800_3.1.0-2+b2_i386
  7329.      FOLDR="$(echo "$i"|sed 's/\.deb$//')"
  7330.    done
  7331.  
  7332.    #make the new dir, copy the deb file into it, and ci into it
  7333.    mkdir -p "$FOLDR"; cp "$DEB" "$FOLDR"; cd "$FOLDR";
  7334.  
  7335.    #get the new full path and filename
  7336.    DEB="`ls | grep ".deb"`"
  7337.  
  7338.    # extract into current dir and remov ethe copied deb file
  7339.    pkg_unpack "$DEB" 1>/dev/null
  7340.  
  7341.    #this will be something like  PKGNAME=atari800_3.1.0-2+b2_i386
  7342.    PKGNAME="`basename "$DEB" .deb`"
  7343.  
  7344.    #now we package up the stuff into a pet
  7345.    [ -d "${FOLDR}/$PKGNAME" ] && dir2pet "${FOLDR}/$PKGNAME" || dir2pet "$PKGNAME"
  7346.  
  7347.    [ ! -f "$FOLDR.pet" -a ! -f "${CURDIR}/${PKGNAME}.pet" ] && error "$FOLDR.deb NOT converted to PET package!"
  7348.  
  7349.    #clean up
  7350.    rm -rf "$FOLDR"
  7351.  
  7352.  else
  7353.    echo "Package '$(basename $DEB)' not found in '$CURDIR'."
  7354.    return 1
  7355.  fi
  7356. }
  7357.  
  7358.  
  7359. dir2pet(){                        # dir to PET, $1 must be valid dir FUNCLIST
  7360.  
  7361.  # exit if no options
  7362.  [ ! -d "$1" ] && print_usage dir2pet && exit 1
  7363.  
  7364.  DIR="$1"
  7365.  SUFFIX=''
  7366.  
  7367.  [ "$2" != "" ] && SUFFIX="$2"
  7368.  
  7369.  #[ -d "$DIR" ] && cd `dirname "$DIR"`
  7370.  
  7371.  # get pkg name only,
  7372.  DIR="$(basename "${DIR}")"
  7373.  [ "$SUFFIX" != "" ] && cp -R "$DIR" "${DIR}${SUFFIX}" && DIR="${DIR}${SUFFIX}"
  7374.  
  7375.  echo -e "Converting directory ${lightblue}$DIR${endcolour} to .pet package."
  7376.  
  7377.  # we must have a dir with same name as a valid pkg
  7378.  if [ -d "$DIR" ]; then
  7379.    # move it to the build dir, to work on
  7380.    mkdir -p "$CURDIR/build_pkg/"
  7381.    [ ! -e "$CURDIR/build_pkg/$DIR" ] && cp -R "$DIR" "$CURDIR/build_pkg/"
  7382.  
  7383.    ARCHINDEPENDENT='yes'
  7384.    for ONEEXEC in `find $CURDIR/build_pkg/${DIR}/ -maxdepth 6 -type f -perm -o+x`
  7385.    do
  7386.      [ -f $ONEEXEC ] && [ "`file $ONEEXEC | grep ' ELF '`" != "" ] && ARCHINDEPENDENT='no'
  7387.    done
  7388.  
  7389.    # if it's a _DEV pkg.. it can't be ARCHINDEPENDENT
  7390.    case "${BASEPKG}" in *"_DEV"*) ARCHINDEPENDENT='no' ;; esac
  7391.    [ "`find $CURDIR/build_pkg/${DIR}/ -maxdepth 6 -type f -name '*.a' -o -type f -name 'lib*.so*' -o -type f -name '*.la'`" != "" ] && ARCHINDEPENDENT='no'
  7392.    [ "$ARCHINDEPENDENT" = "no" ] && COMPAT=$DISTRO_BINARY_COMPAT V=$DISTRO_COMPAT_VERSION
  7393.  
  7394.        #...borrowed from dir2pet script
  7395.    #w482 directory may already have a pet.specs, reuse it...
  7396.    NAMEONLY=""
  7397.    PUPMENUDESCR=""
  7398.    PUPOFFICIALDEPS=""
  7399.    PUPCATEGORY=""
  7400.    PUPPATH="" #100201
  7401.    ARCHDEPENDENT="yes" #100201
  7402.    DEFREPO="" #100201
  7403.    if [ -f $CURDIR/build_pkg/${DIR}/pet.specs ]; then #160803
  7404.     #new: pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|
  7405.     #optionally on the end: compileddistro|compiledrelease|repo| (fields 11,12,13)
  7406.     PETSPECS="`cat $CURDIR/build_pkg/${DIR}/pet.specs | head -n 1`" #160803
  7407.     while IFS="|" read -r F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 <&3
  7408.     do
  7409.       DB_pkgname="$F1"
  7410.       DB_nameonly="$F2"
  7411.       NAMEONLY="$DB_nameonly"
  7412.       DB_version="$F3"
  7413.       DB_pkgrelease="$F4"
  7414.       DB_category="$F5"
  7415.       PUPCATEGORY="$DB_category"
  7416.       DB_size="$F6"
  7417.       DB_path="$F7"
  7418.       PUPPATH="$DB_path" #100201
  7419.       DB_fullfilename="$F8"
  7420.       DB_dependencies="$F9"
  7421.       PUPOFFICIALDEPS="$DB_dependencies"
  7422.       DB_description="$F10"
  7423.       PUPMENUDESCR="$DB_description"
  7424.       DB_compileddistro="$F11"
  7425.       DB_compiledrelease="$F12"
  7426.       ARCHDEPENDENT="${DB_compileddistro}|${DB_compiledrelease}"
  7427.       DB_repo="$F13"
  7428.       DEFREPO="$DB_repo"
  7429.      done 3< $CURDIR/build_pkg/${DIR}/pet.specs
  7430.    else
  7431.      echo -e "\nCategories:
  7432.  BuildingBlock, Desktop, System, Setup, Utility,
  7433.  Filesystem, Graphic, Document, Business, Personal,
  7434.  Network, Internet, Multimedia, Fun\n"
  7435.      read -p 'Type one of the categories above, then hit ENTER: ' CAT_ENTRY </dev/tty;
  7436.  
  7437.      echo
  7438.      read -p "Add a short description, then hit ENTER: " DESC_ENTRY </dev/tty;
  7439.  
  7440.      echo -e "\nAdd dependencies in this format:  ${bold}+libglib,+ffmpeg${endcolour}\n"
  7441.      read -p 'Enter dependencies (if any), then hit ENTER: ' DEPS_ENTRY </dev/tty;
  7442.  
  7443.      echo "${DIR}|$(get_pkg_name_only "$DIR")|$(get_pkg_version "$DIR")||${CAT_ENTRY:-Utility}|$(du -h "$CURDIR/build_pkg/${DIR}/" | tail -1 | cut -f1)||${DIR}.pet|${DEPS_ENTRY}|${DESC_ENTRY:-No description}|$COMPAT|$V||" > $CURDIR/build_pkg/${DIR}/pet.specs
  7444.    fi
  7445.  
  7446.    # build .pet.specs
  7447.  
  7448.    BASEPKG="`basename $DIR`"
  7449.    DIRPKG="`dirname $DIR`"
  7450.    [ "$DIRPKG" = "/" ] && DIRPKG=""
  7451.  
  7452.    #difficult task, separate package name from version part...
  7453.    #not perfect, some start with non-numeric version info...
  7454.    [ "$NAMEONLY" = "" ] && NAMEONLY=`get_pkg_name_only "$BASEPKG"`
  7455.  
  7456.    # get pet details from the pre-existing pet.specs (if any): deps, category, descr, version, nameonly, arch, distro version
  7457.    PUPOFFICIALDEPS="$DB_dependencies"
  7458.  
  7459.    TOPCAT="$PUPCATEGORY"
  7460.    [ -z "$TOPCAT" ] && TOPCAT=BuildingBlock
  7461.  
  7462.    PUPMENUDESCR="$DB_description"
  7463.    [ -z "${PUPMENUDESCR}" ] && PUPMENUDESCR="No description provided"
  7464.  
  7465.    VERSION="`get_pkg_version "$BASEPKG"`"
  7466.  
  7467.    # build pet spec
  7468.    if [ ! -f $CURDIR/build_pkg/${DIR}/pet.specs ]; then
  7469.      echo "$BASEPKG|${NAMEONLY}|$VERSION|$DB_pkgrelease|$TOPCAT|$DB_size|$REPO_SUBDIR|${BASEPKG}.pet|$PUPOFFICIALDEPS|$PUPMENUDESCR|$COMPAT|$V||" > $CURDIR/build_pkg/${DIR}/pet.specs
  7470.    fi
  7471.  
  7472.    # delete the slackware package management files
  7473.    #if [ -d "$CURDIR/build_pkg/install" ]; then
  7474.    # rm -r "$CURDIR/build_pkg/install"; rmdir "$CURDIR/build_pkg/install";
  7475.    #fi
  7476.  
  7477.    # delete arch pkg stuff
  7478.    #rm -r "$CURDIR/build_pkg/.INSTALL" &>/dev/null
  7479.    #rm "$CURDIR/build_pkg/.PKGINFO" &>/dev/null
  7480.  
  7481.    # now tar up the folder, ready to make into tar.gz, then into .pet
  7482.    cd $CURDIR/build_pkg/
  7483.  
  7484.    # we need to choose xz or gzip pets
  7485.    arch=`uname -m`
  7486.    if [ "${arch:0:3}" = "arm" ]; then
  7487.      TAREXT="gz"
  7488.    else
  7489.      TAREXT="xz"
  7490.    fi
  7491.  
  7492.    # if in a pre-woof puppy then we dont use xz
  7493.    if [ ! -f "$WOOF_INST_PKGS_FILE" -o ! -f /etc/DISTRO_SPECS ]; then
  7494.      # pre woof format
  7495.      echo "PETMENUDESCR='${DIR}'" >  $CURDIR/build_pkg/${DIR}/${DIR}.pet.specs
  7496.      echo "PETOFFICIALDEPS=''"    >> $CURDIR/build_pkg/${DIR}/${DIR}.pet.specs
  7497.      echo "PETREGISTER='yes'"     >> $CURDIR/build_pkg/${DIR}/${DIR}.pet.specs
  7498.      compression=xz
  7499.      TAREXT="xz"
  7500.    fi
  7501.    tar -c -f ${DIR}.tar ${DIR} 1>/dev/null
  7502.    sync
  7503.    [ "`which xz`" = "" ] && TAREXT=gz
  7504.    case "$TAREXT" in
  7505.      xz) xz -z -9 -e ${DIR}.tar; ;;
  7506.      gz) gzip --best ${DIR}.tar; ;;
  7507.    esac
  7508.  
  7509.    # now get info needed to make a pet file
  7510.    TARBALL="${DIR}.tar.$TAREXT"
  7511.    FULLSIZE="`stat --format=%s ${TARBALL}`"
  7512.    MD5SUM="`md5sum $TARBALL | cut -f 1 -d ' '`"
  7513.  
  7514.    # add the info to the file
  7515.    echo -n "$MD5SUM" >> $TARBALL
  7516.    sync
  7517.  
  7518.    # now rename it to .pet
  7519.    mv -f $TARBALL ${DIR}.pet
  7520.    sync
  7521.  
  7522.    # move the created pet out of build_pkg, into WORK_DIR
  7523.    mv ${DIR}.pet $CURDIR/${DIR}.pet
  7524.    cd $CURDIR
  7525.  
  7526.    # clean up
  7527.    rm -f -R $CURDIR/build_pkg/
  7528.    echo
  7529.    echo -e "Package ${magenta}${DIR}.pet${endcolour} created."
  7530.    echo "The md5sum is: `md5sum ${DIR}.pet | cut -f1 -d' '`."
  7531.  else
  7532.    echo "Directory '$DIR' not found."
  7533.    exit 1
  7534.  fi
  7535. }
  7536.  
  7537.  
  7538. dir2sfs(){                        # $1 must be dir of pkg contents FUNCLIST
  7539.  
  7540.  if [ ! -d "$1" ]; then
  7541.    print_usage dir2sfs
  7542.    exit 1
  7543.  fi
  7544.  
  7545.  # if found, we will append it to the SFS filename
  7546.  [ "$DISTRO_VERSION" != "" ] && SFS_SUFFIX="_${DISTRO_VERSION}" || SFS_SUFFIX=''
  7547.  
  7548.  # get sfs name only, no extension or path
  7549.  SFS_DIR="`basename "$1" .sfs`"
  7550.  
  7551.  # if a valid dir
  7552.  if [ -d "$SFS_DIR" ]; then
  7553.    rm "${SFS_DIR}/"*pet*specs 2>/dev/null #240613
  7554.    #start building the sfs file
  7555.    echo "Please wait... building SFS file.."
  7556.    local SFS_NAME="${SFS_DIR/$SFS_SUFFIX/}${SFS_SUFFIX}.sfs"
  7557.    LANG=C mksquashfs "$SFS_DIR" "$SFS_NAME" -noappend &>/dev/null && SUCCESS="y" || SUCCESS=""
  7558.    if [ -z "$SUCCESS" -a ! -f "${SFS_DIR}${SFS_SUFFIX}.sfs" ]; then
  7559.      echo "Failed to create $SFS_NAME."
  7560.      exit 1
  7561.    fi
  7562.    chmod 644 "$SFS_NAME" #shinobar
  7563.    sync
  7564.    #get size, and output msg
  7565.    s=`LANG=C du -m "$SFS_NAME" | sed "s/\s.*//"`
  7566.    MD5SUM=`md5sum "$SFS_NAME" | cut -f1 -d ' '`
  7567.    echo -e "Created ${magenta}$SFS_NAME${endcolour} ( $s MB )"
  7568.    echo -e "The md5 checksum: $MD5SUM"
  7569.  else
  7570.    echo "Cannot create SFS:  '${SFS_DIR}' is not a directory."
  7571.    exit 1
  7572.  fi
  7573.  
  7574.  # clean up
  7575.  #rm -rf "${SFS_DIR}" &>/dev/null
  7576.  #rm -f "${SFS}${SFS_SUFFIX}"*.sfs-md5.txt &>/dev/null
  7577.  rm -f "${SFS}${SFS_SUFFIX}" &>/dev/null
  7578. }
  7579.  
  7580.  
  7581. dir2tgz(){                        # requires $1 as valid dir FUNCLIST
  7582.  
  7583.  [ ! -d "$1" ] && print_usage dir2tgz && exit 1
  7584.  
  7585.  DIR="${1/.t*gz/}"
  7586.  
  7587.  # remove trailing slash
  7588.  DIRNAME="`echo -n $DIR | sed -e 's%/$%%'`"
  7589.  DIRNAME="`basename ${DIRNAME}`" # get name only, no path
  7590.  
  7591.  # make sure any pet specs are named correctly - this new tar file might later be converted to a .pet
  7592.  echo "`ls -1 ${DIRNAME} | grep 'pet.specs'`" | while read LINE
  7593.  do
  7594.    mv "${DIRNAME}/$LINE" "${DIRNAME}/pet.specs" 2>/dev/null;
  7595.  done
  7596.  
  7597.  # create the tar file
  7598.  ##echo "Adding directory to '${DIRNAME}.tar.gz'.."
  7599.  tar -c -f "${DIRNAME}.tar" "${DIRNAME}/"
  7600.  gzip "${DIRNAME}.tar"
  7601.  sync
  7602.  
  7603.  # print message
  7604.  if [ -f "${DIRNAME}.tar.gz" ]; then
  7605.    echo -e "Package ${magenta}${DIRNAME}.tar.gz${endcolour} created."
  7606.    echo "The md5sum is: `md5sum ${DIRNAME}.tar.gz | cut -f1 -d' '`."
  7607.  else
  7608.    echo "Package '${DIRNAME}.tar.gz' NOT created."
  7609.    return 1
  7610.  fi
  7611. }
  7612.  
  7613.  
  7614. pet2sfs(){                        # convert pet to sfs, $1 must be file or repo pkg FUNCLIST
  7615.  
  7616.  # require valid option or quit
  7617.  [ ! -f "$1" ] && print_usage pet2sfs && exit 1
  7618.  
  7619.  pkg_ext=`get_pkg_ext "$1"`
  7620.  
  7621.  [ "$pkg_ext" != pet ] && print_usage pet2sfs && exit 1
  7622.  
  7623.  #110213,0.9.1 we want the file in $WORKDIR
  7624.  #[ ! -f "${WORKDIR}/$(basename "$1" .pet).pet" ] && cp -f "${1/.pet/}.pet" "${WORKDIR}/$(basename "$1" .pet)" 2>/dev/null
  7625.  
  7626.  PKGNAME="$(basename "$1" .pet)" #get pkg name only, no extension or path
  7627.  PKGFILE="${CURDIR}/${PKGNAME}.pet" #the file to install
  7628.  
  7629.  # determine the compression, extend test to 'XZ'
  7630.  file -b "${PKGFILE}" | grep -i -q "^xz" && TAREXT=xz || TAREXT=gz
  7631.  [ "$TAREXT" = 'xz' ] && taropts='-xJf' || taropts='-zxf'
  7632.  
  7633.  pet2tgz "${PKGFILE}" 1>/dev/null
  7634.  
  7635.  # now extract the files into $CURDIR/$PKGNAME/
  7636.  tar $taropts "${PKGFILE//.pet/.tar.$TAREXT}" 2> $TMPDIR/$SELF-cp-errlog
  7637.  
  7638.  # if DISTRO_VERSION found, the sfs will prob have a suffix
  7639.  [ "$DISTRO_VERSION" != "" ] && SFS_SUFFIX="_${DISTRO_VERSION}" || SFS_SUFFIX=''
  7640.  
  7641.  # remove the old one, if needed
  7642.  rm "${PKGNAME}${SFS_SUFFIX}.sfs" 2>/dev/null
  7643.  
  7644.  # create the new one
  7645.  dir2sfs "${CURDIR}/${PKGNAME}" && rm -rf "${CURDIR}/${PKGNAME}"
  7646.  
  7647.  # remove the .tar.$TAREXT file
  7648.  rm -f "${PKGNAME}.tar.$TAREXT"
  7649.  
  7650.  # if pkg put the pet in $CURDIR (not user), remove it
  7651.  #[ -f "${PKGFILE}" -a "`dirname "$1"`" != "$CURDIR" ] && rm -f "${PKGFILE}" 2>/dev/null
  7652.  
  7653.  # print message
  7654.  if [ ! -f "${PKGNAME}.sfs" -a ! -f "${PKGNAME}${SFS_SUFFIX}.sfs" ]; then
  7655.    echo "Package '${PKGNAME}.pet' not converted."
  7656.    return 1
  7657.  fi
  7658. }
  7659.  
  7660.  
  7661. pet2tgz(){                        # convert to tar.gz, $1 must be valid file FUNCLIST
  7662.  
  7663.  # if $1 is not a valid pkg name or a file that exists then exit..
  7664.  [ ! -f "$1" ] && print_usage pet2tgz && exit 1
  7665.  
  7666.  pkg_ext=`get_pkg_ext "$1"`
  7667.  [ "$pkg_ext" != "pet" ] && print_usage pet2tgz && exit 1
  7668.  
  7669.  PKGNAME="$(basename "$1" .pet)" # get pkg name only, no extension or path
  7670.  PKGFILE="$1"          # build the pkg file path
  7671.  
  7672.  # backup the package, to restore it later
  7673.  cp -f "$PKGFILE" "$TMPDIR/`basename $PKGFILE`.backup" 1>/dev/null
  7674.  
  7675.  chmod +w "$PKGFILE" # make it writable.
  7676.  FOOTERSIZE="32"
  7677.  
  7678.  # determine the compression, extend test to 'XZ'
  7679.  finfo=`file -b "$PKGFILE"`
  7680.  case $finfo in
  7681.    gz*|GZ*) EXT=gz ;;
  7682.    xz*|XZ*) EXT=xz ;;
  7683.    *) error "Unsupported compression type, or corrupted package." && exit 1 ;;
  7684.  esac
  7685.  
  7686.  # get the md5
  7687.  MD5SUM="`tail -c $FOOTERSIZE \"$PKGFILE\"`"
  7688.  NEWNAME="`echo -n \"$PKGFILE\" | sed -e "s/\\.pet$/\\.tar\\.$EXT/g"`" #131122
  7689.  head -c -$FOOTERSIZE "$PKGFILE" > $NEWNAME
  7690.  NEWMD5SUM="`md5sum \"$NEWNAME\" | cut -f 1 -d ' '`"
  7691.  sync
  7692.  [ ! "$MD5SUM" = "$NEWMD5SUM" ] && exit 1
  7693.  
  7694.  # restore original pet pkg
  7695.  [ -f "$TMPDIR/`basename $PKGFILE`.backup" ] && mv -f "$TMPDIR/`basename $PKGFILE`.backup" "$PKGFILE"
  7696.  
  7697.  # print message
  7698.  if [ -f "$NEWNAME" ]; then
  7699.    echo -e "${green}Success${endcolour}: Package ${magenta}`basename ${NEWNAME}`${endcolour} created."
  7700.    echo "The md5sum is `md5sum "$NEWNAME" | cut -f1 -d' '`."
  7701.  else
  7702.    error "Package ${PKGNAME}.pet not converted!"
  7703.    return 1
  7704.  fi
  7705. }
  7706.  
  7707.  
  7708. pet2txz(){                        # calls pet2tgz FUNCLIST
  7709.  pet2tgz "$1"
  7710. }
  7711.  
  7712.  
  7713. sfs2pet(){                        # convert sfs to pet, requires $1 as a valid sfs FUNCLIST
  7714.  
  7715.  # exit if no valid options
  7716.  [ ! -f "$1" ] && print_usage sfs2pet && exit 1
  7717.  
  7718.  # exit if user did not give an SFS file, or didnt give a file at all
  7719.  [ "`file "$1" | grep  Squashfs`" = '' ] && print_usage sfs2pet && exit 1
  7720.  
  7721.  # we want the file in $CURDIR
  7722.  #[ ! -f "${WORKDIR}/$(basename "${1}")" ] && cp -f "$1" "${WORKDIR}/$(basename "${1}")" 2>/dev/null
  7723.  
  7724.  SFSNAME="$(basename "$1")"
  7725.  SFSDIR="$(dirname "$1")"
  7726.  SFSEXT="${SFSNAME##*.}"
  7727.  
  7728.  # if DISTRO_VERSION found, the sfs will prob have a suffix
  7729.  [ "$DISTRO_VERSION" != "" ] && SFS_SUFFIX="_${DISTRO_VERSION}" || SFS_SUFFIX=''
  7730.  
  7731.  # create the name without path or extension (may include a $SUFFIX)
  7732.  ROOTNAME=`basename "$SFSNAME" ".$SFSEXT"`
  7733.  
  7734.  # build the file name we will work on
  7735.  SFS="${SFSDIR}/${SFSNAME}"
  7736.  
  7737.  if [ -f "$SFS" -a "$SFS" != "" ]; then
  7738.  
  7739.    echo "Unsquashing $SFSNAME.. Please wait.."
  7740.  
  7741.    # remove any older dirs, and unsquash
  7742.    rm -rf "${CURDIR}/squashfs-root/"
  7743.    LANG=C unsquashfs "$SFS"  &>/dev/null
  7744.  
  7745.    # lets remove the SFS suffix, to get back the the 'valid' PET name
  7746.    ROOTNAME_NOSUFFIX=`echo "$ROOTNAME" | sed -e "s/$SFS_SUFFIX//"`
  7747.  
  7748.    # create the folder we will package up
  7749.    mv squashfs-root "${ROOTNAME_NOSUFFIX}/"
  7750.  
  7751.    dir2tgz "${ROOTNAME_NOSUFFIX}" 1>/dev/null
  7752.  
  7753.    tgz2pet "${ROOTNAME_NOSUFFIX}".tar.gz 1>/dev/null
  7754.  
  7755.    # remove the dir and the tgz we just created
  7756.    rm -rf "${ROOTNAME_NOSUFFIX}"
  7757.    rm -f "${ROOTNAME_NOSUFFIX}".tar.gz
  7758.  
  7759.    if [ -f "${ROOTNAME_NOSUFFIX}.pet" ]; then
  7760.      echo -e "${green}Success${endcolour}: Package '${magenta}${ROOTNAME_NOSUFFIX}.pet${endcolour}' created."
  7761.      echo "The md5sum is: `md5sum ${ROOTNAME_NOSUFFIX}.pet | cut -f1 -d' '`"
  7762.    else
  7763.      echo "Package '${ROOTNAME_NOSUFFIX}.pet' not converted."
  7764.      return 1
  7765.    fi
  7766.  fi
  7767. }
  7768.  
  7769.  
  7770. tgz2pet(){                        # convert $1 (a tar.gz or tgz) to .pet FUNCLIST
  7771.  
  7772.  [ ! -f "$1" ] && print_usage tgz2pet && exit 1
  7773.  sync
  7774.  TARBALL="$1"
  7775.  [ ! -f "$TARBALL" ] && echo "The archive '$TARBALL' could not be found." && exit 1
  7776.  
  7777.  cp -f $TARBALL "$TMPDIR/`basename $TARBALL`.backup" 1>/dev/null
  7778.  
  7779.  TARBALL="${CURDIR}/$(basename "${TARBALL}")"
  7780.  chmod 644 "$TARBALL" #make it writable.
  7781.  echo "Converting `basename ${TARBALL}`.."
  7782.  
  7783.  #only accept .tgz or .tar.gz .tar.xz files...
  7784.  EXT=''
  7785.  case ${TARBALL} in
  7786.    *.tar.gz) EXT='.tar.gz' ;;
  7787.    *.tgz)    EXT='.tgz' ;;
  7788.    *.tar.xz) EXT='.tar.xz' ;;
  7789.    *.txz)    EXT='.txz' ;;
  7790.    *) echo "${1##*/}: File extension not allowed" >&2 ; exit 1 ;;
  7791.  esac
  7792.  [ "$EXT" = "" ] && error "$TARBALL must be a .tgz, .tar.gz, .txz or .tar.xz file" && exit 1
  7793.  
  7794.  #split TARBALL path/filename into components...
  7795.  BASEPKG="`basename $TARBALL $EXT`"
  7796.  DIRPKG="$CURDIR"
  7797.  [ "$DIRPKG" = "/" ] && DIRPKG=""
  7798.  case $EXT in
  7799.  *gz)OPT=-z;;
  7800.  *xz)OPT=-J;;
  7801.  esac
  7802.  
  7803.  # move pkg, update extensions (tgz -> .tar.gz, txz -> .tar.xz)
  7804.  # make code later more readable
  7805.  case $EXT in
  7806.  *tgz)mv -f $TARBALL $DIRPKG/${BASEPKG}.tar.gz
  7807.   TARBALL="$DIRPKG/${BASEPKG}.tar.gz"
  7808.   EXT='.tar.gz';;
  7809.  *txz)mv -f $TARBALL $DIRPKG/${BASEPKG}.tar.xz
  7810.   TARBALL="$DIRPKG/${BASEPKG}.tar.xz"
  7811.   EXT='.tar.xz';;
  7812.  esac
  7813.  
  7814.  # if tarball expands direct to '/' want to wrap around it (slackware pkg)... 100628 add -z ...
  7815.  # man bad bug here... the thing isn't expanded! #131122
  7816.  if [ "`tar ${OPT} --list -f ${TARBALL} | head -n 1`" = "./" ]; then
  7817.    tar --one-top-level=${BASEPKG} -xf ${TARBALL}
  7818.    tar --remove-files -c -f ${DIRPKG}/${BASEPKG}.tar ${BASEPKG}/
  7819.    case $EXT in
  7820.    *gz) gzip --force --best ${DIRPKG}/${BASEPKG}.tar ;;
  7821.    *xz) xz --force -z -9 -e ${DIRPKG}/${BASEPKG}.tar ;;
  7822.    esac
  7823.  fi
  7824.  
  7825.  FULLSIZE="`stat --format=%s ${TARBALL}`"
  7826.  MD5SUM="`md5sum $TARBALL | cut -f 1 -d ' '`"
  7827.  echo -n "$MD5SUM" >> $TARBALL
  7828.  sync
  7829.  mv -f $TARBALL $DIRPKG/${BASEPKG}.pet
  7830.  sync
  7831.  
  7832.  # restore original tar file
  7833.  [ -f "$TMPDIR/`basename $TARBALL`.backup" ] && mv -f "$TMPDIR/`basename $TARBALL`.backup" "$TARBALL"
  7834.  
  7835.  # print msg
  7836.  if [ -f "${DIRPKG}/${BASEPKG}.pet" ]; then
  7837.    echo -e "${green}Success:${endcolour} Created ${magenta}${BASEPKG}.pet${endcolour}."
  7838.    echo "This is the md5sum: `md5sum ${DIRPKG}/${BASEPKG}.pet | cut -f1 -d' '`"
  7839.  else
  7840.    error "Could not convert ${DIRPKG}/${BASEPKG}.$EXT"
  7841.    return 1
  7842.  fi
  7843. }
  7844.  
  7845.  
  7846. txz2pet(){                        # convert txz to pet, requires $1 as valid file FUNCLIST
  7847.  [ ! -f "$1" ] && print_usage txz2pet && exit 1
  7848.  FILE="$1"   # full path, filename and extension
  7849.  #keep old file, download it if needed
  7850.  #[ -f "$(basename "$1" .txz)" ] || pkg_download "$(basename "$1" .txz)" #  200813 try to download it
  7851.  # we want $FILE in $CURDIR
  7852.  [ ! -f "${CURDIR}/$(basename "${FILE}")" ] && cp -f "$FILE" "${CURDIR}/$(basename "${FILE}")" 2>/dev/null
  7853.  FILE="${CURDIR}/$(basename "${FILE}")"
  7854.  #set vars
  7855.  FILENAME="`basename "$FILE"`" # filename and extension only
  7856.  BARENAME="${FILENAME/.t*xz/}" # filename, no extension..
  7857.  PETFILE="${CURDIR}/${BARENAME}.pet" # the full path and filename of the pet file
  7858.  PETNAME="`basename "$PETFILE"`" # the filename of the pet file
  7859.  # create the pet directory, if needed
  7860.  mkdir -p "${CURDIR}/${BARENAME}"
  7861.  [ ! -d "${CURDIR}/${BARENAME}" ] && { echo "PET directory not created or found."; exit 1; }
  7862.  # unpack the file
  7863.  [ -f "$FILENAME" ] && tar -Jxvf "$FILENAME" -C "${BARENAME}/" 1>/dev/null || echo "Cannot untar $FILENAME"
  7864.  # create the pet file
  7865.  dir2pet "$BARENAME" || echo "PET working directory not found"
  7866.  
  7867.  if [ -f "$PETFILE" ]; then # if pet file was created
  7868.    # remove the pet file working directory
  7869.    rm -R "${CURDIR}/${BARENAME}/" 2>/dev/null
  7870.    rmdir "${CURDIR}/${BARENAME}/" 2>/dev/null
  7871.    # report the file was created #nope, no need, dir2pet will do it
  7872.    #echo "Package '$PETNAME' created successfully."
  7873.  else
  7874.    echo "Package "$PETFILE" file not created"
  7875.    return 1
  7876.  fi
  7877.  # delete the original txz file
  7878.  rm -f "$FILE" &>/dev/null
  7879. }
  7880.  
  7881.  
  7882. # other
  7883.  
  7884. menu_entry_msg(){                 # show menu entry details of installed pkg FUNCLIST
  7885.  
  7886.  # exit if not valid usage
  7887.  [ ! "$1" -o "$1" = "-" ] && exit 1
  7888.  
  7889.  . ${PKGRC}
  7890.  
  7891.  local MENUFILE=''
  7892.  local pkg_file_list=''
  7893.  local no_display=''
  7894.  local terminal_only=''
  7895.  local pkg_command=''
  7896.  
  7897.  # get the menu file, if it exists
  7898.  pkg_file_list=$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${1}*.files")
  7899.  
  7900.  [ -f "$pkg_file_list" ] && MENUFILE="`LANG=C grep -m1 '/usr/share/applications/' "$pkg_file_list" | grep -i -m1 ".desktop\$"`" || return 1
  7901.  
  7902.  # if pkg has a .desktop file
  7903.   if [ -f "$MENUFILE" ]; then
  7904.  
  7905.    # exit if the menu item is set to NoDisplay
  7906.    no_display="`grep -m1 ^NoDisplay=true "$MENUFILE" 2>/dev/null`"
  7907.    terminal_only="`grep -m1 ^Terminal=true "$MENUFILE" 2>/dev/null`"
  7908.  
  7909.    # fix menu entries
  7910.    sed -i 's/ %u//g' "$MENUFILE"
  7911.    sed -i 's/ %U//g' "$MENUFILE"
  7912.    sed -i 's/ %f//g' "$MENUFILE"
  7913.    sed -i 's/ %F//g' "$MENUFILE"
  7914.    sed -i 's/ %d//g' "$MENUFILE"
  7915.    sed -i 's/ %D//g' "$MENUFILE"
  7916.  
  7917.    update_menus &
  7918.  
  7919.    # get the details for a final msg
  7920.    MENU_CAT="`LANG=C grep -m1 "^Categories=" $MENUFILE 2>/dev/null | cut -f2 -d'=' | sed -e 's/\(.*\);/\1/g' -e 's/.*;//g' | head -1`"
  7921.    APATTERN="[ ,]${MENU_CAT}" #MHHP
  7922.    TOPMENU="`LANG=C grep -m1 "${APATTERN}" /etc/xdg/menus/hierarchy | cut -f1 -d' ' | head -1`"
  7923.    if [ "$TOPMENU" = "" ]; then
  7924.      APATTERN="[ ,]${MENU_CAT};"
  7925.      TOPMENU="`LANG=C grep -m1 "${APATTERN}" /etc/xdg/menus/hierarchy | cut -f1 -d' ' | head -1`"
  7926.    fi
  7927.    MENU_NAME="`LANG=C grep -m1 "^Name=" $MENUFILE | cut -f2 -d'=' | head -1`"
  7928.  
  7929.    [ "$TOPMENU" = '' ] && TOPMENU="$MENU_CAT"
  7930.  
  7931.    # replace the Categories= line with one which has only the top, main category..
  7932.    # .. this should prevent duplicate menu entries
  7933.    #cat "$MENUFILE" | grep -v ^Categories > ${TMPDIR}/fixed.desktop
  7934.    #echo "Categories=${TOPMENU};" >> ${TMPDIR}/fixed.desktop
  7935.    #mv ${TMPDIR}/fixed.desktop $MENUFILE
  7936.  
  7937.    # print msg
  7938.    if [ "$no_display" != "" -o "$terminal_only" != '' ]; then
  7939.      pkg_command="`grep -m1 ^Exec= "$MENUFILE" | cut -f2 -d'='`"
  7940.      [ "$pkg_command" != '' ] && echo -e "To run, type:  ${bold}${pkg_command}${endcolour}"
  7941.    else
  7942.      echo -e "Menu entry:     ${lightblue}${TOPMENU:-[None]}${endcolour} -> ${lightblue}${MENU_NAME:-[None]}${endcolour}"
  7943.    fi
  7944.  
  7945.    return 0
  7946.  fi
  7947.  return 1
  7948. }
  7949.  
  7950.  
  7951. update_menus() {                  # update menus, calls fixmenus, refreshes WM
  7952.  echo started > /tmp/pkg/update_menus_busy
  7953.  fixmenus &>/dev/null
  7954.  # refresh JWM menus if using JWM window manager
  7955.  [ "`which jwm`" != "" -a "`ps -e | grep jwm`" != "" ] && jwm -reload &>/dev/null
  7956.  rm /tmp/pkg/update_menus_busy 2>/dev/null
  7957.  return 0
  7958. }
  7959.  
  7960.  
  7961. check_net(){                      # check net connection (before downloading) FUNCLIST
  7962.  [ -f $TMPDIR/internetsuccess ] && echo 0 && exit 0 #220613
  7963.  [ ! "$1" -o "$1" = "-" ] && URL="8.8.8.8" || URL="`echo  ${1} | awk -F/ '{print $3}'`"
  7964.  LANG=C ping -4 -c1 -q "$URL" &>/dev/null #220613
  7965.  REPLY=$?
  7966.  [ $REPLY -eq 0 ] && echo -n "ok" > $TMPDIR/internetsuccess #220613
  7967.  echo 0
  7968. }
  7969.  
  7970.  
  7971. get_stdin(){                      # read from stdin (when - is last option) FUNCLIST
  7972.  read -t 0.1 STDINVAR
  7973. }
  7974.  
  7975.  
  7976. not_found(){                      # list alternative packages when no match in repos FUNCLIST
  7977.  PKGNAME="${1/.pet/}"
  7978.  #echo "$APPTITLE"
  7979.  #echo "Package '${PKGNAME}' not found in current repo..  "
  7980.  if [ "$(ls "${WORKDIR}/"| grep "${PKGNAME}")" != "" ]; then
  7981.    [ "`list_downloaded_pkgs "${PKGNAME}"`" != "" ] && echo "These downloaded packages match your search:
  7982. `list_downloaded_pkgs "${PKGNAME}"`"
  7983.  fi
  7984.    [ "`$PKGSEARCH "${PKGNAME}"`" != "" ] && echo "These packages in the repos match your search:
  7985. `$PKGSEARCH "${PKGNAME}"`"
  7986.  return 1
  7987. }
  7988.  
  7989.  
  7990. first_run (){                     # welome message on first run
  7991.  
  7992.  # quit if not the first run
  7993.  [ ! -f ~/.pkg/firstrun ] && return 0
  7994.  # print msg
  7995.  echo '============================================================'
  7996.  echo -e "  ${bold}$APPNAME $APPVER${endcolour} - a command-line package manager"
  7997.  echo '============================================================'
  7998.  echo
  7999.  echo "  pkg repo-update      # update the contents of each installed repo"
  8000.  echo "  pkg repo-list        # list all available repos"
  8001.  echo "  pkg repo <name>      # change to the chosen repo"
  8002.  echo "  pkg show-config      # show current Pkg settings"
  8003.  echo "  pkg workdir <DIR>    # change where to keep downloaded packages"
  8004.  echo
  8005.  echo "  pkg search <term>    # search all fields in the current repo"
  8006.  echo "  pkg names <pkgname>  # search package names in the current repo"
  8007.  echo "  pkg status <pkgname> # print info about the given package"
  8008.  echo "  pkg add <pkgname>    # install & download packages and dependencies"
  8009.  echo
  8010.  echo " You can run the commands below to learn more about Pkg:"
  8011.  echo
  8012.  echo "  pkg help             # help info, all options and some tips"
  8013.  echo "  pkg help-all         # full help info, including advanced usage"
  8014.  echo "  pkg usage [cmd]      # show every command, or info on the given one"
  8015.  echo "  pkg examples         # show lots more example Pkg commands"
  8016.  echo
  8017.  echo " HINT: Use TAB to auto-complete package and repo names (and more).."
  8018.  echo
  8019.  echo '============================================================'
  8020.  
  8021.  # get the right list of repos
  8022.  update_sources &>/dev/null &
  8023.  
  8024.  # delete first run flag
  8025.  rm -f ~/.pkg/firstrun 2>/dev/null
  8026.  
  8027.  # delete other tmp stuff, make it like a first run
  8028.  rm $TMPDIR/func_list        &>/dev/null
  8029.  rm $TMPDIR/curr_repo_url    &>/dev/null
  8030.  rm $TMPDIR/internetsuccess  &>/dev/null
  8031.  rm $TMPDIR/pkg_aliases      &>/dev/null
  8032.  rm $TMPDIR/pkglist          &>/dev/null
  8033.  rm $TMPDIR/pkglist_*        &>/dev/null
  8034.  rm $TMPDIR/USRPKGLIST       &>/dev/null
  8035.  rm $TMPDIR/PKGSDONE         &>/dev/null
  8036.  rm $TMPDIR/deps_installed   &>/dev/null
  8037.  rm $TMPDIR/deps_missing     &>/dev/null
  8038.  rm $TMPDIR/$SELF-cp-errlog  &>/dev/null
  8039.  rm $TMPDIR/*-MISSING.txt    &>/dev/null
  8040.  
  8041.  exit 0
  8042. }
  8043.  
  8044.  
  8045. #====================  main functions  ======================#
  8046.  
  8047.  
  8048.  
  8049. #=====================  final checks  =======================#
  8050.  
  8051. # try to set a default repo if none was found
  8052. if [ ! -f "$REPO_DB_FILE_DIR/$REPOFILE" -o ! -f "$HOME/.pkg/sources" ]; then
  8053.  
  8054.  mkdir -p $HOME/.pkg 2>/dev/null
  8055.  mkdir -p $REPO_DIR 2>/dev/null
  8056.  [ ! -f "$HOME/.pkg/sources" ] && touch $HOME/.pkg/sources
  8057.  # try to get repo file from /root if it exists
  8058.  if [ -f "$REPO_DB_FILE_DIR/Packages-puppy-noarch-official" ]; then
  8059.    # copy any available Puppy repo files in root to $REPO_DIR
  8060.    mv "$REPO_DB_FILE_DIR"/Packages-puppy-* $REPO_DB_FILE_DIR/
  8061.  fi
  8062.  
  8063.  ## if repo file was NOT added to home dir, fata error, exit
  8064.  #if [ ! -f $REPO_DIR/Packages-puppy-noarch-official ]; then
  8065.  #  error "Repo files not found. Check ${magenta}$REPO_DIR${endcolour}"
  8066.  #  exit 1
  8067.  #fi
  8068.  
  8069.  # if we got here, the noarch repo file is in $REPO_DIR, lets use it as the default/only repo
  8070.  echo -e "${yellow}Warning:${endcolour} No repo files found. Setting the default repo to 'noarch'."
  8071.  update_sources 1>/dev/null
  8072.  set_current_repo noarch 1>/dev/null
  8073.  # if repos added ok
  8074.  if [ $? -eq 0 ]; then
  8075.    echo -e "${green}Success:${endcolour} 'noarch' added."
  8076.    echo "Re-running: $SELF $@"
  8077.    echo
  8078.    $SELF $@
  8079.    exit 0
  8080.  fi
  8081.  
  8082. fi
  8083.  
  8084. # create the user-installed pkgs file if it doesnt exists
  8085. [ ! -f $REPO_DIR/user-installed-packages ] && touch $REPO_DIR/user-installed-packages
  8086.  
  8087. first_run
  8088.  
  8089.  
  8090. #=====================  final checks  =======================#
  8091.  
  8092.  
  8093.  
  8094. #====================  main interface  ======================#
  8095.  
  8096.  
  8097. while [ $# != 0 ]; do # get all options ($# means all options)
  8098.   I=1
  8099.   while [ ! -z $# -a $I -le `echo $# | wc -c` ]; do
  8100.  
  8101.    # accept from stdin, (using dash (-) as final option)
  8102.    for x in "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
  8103.    do
  8104.      case "$x" in
  8105.        -)
  8106.          while true; do
  8107.            read ALINE
  8108.            [ "$ALINE" ] || break
  8109.            [ "$(echo $ALINE| cut -b1)" = '#' ] && continue # skip comment
  8110.            [ "$ALINE" = ':' ] && continue  # skip colon
  8111.            if [ "$ALINE" = "- " -o "$ALINE" = " -" -o "$ALINE" != "-" ]; then
  8112.              OPTS="${@%-}" #get all opts, without last dash
  8113.              OPTS="${OPTS% }" #get all opts, strip last space
  8114.              $SELF $OPTS "$ALINE"
  8115.            fi
  8116.          done
  8117.          break
  8118.        ;;
  8119.        --ask|-a)         ASK=true ; QTAG="?  (y/N)";;
  8120.        --quiet|-q)       QUIET=true ;;
  8121.        --force|-f)       FORCE=true ;;
  8122.        --no-color)       green='' red='' magenta='' lightblue='' yellow='' bold='' endcolour='' ;;
  8123.        --no-colour|-nc)  green='' red='' magenta='' lightblue='' yellow='' bold='' endcolour='' ;;
  8124.        '--configure='*)  PKG_CONFIGURE="$(echo "$x" | sed -e 's/^--configure=//')" ;;
  8125.        '--cflags='*)     PKG_CFLAGS="$(echo "$x" | sed -e 's/^--cflags=//')"       ;;
  8126.      esac
  8127.    done
  8128.  
  8129.    # enable passing multiple pkgs separated by comma, space,
  8130.    # works with or without double quotes around pkgs
  8131.    opt="`shift; echo "$@"`"
  8132.    opt="${opt//,/ }"
  8133.    opt="${opt//|/ }"
  8134.    # only keep params up to the next option
  8135.    opt="${opt// -*/}"
  8136.  
  8137.    ## main options
  8138.    case $1 in
  8139.  
  8140.      -) # accept from stdin, pkg -i (defaults to pkg add $1 or pkg -u $1)
  8141.        if [ "$2" = "" ]; then
  8142.          while true; do
  8143.            read ALINE
  8144.            [ "$ALINE" -a "$ALINE" != '-' ] || break
  8145.            [ "$(echo $ALINE| cut -b1)" = '#' ] && continue # skip comment
  8146.            ALINE=`echo $ALINE` #strip spaces
  8147.            if [ "$ALINE" != "-" -a "`$PKGSEARCH "$ALINE"`" != "" ]; then
  8148.              [ "`is_installed_pkg $ALINE`" = false ] && pkg_get "$ALINE" || \
  8149.              pkg_uninstall "$ALINE"
  8150.            fi
  8151.          done
  8152.          shift
  8153.        fi
  8154.      ;;
  8155.  
  8156.      --ask|-a)         ASK=true ; QTAG="?  (y/N)";;
  8157.      --quiet|-q)       QUIET=true ;;
  8158.      --force|-f)       FORCE=true ;;
  8159.      --no-color)       green='' red='' magenta='' lightblue='' yellow='' bold='' endcolour='' ;;
  8160.      --no-colour|-nc)  green='' red='' magenta='' lightblue='' yellow='' bold='' endcolour='' ;;
  8161.      '--configure='*)  PKG_CONFIGURE="$(echo "$1" | sed -e 's/^--configure=//')" ;;
  8162.      '--cflags='*)     PKG_CFLAGS="$(echo "$1" | sed -e 's/^--cflags=//')"       ;;
  8163.  
  8164.      # package search
  8165.      --all-pkgs|--all)                 [ ! "$2" ] && cat "$REPO_DB_FILE_DIR/${REPOFILE}" || cat "$REPO_DB_FILE_DIR/${REPOFILE}" | grep "$2"; exit 0;;
  8166.      --search|-s|search|s)             [ ! "$2" ] && search_pkgs          || for x in "$opt"; do [ "$x" -a "$x" != "-" ] && search_pkgs "$x";          done; ;;
  8167.      -ss)                              [ ! "$2" ] && search_fast          || for x in "$opt"; do [ "$x" -a "$x" != "-" ] && search_fast "$x";          done; ;;
  8168.      --search-all|-sa|search-all|sa)   [ ! "$2" ] && search_all_pkgs      || for x in "$opt"; do [ "$x" -a "$x" != "-" ] && search_all_pkgs "$x";      done; ;;
  8169.      -ssa|ssa)                         [ ! "$2" ] && search_all_fast      || for x in "$opt"; do [ "$x" -a "$x" != "-" ] && search_all_fast "$x";      done; ;;
  8170.      --names|-n|names|n)               [ ! "$2" ] && list_pkg_names       || for x in $opt; do [ "$x" -a "$x" != "-" ] && list_pkg_names "$x";         done; ;;
  8171.      --names-exact|-ne|names-exact|ne) [ ! "$2" ] && list_pkg_names       || for x in $opt; do [ "$x" -a "$x" != "-" ] && list_pkg_names "$x\$";       done; ;;
  8172.      --names-all|-na|names-all|na)     [ ! "$2" ] && list_all_pkg_names   || for x in $opt; do [ "$x" -a "$x" != "-" ] && list_all_pkg_names "$x";     done; ;;
  8173. --list-installed|list-installed|-li|li) [ ! "$2" ] && list_installed_pkgs  || for x in $opt; do [ "$x" -a "$x" != "-" ] && list_installed_pkgs "$x";    done; exit 0 ;;
  8174. --list-downloaded|list-downloaded|-ld|ld) [ ! "$2" ] && list_downloaded_pkgs || list_downloaded_pkgs "$2"; exit 0 ;;
  8175.      -LI|LI)                           [ ! "$2" ] && HIDE_BUILTINS=false list_installed_pkgs || HIDE_BUILTINS=false list_installed_pkgs "$2"; exit 0 ;; # list all installed pkgs inc builtins
  8176.  --names-exact-all|-nea|names-exact-all|nea) [ ! "$2" ] && list_all_pkg_names || for x in $opt; do [ "$x" -a "$x" != "-" ] && list_all_pkg_names "$x\$"; done; exit 0 ;;
  8177.  
  8178.      # get, download, install, remove, pkgs
  8179.      --download|-d|download|d)
  8180.        AUTOCLEAN_override='no'  
  8181.        [ ! "$2" ] && pkg_download || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_download "$x";  done;;
  8182.      --install|-i|install|i)           [ ! "$2" ] && pkg_install  || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_install "$x";   done;;
  8183.      --uninstall|-u|uninstall|u)       [ ! "$2" ] && pkg_uninstall|| for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_uninstall "$x"; done;;
  8184.      --remove|-rm|remove|rm)           [ ! "$2" ] && pkg_remove   || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_remove "$x";    done;;
  8185.      --unpack|--extract|unpack|extract)[ ! "$2" ] && pkg_unpack   || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_unpack "$x";    done;;
  8186.      --clean|clean)                    [ ! "$2" ] && clean_pkgs   || for x in $opt; do [ "$x" -a "$x" != "-" ] && clean_pkgs "$x";    done;;
  8187.      --get|-g|get|g|--add|add|a)       [ ! "$2" ] && pkg_get      || for x in $opt; do [ "$x" -a "$x" != "-" ] && { choose_pkg "$x"; pkg_get "$x"; }; done ;;
  8188.      --get-only|-go|get-only|go)       [ ! "$2" ] && print_usage get-only || { [ -z "$PKG_MATCH_FLEXIBILITY" && export PKG_MATCH_FLEXIBILITY=2 ;for x in $opt; do if [ "$x" -a "$x" != "-" ]; then choose_pkg "$x"; NO_INSTALL=true  pkg_get "$x"; fi; done; } ;;
  8189.      --delete|-l|delete|l)             [ ! "$2" ] && print_usage delete   || for x in $opt; do [ "$x" = '-' ] && continue; if [ -f "${WORKDIR}/$(basename "$x")" ]; then if [ "$ASK" = true ]; then echo -n "Remove package `basename $x`$QTAG "; read -n 1 CONFIRM </dev/tty; echo; else CONFIRM=y; fi; [ "$CONFIRM" = "y" ] && rm -v "${WORKDIR}/$(basename "$x")"; else not_found "$x"; fi; done ;; #110213,0.9.1  renamed to --delete|-l
  8190.      --delete-all|-la|delete-all|la)   ASK=true; QTAG="?  (y/N)"; if [ "$ASK" = true ]; then echo -en "Remove all downloaded packages in ${lightblue}${WORKDIR}/${endcolour}$QTAG"; read -n 1 CONFIRM </dev/tty; echo; else CONFIRM=y; fi; [ "$CONFIRM" = "y" ] && rm -v ${WORKDIR}/*.pet ${WORKDIR}/*.pkg.* ${WORKDIR}/*.sfs ${WORKDIR}/*.tar.* ${WORKDIR}/*.tcz ${WORKDIR}/*.txz ${WORKDIR}/*.tgz ${WORKDIR}/*.deb ${WORKDIR}/*.rpm ${WORKDIR}/*.apk ${WORKDIR}/*.gz ${WORKDIR}/*.xz 2>/dev/null; exit 0;;
  8191.      --install-all|-ia)                list_downloaded_pkgs | while read pkg; do [ "$pkg" -a "$pkg" != "-" ] && pkg_install "$pkg";   done;;
  8192.      --uninstall-all|-ua)              list_installed_pkgs  | while read pkg; do [ "$pkg" -a "$pkg" != "-" ] && pkg_uninstall "$pkg"; done;;
  8193.  
  8194.      # pkg status, info
  8195.      --pkg-installed|-pi|installed|pi)   [ ! "$2" ] && is_installed_pkg || for x in $opt; do [ "$x" -a "$x" != "-" ] && is_installed_pkg "$x"; done;;
  8196.      --pkg-entry|-pe|entry|pe)           [ ! "$2" ] && pkg_entry   || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_entry "$x";    done;;  # print repo entry, each field on a new line
  8197.      --pkg-status|-ps|status|ps)         [ ! "$2" ] && pkg_status  || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_status "$x";   done;;
  8198.      --contents|-c|contents|c)           [ ! "$2" ] && pkg_contents|| for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_contents "$x" 2>/dev/null; done;;
  8199.      --which|-w|which|w)                 [ ! "$2" ] && which_pkg   || for x in $opt; do [ "$x" -a "$x" != "-" ] && which_pkg "$x";    done;;
  8200.      --which-repo|-wr|which-repo|wr)     [ ! "$2" ] && which_repo  || for x in $opt; do [ "$x" -a "$x" != "-" ] && which_repo "$x";   done;;
  8201.      --pkg-repack|-pr|repack|pr)         [ ! "$2" ] && pkg_repack  || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_repack "$x";   done;;
  8202.      --pkg-update|-pu|update|pu)         [ ! "$2" ] && pkg_update  || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_update "$x";   done;;
  8203.      --pkg-combine|-pc|pkg-combine|pc)   [ ! "$2" ] && pkg_combine || for x in $opt; do [ "$x" -a "$x" != "-" ] && COMBINE2SFS=false pkg_combine "$x"; done;;
  8204.      --pkg-split|split)                  [ ! "$2" ] && split_pkg   || for x in $opt; do [ "$x" -a "$x" != "-" ] && split_pkg "$x"; done;;
  8205.      --pkg-merge|merge)                  [ ! "$2" ] && merge_pkg   || merge_pkg "$2" "$3"; exit 0;;
  8206.      --sfs-combine|-sc|sfs-combine|sc)   [ ! "$2" ] && pkg_combine || for x in $opt; do [ "$x" -a "$x" != "-" ] && COMBINE2SFS=true  pkg_combine "$x"; done;;
  8207.      -PS|PS)                             [ ! "$2" ] && pkg_status  || for x in $opt; do [ "$x" -a "$x" != "-" ] && HIDE_USER_PKGS=false FULL_PKG_STATUS=true pkg_status "$x"; done;; # full pkg status, with sorted deps
  8208.  
  8209.      # pkg compiling
  8210.      --pkg-build|-pb|build|pb)           [ ! "$2" ] && pkg_build || for x in $opt; do [ "$x" -a "$x" != "-" ] && PKG_CONFIGURE="$PKG_CONFIGURE" PKG_CFLAGS="$PKG_CFLAGS" pkg_build "$x"; done;;
  8211.   --pkg-build-list|-pbl|build-list|pbl)  [ ! "$2" ] && list_build_scripts || for x in $opt; do [ "$x" -a "$x" != "-" ] && list_build_scripts "$x"; done;;
  8212.  
  8213.      # dependencies
  8214.      --what-needs|-wn|what-needs|wn)     [ ! "$2" ] && list_dependents; for x in $opt; do [ "$x" -a "$x" != "-" ] && list_dependents "$x";    done ;; # list pkgs depending on $x, not including builtins by default
  8215.      --list-deps|-le|list-deps|le)       [ ! "$2" ] && list_deps; for x in $opt; do [ "$x" -a "$x" != "-" ] && list_deps "$x";                done ;; # list pkg deps, not including builtins
  8216.      --deps|-e|deps|e)                   [ ! "$2" ] && get_deps;  for x in $opt; do [ "$x" -a "$x" != "-" ] && get_deps  "$x";                done ;;
  8217.    --deps-download|-ed|deps-download|ed) [ ! "$2" ] && get_deps;  for x in $opt; do [ "$x" -a "$x" != "-" ] && NO_INSTALL=true get_deps "$x"; done ;;
  8218.      --has-deps|-he|has-deps|he)         [ ! "$2" ] && has_deps;  for x in $opt; do [ "$x" -a "$x" != "-" ] && has_deps  "$x";                done ;;
  8219.      --deps-all|-ea|deps-all|ea)         [ ! "$2" ] && get_deps;  [ "$2" != "-" ] && NO_INSTALL=false get_all_deps "$2" ;;
  8220.      --deps-check|-ec|deps-check|ec|ldd) [ ! "$2" ] && print_usage deps-check || pkg_ldd_msg "$2" ;;
  8221.      -LE|LE)                             [ ! "$2" ] && HIDE_BUILTINS=false list_deps || for x in $opt; do [ "$x" -a "$x" != "-" ] && HIDE_BUILTINS=false list_deps "$x"; done ;;  # list a pkg deps, including builtins
  8222.  
  8223.      # repo
  8224.      --repo|-r|repo|r)                     [ ! "$2" ] && echo $REPONAME || set_current_repo "$2" ;;
  8225.      --repo-convert|-rc|repo-convert|rc)   [ ! "$2" ] && print_usage repo-convert || for x in $opt; do [ "$x" -a "$x" != "-" ]  && convert_repofile "$x"; done ;;
  8226.      --repo-list|-rl|repo-list|rl)         [ "$2" != "-" ] && repo_list "$2"      ;;
  8227.      --repo-info|-ri|repo-info|ri)         [ "$2" != "-" ] && print_repo_info "$2";;
  8228.  --repo-file-list|-rfl|repo-file-list|rfl) [ "$2" != "-" ] && repo_file_list "$2" ;;
  8229.      --repo-update|-ru|repo-update|ru)     update_repo "$2";;
  8230.      --add-source|add-source)              [ "`echo $2|grep '|'`" = '' -o "$2" = '-' ] && print_usage add-source || add_source "$2";;
  8231.      --add-repo|add-repo)                  [ ! "$2" -o "$2" = '-'  ] && print_usage add-repo || shift; add_repo "$@"; exit 0;;
  8232.      --rm-repo|rm-repo)                    [ ! "$2" -o "$2" = '-'  ] && print_usage rm-repo  || rm_repo "$2"; exit 0;;
  8233.      --update-sources|update-sources)      update_sources ;;
  8234.  
  8235.      # set repo settings
  8236.      --repo-pkg-scope|-rps|rps|repo-pkg-scope)  if [ "$2" -a "$2" != "-" ]; then set_pkg_scope "$2";      else echo "$PKGSCOPE"; fi; exit 0;;
  8237.      --repo-dep-scope|-rds|repo-dep-scope|rds)  if [ "$2" -a "$2" != "-" ]; then set_dep_scope "$2";      else echo "$DEPSCOPE"; fi; exit 0;;
  8238.      --bleeding-edge|-be|bleeding-edge|be)      if [ "$2" -a "$2" != "-" ]; then set_bleeding_edge "$2";  else echo "$BLEDGE";   fi; exit 0;;
  8239.      --rdep-check|-rdc|rdep-check|rdc)          if [ "$2" -a "$2" != "-" ]; then set_recursive_deps "$2"; else echo "$RDCHECK";  fi; exit 0;;
  8240.  
  8241.      # convert
  8242.      --deb2pet|deb2pet) [ ! "$2" ] && deb2pet || for x in $opt; do deb2pet "$x"; done ;;
  8243.      --dir2pet|dir2pet) [ ! "$2" ] && dir2pet || for x in $opt; do dir2pet "$x"; done ;;
  8244.      --dir2sfs|dir2sfs) [ ! "$2" ] && dir2sfs || for x in $opt; do dir2sfs "$x"; done ;;
  8245.      --dir2tgz|dir2tgz) [ ! "$2" ] && dir2tgz || for x in $opt; do dir2tgz "$x"; done ;;
  8246.      --pet2sfs|pet2sfs) [ ! "$2" ] && pet2sfs || for x in $opt; do pet2sfs "$x"; done ;;
  8247.      --pet2tgz|pet2tgz) [ ! "$2" ] && pet2tgz || for x in $opt; do pet2tgz "$x"; done ;;
  8248.      --pet2txz|pet2txz) [ ! "$2" ] && pet2txz || for x in $opt; do pet2txz "$x"; done ;;
  8249.      --sfs2pet|sfs2pet) [ ! "$2" ] && sfs2pet || for x in $opt; do sfs2pet "$x"; done ;;
  8250.      --tgz2pet|tgz2pet) [ ! "$2" ] && tgz2pet || for x in $opt; do tgz2pet "$x"; done ;;
  8251.      --txz2pet|txz2pet) [ ! "$2" ] && txz2pet || for x in $opt; do txz2pet "$x"; done ;;
  8252.    --dir2repo|dir2repo) [ ! "$2" ] && dir2repo|| for x in $opt; do dir2repo "$x"; done ;;
  8253.  
  8254.      # other
  8255.      --func-list|func-list)  [ -f $TMPDIR/func_list ] && cat $TMPDIR/func_list || func_list; exit 0;;
  8256.      --workdir|workdir)      [ "$2" != "-" ] && set_workdir "$2"; exit $?;;
  8257.      --autoclean|autoclean)  [ "$2" = "yes" -o "$2" = "no" ] && set_autoclean "$2" || { echo "$AUTOCLEAN"; exit 1; } ;;
  8258.  
  8259.      # usage, help funcs, version info, etc
  8260.      --welcome|welcome)          touch $HOME/.pkg/firstrun; first_run; exit 0;;
  8261.      --show-config|show-config)  show_config; exit 0;;
  8262.      --version|-v|version|v)     echo "$APPNAME $APPVER"; exit 0;;
  8263.      --usage|usage)              print_usage "$2"; exit 0;;
  8264.  
  8265.      --examples|-ex|examples|ex)
  8266.        . /usr/share/pkg/docs/examples.txt;
  8267.        echo -e "$EXAMPLES\n"
  8268.        exit 0
  8269.        ;;
  8270.      --help-all|-H|help-all|H)
  8271.          . /usr/share/pkg/docs/env_vars.txt
  8272.          . /usr/share/pkg/docs/help.txt
  8273.          . /usr/share/pkg/docs/examples.txt
  8274.          . /usr/share/pkg/docs/help-all.txt
  8275.          echo -e "$HELP_ALL\n\n" | less -R
  8276.          exit 0
  8277.          ;;
  8278.  
  8279.      --help|-h|help|h)
  8280.          . /usr/share/pkg/docs/env_vars.txt
  8281.          . /usr/share/pkg/docs/help.txt
  8282.          echo -e "$HELP\n"
  8283.          exit 0
  8284.          ;;
  8285.  
  8286.      # any other options
  8287.      -?*|--?*)
  8288.          # exit if bad options
  8289.          echo "Unknown option '$1'" && exit 1
  8290.        ;;
  8291.  
  8292.      ?*)
  8293.          # all other options .. might be an internal func, if so, execute it with all given options
  8294.          [ "`func_list | tr '\t' ' ' | cut -f2 -d' ' | grep -m1 "^${1}"'()'`" != '' ] && "$@" && exit 0
  8295.        ;;
  8296.  
  8297.      esac
  8298.  
  8299.      shift
  8300.      I=$(($I + 1))
  8301.    done
  8302. done
  8303.  
  8304.  
  8305.  
  8306. #====================  main interface  ======================#
  8307. code=$?
  8308. [ ! -z "$AUTOCLEAN_override" ] && AUTOCLEAN="$AUTOCLEAN_override"
  8309. # if AUTOCLEAN=true silently delete all pkgs in WORKDIR that are already installed
  8310. if [ "$AUTOCLEAN" = 'yes' -a "`HIDE_BUILTINS=true list_installed_pkgs`" != '' ]; then
  8311.   clean_pkgs &>/dev/null
  8312. fi
  8313.  
  8314.  
  8315.  
  8316. # remove tmp dir used by pkg_get and get_deps
  8317. rm $TMPDIR/PKGSDONE 2>/dev/null
  8318.  
  8319. # reset lang options as we found them
  8320. LANG=$USER_LANG
  8321. LC_ALL=$USER_LC_ALL
  8322.  
  8323. exit $code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement