Advertisement
s243a

pkg-classic

Oct 22nd, 2019
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 299.82 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.  
  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.  
  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 "$PKGS_DIR" ] && export PKGS_DIR="$(realpath "${HOME}/.packages")"
  62. #These are files of the form
  63. #[ -z "$PET_SPEC_DIR_ROOT" ] && PET_SPEC_DIR_ROOT="$PKGS_DIR"
  64. if [ -z "$REPO_DB_FILE_DIR" ]; then
  65.   if [ -d "$PKGS_DIR/repo" ]; then #This imples mistfires ppm3 is installed
  66.     export REPO_DB_FILE_DIR="$(realpath "$PKGS_DIR/repo")"
  67.   else
  68.     export REPO_DB_FILE_DIR="$PKGS_DIR"
  69.   fi
  70. fi
  71. if [ -z "$PACKAGE_FILE_LIST_DIR" ]; then
  72.   if [ -d "$PKGS_DIR/package-files" ]; then #This imples mistfires ppm3 is installed
  73.     export PACKAGE_FILE_LIST_DIR="$(realpath "$PKGS_DIR/package-files")"
  74.   else
  75.     export PACKAGE_FILE_LIST_DIR="$(realpath "$PKGS_DIR")"
  76.   fi
  77. fi
  78. [ -z "$BUILTIN_FILE_LIST_DIR" ] && BUILTIN_FILE_LIST_DIR="$PKGS_DIR/builtin_files"
  79. #[ -z "$PET_SPEC_DIR_BUILTIN" ] && PET_SPEC_DIR_BUILTIN=\
  80. #  "$(realpath "$PKGS_DIR/builtin_files")"
  81. #
  82. #[ -z "$PET_SPEC_DIR_USR_INST" ] && PET_SPEC_DIR_USR_INST="$PKGS_DIR"
  83.  
  84.  
  85. #mk_spec_file_list_arry(){
  86.  declare -ga SPEC_DB_PATHS=()
  87.  #Can't put the loop in the loop in a pipeline if you want to assign array values:
  88.  #https://stackoverflow.com/questions/13091700/why-is-my-array-gone-after-exiting-loop  
  89.  #find / -wholename "$PKGS_DIR"/'*-installed-packages' |
  90.  while read SPEC_FILE; do
  91.    bname="`basename "$SPEC_FILE"`"
  92.    case "$bname" in #Consider lowering case
  93.    "layers-installed-packages")
  94.      #Do Nothing
  95.      export LAYER_INST_PKGS_FILE="$SPEC_FILE" ;;
  96.    "woof-installed-packages")
  97.      export WOOF_INST_PKGS_FILE="$SPEC_FILE" ;;
  98.    "user-installed-packages")
  99.      export USER_INST_PKGS_FILE="$SPEC_FILE" ;;
  100.    "devx-only-installed-packages")
  101.      export DEVX_INST_PKGS_FILE="$SPEC_FILE" ;;    
  102.    *)
  103.      SPEC_DB_PATHS+=( "$SPEC_FILE" ) ;;
  104.    esac
  105.    #s243a: We don't need realpath for the next four vars but it will provide usefull debugging information.
  106.    #s243a: TODO search for the following files if they don't exist.
  107.    [ -z "$USER_INST_PKGS_FILE" ] && \
  108.      USER_INST_PKGS_FILE="$PKGS_DIR/user-installed-packages"
  109.    [ -z "$WOOF_INST_PKGS_FILE" ] && \
  110.      WOOF_INST_PKGS_FILE="$PKGS_DIR/woof-installed-packages"
  111.    [ -z "$LAYER_INST_PKGS_FILE" ] && \
  112.      LAYER_INST_PKGS_FILE="$PKGS_DIR/layers-installed packages"
  113.    [ -z "$DEVX_INST_PKGS_FILE" ] && \
  114.      DEVX_INST_PKGS_FILE="$PKGS_DIR/devx-only-installed-packages"
  115.    SPEC_DB_PATHS=( "$USER_INST_PKGS_FILE" "$WOOF_INST_PKGS_FILE" "${SPEC_DB_PATHS[@]}" )
  116.  done < <( find "$PKGS_DIR" -name '*-installed-packages' )
  117. #}  
  118. #mk_spec_file_list_arry
  119. #echo "SPEC_DB_PATHS=${SPEC_DB_PATHS[@]}"
  120.  
  121. #mk_all_repo_file_list_arry(){
  122.  declare -gA ALL_REPO_DB_PATHS=()
  123.  #ALL_REPO_DB_PATHS=()
  124.  bname=''
  125.  
  126.  #Can't put the loop in the loop in a pipeline if you want to assign array values:
  127.  #https://stackoverflow.com/questions/13091700/why-is-my-array-gone-after-exiting-loop
  128.  #find / -wholename "$REPO_DB_FILE_DIR"/'Packages-*' |
  129.  while read REPO_FILE; do
  130.      #s243a: I was thinking of using realpath but it might cause issues.
  131.      #REPO_FILE="$(realpath "$REPO_FILE")"
  132.      bname="$(basename "$REPO_FILE")"  
  133.      ALL_REPO_DB_PATHS+=( ["$bname"]="$REPO_FILE" )
  134.      #echo "ALL_REPO_DB_PATHS=${ALL_REPO_DB_PATHS[@]}"
  135.      #echo "ALL_REPO_DB_PATHS=${ALL_REPO_DB_PATHS[@]}"
  136.  done < <( find "$REPO_DB_FILE_DIR" -name 'Packages-*' )
  137. #}  
  138. #mk_all_repo_file_list_arry
  139. #echo "exited: mk_all_repo_file_list_arry()"
  140. #echo "ALL_REPO_DB_PATHS=${ALL_REPO_DB_PATHS[@]}"
  141.  
  142. #Moved after the function repo_file_list
  143. #mk_repo_file_list_arry
  144. # but disable colours if called as gpkg, or the ENV variable PKG_NO_COLOURS is true
  145. if [ "`basename $0`" != "pkg" -o "$PKG_NO_COLOURS" = true ]; then
  146.   green='' red='' magenta='' lightblue='' yellow='' bold='' endcolour=''
  147. fi
  148.  
  149. # report errors better than just echo
  150. error(){
  151.   echo -e 1>&2 "${red}Error:${endcolour} $1"
  152. }
  153.  
  154. # for correct error output, trap commands early
  155. cleanup(){
  156.   rm -f ${TMPDIR}/missing_dep* ${TMPDIR}/installed_pkg* ${TMPDIR}/all_dep* ${TMPDIR}/DEP_DONE /tmp/pkg/list_deps_busy ${TMPDIR}/pkg_file_list ${TMPDIR}/dependents_list ${TMPDIR}/DEP_DONE ${TMPDIR}/ldd_file_list &>/dev/null
  157.   rm -rf /tmp/pkg/build_pkg/ &>/dev/null
  158.  
  159.   # prevent double msg output if error=130 (user Ctrl-C)
  160.   [ -f /tmp/pkg/error130 -a "$2" = '130' ] && exit "$2"
  161.   # make the tmp dir we'll use
  162.   mkdir -p /tmp/pkg &>/dev/null
  163.   # only add line numbers if it was given
  164.   [ "$1" != 'none' ] && lineno="line $1, " || lineno=''
  165.   # get the right error msg
  166.   case "$2" in
  167.   130) echo; msg="User cancelled operation!"; touch /tmp/pkg/error130;;
  168.   8)   msg="Package URL invalid.";;
  169.   7)   msg="Package extraction failed.";;
  170.   6)   msg="Missing package file.";;
  171.   5)   msg="Missing directory.";;
  172.   4)   msg="Copy failed.";;
  173.   3)   msg="Setup failed.";;
  174.   2)   msg="Code error!";;
  175.   1)   exit 1 ;; # user gave wrong options or pkg name, not serious
  176.   *)   msg="Error code = $2" ;;
  177.   esac
  178.   # print msg
  179.   [ "$2" != "0" ]  && echo -e 1>&2 "${red}Error${endcolour} ${2}: ${lineno}$msg"
  180.   exit $2
  181. }
  182. trap 'cleanup ${LINENO:-none} $?' EXIT INT TERM
  183.  
  184. #run as root only
  185. whoami=$(whoami) #s243a Copied to before line #40. See issue: http://murga-linux.com/puppy/viewtopic.php?p=1030838#1030838
  186. [ "$whoami" != "root" ] && echo "You must be root to run Pkg." && exit 1
  187.  
  188. #080413 make sure all config files are present, copy from /etc if need be
  189. if [ ! -f ${PKGRC} ]; then
  190.   echo "Restoring default settings from /etc/pkg"
  191.   [ ! -d /etc/pkg/ ] && error "Default config files  in /etc/pkg/ not found." && exit 3
  192.   mkdir -p "$HOME/.pkg"
  193.   [ ! -d "$HOME/.pkg" ] && error "Default user config dir '$HOME/.pkg/' could not be created." && exit 3
  194.   cp --force -a /etc/pkg/* "$HOME/.pkg/" || exit 3
  195. fi
  196.  
  197. # get puppy distro env vars
  198. [ -f /etc/rc.d/PUPSTATE ]                   && . /etc/rc.d/PUPSTATE                    #this has PUPMODE and SAVE_LAYER.
  199. [ -f /etc/DISTRO_SPECS ]                    && . /etc/DISTRO_SPECS                     #has DISTRO_BINARY_COMPAT, DISTRO_COMPAT_VERSION
  200. [ -f /etc/rc.d/BOOTCONFIG ]                 && . /etc/rc.d/BOOTCONFIG                  #has EXTRASFSLIST PREVUNIONRECORD, LASTUNIONRECORD (sfs stuff)
  201. [ -f /etc/xdg/menus/hierarchy ]             && . /etc/xdg/menus/hierarchy              #w478 has PUPHIERARCHY variable.
  202. [ -f $PKGS_DIR/DISTRO_PKGS_SPECS ]    && . $PKGS_DIR/DISTRO_PKGS_SPECS     #has lot sof essential info
  203. [ -f $PKGS_DIR/PKGS_MANAGEMENT ]      && . $PKGS_DIR/PKGS_MANAGEMENT       #has PKG_NAME_IGNORE, PKG_PET_THEN_BLACKLIST_COMPAT_KIDS, PKG_REPOS_ENABLED
  204. [ -f $PKGS_DIR/DISTRO_COMPAT_REPOS ]  && . $PKGS_DIR/DISTRO_COMPAT_REPOS   #has repo URL related vars
  205.  
  206. # set the package name suffix appended to combined pkgs (pkg+deps)
  207. CP_SUFFIX="WITHDEPS_${DISTRO_FILE_PREFIX}"
  208.  
  209. # set correct arch for repo URLs (used by some slack-pup repos)
  210. case "$DISTRO_TARGETARCH" in
  211.   x86)    DBIN_ARCH=i486                ;;
  212.   x86_64) DBIN_ARCH=x86_64 ; DSUFFIX=64 ;;
  213.   *)      DBIN_ARCH=i486                ;;
  214. esac
  215.  
  216. # needed for some debian based repos
  217. case $DISTRO_COMPAT_VERSION in
  218.   wheezy) DDB_COMP=bz2 ;; # older versions
  219.   *)      DDB_COMP=xz  ;;
  220. esac
  221.  
  222. # now create 'layers-installed': will contain builtins (and devx packages, if devx is loaded)
  223. #130511 need to include devx-only-installed-packages, if loaded...
  224. if which gcc &>/dev/null; then
  225.   [ ! -f /tmp/ppm-layers-installed-packages ] && cp -f "$WOOF_INST_PKGS_FILE" /tmp/ppm-layers-installed-packages &>/dev/null
  226.   cat "$PET_SPEC_DIR_DEFAULT/devx-only-installed-packages" >> /tmp/ppm-layers-installed-packages &>/dev/null
  227.   sort -u /tmp/ppm-layers-installed-packages > "$LAYER_INST_PKGS_FILE" &>/dev/null
  228. else
  229.   cp -f "$WOOF_INST_PKGS_FILE" "$LAYER_INST_PKGS_FILE" &>/dev/null
  230. fi
  231.  
  232. # set $DIRECTSAVEPATH (where we want to install pkgs)
  233. if [ $PUPMODE -eq 3 -o $PUPMODE -eq 7 -o $PUPMODE -eq 13 ]; then
  234.   DIRECTSAVEPATH="/initrd${SAVE_LAYER}" #SAVE_LAYER is in /etc/rc.d/PUPSTATE.
  235. elif [ "$PUPMODE" = "2" ]; then
  236.   DIRECTSAVEPATH=""
  237. fi
  238. # s243a: Need the real path to avoid overwriting sylinks. See:
  239. # http://murga-linux.com/puppy/viewtopic.php?p=1030958#1030958  
  240. # https://github.com/puppylinux-woof-CE/woof-CE/issues/1469#issuecomment-505706014
  241. [ -L "$DIRECTSAVEPATH" ] && DIRECTSAVEPATH="$(readlink "$DIRECTSAVEPATH")"
  242. # get repo details, workdir, search settings and so on.. you could
  243. # you can also add any ENVIRONMENT VARS above to PKGRC, to override the defaults
  244. . ${PKGRC}
  245.  
  246. # set and create workdir if no valid dir set
  247. [ ! -d "$WORKDIR" ] && mkdir -p "$WORKDIR"
  248. if [ ! -d "$WORKDIR" ]; then
  249.   error "Can't create $WORKDIR. Please create it."
  250.   exit 3
  251. fi
  252. WORKDIR=~/pkg
  253.  
  254. # add to tab completion settings to bashrc and print hint
  255. if [ -z "$PKG_TAB_COMPLETION" ]; then
  256.   if [ "$(grep 'export PKG_TAB_COMPLETION=true' ~/.bashrc)" = "" ]; then
  257.     # add to bashrc
  258.     echo "" >> ~/.bashrc
  259.     echo "# enable $APPNAME $APPVER TAB completion" >> ~/.bashrc
  260.     echo "export PKG_TAB_COMPLETION=true" >> ~/.bashrc
  261.     echo ". /etc/bash_completion.d/pkg 2>/dev/null" >> ~/.bashrc
  262.   fi
  263. fi
  264.  
  265. # make tmp dir writable by everyone if needed
  266. if [ ! -d "$TMPDIR" ]; then
  267.   mkdir -p "$TMPDIR" 2>/dev/null
  268.   chmod -R 1777 /tmp/pkg/ 2>/dev/null
  269. fi
  270.  
  271. # if no tmp dir created or accessible, exit
  272. [ ! -d "$TMPDIR" ] && error "Cannot create temp dir ${lightblue}$TMPDIR${endcolour}" && exit 3
  273.  
  274. # support aliases, create ALIASES tmp file
  275. 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'
  276. [ ! -f "$TMPDIR/pkg_aliases" ] && echo "$PKG_NAME_ALIASES" | tr ' ' '\n' > $TMPDIR/pkg_aliases
  277.  
  278. # remove error code flag if we got here
  279. rm -f /tmp/pkg/error130 &>/dev/null
  280.  
  281. # if not first run, and no options given, print the help
  282. [ ! -f ~/.pkg/firstrun -a "$1" = "" ] && $SELF -H && exit 1 #200913 more help by default
  283.  
  284. #==================  setup script vars  =====================#
  285.  
  286.  
  287.  
  288. #====================  main functions  ======================#
  289.  
  290. set -a
  291.  
  292. # utility funcs
  293.  
  294. print_usage(){                    # print usage text. Requires $1, a valid Pkg cmd FUNCLIST
  295.  
  296.   local examples_file=/usr/share/pkg/docs/examples.txt
  297.  
  298.   [ -f $examples_file ] && source $examples_file
  299.  
  300.   # get and print the usage info from its usage file
  301.   if [ -f /usr/share/pkg/docs/usage/$1 -a "$1" ]; then
  302.     source /usr/share/pkg/docs/usage/$1
  303.     echo -e "\n$USAGE"
  304.     # show examples, taken from $EXAMPLES, if they exist
  305.     if [ "`echo "$EXAMPLES" | grep "$SELF" | grep "\-$1"`" != '' ]; then
  306.       echo -e "\n${bold}Usage Examples${endcolour}:\n"
  307.       echo -e "$EXAMPLES" | grep "$SELF " | grep "$1 "
  308.     fi
  309.   else
  310.     echo -e "Usage: ${bold}$SELF usage CMD${endcolour}"
  311.     echo
  312.     echo "Commands:"
  313.     echo -e "add-repo       get-only         repo-convert
  314. add-source     help             repo-dep-scope
  315. all-pkgs       help-all         repo-file-list
  316. add            remove           repo-info
  317. ask            install          repo-list
  318. autoclean      install-all      repo-pkg-scope
  319. bleeding-edge  list-deps        repo-update
  320. clean          list-downloaded  search
  321. contents       list-installed   search-all
  322. deb2pet        names            sfs2pet
  323. delete         names-all        sfs-combine
  324. delete-all     names-exact      show-config
  325. deps           names-exact-all  split
  326. deps-all       pet2sfs          tgz2pet
  327. deps-check     pet2tgz          uninstall
  328. deps-download  pet2txz          uninstall-all
  329. dir2pet        build            unpack
  330. dir2sfs        build-list       update-sources
  331. dir2tgz        pkg-combine      version
  332. download       installed        which
  333. examples       repack           which-repo
  334. extract        status           rdep-check
  335. force          update           what-needs
  336. func-list      workdir          rm-repo
  337. txz2pet        dir2repo
  338.  
  339. Usage: ${bold}$SELF usage CMD${endcolour}"
  340.     #cd /usr/share/pkg/docs/usage/; echo `ls` | fold -w 70 -s
  341.   fi
  342.   exit 1
  343. }
  344.  
  345.  
  346. func_list(){                      # list all functions available in this script FUNCLIST
  347.   [ -f $TMPDIR/func_list ] && cat $TMPDIR/func_list | sort && exit 0
  348.   grep "(){" $0 | grep '#' | grep FUNCLIST | sed -e 's|^| |g' -e 's|{||g' -e 's|  | |g' -e 's|# ||g' -e 's| FUNCLIST||g' | grep -v 'FUNCLIST $0' | sort > $TMPDIR/func_list
  349.   cat $TMPDIR/func_list | sort
  350.   exit 0
  351. }
  352.  
  353.  
  354. get_pkg_ext(){                    # return file extension of $1 FUNCLIST
  355.  
  356.   # get valid usage or exit
  357.   [ ! "$1" ] && echo 'Usage: get_pkg_ext PKGNAME' && exit 1
  358.  
  359.   local pkg_installed=''
  360.   local pkg_filename=''
  361.   local pkg_ext=''
  362.   local pkg_name=''
  363.  
  364.   # check given file type, see if it matches our supported pkg types
  365.   case "$1" in
  366.     *.bz2)        echo bz2        ;;
  367.     *.pet)        echo pet        ;;
  368.     *.deb)        echo deb        ;;
  369.     *.pkg.tar.gz) echo pkg.tar.gz ;;
  370.     *.pkg.tar.xz) echo pkg.tar.xz ;;
  371.     *.pkg.tgz)    echo pkg.tgz    ;;
  372.     *.pkg.txz)    echo pkg.txz    ;;
  373.     *.tar.bz2)    echo tar.bz2    ;;
  374.     *.tar.lzma)   echo tar.lzma   ;;
  375.     *.tar.gz)     echo tar.gz     ;;
  376.     *.tar.xz)     echo tar.xz     ;;
  377.     *.tbz)        echo tbz        ;;
  378.     *.tgz)        echo tgz        ;;
  379.     *.tlz)        echo tlz        ;;
  380.     *.txz)        echo txz        ;;
  381.     *.gz)         echo gz         ;;
  382.     *.xz)         echo xz         ;;
  383.     *.rpm)        echo rpm        ;;
  384.     *.sfs)        echo sfs        ;;
  385.     #*.tcz)        echo tcz       ;;
  386.     #*.tpkg)       echo tpkg      ;;
  387.     #*.apk)        echo apk       ;;
  388.     *)
  389.       # if it's an installed pkg, get the extension from $PKGS_DIR/*
  390.       pkg_installed=`list_installed_pkgs "$1" | grep -m1 "^$1"`
  391.  
  392.       # if $pkg_check not empty, then $1 is an installed pkg
  393.       if [ "$pkg_installed" != '' ]; then
  394.         pkg_filename=`cut -f8 -d'|' "$USER_SPEC_FILE" |grep -m1 ^$1`
  395.         [ "$pkg_filename" = '' ] && pkg_filename=`cut -f8 -d'|' "${SPEC_DB_PATHS[@]}" |grep -m1 ^$1`
  396.         [ "$pkg_filename" != '' ] && pkg_ext=`get_pkg_ext "$pkg_filename"`
  397.       else
  398.         # if it's a repo package, get it's extension from $PKGS_DIR/*
  399.         pkg_filename=`grep -m1 "^$1" "${REPO_DB_PATHS[@]}" |cut -f8 -d'|'`
  400.        [ "$pkg_filename" != '' ] &&  pkg_ext=`get_pkg_ext "$pkg_filename"`
  401.       fi
  402.       [ "$pkg_ext" != '' ] && echo $pkg_ext
  403.       ;;
  404.   esac
  405. }
  406.  
  407.  
  408. get_pkg_version(){                # returns version of PKGNAME ($1) FUNCLIST
  409.  
  410.   # exit if no pkg given
  411.   [ ! "$1" ] && exit 1
  412.  
  413.   local PKGNAME=`get_pkg_name "$1"`
  414.   local PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  415.   local pkg_ext=`get_pkg_ext "$PKGNAME"`
  416.   local pkg_version=''
  417.  
  418.   PKGNAME=$(basename "$PKGNAME" .$pkg_ext)
  419.  
  420.   pkg_version=`echo $PKGNAME | sed -e "s/^${PKGNAME_ONLY}_//g"`
  421.   # if $pkg_version is same as $PKGNAME, try cutting again, using '-' instead of '_'
  422.   [ "$pkg_version" = "$PKGNAME" ] && pkg_version=`echo $PKGNAME | sed -e "s/^${PKGNAME_ONLY}-//g"`
  423.  
  424.   # now trim off the other unneeded bits
  425.   pkg_version=`echo  $pkg_version | sed \
  426.     -e "s/^-//g" \
  427.     -e "s/^_//g" \
  428.     -e "s/^DEV-//g" \
  429.     -e "s/^DOC-//g" \
  430.     -e "s/^NLS-//g" \
  431.     -e "s/^dev-//g" \
  432.     -e "s/^doc-//g" \
  433.     -e "s/^nls-//g" \
  434.     -e "s/+deb.*//g" \
  435.     -e "s/+ubuntu.*//g" \
  436.     -e "s/-i[346].*//g" \
  437.     -e "s/-x[86].*//g" \
  438.     -e "s/-pup.*//g" \
  439.     -e "s/-q[0-9].*//g" \
  440.     -e "s/-WITHDEPS.*//g" \
  441.     -e "s/-PLUSDEPS.*//g" \
  442.     -e 's@\\\@@g'`
  443.  
  444.   # get $REPONAME and $EX
  445.   . ${PKGRC}
  446.  
  447.   [ "$pkg_ext" = '' ] && pkg_ext=$EX
  448.  
  449.   echo $pkg_version
  450.   return 0
  451. }
  452.  
  453.  
  454. get_pkg_name_only_from_ext(){     # return package name from $1 (must include file extension)
  455.  
  456.   # get valid usage or exit
  457.   [ ! "$1" ] && echo 'Usage: get_pkg_name_only_from_ext <PKG_FILENAME>' && exit 1
  458.  
  459.   # get the extension of the given package name, if any
  460.   local pkg_ext=`get_pkg_ext "$1"`
  461.   # get the name without path and extension
  462.   local pkg_name=`basename "$1" .$pkg_ext`
  463.   local PKGNAME=''
  464.   local PKGNAME_ONLY=''
  465.   local DB_pkgrelease=''
  466.   local prPATTERN=''
  467.   local DB_version=''
  468.   local xDB_version=''
  469.   local xPATTERN=''
  470.  
  471.   #split PKGMAIN, ex: FULLPKGNAME=xvidtune-1.0.1-i486-1.tgz has PKGNAME=xvidtune-1.0.1
  472.   case $pkg_ext in
  473.     deb)
  474.      #deb ex: xsltproc_1.1.24-1ubuntu2_i386.deb  xserver-common_1.5.2-2ubuntu3_all.deb
  475.      PKGNAME_ONLY="`echo -n "$pkg_name" | cut -f 1 -d '_'`"
  476.      DB_pkgrelease="`echo -n "$pkg_name" | rev | cut -f 2 -d '_' | cut -f 1 -d '-' | rev`"
  477.      prPATTERN="s%\\-${DB_pkgrelease}.*%%"
  478.      PKGNAME="`echo -n "$pkg_name" | sed -e "$prPATTERN" 2>/dev/null`"
  479.     ;;
  480.     pet)
  481.      PKGNAME="$pkg_name"
  482.      DB_version="`echo -n "$PKGNAME" | grep -o '\\-[0-9].*' | sed -e 's%^\-%%'`"
  483.      xDB_version="`echo -n "$DB_version" | sed -e 's%\\-%\\\\-%g' -e 's%\\.%\\\\.%g'`"
  484.      xPATTERN="s%${xDB_version}%%"
  485.      PKGNAME_ONLY="`echo -n "$PKGNAME" | sed -e "$xPATTERN" -e 's%\\-$%%' 2>/dev/null`"
  486.     ;;
  487.     tgz|txz)
  488.      #slack ex: xvidtune-1.0.1-i486-1.tgz  printproto-1.0.4-noarch-1.tgz
  489.      PKGNAME="`echo -n "$pkg_name" | sed -e 's%\\-i[3456]86.*%%' -e 's%\\-noarch.*%%'`"
  490.      DB_version="`echo -n "$PKGNAME" | grep -o '\\-[0-9].*' | sed -e 's%^\\-%%'`"
  491.      xDB_version="`echo -n "$DB_version" | sed -e 's%\\-%\\\\-%g' -e 's%\\.%\\\\.%g'`"
  492.      xPATTERN="s%${xDB_version}%%"
  493.      PKGNAME_ONLY="`echo -n "$PKGNAME" | sed -e "$xPATTERN" -e 's%\-$%%' 2>/dev/null`"
  494.     ;;
  495.     tar.gz)
  496.      #arch ex: xproto-7.0.14-1-i686.pkg.tar.gz  trapproto-3.4.3-1.pkg.tar.gz
  497.      PKGNAME="`echo -n "$pkg_name" | sed -e 's%\\-i[3456]86.*%%' -e 's%\\.pkg$%%' | rev | cut -f 2-9 -d '-' | rev`"
  498.      DB_version="`echo -n "$PKGNAME" | grep -o '\\-[0-9].*' | sed -e 's%^\\-%%'`"
  499.      xDB_version="`echo -n "$DB_version" | sed -e 's%\\-%\\\\-%g' -e 's%\\.%\\\\.%g'`"
  500.      xPATTERN="s%${xDB_version}%%"
  501.      PKGNAME_ONLY="`echo -n "$PKGNAME" | sed -e "$xPATTERN" -e 's%\\-$%%' 2>/dev/null`"
  502.     ;;
  503.     rpm) #110523
  504.      #exs: hunspell-fr-3.4-1.1.el6.noarch.rpm
  505.      PKGNAME="$pkg_name"
  506.      DB_version="`echo -n "$PKGNAME" | grep -o '\\-[0-9].*' | sed -e 's%^\-%%'`"
  507.      xDB_version="`echo -n "$DB_version" | sed -e 's%\\-%\\\\-%g' -e 's%\\.%\\\\.%g'`"
  508.      xPATTERN="s%${xDB_version}%%"
  509.      PKGNAME_ONLY="`echo -n "$PKGNAME" | sed -e "$xPATTERN" -e 's%\\-$%%' 2>/dev/null`"
  510.     ;;
  511.   esac
  512.   [ "$PKGNAME_ONLY" != '' ] && echo $PKGNAME_ONLY || echo "$1"
  513. }
  514.  
  515.  
  516. get_pkg_name_only(){              # return pkg name only from $1, with no version or suffix FUNCLIST
  517.  
  518.   # exit if no valid options
  519.   [ ! "$1" ] && exit 1
  520.  
  521.   # get config settings, inc current repo file
  522.   #. ${PKGRC}
  523.  
  524.   local pkg_name=''
  525.   local pkg_name_only=''
  526.   local name_only=''
  527.   local pkg_ext=`get_pkg_ext "$1"`
  528.   local repo_of_pkg=''
  529.   local repo_pkg_with_ext=''
  530.   local repo_ext=$EX      # from $PKGRC
  531.  
  532.   # check the repos
  533.   local pkg_name_only="`grep -m1 "|${1}|" "${REPO_DB_PATHS[@]}" 2>/dev/null | cut -f2 -d'|'`"
  534.   [ "$pkg_name_only" = '' ] && pkg_name_only="`grep -m1 "^${1}|" "${REPO_DB_PATHS[@]}" 2>/dev/null | cut -f2 -d'|' | head -1`"
  535.   [ "$pkg_name_only" = '' ] && pkg_name_only="`grep -m1 "|${1}.${pkg_ext}|" "${REPO_DB_PATHS[@]}" 2>/dev/null | cut -f2 -d'|' | head -1`"
  536.  
  537.   pkg_name_only=${pkg_name_only// */}
  538.  
  539.   # if we found it, print it and exit
  540.   if [ "$pkg_name_only" != '' ]; then
  541.       echo ${pkg_name_only}
  542.       return 0
  543.   fi
  544.  
  545.   # if not, but we have an extension, try  get_pkg_name_only_from_ext()
  546.   case $DISTRO_BINARY_COMPAT in ubuntu|trisquel|debian|devuan)
  547.     name_only=`get_pkg_name_only_from_ext "$(basename "${1}.${repo_ext}")"| head -1`
  548.     name_only=${name_only// */}
  549.     [ "$name_only" != '' -a "$name_only" != "$1" ] && echo $name_only && return 0
  550.     ;;
  551.   esac
  552.  
  553.   ## ...if we didn't find the name using the above, do the horrible checks below
  554.  
  555.   # replace the dash before the version number with an @ symbol.
  556.   # INFO: sed /-[^-][^+][^a-zA-Z][0-9.]*/ (hopefully?) replaces '-$VER' with '@'
  557.   # not including when $VER starts with a number/letter
  558.   pkg_name_only="`echo "$(basename "${1}")" | sed -e 's/-[^-][^+][^a-zA-Z][0-9.]*/@/'`"
  559.  
  560.   # do 'ioquake3+u20130504' => 'oquake3'
  561.   pkg_name_only="`echo "${pkg_name_only}" | sed -e 's/+[a-z]/@/'`"
  562.  
  563.   # note, we haven't cut away version numbers preceeded with underscores (_) yet
  564.  
  565.   # if the version number was preceeded by an underscore, the version
  566.   # will still be in the $pkg_name_only - pkgname_2.3.4 - so cut it out
  567.   if [ "`echo "$pkg_name_only" | grep -o "_[0-9]\."`" != '' ]; then
  568.     pkg_name_only="`echo "$pkg_name_only" | sed -e 's/_[^a-z][^A-Z][0-9.]*/@/'`"
  569.  
  570.   # maybe the underscore preceeds a version that starts with a letter
  571.   elif [ "`echo "$pkg_name_only" | grep -o "_[a-zA-Z][0-9]\."`" != '' ]; then
  572.     pkg_name_only="`echo "$pkg_name_only" | sed -e 's/_[a-zA-Z][0-9.]*/@/'`"
  573.   fi
  574.  
  575.   # now cut away everything after the @, that will leave only the package name
  576.   pkg_name_only1="${pkg_name_only/@*/}"
  577.   pkg_name_only="$pkg_name_only1"
  578.  
  579.   # we might still have foo_bar_v1.2.3, so lets chop off * after the last _
  580.   if [ "`echo "$pkg_name_only" | grep -o "_[a-zA-Z][0-9]\."`" != '' ]; then
  581.     pkg_name_only="${pkg_name_only/_[a-zA-Z][0-9]*/}"
  582.   fi
  583.  
  584.   # another chop, we might have foo_bar_vv1.2.3, so lets chop off
  585.   # everything after the last underscore, if we still have version numbers
  586.   if [ "`echo "$pkg_name_only" | grep -o "_[a-zA-Z][a-zA-Z][0-9]\."`" != '' ]; then
  587.     pkg_name_only="${pkg_name_only/_[a-zA-Z][a-zA-Z][0-9]*/}"
  588.   fi
  589.  
  590.   # we might still have foo_bar_vvv1.2.3, so lets chop off
  591.   # everything after the last underscore, if we still have version numbers
  592.   if [ "`echo "$pkg_name_only" | grep -o "_[a-zA-Z*][0-9]\."`" != '' ]; then
  593.     pkg_name_only="${pkg_name_only/_[a-zA-Z*][0-9]*/}"
  594.   fi
  595.  
  596.   # chop again, we might have abc_xwy-zzz1.2.3-blah-etc, so lets chop off
  597.   # everything after the last dash-before-number
  598.   if [ "`echo "$pkg_name_only" | grep -o "\-[a-zA-Z*]*[0-9]\."`" != '' ]; then
  599.     pkg_name_only="${pkg_name_only/-[a-zA-Z*]*[0-9]*/}"
  600.   fi
  601.  
  602.   # another fix, if we still have PKGNAME-1.2, remove the '-1.2'
  603.   if [ "`echo "$pkg_name_only" | grep -o "\-[0-9]"`" != '' ]; then
  604.     pkg_name_only="${pkg_name_only/-[0-9]*/}"
  605.   fi
  606.  
  607.   # the sed bit changes 'liblzma5+20120614' to 'liblzma5'
  608.   [ "$pkg_name_only" != '' ] && echo $pkg_name_only | sed -e 's/++/+++/g' -e 's/+[a-z0-9].*//g' || return 1
  609. }
  610.  
  611.  
  612. get_pkg_name(){                   # return full pkg name (with version) from $1 FUNCLIST
  613.  
  614.   # get config settings, inc current repo file
  615.   #. ${PKGRC}
  616.  
  617.   local pkg_ext=''
  618.   local pkg_name=''
  619.   local full_name=''
  620.   local supported_repo_files=''
  621.   local repo_contents=''
  622.  
  623.   # get the extension of the given package name, if any
  624.   local pkg_ext=`get_pkg_ext "$1"`
  625.   # get the name without path and extension
  626.   local pkg_name="`basename "$1" .$pkg_ext`"
  627.  
  628.   # get the repos files in the correct (fall back) order, then add file paths
  629.  
  630.   #s243a: `repo_file_list` is called in mk_repo_file_list_arry
  631.   #supported_repo_files="`repo_file_list | sed -e "s#^#$PKGS_DIR/#g"`"
  632.  
  633.   # get the relevant repo contents (fields 1,2,8) from all repos,
  634.   # then add pipes to line start/end for easier parsing later, then do
  635.   # a basic search, for $pkg* (we'll refine it later), to avoid 'Argument list too long'..
  636.   #cut -f1,2,8 "$USER_SPEC_FILE" "$PET_SPEC_DIR_DEFAULT/woof-installed-packages" $supported_repo_files | sed -e "s/^/|/g" -e "s/$/|/g" 2>/dev/null | grep -i "|$pkg_name" > ${TMPDIR}/repo_contents
  637.   cut -f1,2,8 "$USER_INST_PKGS_FILE" "$WOOF_INST_PKGS_FILE" "${REPO_DB_PATHS[@]}" | sed -e "s/^/|/g" -e "s/$/|/g" 2>/dev/null | grep -i "|$pkg_name" > ${TMPDIR}/repo_contents
  638.   # get fields 1,2 and 8 (the 3 name fields) from all supported repo files, then
  639.   # add '|' to start and end of lines, then find exact match of $pkg_name..
  640.   # if no exact match, look for $pkg_name-*, then $pkg_name_*
  641.   if [ -f ${TMPDIR}/repo_contents ]; then
  642.     full_name=`cat ${TMPDIR}/repo_contents 2>/dev/null| grep -m1 "|${pkg_name}|"` \
  643.       || full_name=`cat ${TMPDIR}/repo_contents 2>/dev/null|grep -m1 "|${pkg_name//-*/}|"` \
  644.       || full_name=`cat ${TMPDIR}/repo_contents 2>/dev/null|grep -m1 "|${pkg_name//_*/}|"` \
  645.       || full_name=`cat ${TMPDIR}/repo_contents 2>/dev/null|grep -m1 "|${pkg_name}-"` \
  646.       || full_name=`cat ${TMPDIR}/repo_contents 2>/dev/null|grep -m1 "|${pkg_name}_"` \
  647.       || full_name=`cat ${TMPDIR}/repo_contents 2>/dev/null|grep -m1 "|${pkg_name}"` \
  648.       || full_name=`cat ${TMPDIR}/repo_contents 2>/dev/null|grep -m1 "^${pkg_name}|"` \
  649.       || full_name=`cat ${TMPDIR}/repo_contents 2>/dev/null|grep -m1 "^${pkg_name}-"` \
  650.       || full_name=`cat ${TMPDIR}/repo_contents 2>/dev/null|grep -m1 "^${pkg_name}_"`
  651.  
  652.     rm ${TMPDIR}/repo_contents &>/dev/null
  653.   fi
  654.  
  655.   # if we found pkg in repo, keep only the package name ('|pkg-123|pkg|blah|' -> 'pkg-123')
  656.   [ "$full_name" != '' ] && full_name=`echo $full_name| cut -f2 -d '|' | tr -d '|'` || full_name=$pkg_name
  657.  
  658.   # if we have a package name, return it here and leave the func
  659.   [ "$full_name" != '' ] && echo $full_name && return 0
  660.  
  661.   # if no pkg found in repos, we cant translate/lookup its longer name,
  662.   # so just return what we got, without path, without pkg extension
  663.   [ "$full_name" = '' ] && echo "`basename "$pkg_name" .$pkg_ext`"
  664. }
  665.  
  666.  
  667. is_blacklisted_pkg(){             # return true if PKGNAME ($1) is blacklisted FUNCLIST
  668.   # exit if no valid options
  669.   [ ! "$1" -o "$1" = "-" ] && print_usage pkg-installed && exit 1
  670.  
  671.   local EX=''
  672.   local PKGNAME=''
  673.   local PKGNAME_ONLY=''
  674.  
  675.   # get pkg extension
  676.   EX=`get_pkg_ext "$1"`
  677.  
  678.   # strip away extension and path
  679.   PKGNAME="$(basename "$1" .$EX)"
  680.   #PKGNAME=`get_pkg_name "$PKGNAME"`
  681.  
  682.   # get the package name without version
  683.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  684.  
  685.   [ "`echo "$PKG_NAME_IGNORE" | grep "$PKGNAME_ONLY "`" != '' ] && echo true || echo false
  686. }
  687.  
  688.  
  689. is_installed_pkg(){               # return true if PKGNAME ($1) is installed, else false FUNCLIST
  690.  
  691.   # exit if no valid options
  692.   [ ! "$1" -o "$1" = "-" ] && print_usage pkg-installed && exit 1
  693.  
  694.   local EX=''
  695.   local PKGNAME=''
  696.   local PKGNAME_ONLY=''
  697.   local check=''
  698.  
  699.   # get pkg extension
  700.   EX=`get_pkg_ext "$1"`
  701.  
  702.   # strip away extension and path
  703.   PKGNAME="$(basename "$1" .$EX)"
  704.   PKGNAME=`get_pkg_name "$PKGNAME"`
  705.  
  706.   # we cant rely on the strings the user passes us, so...
  707.  
  708.   # get the package name without version
  709.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  710.  
  711.   # exit if no valid package name
  712.   [ "$PKGNAME" = '' -o "$PKGNAME_ONLY" = '' ] && print_usage pkg-installed && exit 1
  713.  
  714.   # check the $PKGS_DIR/*.files
  715.   check="$(find "$PACKAGE_FILE_LIST_DIR" -name "$PKGNAME.files")"
  716.   [ "$check" = '' ] && check="$(find "$PACKAGE_FILE_LIST_DIR" -name "${PKGNAME}"'_*.files')"
  717.   [ "$check" = '' ] && check="$(find "$PACKAGE_FILE_LIST_DIR" -name "${PKGNAME}"'-*.files')"
  718.   [ "$check" = '' ] && check="$(find "$PACKAGE_FILE_LIST_DIR" -name "${PKGNAME}"'*.files')"
  719.  
  720.   # slow but reliable & simple: separately check fields 1,2,8 of the various files listing installed pkgs
  721.   [ "$check" = '' ] && check="`cut -f1 -d'|' "${SPEC_DB_PATHS[@]}" 2>/dev/null| grep -m1 "^$PKGNAME\$"`"
  722.   [ "$check" = '' ] && check="`cut -f1,2 -d'|' "${SPEC_DB_PATHS[@]}" 2>/dev/null| grep -m1 "^$PKGNAME|" | grep -m1 "|$PKGNAME_ONLY\$"`"
  723.   [ "$check" = '' ] && check="`cut -f8 -d'|' "${SPEC_DB_PATHS[@]}" 2>/dev/null| grep -m1 "^$PKGNAME"`"
  724.   [ "$check" = '' ] && check="`HIDE_BUILTINS=false list_installed_pkgs | grep -m1 ^"$PKGNAME"`"
  725.  
  726.   # if any checks above returned a result, $check will not be empty (ash didn't like grep -q, not sure why?)
  727.   [ "$check" != '' ] && echo true || echo false
  728. }
  729.  
  730.  
  731. is_builtin_pkg (){                # return true if $1 is a builtin pkg, else false FUNCLIST
  732.  
  733.   # if $1 is an exact match of a pkg short name,
  734.   # long name or file name in woof-installed-packages,
  735.   # we will return true, else we return false
  736.  
  737.   # exit if no valid input
  738.   [ ! "$1"  ] && exit 1
  739.  
  740.   local pkg_builtin=''
  741.   local pkg_name_only=''
  742.  
  743.   pkg_builtin="`cut -f1,2,8 -d '|' "$WOOF_INST_PKGS_FILE" 2>/dev/null\
  744.    | sed -e 's@^@|@' -e 's@$@|@' \
  745.    | grep -m1 "|$1|"`"
  746.  
  747.   # try again.. if user gave only partial name (geany-1.27), the above would fail
  748.   if [ "$pkg_builtin" = '' ]; then
  749.     pkg_name_only=`get_pkg_name_only "$1"`
  750.  
  751.     # so search for pkg_name* AND pkg_name_only (exact match, should be in field 2)
  752.     if [ "$pkg_name_only" != '' ]; then
  753.       pkg_builtin="`cut -f1,2,8 -d '|' "$WOOF_INST_PKGS_FILE" 2>/dev/null\
  754.        | sed -e 's@^@|@' -e 's@$@|@' \
  755.        | grep "|$1" \
  756.        | grep -m1 "|$pkg_name_only|"`"
  757.     fi
  758.   fi
  759.  
  760.   # print result
  761.   [ "$pkg_builtin" != '' ] && echo true || echo false
  762. }
  763.  
  764.  
  765. is_devx_pkg (){                   # return true if $1 is a pkg in the devx, else false FUNCLIST
  766.  
  767.   # if $1 is an exact match of a pkg short name,
  768.   # long name or file name in devx-only-installed-packages,
  769.   # we will return true, else we return false
  770.  
  771.   # exit if no valid input
  772.   [ ! "$1"  ] && exit 1
  773.  
  774.   local devx_pkg=''
  775.   local pkg_name_only=''
  776.  
  777.   devx_pkg="`cut -f1,2,8 -d '|' "$DEVX_INST_PKGS_FILE" 2>/dev/null\
  778.    | sed -e 's@^@|@' -e 's@$@|@' \
  779.    | grep -m1 "|$1|"`"
  780.  
  781.   # try again.. if user gave only partial name (geany-1.27), the above would fail
  782.   if [ "$devx_pkg" = '' ]; then
  783.     pkg_name_only=`get_pkg_name_only "$1"`
  784.  
  785.     # so search for pkg_name* AND |pkg_name_only| (exact match, should be in field 2)
  786.     if [ "$pkg_name_only" != '' ]; then
  787.       devx_pkg="`cut -f1,2,8 -d '|' "$DEVX_INST_PKGS_FILE" 2>/dev/null\
  788.        | sed -e 's@^@|@' -e 's@$@|@' \
  789.        | grep "|$1" \
  790.        | grep -m1 "|$pkg_name_only|"`"
  791.     fi
  792.   fi
  793.  
  794.   # print result
  795.   [ "$devx_pkg" != '' ] && echo true || echo false
  796. }
  797.  
  798.  
  799. is_usr_pkg (){                    # return true if $1 is a user installed pkg, else false FUNCLIST
  800.  
  801.   # if $1 is an exact match of a pkg short name,
  802.   # long name or file name in user-installed-packages,
  803.   # we will return true, else we return false
  804.  
  805.   # exit if no valid input
  806.   [ ! "$1"  ] && exit 1
  807.  
  808.   local usr_pkg=''
  809.   local pkg_name_only=''
  810.  
  811.   usr_pkg="`cut -f1,2,8 -d '|' "$USER_INST_PKGS_FILE" 2>/dev/null\
  812.    | sed -e 's@^@|@' -e 's@$@|@' \
  813.    | grep -m1 "|$1|"`"
  814.  
  815.   # try again.. if user gave only partial name (geany-1.27), the above would fail
  816.   if [ "$usr_pkg" = '' ]; then
  817.     pkg_name_only=`get_pkg_name_only "$1"`
  818.  
  819.     # so search for pkg_name* AND |pkg_name_only| (exact match, should be in field 2)
  820.     if [ "$pkg_name_only" != '' ]; then
  821.       usr_pkg="`cut -f1,2,8 -d '|' "$USER_INST_PKGS_FILE" 2>/dev/null\
  822.        | sed -e 's@^@|@' -e 's@$@|@' \
  823.        | grep "|$1" \
  824.        | grep -m1 "|$pkg_name_only|"`"
  825.     fi
  826.   fi
  827.  
  828.   # print result
  829.   [ "$usr_pkg" != '' ] && echo true || echo false
  830. }
  831.  
  832.  
  833. is_repo_pkg (){                   # return true if $1 is a pkg in a supported repo, else false FUNCLIST
  834.  
  835.   # if $1 is an exact match of a pkg short name,
  836.   # long name or file name in Packages-*-
  837.   # we will return true, else we return false
  838.  
  839.   # exit if no valid input
  840.   [ ! "$1"  ] && exit 1
  841.  
  842.   local repo_pkg=''
  843.   local pkg_name_only=''
  844.   local all_supported_repofiles
  845.  
  846.   # all repo files, full paths
  847.   #all_supported_repofiles=`repo_file_list | sed "s#^#$PKGS_DIR/#" | tr '\n' ' '`
  848.  
  849.   #Don't need sed, but could use if you want to do wildcards
  850.   #all_supported_repofiles=`repo_file_list | sed 's#$#*#' | xargs find -name  | tr '\n' ' '`
  851.   #echo "repo_file_list | xargs find \"$PKGS_DIR\" -name  | tr '\n' ' '"
  852.   all_supported_repofiles="$(repo_file_list | while read a; do find "$REPO_DB_FILE_DIR" -name "$a"'*'; done  | tr '\n' ' ')"
  853.   # search all repo files for the $1
  854.   repo_pkg="`cut -f1,2,8 -d '|' $all_supported_repofiles 2>/dev/null\
  855.    | sed -e 's/ //g' -e 's@^@|@' -e 's@$@|@' \
  856.    | grep -m1 "|${1}|"`"
  857.  
  858.   # search all repo files for the $1*
  859.   [ "$repo_pkg" = '' ] \
  860.     && repo_pkg="`cut -f1,2,8 -d '|' $all_supported_repofiles 2>/dev/null\
  861.    | sed -e 's/ //g' -e 's@^@|@' -e 's@$@|@' \
  862.    | grep -m1 "|${1}-\?_\?[0-9.a-zA-Z]*|"`"
  863.  
  864.   # try again.. if user gave only partial name (geany-1.27), the above would fail
  865.   if [ "$repo_pkg" = '' ]; then
  866.     pkg_name_only=`get_pkg_name_only "$1"`
  867.  
  868.     # so search for pkg_name* AND |pkg_name_only| (exact match, should be in field 2)
  869.     if [ "$pkg_name_only" != '' ]; then
  870.       repo_pkg="`cut -f1,2,8 -d '|' $all_supported_repofiles 2>/dev/null\
  871.        | sed -e 's@^@|@' -e 's@$@|@' \
  872.        | grep "|$1" \
  873.        | grep -m1 "|$pkg_name_only|"`"
  874.     fi
  875.   fi
  876.  
  877.   # print result
  878.   [ "$repo_pkg" != '' ] && echo true || echo false
  879. }
  880.  
  881.  
  882. is_current_repo_pkg(){            # takes $PKGNAME ($1), returns true or false FUNCLIST
  883.  
  884.   # exit if no valid input
  885.   [ ! "$1"  ] && exit 1
  886.  
  887.   # get current repo ($REPOFILE)
  888.   . ${PKGRC}
  889.  
  890.   local pkg_in_repo
  891.   #s243a: The repo file path
  892.   local RF_PATH="$(realpath "$(find "$REPO_DB_FILE_DIR" -name "$REPOFILE" | head -n 1 )")"
  893.  # check if given pkg ($1) is in the current repo
  894.  pkg_in_repo="`LANG=C cut -f1,2,7,8 -d'|' "$RF_PATH" 2>/dev/null | sed -e "s/^/|/" -e "s/$/|/" | grep -m1 "|${1}|"`"
  895.  
  896.  # print msg
  897.  [ "$pkg_in_repo" != '' ] && echo true || echo false
  898. }
  899.  
  900.  
  901. is_local_pkg(){                   # returns true if $1 is local package file FUNCLIST
  902.  
  903.  # exit if no valid input
  904.  [ ! "$1"  ] && exit 1
  905.  
  906.  . ${PKGRC}
  907.  
  908.  # if $1 is a local file with a supported package extension
  909.  # then we return true, else, we return false
  910.  
  911.  local is_file=''
  912.  local is_local_pkg=false
  913.  
  914.  # first, check we have a local file
  915.  if [ -f "$1" ]; then
  916.  
  917.    # file *probably* has an extension, lets try to get it..
  918.    # the func get_pkg_ext() will return empty if not a valid package extension
  919.    file_ext=`get_pkg_ext "$1"`
  920.  
  921.    # if not empty, it must be a local file, with valid pkg ext (a local pkg)
  922.    [ "$file_ext" != '' ] && is_local_pkg=true
  923.  
  924.  elif [ -f "$CURDIR/$1" ]; then
  925.  
  926.    file_ext=`get_pkg_ext "$CURDIR/$1"`
  927.    [ "$file_ext" != '' ] && is_local_pkg=true
  928.  
  929.  elif [ -f "$WORKDIR/$1" ]; then
  930.  
  931.    file_ext=`get_pkg_ext "$CURDIR/$1"`
  932.    [ "$file_ext" != '' ] && is_local_pkg=true
  933.  
  934.  fi
  935.  
  936.  # print output
  937.  echo $is_local_pkg
  938. }
  939.  
  940.  
  941.  
  942. # RC file funcs
  943.  
  944. set_workdir(){                    # set new WORKDIR in rc file location FUNCLIST
  945.  
  946.  # get latest rc file values
  947.  . ${PKGRC}
  948.  
  949.  # make sure $1 was given and doesn't already exist
  950.  [ ! "$1"  ] && print_usage workdir && exit 1
  951.  [ -e "$1" ] && echo "Error: directory already exists. Choose a new one." && exit 1
  952.  
  953.  # create the dir
  954.  mkdir -p "$1" 2>/dev/null
  955.  
  956.  # if dir not created, exit with error
  957.  [ $? -eq 1  ] && echo "Error: Could not create $1" && exit 1
  958.  [ ! -d "$1" ] && echo "Error: Could not create directory:  $1" && exit 1
  959.  
  960.  # dir was created, so lets copy our pkgs in there
  961.  list_downloaded_pkgs | while read pkg_file
  962.  do
  963.    cp -v "$WORKDIR/$pkg_name" "$1/"
  964.  done
  965.  
  966.  # if copying everything to new dir failed
  967.  if [ $? -eq 1 ]; then
  968.    # print msg
  969.    echo -e "${yellow}Warning:${endcolour}Could not copy packages from $WORKDIR to $1.."
  970.    echo "You should copy all packages in $WORKDIR into $1."
  971.  fi
  972.  
  973.  # update the RC file
  974.  WORKDIR="$1"
  975.  set_config
  976.  echo "Success. Work directory updated."
  977.  exit 0
  978. }
  979.  
  980.  
  981. set_pkg_scope(){                  # one|all set search for pkgs in current repo or all FUNCLIST
  982.  
  983.  # get old values, any we dont set here are not overwritten
  984.  . ${PKGRC}
  985.  
  986.  [ "$1" != "" ] && PKGSCOPE="$1" || PKGSCOPE="$PKGSCOPE"
  987.  
  988.  case "$1" in
  989.    all)
  990.    # set pkg search to all
  991.    PKGSEARCH="list_all_pkg_names"
  992.    PKGSEARCHEXACT="$SELF -nea"
  993.    echo -e "${green}Success:${endcolour} Find packages in all repos."
  994.    ;;
  995.    one)
  996.    # set pkg search to current only
  997.    PKGSEARCH="list_pkg_names"
  998.    PKGSEARCHEXACT="$SELF -ne"
  999.    echo -e "${green}Success:${endcolour} Find packages in current repo ($REPONAME) only."
  1000.    ;;
  1001.    *)
  1002.    PKGSCOPE="one"
  1003.    # set pkg search to current only
  1004.    PKGSEARCH="list_pkg_names"
  1005.    PKGSEARCHEXACT="$SELF -ne"
  1006.    echo "Find packages in current repo ($REPONAME) only."
  1007.    ;;
  1008.  esac
  1009.  
  1010.  set_config #170213
  1011. }
  1012.  
  1013.  
  1014. set_dep_scope(){                  # all|one set search for deps in current repo or all FUNCLIST
  1015.  
  1016.   # get RC file values, so any we dont set here are not overwritten
  1017.   . ${PKGRC}
  1018.  
  1019.  # make sure we have a valid value
  1020.  [ "$1" != "" ] && DEPSCOPE="$1" || DEPSCOPE="$DEPSCOPE"
  1021.  
  1022.  case "$1" in
  1023.    all)
  1024.    # set pkg search to all
  1025.    DEPSEARCH="list_all_pkg_names"
  1026.    DEPSEARCHEXACT="$SELF -nea"
  1027.    echo -e "${green}Success:${endcolour} Find dependencies in all repos."
  1028.    ;;
  1029.    one)
  1030.    # set pkg search to one
  1031.    DEPSEARCH="list_pkg_names"
  1032.    DEPSEARCHEXACT="$SELF -ne"
  1033.    echo -e "${green}Success:${endcolour} Find dependencies in current repo ($REPONAME) only."
  1034.    ;;
  1035.  esac
  1036.  
  1037.  set_config
  1038. }
  1039.  
  1040.  
  1041. set_recursive_deps(){             # yes|no search for deps of deps or not FUNCLIST
  1042.  [ "`echo $1 | grep -E "^yes\$|^no\$"`" = "" ] && print_usage recursive-dep-check && exit 1
  1043.  
  1044.  # get old values, any we dont set here are not overwritten
  1045.  . ${PKGRC}
  1046.  
  1047.  # make sure we have a valid value
  1048.  RDCHECK="$1"
  1049.  [ "$RDCHECK" = "yes" -o "$RDCHECK" = "no" ] || RDCHECK=no
  1050.  
  1051.  set_config
  1052.  
  1053.  # print final msg
  1054.  if [ "$1" = "yes" ]; then
  1055.    echo -e "${green}Success:${endcolour} 'Recursive dependency checking' enabled."
  1056.  else
  1057.    echo -e "${green}Success:${endcolour} 'Recursive dependency checking' disabled"
  1058.  fi
  1059. }
  1060.  
  1061.  
  1062. set_bleeding_edge(){              # yes|no get latest pkgs, ignore fall backs FUNCLIST
  1063.  [ "`echo $1 | grep -E "^yes\$|^no\$"`" = "" ] && echo usage bleeding-edge && exit 1
  1064.  
  1065.  #get old values, any we dont set here are not overwritten
  1066.  . ${PKGRC}
  1067.  
  1068.  # make sure we have a valid value
  1069.  BLEDGE="$1"
  1070.  [ "$BLEDGE" = "yes" -o "$BLEDGE" = "no" ] || BLEDGE=no
  1071.  
  1072.  set_config
  1073.  
  1074.  # print final msg
  1075.  if [ "$1" = "yes" ]; then
  1076.    echo -e "${green}Success:${endcolour} 'Bleeding edge' package search enabled."
  1077.  else
  1078.    echo -e "${green}Success:${endcolour} 'Bleeding edge' package search disabled."
  1079.  fi
  1080. }
  1081.  
  1082.  
  1083. set_autoclean(){                  # yes|no auto delete installed packages from WORKDIR FUNCLIST
  1084.  [ "`echo $1 | grep -E "^yes\$|^no\$"`" = "" ] && print_usage autoclean && exit 1
  1085.  
  1086.  #get old values, any we dont set here are not overwritten
  1087.  . ${PKGRC}
  1088.  
  1089.  # make sure we have a valid value
  1090.  AUTOCLEAN="$1"
  1091.  [ "$AUTOCLEAN" = "yes" -o "$AUTOCLEAN" = "no" ] || AUTOCLEAN=no
  1092.  
  1093.  set_config
  1094.  
  1095.  # print final msg
  1096.  if [ "$1" = "yes" ]; then
  1097.    echo -e "${green}Success:${endcolour} Auto-remove installed packages ENABLED."
  1098.  else
  1099.    echo -e "${green}Success:${endcolour} Auto-remove installed packages DISABLED."
  1100.  fi
  1101. }
  1102.  
  1103.  
  1104. set_config(){                     # update all options in config file FUNCLIST
  1105.  echo "WORKDIR=$WORKDIR"                    > ${PKGRC}
  1106.  echo "REPONAME=$REPONAME"                 >> ${PKGRC}
  1107.  echo "EX=$EX"                             >> ${PKGRC}
  1108.  echo "REPOFILE=$REPOFILE"                 >> ${PKGRC}
  1109.  echo "REPOURL1=$REPOURL1"                 >> ${PKGRC}
  1110.  echo "REPOURL2=$REPOURL2"                 >> ${PKGRC}
  1111.  #140213 add all urls
  1112.  echo "REPOURL3=$REPOURL3"                 >> ${PKGRC}
  1113.  echo "REPOURL4=$REPOURL4"                 >> ${PKGRC}
  1114.  # seach settings
  1115.  echo "PKGSEARCH=\"$PKGSEARCH\""           >> ${PKGRC}
  1116.  echo "PKGSEARCHEXACT=\"$PKGSEARCHEXACT\"" >> ${PKGRC}
  1117.  echo "DEPSEARCH=\"$DEPSEARCH\""           >> ${PKGRC}
  1118.  echo "DEPSEARCHEXACT=\"$DEPSEARCHEXACT\"" >> ${PKGRC}
  1119.  # multiple repo settings
  1120.  echo "REPOFALLBACKS=\"$REPOFALLBACKS\""   >> ${PKGRC}
  1121.  echo "PKGSCOPE=\"$PKGSCOPE\""             >> ${PKGRC}
  1122.  echo "DEPSCOPE=\"$DEPSCOPE\""             >> ${PKGRC}
  1123.  echo "BLEDGE=\"$BLEDGE\""                 >> ${PKGRC}
  1124.  echo "RDCHECK=\"$RDCHECK\""               >> ${PKGRC} #150813
  1125.  echo "AUTOCLEAN=\"$AUTOCLEAN\""           >> ${PKGRC}
  1126.  echo "BUILDTOOL=$BUILDTOOL"               >> ${PKGRC}
  1127. }
  1128.  
  1129.  
  1130. show_config(){                    # show config settings from ~/.pkg/pkgrc FUNCLIST
  1131.  . ${PKGRC}
  1132.  
  1133.  # set default values
  1134.  PKGSCOPETXT="Search for packages in all repos."
  1135.  DEPSCOPETXT="Search for dependencies in all repos."
  1136.  FALLBACKTXT="Accessing other repos in this order:"
  1137.  FALLBACKTXT="$FALLBACKTXT\n`repo_list | grep -v "^$REPONAME" | tr '\n' ' ' | sed -e "s/ /, /g" -e "s/, $//" | fold -w 54 -s`"
  1138.  RDCHECKTXT="Recursive dependency search is NOT enabled."
  1139.  
  1140.  # update with values from PKGRC file
  1141.  [ "$PKGSCOPE" = "one" ] && PKGSCOPETXT="Search for packages in current repo only."
  1142.  [ "$DEPSCOPE" = "one" ] && DEPSCOPETXT="Search for dependencies in current repo only."
  1143.  [ "$BLEDGE"   = "yes" ] && FALLBACKTXT="Bleeding-edge enabled - searching all repos, for the latest packages versions.."
  1144.  [ "$RDCHECK"  = "yes" ] && RDCHECKTXT="Recursive dependency search is enabled."
  1145.  
  1146.  # print current Pkg config
  1147.  echo "==========================="
  1148.  echo "$APPNAME $APPVER"
  1149.  echo "==========================="
  1150.  echo "Config file:  $PKGRC"
  1151.  echo "Packages dir: ${WORKDIR}/"
  1152.  echo "Autoclean:    $AUTOCLEAN"
  1153.  echo
  1154.  echo "Search settings:"
  1155.  #echo "- $HIDEINSTALLEDTXT"
  1156.  echo "- $PKGSCOPETXT"
  1157.  echo "- $DEPSCOPETXT"
  1158.  echo "- $RDCHECKTXT"
  1159.  echo
  1160.  echo "Package Compiling backend:"
  1161.  echo "- $BUILDTOOL"
  1162.  echo
  1163.  echo "Repo details:"
  1164.  echo "- Current Repo: $REPONAME"
  1165.  echo "- Package type: $EX"
  1166.  echo "- Packages:     $(cat "$REPO_DB_FILE_DIR/${REPOFILE}" | wc -l)"
  1167.  echo "- Mirror 1:     `echo $REPOURL1 | cut -f1-3 -d'/' `"
  1168.  [ "$REPOURL2" != "" ] && echo "- Mirror 2:     `echo $REPOURL2 | cut -f1-3 -d'/' `"
  1169.  [ "$REPOURL3" != "" ] && echo "- Mirror 3:     `echo $REPOURL3 | cut -f1-3 -d'/' `"
  1170.  [ "$REPOURL4" != "" ] && echo "- Mirror 4:     `echo $REPOURL4 | cut -f1-3 -d'/' `"
  1171.  echo
  1172.  echo -e "$FALLBACKTXT"
  1173. }
  1174.  
  1175.  
  1176. # repo and source funcs
  1177.  
  1178. get_repo_info(){                  # return details of given repo name ($1) FUNCLIST
  1179.  [ ! "$1" -o "$1" = "-" ] && print_usage repo-info && exit 1
  1180.  
  1181.  REPOLINE="`list_all_sources $1 | sort -u | uniq`" #170214 always get latest info, not info from rcfile
  1182.  
  1183.  # on first run, this might be needed to set the repo correctly
  1184.  [ "$REPOLINE" = '' ] && REPOLINE="`list_all_sources noarch`"
  1185.  
  1186.  REPONAME="`echo $REPOLINE | cut -f1 -d'|'`"
  1187.        EX="`echo $REPOLINE | cut -f2 -d'|'`"
  1188.  REPOFILE="`echo $REPOLINE | cut -f3 -d'|'`"
  1189.  REPOURL1="`echo $REPOLINE | cut -f4 -d'|'`"
  1190.  REPOURL2="`echo $REPOLINE | cut -f5 -d'|'`"
  1191.  REPOURL3="`echo $REPOLINE | cut -f6 -d'|'`"
  1192.  REPOURL4="`echo $REPOLINE | cut -f7 -d'|'`"
  1193.  REPOFALLBACKS="`echo "$REPOLINE"| cut -f8 -d'|'`"
  1194. }
  1195.  
  1196.  
  1197. set_current_repo(){               # set the current repo to use, give repo name as $1 FUNCLIST
  1198.  
  1199.  # print usage if no valid options
  1200.  [ ! "$1" -o "$1" = "-" -o "$1" = "-h" -o "$1" = "--help" ] && print_usage repo && exit 1
  1201.  
  1202.  # get repo details from rc file
  1203.  . ${PKGRC}
  1204.  
  1205.  # remove the default mirror tmp file, we're changing repo and mirrors
  1206.  rm $TMPDIR/CURREPOURL 2>/dev/null
  1207.  
  1208.  # remove old repo files list (used in list_source_files)
  1209.  rm -f ${TMPDIR}/source_files 2>/dev/null
  1210.  
  1211.  # if not updating the scopes or bleeding-edge,leave them as set in rcfile
  1212.  [ "$PKGSCOPE" = "" ] && PKGSCOPE="$PKGSCOPE"
  1213.  [ "$DEPSCOPE" = "" ] && DEPSCOPE="$DEPSCOPE"
  1214.  [ "$BLEDGE" = "" ]   && BLEDGE="$BLEDGE"
  1215.  
  1216.  # if nothing was found in rcfile, we need to set to default
  1217.  [ "`echo $PKGSCOPE | grep -E "^one\$|^all\$"`" = "" ] && PKGSCOPE="one"
  1218.  [ "`echo $DEPSCOPE | grep -E "^one\$|^all\$"`" = "" ] && DEPSCOPE="one"
  1219.  [ "$BLEDGE" = "" ] && BLEDGE="no"
  1220.  
  1221.  # check if $1 is valid repo
  1222.  if [ "$(LANG=C list_sources "$1")" != "" ]; then
  1223.    # set repo details
  1224.    LANG=C get_repo_info "$1"
  1225.    LANG=C set_config #170213
  1226.    LANG=C update_sources 1>/dev/null #update the order of sources to match new fallback list of new current repo
  1227.    LANG=C print_repo_info "$1" #output msg, repo info
  1228.  else
  1229.    # not a valid repo, print message
  1230.    echo "The name '$1' is not a valid repo name. These are:"
  1231.    LANG=C repo_list #250613
  1232.  fi
  1233. }
  1234.  
  1235.  
  1236. repo_list(){                      # list names of all avail repos [optionally matching $1] FUNCLIST
  1237.  
  1238.  # we need to list these repos in the order defined in the RC file
  1239.  # so Pkg can 'fall back' to other repos in that order
  1240.  
  1241.  # get current config
  1242.  . ${PKGRC}
  1243.  
  1244.  # set vars for this func
  1245.  local list=''
  1246.  local repo_file=''
  1247.  local repo_names=`cut -f1 -d'|' ${HOME}/.pkg/sources`
  1248.  local current_fallback_list=`grep "^$REPONAME|" ${HOME}/.pkg/sources | cut -f8 -d'|' | grep -v "^\$"`
  1249.  
  1250.  # get current repo fallback list order.. note if repo not listed in fallback list, it will be appended after
  1251.  for line in $current_fallback_list
  1252.  do
  1253.    # get the repo file
  1254.    repo_file="`grep "^$line|" ${HOME}/.pkg/sources | cut -f3 -d'|'`"
  1255.    # add it to the list
  1256.    [ "$repo_file" != "" ] && list="$list$line "
  1257.  done
  1258.  
  1259.  # now add all other avail repos to the end of fallback list
  1260.  for repo_name in $repo_names
  1261.  do
  1262.    #if not blank, not already in the repo list and not the current repo ($REPONAME from rc file) then add to list
  1263.    [ "$repo_name" != "" -a "`echo "$list" | grep "$repo_name "`" = ""  -a "$repo_name" != "$REPONAME" ] && list="$list$repo_name "
  1264.  done
  1265.  
  1266.  # list the current repo first
  1267.  echo $REPONAME
  1268.  # then the other repos
  1269.  echo "$list" | tr ' ' '\n' | grep -v "^\$"
  1270. }
  1271.  
  1272.  
  1273. repo_file_list(){                 # list available repo files, $1 optional FUNCLIST
  1274.  
  1275.  # we need to list these repos in the order defined in the RC file
  1276.  # so Pkg can 'fall back' to other repos in that order
  1277.  
  1278.  # get current config
  1279.  . ${PKGRC}
  1280.  
  1281.  # set vars for this func
  1282.  local list=''
  1283.  local repo_file=''
  1284.  local repo_files=`cut -f3 -d'|' ${HOME}/.pkg/sources`
  1285.  local current_fallback_list=`grep "^$REPONAME|" ${HOME}/.pkg/sources | cut -f8 -d'|' | grep -v "^\$"`
  1286.  
  1287.  # get current repo fallback list order.. note if repo not listed in fallback list, it will be appended after
  1288.  for line in $current_fallback_list
  1289.  do
  1290.    # get the repo file
  1291.    repo_file="`grep "^$line|" ${HOME}/.pkg/sources | cut -f3 -d'|'`"
  1292.    # add it to the list
  1293.    [ "$repo_file" != "" ] && list="$list$repo_file "
  1294.  done
  1295.  
  1296.  # now add all other avail repos to the end of fallback list
  1297.  for repo_file in $repo_files
  1298.  do
  1299.    #if not blank, not already in the repo list and not the current repo ($REPONAME from rc file) then add to list
  1300.    [ "$repo_file" != "" -a "`echo "$list" | grep "$repo_file "`" = ""  -a "$repo_file" != "$REPOFILE" ] && list="$list$repo_file "
  1301.  done
  1302.  
  1303.  # list the current repo first
  1304.  echo $REPOFILE
  1305.  # then the other repos
  1306.  echo "$list" | tr ' ' '\n' | grep -v "^\$"
  1307. }
  1308. declare -ga REPO_DB_PATHS
  1309. #mk_repo_file_list_arry(){
  1310.  
  1311.  #REPO_DB_PATHS=()
  1312.  #declare -A REPO_DB_PATHS
  1313.  
  1314. #Can't put the loop in the loop in a pipeline if you want to assign array values:
  1315. #https://stackoverflow.com/questions/13091700/why-is-my-array-gone-after-exiting-loop
  1316. # repo_file_list |
  1317. while read REPO_FILE_NAME; do
  1318.    #echo "REPO_FILE_NAME=$REPO_FILE_NAME"
  1319.    RF_FILE_PATH="${ALL_REPO_DB_PATHS[$REPO_FILE_NAME]}"
  1320.    if [ -e "$RF_FILE_PATH" ]; then
  1321.      REPO_DB_PATHS+=( "$RF_FILE_PATH" )
  1322.    fi
  1323. done < <( repo_file_list )
  1324. #echo "REPO_DB_PATHS=${ALL_REPO_DB_PATHS[@]}"
  1325. #echo "REPO_DB_PATHS_keys=${!ALL_REPO_DB_PATHS[@]}"  
  1326. #}
  1327. #mk_repo_file_list_arry
  1328.  
  1329. dir2repo(){                       # create a native-Puppy repo from a dir of packages FUNCLIST
  1330.  
  1331.  # exit if not a valid directory
  1332.  if [ ! -d "$1" ]; then
  1333.    print_usage dir2repo && exit 1
  1334.  fi
  1335.  
  1336.  local filename=''
  1337.  local filepath=''
  1338.  local fileext=''
  1339.  local prevext=''
  1340.  local repo_name
  1341.  
  1342.  # get the repo directory
  1343.  local repo_dir="$(realpath "$1")"
  1344.  
  1345.  # create a temp repo_file name
  1346.  local repo_file="${repo_dir}/Packages-$DISTRO_BINARY_COMPAT-$DISTRO_COMPAT_VERSION"
  1347.  
  1348.  # remove any old files
  1349.  rm "${repo_dir}/install" "${repo_dir}/Packages-"* "$repo_file" &>/dev/null
  1350.  
  1351.  echo
  1352.  echo "Creating repo from contents of: $repo_dir"
  1353.  echo
  1354.  
  1355.  # exit if we encounter multiple file extensions
  1356.  for file in $(find "$repo_dir" -maxdepth 5 -type f)
  1357.  do
  1358.    filename=$(basename $file)
  1359.    fileext="$(get_pkg_ext $filename)"
  1360.    if [ "$fileext" != "$prevext" ] && [ "$prevext" != "" ]; then
  1361.      echo "All packages must have the same extension."
  1362.      echo "Extensions '$fileext' and '$prevext' don't match."
  1363.      echo "Package '$filename' needs to be converted to $prevext or removed."
  1364.      exit 1
  1365.    fi
  1366.    prevext="$fileext"
  1367.  done
  1368.  
  1369.  
  1370.  # get the packages in $repo_dir
  1371.  local package_list="$(find "$repo_dir" -maxdepth 5 -type f | grep -v ^Packages | grep -v "^install$")"
  1372.  
  1373.  if [ "$package_list" = "" ]; then
  1374.    echo "No packages found in ${repo_dir}!"
  1375.    exit 1
  1376.  fi
  1377.  
  1378.  # for each file in the repo dir
  1379.  for file in $package_list
  1380.  do
  1381.    # skip if not a recognised package type
  1382.    [ "$(is_local_pkg "$file")" != true ] && continue
  1383.  
  1384.    # get the package info
  1385.    filename=$(basename $file)
  1386.    fileext="$(get_pkg_ext $filename)"
  1387.    filepath="$(realpath $(dirname $file))"
  1388.    pathname="$(dirname ${filepath})"
  1389.    repopath="$(echo $filepath | sed -e "s#${repo_dir}##" -e "s/^\///")"
  1390.    pkgname=$(basename $filename .$fileext)
  1391.    pkgnameonly="$(get_pkg_name_only $pkgname)"
  1392.    pkgversion="$(get_pkg_version $pkgname)"
  1393.    pkgcat="$(grep -m1 "|$pkgnameonly|" "$REPO_DB_FILE_DIR"/Packages-* | cut -f5 -d'|')"
  1394.    pkgsize="$(du "$file" 2>/dev/null | cut -f1)K"
  1395.    [ "$pkgsize" = "" ] && pkgsize="$(grep -m1 "|$pkgnameonly|" "$REPO_DB_FILE_DIR"/Packages-* | cut -f6 -d'|')"
  1396.    pkgdeps="$(grep -m1 "|$pkgnameonly|" "$REPO_DB_FILE_DIR"/Packages-* | cut -f9 -d'|')"
  1397.    # dont include deps if we're adding a combined package + plus deps (they have $CP_SUFFIX in the name)
  1398.     [ "$(echo "$filename" | grep "$CP_SUFFIX")" != "" ] && pkgdeps=''
  1399.     pkgdesc="$(grep -m1 "|$pkgnameonly|" "$REPO_DB_FILE_DIR"/Packages-* | cut -f10 -d'|')"
  1400.     pkgdistro="$DISTRO_BINARY_COMPAT"
  1401.     pkgdistroversion="$DISTRO_COMPAT_VERSION"
  1402.  
  1403.     # if we don't have the required package info, dont add it to the repo file
  1404.     [ "$pkgname" = "" -o "$pkgnameonly" = "" -o "$filename" = "" ] && continue
  1405.  
  1406.     # add this package to the repo file
  1407.     echo "$pkgname|$pkgnameonly|$pkgversion||${pkgcat:-BuildingBlock}|$pkgsize|$repopath|$filename|$pkgdeps|${pkgdesc:-No description}|$pkgdistro|$pkgdistroversion|" >> "$repo_file"
  1408.   done
  1409.  
  1410.   if [ ! -f "$repo_file" ]; then
  1411.     echo "Error: file '$repo_file' not created."
  1412.     exit 1
  1413.   fi
  1414.  
  1415.   echo "Step 1 of 3: CHOOSE A REPO NAME"
  1416.   echo "(something like 'distroversion-username-repo', such as 'bionic-bob-main' or 'stretch-sc0ttman-games')"
  1417.   echo
  1418.   bash -c 'read -e -r -p "Type a repo name and hit ENTER: " repo_name; echo "$repo_name" > /tmp/pkg/repo_name'
  1419.   repo_name="$(cat /tmp/pkg/repo_name)"
  1420.  
  1421.   # rename the repo file to the new name
  1422.   mv "$repo_file"   "${repo_dir}/Packages-${DISTRO_BINARY_COMPAT:-puppy}-$repo_name"
  1423.   repo_file="${repo_dir}/Packages-${DISTRO_BINARY_COMPAT:-puppy}-$repo_name"
  1424.  
  1425.   # build repo entry
  1426.   local repo_entry="$repo_name|$fileext|$repo_file|$repo_url||||$repo_fallbacks"
  1427.  
  1428.   # get the URL where the repo file and packages will live
  1429.   echo
  1430.   echo "Step 2 of 3: ADD THE REPO URL"
  1431.   echo "(the full URL where you will upload your repo file and packages)"
  1432.   echo
  1433.   bash -c 'read -e -r -i "http://" -p "Type a repo URL and hit ENTER: " mirror1; echo "$mirror1" > /tmp/pkg/mirror1'
  1434.   mirror1="$(cat /tmp/pkg/mirror1)"
  1435.  
  1436.   # get fallback repos list
  1437.   echo
  1438.   echo "Step 3 of 3: ADD FALLBACK REPOS"
  1439.   echo "(the other repos to fall back to when looking for dependencies)"
  1440.   echo
  1441.   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'
  1442.   fallback_repos="$(cat /tmp/pkg/fallback_repos)"
  1443.  
  1444.   # remove the tmp files which store user input, they're no longer needed
  1445.   rm /tmp/pkg/repo_name /tmp/pkg/mirror1 /tmp/pkg/fallback_repos
  1446.  
  1447.   # add a trailing slash to the URL
  1448.   if [ "$(echo "${mirror1}" | grep "/$")" = "" ]; then
  1449.     mirror1="${mirror1}/"
  1450.   fi
  1451.  
  1452.   # create repo installer file in $repo_dir
  1453.   echo "REPONAME=$repo_name
  1454. EX=$fileext
  1455. REPOFILE=$(basename $repo_file)
  1456. URL1=${mirror1}
  1457. URL2=''
  1458. URL3=''
  1459. URL4=''
  1460. FALLBACKS='$fallback_repos'" > "${repo_dir}/install"
  1461.  
  1462.   # print final message
  1463.   echo
  1464.   echo -e "${green}Success${endcolour}: Repo ${yellow}$repo_name${endcolour} created."
  1465.   echo
  1466.   echo -e "You should upload everything in ${lightblue}$repo_dir${endcolour} to:"
  1467.   echo
  1468.   echo "  $mirror1"
  1469.   echo
  1470.   echo "You (and anyone else) can then install the repo using:"
  1471.   echo
  1472.   echo "  pkg add-repo ${mirror1}"
  1473.   echo
  1474.   echo -e "NOTE: You can edit the ${yellow}install${endcolour} and ${yellow}$(basename $repo_file)${endcolour}"
  1475.   echo "files in a text editor, before you upload your new repo."
  1476.   echo
  1477. }
  1478.  
  1479.  
  1480. add_pkg_repo() {                  # add a Pkg-created repo, called by add_repo() FUNCLIST
  1481.   [ ! "$1" ] && return 1
  1482.   mkdir -p /tmp/pkg/
  1483.   # this is probably a Pkg created Puppy repo, get info from 'install' file
  1484.   rm /tmp/pkg/repo_info &>/dev/null
  1485.  
  1486.   # check if the 'install' file we need exists
  1487.   wget --no-check-certificate -O "/tmp/pkg/repo_info" -4 "$1" &>/dev/null
  1488.   if [ ! -f /tmp/pkg/repo_info ]; then
  1489.     echo "Error: Repo installer file not downloaded!"
  1490.     return 1
  1491.   elif [ "$(grep -m1 "^REPONAME" /tmp/pkg/repo_info 2>/dev/null)" = "" ]; then
  1492.     echo "Error: invalid repo installer file"
  1493.     return 1
  1494.   fi
  1495.  
  1496.   # build a valid repo entry for the ~/.pkg/sources* files
  1497.   # (must have some fallbacks, strip quotes, newlines, no trailing pipes)
  1498.   local repo_entry="$(cat /tmp/pkg/repo_info \
  1499.    | sed -e "s/FALLBACKS=''/FALLBACKS='noarch common32'/" -e 's/.*=//g' \
  1500.    | tr -d '"' \
  1501.    | tr -d "'" \
  1502.    | tr '\n' '|' \
  1503.    | head -c -1)"
  1504.  
  1505.   if [ "$repo_entry" = "" ]; then
  1506.     echo "Error: repo entry not created."
  1507.     return 1
  1508.   fi
  1509.  
  1510.   # get repo info
  1511.   local repo_name="$(echo "$repo_entry" | cut -f1 -d'|')"
  1512.   local repo_filename="$(echo "$repo_entry" | cut -f3 -d'|')"
  1513.   local repo_file_url="$(echo ${1} | sed 's#/install$##')/${repo_filename}"
  1514.  
  1515.   # download the repo file
  1516.   wget --no-check-certificate -O "/tmp/pkg/$repo_filename" -4 "$repo_file_url" &>/dev/null
  1517.   if [ -z /tmp/pkg/$repo_filename ] || [ ! -f /tmp/pkg/$repo_filename ]; then
  1518.     echo "Error: Repo file $repo_filename not downloaded!"
  1519.     return 1
  1520.   fi
  1521.  
  1522.   # install the repo file
  1523.   mv /tmp/pkg/$repo_filename "$REPO_DB_FILE_DIR/$repo_filename"
  1524.   RETVAL=$?
  1525.  
  1526.   # add repo entry to ~/.pkg/sources
  1527.   echo
  1528.   add_source "${repo_entry}"
  1529.   echo
  1530.  
  1531.   # refresh list of available repos
  1532.   update_sources
  1533.   echo
  1534.   echo "Repo info:"
  1535.   print_repo_info $repo_name
  1536.   echo
  1537.  
  1538.   if [ "$(cat ~/.pkg/sources 2>/dev/null | grep -m1 "^$repo_name|")" != "" ]; then
  1539.     echo "Success! Repo added and available to use."
  1540.     echo
  1541.     echo "To use this repo, simply type the following and hit ENTER:"
  1542.     echo "  pkg repo $repo_name"
  1543.     echo
  1544.  
  1545.     # register as a user installed repo, so we can remove it later (using rm_repo)
  1546.     touch ~/.pkg/sources-user
  1547.     echo "$repo_entry" >> ~/.pkg/sources-user
  1548.     local user_repos="$(sort -u ~/.pkg/sources-user | uniq)"
  1549.     echo "$user_repos" > ~/.pkg/sources-user
  1550.  
  1551.   fi
  1552.  
  1553.   return $RETVAL
  1554. }
  1555.  
  1556.  
  1557. add_repo(){                       # add Pkg/Ubuntu/Debian/Slackware third-party repos FUNCLIST
  1558.  
  1559.   local slack_repo_url
  1560.  
  1561.   if [ ! "$1" -o "$1" = "-" -o "$1" = "-h" -o "$1" = "--help" ]; then
  1562.     print_usage add_repo && exit 1
  1563.   fi
  1564.  
  1565.   # work out which kind of repo we are processing
  1566.   case "$1" in
  1567.     'http'*'/PACKAGES.TXT.gz'|'http'*'/PACKAGES.TXT')
  1568.       # strip trailing /PACKAGES.TXT[.gz]
  1569.       slack_repo_url="${1//PACKAGES.TXT.gz/}"
  1570.       slack_repo_url="${slack_repo_url//\/.gz/}"
  1571.       slack_repo_url="${slack_repo_url//.gz\//}"
  1572.       slack_repo_url="${slack_repo_url//PACKAGES.TXT/}"
  1573.       # add trailing slash, if needed
  1574.       if [ "$(echo $slack_repo_url | grep '/$')" = '' ]; then
  1575.         slack_repo_url="${slack_repo_url}/"
  1576.       fi
  1577.       slack2pup "$@"
  1578.       retval=$?
  1579.       if [ $retval -eq 0 ]; then
  1580.         # register repo, so we know to update it in update_sources()
  1581.         touch ~/.pkg/sources-user
  1582.         echo "$repo_entry" >> ~/.pkg/sources-user
  1583.         local user_repos="$(sort -u ~/.pkg/sources-user | uniq)"
  1584.         echo "$user_repos" > ~/.pkg/sources-user
  1585.  
  1586.         # register in a slack supported file too
  1587.         mkdir -p /etc/slackpkg/
  1588.         touch /etc/slackpkg/mirrors
  1589.         echo "$slack_repo_url" >> /etc/slackpkg/mirrors
  1590.         local slack_repos="$(sort -u /etc/slackpkg/mirrors | uniq)"
  1591.         echo "$slack_repos" > /etc/slackpkg/mirrors
  1592.       fi
  1593.       ;;
  1594.  
  1595.     'http'*'/install')
  1596.       add_pkg_repo "$1"
  1597.       ;;
  1598.  
  1599.     'ppa:'*|'http://'*|'https://'*)
  1600.       wget --quiet --timeout=2 --no-parent --spider "${1}install"
  1601.       REPLY=$?
  1602.       if [ "$REPLY" = 0 ]; then
  1603.         add_pkg_repo "${1}install"
  1604.         exit $?
  1605.       fi
  1606.  
  1607.       ppa2pup "$@"
  1608.       retval=$?
  1609.       if [ $retval -eq 0 ]; then
  1610.         # register repo, so we know to update it in update_sources()
  1611.         touch ~/.pkg/sources-user
  1612.         echo "$repo_entry" >> ~/.pkg/sources-user
  1613.         local user_repos="$(sort -u ~/.pkg/sources-user | uniq)"
  1614.         echo "$user_repos" > ~/.pkg/sources-user
  1615.  
  1616.         # register in an apt-supported file too
  1617.         mkdir -p /etc/apt/
  1618.         touch /etc/apt/sources.list
  1619.         echo "deb $@" >> /etc/apt/sources.list
  1620.         local deb_repos="$(sort -u /etc/apt/sources.list | uniq)"
  1621.         echo "$deb_repos" > /etc/apt/sources.list
  1622.       fi
  1623.       ;;
  1624.  
  1625.     *)
  1626.       print_usage add_repo && exit 1
  1627.       ;;
  1628.  
  1629.   esac
  1630.  
  1631. }
  1632.  
  1633.  
  1634. rm_repo(){                        # remove an installed repo by name ($1) FUNCLIST
  1635.   if [ ! "$1" -o "$1" = "-" -o "$1" = "-h" -o "$1" = "--help" ]; then
  1636.     print_usage rm_repo
  1637.     exit 1
  1638.   fi
  1639.  
  1640.   # $1 must be a valid, installed repo name
  1641.   if [ "$(repo_list | grep -m1 "^${1}$")" = "" ]; then
  1642.     print_usage rm-repo
  1643.     exit 1
  1644.   fi
  1645.  
  1646.   # get the repo file and URL of the given repo
  1647.   local repo_file="$(grep "^$1" ~/.pkg/sources-all | cut -f3 -d'|' )"
  1648.   local repo_url="$(grep "^$1" ~/.pkg/sources-all | cut -f4 -d'|')"
  1649.  
  1650.   # dont allow removal of 'built in' repos, only user-added repos
  1651.   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)"
  1652.   if [ "${matching_user_added_repos}" = "" ]; then
  1653.     echo "You can only remove the repositories that you installed yourself."
  1654.     echo -e "Use the command  ${bold}pkg repo-list${endcolour}  to see which repos are installed."
  1655.     exit 1
  1656.   fi
  1657.  
  1658.   # dont allow removal of current repo
  1659.   if [ "$REPONAME" = "$1" ]; then
  1660.     echo "Can't remove current repo."
  1661.     echo -e "Switch to another repo first using:  ${bold}pkg repo REPONAME${endcolour}"
  1662.     exit 1
  1663.   fi
  1664.  
  1665.   # remove the repo file
  1666.   echo "Removing '$1'.. Please wait.."
  1667.   [ -f "$REPO_DB_FILE_DIR/$repo_file" ] && rm "$REPO_DB_FILE_DIR/$repo_file"
  1668.  
  1669.   # remove from sources
  1670.   grep -v "^$1|" ~/.pkg/sources > /tmp/pkg/sources
  1671.   grep -v "^$1|" ~/.pkg/sources-all > /tmp/pkg/sources-all
  1672.   [ -f /tmp/pkg/sources-all ] && mv /tmp/pkg/sources-all ~/.pkg/sources-all
  1673.   [ -f /tmp/pkg/sources     ] && mv /tmp/pkg/sources ~/.pkg/sources
  1674.  
  1675.   # remove from third-party lists
  1676.   grep -v  "|${repo_url}|"  ~/.pkg/sources-user > /tmp/pkg/sources-user
  1677.   grep -vE "$repo_url|${repo_name//*-/}" /etc/apt/sources.list > /tmp/pkg/sources.list
  1678.   grep -v  "$repo_url" /etc/slackpkg/mirrors > /tmp/pkg/mirrors
  1679.   [ -f /tmp/pkg/sources-user ] && mv /tmp/pkg/sources-user ~/.pkg/sources-user
  1680.   [ -f /tmp/pkg/sources.list ] && mv /tmp/pkg/sources.list /etc/apt/sources.list
  1681.   [ -f /tmp/pkg/mirrors ] && mv /tmp/pkg/mirrors /etc/slackpkg/mirrors
  1682.  
  1683.   if [ "$(repo_list | grep -m1 "^${1}$")" = "" ]; then
  1684.     update_sources &>/dev/null
  1685.     echo -e "${green}Success${endcolour}: Repo removed."
  1686.   fi
  1687.  
  1688. }
  1689.  
  1690.  
  1691. add_source(){                     # add a new repo to your repo 'sources' list FUNCLIST
  1692.   [ ! "$1" \
  1693.     -o "$1" = "-" \
  1694.     -o "$1" = "-h" \
  1695.     -o "$1" = "--help" \
  1696.     -o "`echo "$1" |  grep '|'`" = "" \
  1697.     -o "`echo "$1" | cut -f2 -d'|'`" = "" \
  1698.     -o "`echo "$1" | cut -f4 -d'|'`" = "" \
  1699.     -o "`echo "$1" | cut -f8 -d'|'`" = "" ] && \
  1700.     print_usage add_source && exit 1
  1701.  
  1702.   # get repo file to add to sources
  1703.   REPOTOADD="`echo $1 | cut -f1 -d'|'`"
  1704.   REPOFILETOADD="`echo $1 | cut -f3 -d'|'`"
  1705.  
  1706.   # do checks before adding repo (dont add duplicate, make sure file exists, etc)
  1707.  
  1708.   # check if repo name already in sources-all
  1709.   #if [ "`grep "^$REPOTOADD\$" ~/.pkg/sources-all`" != "" ] || \
  1710.   #   [ "`repo_list | grep "^$REPOTOADD\$"`" != "" ]; then
  1711.   #  echo "Repo with the name '$REPOTOADD' already in the list"
  1712.   #fi
  1713.  
  1714.   # check if repo filename already  exists in sources-all
  1715.   #if [ "`repo_file_list | grep -m1 "^$REPOFILETOADD\$"`" != "" ]; then
  1716.   #  echo "Repo with database file $PKGS_DIR/'$REPOFILETOADD' already in the list"
  1717.   #fi
  1718.  
  1719.   # check if the repo file exists in $PKGS_DIR
  1720.   #if [ ! -f "`find $PKGS_DIR/ -name "$REPOFILETOADD"`" ]; then
  1721.   #  echo "The repo database file '$PKGS_DIR/$REPOFILETOADD' not found."
  1722.   #fi
  1723.  
  1724.   # add the repo to sources-all, if not already there
  1725.   if [ "$(grep -m1 "^$REPOTOADD|" ${HOME}/.pkg/sources-all)" = "" ]; then
  1726.     # all good, so add repo entry to sources-all
  1727.     echo "$1" >> ${HOME}/.pkg/sources-all
  1728.   fi
  1729.  
  1730.   # update users repo sources to get the new repo
  1731.   update_sources 1>/dev/null || { echo "Could not update repo sources."; exit 2; }
  1732.  
  1733.   # print msg
  1734.   echo "Repo '$REPOTOADD' added successfully."
  1735. }
  1736.  
  1737.  
  1738. update_sources(){                 # create the list of available repos FUNCLIST
  1739.  
  1740.   # get current repo values and Pkg settings
  1741.   . ${PKGRC}
  1742.  
  1743.   #list current repo first in sources
  1744.   get_repo_info "${REPONAME:-noarch}"
  1745.  
  1746.  
  1747.   # only add the current repo to the list of available sources if it exists
  1748.   if [ "$(find "$REPO_DB_FILE_DIR" -iname "$REPOFILE" )" != '' ]; then
  1749.     echo "$REPONAME|$EX|$REPOFILE|$REPOURL1|$REPOURL2|$REPOURL3|$REPOURL4|$REPOFALLBACKS" > ${HOME}/.pkg/sources
  1750.   #elif [ "$(find "$(realpath "/var/packages/")" -iname "$REPOFILE" )" != '' ]; then
  1751.   #  echo "$REPONAME|$EX|$REPOFILE|$REPOURL1|$REPOURL2|$REPOURL3|$REPOURL4|$REPOFALLBACKS" > ${HOME}/.pkg/sources
  1752.   fi
  1753.  
  1754.  
  1755.   # get repos in order of fallbacks, pkg will then 'fall back' to each repo in that order
  1756.   FALLBACKS="`grep -m1 "^${REPONAME}|" ${HOME}/.pkg/sources-all 2>/dev/null | grep -v ^'#' | cut -f8 -d'|'`"
  1757.  
  1758.   # for each repo in fallback list
  1759.   for FBACK in $REPOFALLBACKS; do
  1760.     # dont add current repo, its already added
  1761.     [ "$FBACK" = "$REPONAME" ] && continue
  1762.  
  1763.     # check if repo is supported (has entries in sources-all)
  1764.     LINE="`grep -m1 "^$FBACK|" ${HOME}/.pkg/sources-all 2>/dev/null | grep -v ^'#' | grep -v "^${REPONAME}|"`"
  1765.     [ "$LINE" = "" ] && continue
  1766.  
  1767.     # if repo is valid, add to users list of in-use repos (only valid repos from sources-all => sources)
  1768.     if [ -f "$REPO_DB_FILE_DIR/`echo "$LINE" | cut -f3 -d'|'`" ]; then
  1769.  
  1770.       if [ "$(grep -m1 "^$FBACK|" ${HOME}/.pkg/sources)" = "" ]; then
  1771.         echo "Adding repo: `echo "$LINE"|cut -f1 -d'|'`.."
  1772.         echo "$LINE" >> ${HOME}/.pkg/sources
  1773.       fi
  1774.     fi
  1775.   done
  1776.  
  1777.   cleaned_repo_list="`cat ${HOME}/.pkg/sources-all 2>/dev/null | grep -v ^'#'| grep -v ^$`"
  1778.  
  1779.   # now add any other repos to the list (repos that are installed, but not added from fallback list)
  1780.   echo "$cleaned_repo_list" | uniq | while read repo_entry
  1781.   do
  1782.     # dont add current repo, its already added
  1783.     [ "`echo "$repo_entry" | cut -f1 -d'|'`" = "$REPONAME" ] && echo "Adding repo: $REPONAME.." && continue
  1784.  
  1785.     # get the repo name
  1786.     repo_name="`echo "$repo_entry"|cut -f1 -d'|'`"
  1787.  
  1788.     # build the repo file (full path)
  1789.     repo_file="$REPO_DB_FILE_DIR"/`echo "$repo_entry" | cut -f3 -d'|'`
  1790.  
  1791.     # set a flag true if repo already in repo, false if not
  1792.     already_in_repo=false
  1793.     [ "`grep -m1 "^$repo_entry" ${HOME}/.pkg/sources 2>/dev/null`" != "" ] && already_in_repo=true
  1794.  
  1795.     if [ -f "$repo_file" -a "$already_in_repo" = false ]; then
  1796.       echo "Adding repo: $repo_name.."
  1797.       echo "$repo_entry" >> ${HOME}/.pkg/sources
  1798.     fi
  1799.   done
  1800.  
  1801.   # finished, print message
  1802.   [ -f ${HOME}/.pkg/sources ] && echo "Sources updated." && . ${PKGRC}
  1803. }
  1804.  
  1805.  
  1806. update_repo(){                    # update the current repo from a file stored online FUNCLIST
  1807.  
  1808.   # check internet, net connection required
  1809.   NET="`check_net`"; [ $NET -eq 1 ] && echo "You need an internet connection" && exit 1
  1810.  
  1811.   # remove the repo update tmp file
  1812.   rm -f $TMPDIR/update_repo_results 2>/dev/null
  1813.  
  1814.   echo "Updating system repositories, please wait.."
  1815.   echo
  1816.  
  1817.   # use petget for now .. not ideal, petget wont accept $1 and only do that repo,
  1818.   # also petget uses loads of other files pkg doesnt have/need (in $PKGS_DIR)
  1819.   # also, we're limited to updating only the repos that petget supports, not all the ones Pkg supports..
  1820.   # .. on the plus side petget code is way faster than mine
  1821.   mkdir -p /var/local/petget/
  1822.   chmod 777 /var/local/petget/
  1823.   echo 'false' > /var/local/petget/db_verbose
  1824.  
  1825.   # now call petget 0setup.. the ENV options prevent popup windows, and need for user input
  1826.   DISPLAY='' SETUPCALLEDFROM=ppm /usr/local/petget/0setup &>$TMPDIR/update_repo_results
  1827.  
  1828.   # if the repos updated ok
  1829.   if [ $? -eq 0 ]; then
  1830.     [ "`which logger`" != '' ] && logger "$0 Repo files updated by $APP $APPVER"
  1831.     echo -e "Repo files updated:"
  1832.     grep ^Processing $TMPDIR/update_repo_results | cut -f2 -d' '
  1833.     # remove the repo update tmp file
  1834.     rm -f $TMPDIR/update_repo_results 2>/dev/null
  1835.   else
  1836.     # repo did not update ok
  1837.     error "Repos NOT updated."
  1838.     cat $TMPDIR/update_repo_results | tail -20
  1839.     exit 2
  1840.   fi
  1841.  
  1842.   # update Pkg created, user-added (third-party) repos
  1843.   if [ -f ~/.pkg/sources-user ] || [ -f /etc/apt/sources.list ] || [ -f /etc/slackpkg/mirrors ]; then
  1844.     echo
  1845.     echo "Updating third-party repos.. This may take a while.."
  1846.     echo
  1847.   fi
  1848.  
  1849.   # update Pkg created repos
  1850.   if [ -f ~/.pkg/sources-user ]; then
  1851.     pkg_repos="$(sort -u ~/.pkg/sources-user | grep -v ^$ | uniq | cut -f1 -d'|')"
  1852.     for pkg_repo in $pkg_repos
  1853.     do
  1854.       local pkg_repo_url="$(cat ~/.pkg/sources-user | grep -m1 "^$pkg_repo|" | cut -f4 -d'|')"
  1855.       echo "Processing:  $pkg_repo_url"
  1856.  
  1857.       ANSWER=y
  1858.       if [ "$ASK" = true ]; then
  1859.         bash -c 'read -r -N 1 -p "Update repo $pkg_repo? [y/n] " ANSWER; echo $ANSWER > /tmp/pkg/ANSWER'
  1860.         echo
  1861.         ANSWER="$(cat /tmp/pkg/ANSWER)"
  1862.       fi
  1863.  
  1864.       if [ "$ANSWER" = 'y' ] || [ "$ANSWER" = 'Y' ]; then
  1865.         echo "Please wait..."
  1866.         add_pkg_repo ${pkg_repo_url}install 1>/dev/null && echo -e "${green}Success${endcolour}: Updated repo '$pkg_repo'"
  1867.       fi
  1868.     done
  1869.   fi
  1870.  
  1871.   # update third-party non-native repos.. These repos comes 'from source',
  1872.   # and are not in native Puppy format - they need to be downloaded, converted
  1873.   # into the Puppy format, then installed... so this will be slow..
  1874.   if [ -f /etc/apt/sources.list ] || [ -f /etc/slackpkg/mirrors ]; then
  1875.  
  1876.     # update third-party Ubuntu/Debian repos
  1877.     # search inside /etc/apt/sources.list /etc/apt/sources.list.d/*.list
  1878.     # ...look for lines starting with 'deb ', ignore others
  1879.     # ...(use grep -h, to remove the preppended filenames if grepping multiple files)
  1880.     # ...remove any arch stuff from the entries, ppa2pup will handle that,
  1881.     # ...convert spaces to | chars, so we can process each line as a whole later
  1882.     local apt_sources_list="$(grep -h '^deb ' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null \
  1883.      | sed \
  1884.        -e "s/^deb //g" \
  1885.        -e "s/^tor+//g" \
  1886.        -e 's/\[arch=[a-z,0-9].*\] //g' \
  1887.        -e 's/ /|/g'\
  1888.    )"
  1889.  
  1890.     # for each repo in $apt_sources_list, use `ppa2pup` to update the repo
  1891.     for line in $apt_sources_list
  1892.     do
  1893.       [ "$line" = "" ] && continue
  1894.       [ "$line" = "\n" ] && continue
  1895.  
  1896.       local ppa_repo_url="$(echo ${line//|/ } | cut -f1 -d" ")"
  1897.       local ppa_repo_name=$(cat ~/.pkg/sources-all | grep -m1 $ppa_repo_url | cut -f1 -d'|')
  1898.  
  1899.       # if a PPA repo, get a user-friendly repo name from the /etc/sources.list entry
  1900.       if [ "$ppa_repo_name" = "" ]; then
  1901.         ppa_repo_name="$(echo ${line} | cut -f3 -d'|')-$(echo $line | cut -f2 -d':' | cut -f1 -d '/' | tr -d '-' | tr -d '_')"
  1902.       fi
  1903.  
  1904.       echo
  1905.       echo "Processing:  ${line//|/ }"
  1906.  
  1907.       ANSWER=y
  1908.       if [ "$ASK" = true ]; then
  1909.         # ask user to update repo
  1910.         bash -c 'read -r -N 1 -p "Update repo $ppa_repo_name? [y/n] " ANSWER; echo $ANSWER > /tmp/pkg/ANSWER'
  1911.         echo
  1912.         ANSWER="$(cat /tmp/pkg/ANSWER)"
  1913.       fi
  1914.  
  1915.       if [ "$ANSWER" = 'y' ] || [ "$ANSWER" = 'Y' ]; then
  1916.         echo "Please wait..."
  1917.         ppa2pup ${line//|/ } 1>/dev/null && echo -e "${green}Success${endcolour}: Updated repo '$ppa_repo_name'"
  1918.         retval=$?
  1919.       fi
  1920.     done
  1921.  
  1922.     # update third-party Slackware repos
  1923.     if [ -f /etc/slackpkg/mirrors ]; then
  1924.       local slack_repos="$(sort -u /etc/slackpkg/mirrors | uniq)"
  1925.       local slack_repo_url=""
  1926.  
  1927.       for slack_repo in $slack_repos
  1928.       do
  1929.         slack_repo_url="${slack_repo//PACKAGES.TXT.gz/}"
  1930.         slack_repo_url="${slack_repo_url//\/.gz/}"
  1931.         slack_repo_url="${slack_repo_url//.gz\//}"
  1932.         slack_repo_url="${slack_repo_url//PACKAGES.TXT/}"
  1933.  
  1934.         # add trailing slash, if needed
  1935.         if [ "$(echo $slack_repo_url | grep '/$')" = '' ]; then
  1936.           slack_repo_url="${slack_repo_url}/"
  1937.         fi
  1938.  
  1939.         local slack_repo_name=$(cat ~/.pkg/sources-all | grep -m1 $slack_repo_url | cut -f1 -d'|')
  1940.  
  1941.         echo
  1942.         echo "Processing:  $slack_repo_url"
  1943.  
  1944.         ANSWER=y
  1945.         if [ "$ASK" = true ]; then
  1946.           # ask user to update repo
  1947.           bash -c 'read -r -N 1 -p "Update repo '"$slack_repo_name"'? [y/n] " ANSWER; echo $ANSWER > /tmp/pkg/ANSWER'
  1948.           echo
  1949.           ANSWER="$(cat /tmp/pkg/ANSWER)"
  1950.         fi
  1951.  
  1952.         if [ "$ANSWER" = 'y' ] || [ "$ANSWER" = 'Y' ]; then
  1953.           echo "Please wait..."
  1954.           slack2pup "${slack_repo_url}/PACKAGES.TXT" $slack_repo_name 1>/dev/null && echo -e "${green}Success${endcolour}: Updated repo '$slack_repo_name'"
  1955.         fi
  1956.       done
  1957.     fi
  1958.   fi
  1959.  
  1960.   echo
  1961.   exit 0
  1962.  
  1963. }
  1964.  
  1965.  
  1966. list_sources(){                   # return available (matching) repos (~/.pkg/sources) FUNCLIST
  1967.   grep -m1 "^$1" ${HOME}/.pkg/sources ${HOME}/.pkg/sources-user | cut -f2-99 -d':' | uniq
  1968. }
  1969.  
  1970.  
  1971. list_all_sources(){               # return all (or matching) repos (~/.pkg/sources-all) FUNCLIST
  1972.   grep -m1 "^$1" ${HOME}/.pkg/sources-all ${HOME}/.pkg/sources-user | cut -f2-99 -d':' | uniq
  1973. }
  1974.  
  1975.  
  1976. convert_repofile(){               # convert repo files formats (pre-woof/post-woof) FUNCLIST
  1977.  
  1978.   # get the file name (FILENAME) and full path (FILE)
  1979.   FILENAME="`basename "$1"`"
  1980.   FILE="${CURDIR}/${FILENAME}"
  1981.  
  1982.   # check for valid options
  1983.   [ ! -f "$FILE" ] && print_usage repo-convert && exit 1
  1984.  
  1985.   # dont replace repo unless -f was given
  1986.   [ "$FORCE" != true -a -f "$REPO_DB_FILE_DIR/${FILENAME}" ] && echo "File '$REPO_DB_FILE_DIR/$FILENAME' already exists."  && exit 1
  1987.  
  1988.   # remove tmp files
  1989.   rm $TMPDIR/$FILENAME &>/dev/null
  1990.   rm $TMPDIR/${FILENAME}_subdirs &>/dev/null
  1991.  
  1992.   # check repo file format (woof or pre-woof)
  1993.   if [ -f "$FILE" -a "`cat "$FILE" | head -1 | grep -m1 '^"'`" = "" ]; then #if is a new format  #'
  1994.  
  1995.     # convert woof repo file to pre-woof repo file.. takes ages..
  1996.     echo "Converting '${FILE}' to pre-woof format.. This might take a while.."
  1997.     cat "$FILE" | while read LINE
  1998.     do
  1999.       PKGNAME="`echo $LINE| cut -f1 -d'|'`"
  2000.       PKGNAME1=''
  2001.       # we need to get the package name, try lots of different extensions
  2002.       [ "`echo $PKGNAME1 | grep ".deb\$"`" != "" ]  && PKGNAME1="`echo $LINE| cut -f8 -d'|'`"
  2003.       [ "`echo $PKGNAME1 | grep ".deb\$"`" != "" ]  && PKGNAME="`basename $PKGNAME1 .deb`"
  2004.       [ "`echo $PKGNAME1 | grep ".rpm\$"`" != "" ]  && PKGNAME="`basename $PKGNAME1 .rpm`"
  2005.       [ "`echo $PKGNAME1 | grep ".txz\$"`" != "" ]  && PKGNAME="`basename $PKGNAME1 .txz`"
  2006.       [ "`echo $PKGNAME1 | grep ".tgz\$"`" != "" ]  && PKGNAME="`basename $PKGNAME1 .tgz`"
  2007.       [ "`echo $PKGNAME1 | grep ".tar.xz\$"`" != "" ] && PKGNAME="`basename $PKGNAME1 .tar.xz`"
  2008.       [ "`echo $PKGNAME1 | grep ".tar.gz\$"`" != "" ] && PKGNAME="`basename $PKGNAME1 .tar.gz`"
  2009.       # get size
  2010.       SIZE=" `echo $LINE| cut -f6 -d'|'`"
  2011.       # get category
  2012.       CAT="`echo $LINE| cut -f5 -d'|'`"
  2013.       #150813 remove extra categories .. example 'Setup;Installation' .. remove 'Installation'
  2014.       [ "`echo "$CAT" | grep ';'`" != "" ] && CAT="`echo "$CAT" | cut -f1 -d';'`"
  2015.       # get sub dir in repo
  2016.       SUBDIR="`echo $LINE| cut -f7 -d'|'`" #150213
  2017.       # get deps
  2018.       DEPS=" `echo $LINE| cut -f9 -d'|'| grep '+'`"
  2019.       # get desc
  2020.       DESC="`echo $LINE| cut -f10 -d'|'`"
  2021.       # add repo entry to tmp file
  2022.       [ "$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
  2023.       #150213 now do subdirs... slow..
  2024.       [ "`echo $SUBDIR | grep "/"`" != "" -a "`echo $SUBDIR | grep -i pet_packages`" = "" ] && echo "$PKGNAME|/$SUBDIR" >> $TMPDIR/${FILENAME}_subdirs
  2025.     done
  2026.     # done making a pre-woof repo file, print message
  2027.     [ -f $TMPDIR/$FILENAME ] && echo "Finished converting $FILENAME to pre-woof format." || exit 1
  2028.  
  2029.   else
  2030.  
  2031.     # convert pre-woof repo file to woof repo file.. takes ages..
  2032.     echo "Converting '$FILENAME' to a 'woof' compatible repo file. This could take a while..."
  2033.  
  2034.     # parse a pre-woof repo file
  2035.     cat "$FILE" | while read LINE
  2036.     do
  2037.       PKGNAME='' PKGNAMEONLY='' PKGVER='' SIZE='' CAT='' DEPS='' BUILD='' SUBDIR='' PKGEXT='.pet' DESC=''
  2038.  
  2039.       PKGNAME="`echo "$LINE" | cut -d'"' -f2`" #'micro
  2040.       [ "`echo $PKGNAME | grep ".deb\$"`" != "" ]     && PKGEXT='.deb'    && PKGNAME="`basename $PKGNAME1 .deb`"
  2041.       [ "`echo $PKGNAME | grep ".pet\$"`" != "" ]     && PKGEXT='.pet'    && PKGNAME="`basename $PKGNAME1 .pet`"
  2042.       [ "`echo $PKGNAME | grep ".rpm\$"`" != "" ]     && PKGEXT='.rpm'    && PKGNAME="`basename $PKGNAME1 .rpm`"
  2043.       [ "`echo $PKGNAME | grep ".tcz\$"`" != "" ]     && PKGEXT='.tcz'    && PKGNAME="`basename $PKGNAME1 .tcz`"
  2044.       [ "`echo $PKGNAME | grep ".tgz\$"`" != "" ]     && PKGEXT='.tgz'    && PKGNAME="`basename $PKGNAME1 .tgz`"
  2045.       [ "`echo $PKGNAME | grep ".txz\$"`" != "" ]     && PKGEXT='.txz'    && PKGNAME="`basename $PKGNAME1 .txz`"
  2046.       [ "`echo $PKGNAME | grep ".tar.gz\$"`" != "" ]    && PKGEXT='.tar.gz'   && PKGNAME="`basename $PKGNAME1 .tar.gz`"
  2047.       [ "`echo $PKGNAME | grep ".tar.xz\$"`" != "" ]    && PKGEXT='.tar.xz'   && PKGNAME="`basename $PKGNAME1 .tar.xz`"
  2048.       [ "`echo $PKGNAME | grep ".pkg.tar.gz\$"`" != "" ]  && PKGEXT='.pkg.tar.gz' && PKGNAME="`basename $PKGNAME1 .pkg.tar.gz`"
  2049.       [ "`echo $PKGNAME | grep ".pkg.tar.xz\$"`" != "" ]  && PKGEXT='.pkg.tar.xz' && PKGNAME="`basename $PKGNAME1 .pkg.tar.xz`"
  2050.  
  2051.       # get pkg name only .. without versions or suffix
  2052.       PKGNAME_ONLY="`echo "${PKGNAME}" | sed -e 's/-[0-9.]*/-/g' -e 's/-$//'`"
  2053.       # if that didnt work, use the old method
  2054.       if [ "$PKGNAME_ONLY" = '' -o "$PKGNAME_ONLY" = "$PKGNAME" ]; then
  2055.         # get pkg name without version
  2056.         PKGNAMEONLY="`echo $PKGNAME | cut -d'-' -f1`"
  2057.       fi
  2058.       PKGNAME_ONLY="${PKGNAME_ONLY//-*/}"
  2059.  
  2060.       # get pkg version
  2061.       PKGVER="`LANG=C echo "$LINE" | sed -e 's/^[^0-9]*-//g' | cut -f1 -d'_' | cut -f1 -d'-'`"
  2062.       # get pkg size
  2063.       SIZE="`echo $LINE| cut -d'"' -f6 | cut -d' ' -f3`" #'micro
  2064.       SIZE=${SIZE## }
  2065.       SIZE=${SIZE%% }
  2066.       # must check again if pkg had no deps
  2067.       [ "$SIZE" = "" ] && SIZE=" `echo $LINE| cut -d'"' -f6|cut -d' ' -f2`"
  2068.       # get pkg category
  2069.       CAT="`echo $LINE | cut -d'"' -f6 | cut -d' ' -f1`"
  2070.       # remove extra categories
  2071.       [ "`echo "$CAT" | grep ';'`" != "" ] && CAT="`echo "$CAT" | cut -f1 -d';'`"
  2072.       # get pkg deps
  2073.       DEPS="`echo "$LINE" | cut -d'"' -f6 | cut -d' ' -f2 | grep ^'+'`"
  2074.       DESC="`echo "$LINE" | cut -f10 -d'|'`" #'micro
  2075.       # build the woof compatible repo file
  2076.       [ "$PKGNAME" != "" ] && echo "$PKGNAME|$PKGNAMEONLY|$VER|$BUILD|$CAT|$SIZE|$SUBDIR|${PKGNAME}${PKGEXT}|$DEPS|$DESC|$DISTRO_BINARY_COMPAT|$DISTRO_COMPAT_VERSION||" >> $TMPDIR/$FILENAME
  2077.     done
  2078.     #done making a woof repo file, print message
  2079.     [ -f $TMPDIR/$FILENAME ] && echo "Finished converting $FILENAME to woof format." || exit 1
  2080.   fi
  2081.  
  2082.   # if we are updating the repo, we dont wanna install the converted file straight over
  2083.   # the actual repo file, we wanna parse it for new pkgs and add only those
  2084.   if [ "$2" != "for_repo_update" ]; then
  2085.     # we converted a repo that we actually wanna install, so install it
  2086.     mv $TMPDIR/$FILENAME "$REPO_DB_FILE_DIR/$FILENAME"
  2087.     mv $TMPDIR/${FILENAME}_subdirs "$REPO_DB_FILE_DIR/${FILENAME}_subdirs" 2>/dev/null
  2088.     echo "Repo file '$REPO_DB_FILE_DIR/$FILENAME' created."
  2089.     update_sources #210613 update the list of sources after adding the newly converted repo
  2090.     #exit 0
  2091.   else
  2092.     mv "$TMPDIR/$FILENAME" "$REPO_DB_FILE_DIR/$FILENAME"
  2093.   fi
  2094. }
  2095.  
  2096.  
  2097. print_repo_info(){                # get repo settings, return current repo name FUNCLIST
  2098.  
  2099.   # get latest repo info (from sources file), or from PKGRC if that fails
  2100.   [ "$1" ] && get_repo_info "$1" || . ${PKGRC}
  2101.  
  2102.   local pkg_count=$(cat "$REPO_DB_FILE_DIR/${REPOFILE}" | wc -l)
  2103.  
  2104.   # output the repo info
  2105.   echo "- Repo:          $REPONAME"
  2106.   echo "- Repo file:     $REPOFILE"
  2107.   echo "- Package Type:  $EX"
  2108.   echo "- Packages:      $pkg_count"
  2109.   echo "- URL Mirror 1:  `echo $REPOURL1 | cut -f1-3 -d'/'`"
  2110.   [ "$REPOURL2" != "" ] && echo "- URL Mirror 2:  `echo $REPOURL2  | cut -f1-3 -d'/'`"
  2111.   [ "$REPOURL3" != "" ] && echo "- URL Mirror 3:  `echo $REPOURL3  | cut -f1-3 -d'/'`"
  2112.   [ "$REPOURL4" != "" ] && echo "- URL Mirror 4:  `echo $REPOURL4  | cut -f1-3 -d'/'`"
  2113.   echo
  2114.   echo "- Fall back to:`echo $REPOFALLBACKS | fold -w 50 -s | sed -e "s/ /, /g" -e "s/^/  /g"`"
  2115. }
  2116.  
  2117.  
  2118.  
  2119. # pkg search funcs
  2120.  
  2121. hide_blacklisted_pkgs_from_search_results(){ # hide BUILTIN and installed pkgs from searches NOLIST
  2122.  
  2123.   # dont hide package if using --force (the user may want to force a re-install of already installed pkg, for example)
  2124.   [ "$FORCE" = true ] && return 0
  2125.  
  2126.   # get pkg names (generic names, no versions) of all blacklisted packages in pipe delimited list
  2127.   blacklisted_pkgs_list="$(echo $PKG_NAME_IGNORE | sed -e 's/ /|/g')"
  2128.  
  2129.   # remove blacklisted packages from search list
  2130.   cat $TMPDIR/pkglist | grep -v -E "'$blacklisted_pkgs_list'" > $TMPDIR/pkglist_without_blacklisted
  2131.   mv $TMPDIR/pkglist_without_blacklisted $TMPDIR/pkglist
  2132.  
  2133.   # clean up tmp files
  2134.   rm $TMPDIR/pkglist_* &>/dev/null
  2135. }
  2136.  
  2137.  
  2138. hide_installed_pkgs_from_search_results(){  # hide BUILTIN and installed pkgs from searches NOLIST
  2139.  
  2140.   # dont hide package if using --force (the user may want to force a re-install of already installed pkg, for example)
  2141.   [ "$FORCE" = true ] && return 0
  2142.  
  2143.   # reset tmp file
  2144.   rm $TMPDIR/pkglist_inst &>/dev/null
  2145.  
  2146.   # get pkg names (generic names, no versions) of all installed pkgs (builtins, devx and user installed)
  2147.   inst_pkg_list="`cut -f1 -d'|' "$USER_INST_PKGS_FILE" \
  2148.    "$DEVX_INST_PKGS_FILE" \
  2149.    "$WOOF_INST_PKGS_FILE" 2>/dev/null \
  2150.    | grep -v ^$ \
  2151.    | tr '\n' '|' \
  2152.    | sed -e "s/||/|/g" \
  2153.    | sed -e "s/|\$//g"`"
  2154.  
  2155.   # remove woof and user installed packages from search list
  2156.   cat $TMPDIR/pkglist | grep -v -E "'$inst_pkg_list'" > $TMPDIR/pkglist_without_inst
  2157.   mv $TMPDIR/pkglist_without_inst $TMPDIR/pkglist
  2158.  
  2159.   # clean up tmp files
  2160.   rm $TMPDIR/pkglist_* &>/dev/null
  2161. }
  2162.  
  2163.  
  2164. list_pkg_names(){                 # list pkg names in current repo only ($1 is optional filter) FUNCLIST
  2165.  
  2166.   # remove any previous searches
  2167.   rm $TMPDIR/pkglist* 2>/dev/null
  2168.  
  2169.   # get current repo ($REPOFILE)
  2170.   . ${PKGRC}
  2171.  
  2172.   # create the search results
  2173.   cut -f1 -d'|' "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | grep "^$1" > $TMPDIR/pkglist
  2174.  
  2175.   # filter out builtin and user installed packages
  2176.   if [ $HIDE_INSTALLED = true ]; then
  2177.     hide_installed_pkgs_from_search_results
  2178.   fi
  2179.  
  2180.   hide_blacklisted_pkgs_from_search_results
  2181.  
  2182.   # support pkg name aliases in finding packages
  2183.   if [ $NO_ALIASES = false ]; then
  2184.     local ALIAS_LIST
  2185.     local ALIAS
  2186.     local ALIAS_RES
  2187.     # if we have some results to parse
  2188.     if [ "$1" != "" -a -f $TMPDIR/pkg_aliases ]; then
  2189.       # get the list of aliases
  2190.       ALIAS_LIST="`grep -m1 "$1" $TMPDIR/pkg_aliases  2>/dev/null | tr ',' ' '`";
  2191.       # for each alias
  2192.       echo $ALIAS_LIST | while read ALIAS
  2193.       do
  2194.         [ "$ALIAS" = '' ] && continue
  2195.         # get the match from the current repo (if any)
  2196.         ALIAS_RES="`LANG=C cut -f1 -d'|' "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | grep "^$ALIAS"`" #'
  2197.         [ ! "$ALIAS_RES" ] && ALIAS_RES="`LANG=C cut -f2 -d'|' "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | grep "^$ALIAS"`" #'
  2198.         # if the repo match was found in the search results
  2199.         if [ "$ALIAS_RES" != "" ]; then
  2200.           # add the alias results to the search results
  2201.           echo "$ALIAS_RES" >> $TMPDIR/pkglist
  2202.         fi
  2203.       done # for each alias
  2204.       # sort and clean the search results
  2205.       LANG=C cat $TMPDIR/pkglist | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq > $TMPDIR/pkglist1
  2206.       # replace the original search results
  2207.       mv $TMPDIR/pkglist1 $TMPDIR/pkglist
  2208.     fi
  2209.   fi
  2210.  
  2211.   # return the search results
  2212.   [ -s $TMPDIR/pkglist ] && cat $TMPDIR/pkglist 2>/dev/null
  2213.  
  2214.   # clean up
  2215.   [ ! -f $TMPDIR/pkglist ] && exit 1
  2216.   rm $TMPDIR/pkglist* 2>/dev/null
  2217.  
  2218. }
  2219.  
  2220.  
  2221. list_all_pkg_names(){             # list pkg names in any repo  ($1 is optional filter)FUNCLIST
  2222.  
  2223.   # remove any previous search results
  2224.   rm $TMPDIR/pkglist &>/dev/null
  2225.  
  2226.   # if bleeding edge disabled, output the list repo by repo, in the fallback order, current repo first (that order is set in update_sources)
  2227.   repo_file_list | while read repo_file
  2228.   do
  2229.     cut -f1 -d'|' "$REPO_DB_FILE_DIR/${repo_file}" 2>/dev/null | grep "^$1" | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr >> $TMPDIR/pkglist
  2230.   done
  2231.  
  2232.   # if bleeding edge enabled, re-order the whole list, so Pkg returns the most recent pgk versions from ANY repos
  2233.   if [ "$BLEDGE" = "yes" ]; then
  2234.     LANG=C cat $TMPDIR/pkglist  2>/dev/null | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr > $TMPDIR/pkglist1
  2235.     mv $TMPDIR/pkglist1 $TMPDIR/pkglist
  2236.   fi
  2237.  
  2238.   # filter out built-in and user installed packages
  2239.   if [ $HIDE_INSTALLED = true ]; then
  2240.     hide_installed_pkgs_from_search_results
  2241.   fi
  2242.  
  2243.   hide_blacklisted_pkgs_from_search_results
  2244.  
  2245.   # support pkg name aliases in finding packages
  2246.   if [ $NO_ALIASES = false ]; then
  2247.     local ALIAS_LIST
  2248.     local ALIAS
  2249.     local ALIAS_RES
  2250.     # if we have some results to parse
  2251.     if [ "$1" != "" -a -f $TMPDIR/pkg_aliases ]; then
  2252.       # get the list of aliases
  2253.       ALIAS_LIST="`grep -m1 "$1" $TMPDIR/pkg_aliases  2>/dev/null | tr ',' ' '`";
  2254.       # for each repo
  2255.       LANG=C repo_file_list | while read RF
  2256.       do
  2257.         # and for each alias
  2258.         for ALIAS in $ALIAS_LIST; do
  2259.           # get the match from the current repo (if any)
  2260.           ALIAS_RES="`LANG=C cut -f1 -d'|' "$REPO_DB_FILE_DIR/${RF}" 2>/dev/null | grep -m1 "^$ALIAS"`"
  2261.           [ ! "$ALIAS_RES" ] && ALIAS_RES="`LANG=C cut -f2 -d'|' "$REPO_DB_FILE_DIR/${RF}" 2>/dev/null | grep -m1 "^$ALIAS"`"
  2262.           # if the repo match was found in the search results
  2263.           if [ "$ALIAS_RES" != "" ]; then
  2264.             # add the alias results to the search results
  2265.             echo "$ALIAS_RES" >> $TMPDIR/pkglist
  2266.           fi
  2267.         done # for each alias
  2268.       done # for each repo
  2269.     fi
  2270.   fi
  2271.  
  2272.   # return the search results
  2273.   [ -s $TMPDIR/pkglist ] && LANG=C cat $TMPDIR/pkglist | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq
  2274.  
  2275.   # clean up
  2276.   [ ! -f $TMPDIR/pkglist ] && exit 1
  2277.   rm $TMPDIR/pkglist &>/dev/null
  2278. }
  2279.  
  2280.  
  2281. list_build_scripts(){             # list available build scripts  ($1 is optional filter)FUNCLIST
  2282.  
  2283.   # get settings from RC file
  2284.   . ${PKGRC}
  2285.  
  2286.   # make sure PetBuild is ready
  2287.   [ "$BUILDTOOL" = "petbuild" ] && prepare_petbuild
  2288.  
  2289.   # if no option given, just sort the results
  2290.   [ "$1" != "" ] && FILTER="grep -i $1" || FILTER="sort"
  2291.  
  2292.   # different build tools have their build scripts in different places.
  2293.   # check which BUILDTOOL we are using and list its build scripts
  2294.   case $BUILDTOOL in
  2295.     petbuild)
  2296.       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 ^$
  2297.     ;;
  2298.     buildpet)
  2299.       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 ^$
  2300.     ;;
  2301.     src2pkg)
  2302.       echo "Use: src2pkg FILE|URL or src2pkg -h"
  2303.     ;;
  2304.     sbopkg)
  2305.       sbopkg
  2306.     ;;
  2307.     *)
  2308.       echo "No build system configured. Set BUILDTOOL in $PKGRC."
  2309.       echo "Supported build tools: petbuild, buildpet, src2pkg, sbopkg"
  2310.     ;;
  2311.   esac
  2312. }
  2313.  
  2314.  
  2315. list_downloaded_pkgs(){           # list packages downloaded in WORKDIR ($1 is optional filter) FUNCLIST
  2316.  
  2317.   . ${PKGRC}
  2318.  
  2319.   cd "$WORKDIR" || { error "Cant cd into $WORKDIR"; exit 3; }
  2320.  
  2321.   local pkg
  2322.  
  2323.   # if no pkg given, list all
  2324.   if [ ! "$1" ]; then
  2325.  
  2326.     # get all pkgs in WORKDIR
  2327.     find "${WORKDIR}" -maxdepth 1 -type f | sed -e "s#${WORKDIR}##g" -e "s#^/##g" | grep -v ^'.pkg' | sort \
  2328.       | while read pkg # but go through each and only print it if a valid pkg
  2329.       do
  2330.         [ "`is_local_pkg "$pkg"`" = true ] && echo $pkg || continue
  2331.       done
  2332.  
  2333.     return 0
  2334.  
  2335.   else
  2336.  
  2337.     # $1 might be a pkg, or list of pkgs, loop through them
  2338.     for x in $1; do
  2339.       if [ "$x" != "-" ]; then
  2340.  
  2341.         # print the list of packages matching $x
  2342.         find "${WORKDIR}" -maxdepth 1 -type f | sed -e "s#${WORKDIR}##g" -e "s#^/##g" | grep -v ^'.pkg'|grep ^"$x" | sort \
  2343.           | while read pkg # but go through each and only print it if a valid pkg
  2344.           do
  2345.             [ "`is_local_pkg "$pkg"`" = true ] && echo $pkg || continue
  2346.           done
  2347.  
  2348.         return 0
  2349.       fi
  2350.     done
  2351.   fi
  2352.  
  2353.   cd -
  2354. }
  2355.  
  2356.  
  2357. list_installed_pkgs(){            # list user installed packages ($1 is optional filter) FUNCLIST
  2358.  
  2359.   local user_pkgs_list=''
  2360.   local builtins_list=''
  2361.   local devx_pkgs_list=''
  2362.  
  2363.   user_pkgs_list="$USER_INST_PKGS_FILE"
  2364.  
  2365.   if [ "$HIDE_BUILTINS" != true -a -f "$DEVX_INST_PKGS_FILE" ]; then
  2366.     devx_pkgs_list="$DEVX_INST_PKGS_FILE"
  2367.   fi
  2368.  
  2369.   [ "$HIDE_BUILTINS" != true ] && builtins_list="$WOOF_INST_PKGS_FILE"
  2370.  
  2371.   # search current repo file only .. $1 is the optional pkg filter.. take field1, remove empty lines, remove dashes, remove Pkg from pkg list too
  2372.   if [ ! "$1" ]; then
  2373.     cut -f1 -d'|' $user_pkgs_list $devx_pkgs_list $builtins_list | grep -vE '^\$|^pkg\-'
  2374.     return 0
  2375.   else
  2376.     cut -f1 -d'|' $user_pkgs_list $devx_pkgs_list $builtins_list | grep "^$1" | grep -vE '^\$|^pkg\-'
  2377.     return 0
  2378.   fi
  2379. }
  2380.  
  2381.  
  2382. list_builtin_pkgs(){              # lists builtin packages FUNCLIST
  2383.  
  2384.   local builtins_repo
  2385.  
  2386.   builtins_repo="$WOOF_INST_PKGS_FILE"
  2387.  
  2388.   # 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
  2389.   cut -f1 -d'|' $builtins_repo | grep "^$1" | grep -v ^\$  | grep -v "\-$" | grep -v ^pkg\- | sort
  2390. }
  2391.  
  2392.  
  2393. search_pkgs(){                    # given $1, searches current repo, shows name and desc columns FUNCLIST
  2394.  
  2395.   local name
  2396.   local desc
  2397.   local descfull
  2398.   local search
  2399.  
  2400.   # convert "foo[ -_]bar" into "foo.*bar"
  2401.   search=`echo "$1" | sed -e 's| |.*|g' -e 's|-|.*|g' -e 's|_|.*|g'`
  2402.  
  2403.   # convert repo file to nice columnised output, use cat|grep so empty searches return all results
  2404.   cat "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | grep -i "$search" | while read repo_line;
  2405.   do
  2406.     name=`echo  "$repo_line"|cut -f2  -d'|'`
  2407.     desc="`echo "$repo_line"|cut -f10 -d'|' | head -c 57`"
  2408.     descfull="`echo "$repo_line"|cut -f10 -d'|'`"
  2409.     [ "$desc" != "$descfull" ] && desc="${desc}.."
  2410.     printf "%-20s" "$name" " ${desc:-No description}"
  2411.     echo
  2412.   done
  2413. }
  2414.  
  2415.  
  2416. search_fast(){                    # given $1, searches current repo, show pkg names only, case sensitive FUNCLIST
  2417.   local RES
  2418.   local search
  2419.  
  2420.   # convert "foo[ -_]bar" into "foo.*bar"
  2421.   search=`echo "$1" | sed -e 's| |.*|g' -e 's|-|.*|g' -e 's|_|.*|g'`
  2422.  
  2423.   # get matching pkgs
  2424.   RES="`LANG=C grep "$search" "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | cut -f1 -d'|'`"
  2425.   # error if no result
  2426.   [ ! "$RES" ] && exit 1
  2427.  
  2428.   # put results into file
  2429.   echo "$RES" > $TMPDIR/pkglist
  2430.  
  2431.   # hide built-in, devx and user installed packages
  2432.   if [ $HIDE_INSTALLED = true ]; then
  2433.     hide_installed_pkgs_from_search_results
  2434.   fi
  2435.  
  2436.   hide_blacklisted_pkgs_from_search_results
  2437.  
  2438.   # now build the search results
  2439.   RES="`grep -v "^\$" $TMPDIR/pkglist 2>/dev/null | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq`"
  2440.   rm $TMPDIR/pkglist &>/dev/null
  2441.  
  2442.   #finshed making search results, print results
  2443.   [ "$RES" = "" ] && exit 1
  2444.   echo "$RES"
  2445. }
  2446.  
  2447.  
  2448. search_all_pkgs(){                # given $1, search all repos, show name and desc columns FUNCLIST
  2449.  
  2450.   local name
  2451.   local desc
  2452.   local descfull
  2453.   local search
  2454.  
  2455.   # convert "foo[ -_]bar" into "foo.*bar"
  2456.   search=`echo "$1" | sed -e 's| |.*|g' -e 's|-|.*|g' -e 's|_|.*|g'`
  2457.  
  2458.   for repo_file in `repo_file_list`
  2459.   do #090817
  2460.     # convert repo file to nice columnised output
  2461.     cat "$REPO_DB_FILE_DIR/${repo_file}" 2>/dev/null | grep -i "$search" | while read repo_line;
  2462.     do
  2463.     # get details from repo
  2464.     name=`echo  "$repo_line"|cut -f2  -d'|'`
  2465.     desc="`echo "$repo_line"|cut -f10 -d'|' | head -c 57`"
  2466.     descfull="`echo "$repo_line"|cut -f10 -d'|'`"
  2467.     [ "$desc" != "$descfull" ] && desc="${desc}.."
  2468.     # remove spaces and comments (slackware-extra repo has some dodgy entries.. dogy names, descriptions with comments, etc)
  2469.     name="${name// /}"
  2470.     name="${name//#/}"
  2471.     [ "$name" = '' ] && continue
  2472.     # print columnised output
  2473.     printf "%-20s" "$name" " ${desc:-No description}"
  2474.     echo
  2475.     done
  2476.   done
  2477.  
  2478. }
  2479.  
  2480.  
  2481. search_all_fast(){                # given $1, search all repos, show pkg names only, case sensitive FUNCLIST
  2482.   local RES
  2483.   local search
  2484.  
  2485.   # convert "foo[ -_]bar" into "foo.*bar"
  2486.   search=`echo "$1" | sed -e 's| |.*|g' -e 's|-|.*|g' -e 's|_|.*|g'`
  2487.  
  2488.   #if bleeding edge enabled, combine into 1 list, so
  2489.   # that `pkg -g` etc return most recent from ALL repos
  2490.   rm $TMPDIR/pkglist &>/dev/null
  2491.   if [ "$BLEDGE" = "yes" ]; then
  2492.     RES=''
  2493.     for RF in `repo_file_list`
  2494.     do #090817
  2495.       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
  2496.     done
  2497.  
  2498.   else
  2499.     # if bleeding edge disabled, output the list repo by repo, in #
  2500.     # the fallback order, current repo first (that order is set in update_sources)
  2501.     for RF in `repo_file_list`
  2502.     do #090817
  2503.       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
  2504.     done
  2505.  
  2506.   fi
  2507.  
  2508.   #100817 hide built-in, devx and user installed packages
  2509.   if [ $HIDE_INSTALLED = true ]; then
  2510.     hide_installed_pkgs_from_search_results
  2511.   fi
  2512.  
  2513.   hide_blacklisted_pkgs_from_search_results
  2514.  
  2515.   # now build the search results
  2516.   RES="`grep -v "^\$" $TMPDIR/pkglist 2>/dev/null | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq`"
  2517.   rm $TMPDIR/pkglist &>/dev/null
  2518.  
  2519.   #finshed making search results, print results
  2520.   [ "$RES" = "" ] && exit 1
  2521.  
  2522.   echo "$RES"
  2523. }
  2524.  
  2525.  
  2526. which_repo(){                     # list repo of given package ($1) FUNCLIST
  2527.  
  2528.   # if no valid options, quit
  2529.   [ ! "$1" -o "$1" = "-" -o "$1" = " " ] && print_usage which-repo && exit 1
  2530.  
  2531.   . ${PKGRC}
  2532.  
  2533.   local PKGNAME=''
  2534.   local PKGNAME_ONLY=''
  2535.   local pkg_ext=''
  2536.   local repo_file=''
  2537.   local repo_name=''
  2538.   local pkg_list=''
  2539.  
  2540.   PKGNAME=`get_pkg_name "$1"`
  2541.   PKGNAME_ONLY=`get_pkg_name_only "$1"`
  2542.   pkg_ext=`get_pkg_ext "$PKGNAME"`
  2543.  
  2544.   repo_file="$(grep -l "|$PKGNAME.$pkg_ext|" "$REPO_DB_FILE_DIR"/Packages-*)"
  2545.   if [ "$repo_file" != '' ]; then
  2546.     # get the repo name
  2547.     repo_name="`grep -m1 "$(basename $repo_file)|" ${HOME}/.pkg/sources | cut -f1 -d'|'`"
  2548.     pkg_list="$PKGNAME $repo_name"
  2549.   else
  2550.  
  2551.     # for each repo
  2552.     for repo_file in `repo_file_list`
  2553.     do
  2554.       # get the repo name
  2555.       repo_name="`grep -m1 "$repo_file|" ${HOME}/.pkg/sources | cut -f1 -d'|'`"
  2556.       # create the entries, example: "vlc slacko14.2"
  2557.       # (each line shows a matching package, then a SPACE, then the name of repo the package lives in)
  2558.       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"`"
  2559.       [ "$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"`"
  2560.       [ "$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"`"
  2561.       [ "$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"`"
  2562.       [ "$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"`"
  2563.       [ "$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"`"
  2564.       [ "$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"`"
  2565.     done
  2566.   fi
  2567.  
  2568.   # show results
  2569.   LANG=C echo -e "$pkg_list" | sed -e '/^$/d' | uniq
  2570. }
  2571.  
  2572.  
  2573. which_pkg(){                      # find out which pkg FILE ($1) comes from FUNCLIST
  2574.  
  2575.   # exit if no valid opts
  2576.   [ ! "$1" -o "$1" = '-' -o "$1" = '' ] && print_usage which && exit 1
  2577.  
  2578.   local PKGNAME=''
  2579.   local PKG_FILE_LIST=''
  2580.   local FILENAME=''
  2581.   local FILENAME_ONLY=''
  2582.   local DIRNAME=''
  2583.   local builtins_without_busybox="$(find "$BUILTIN_FILE_LIST_DIR"/* -maxdepth 1 -type f | grep -v "/busybox")"
  2584.  
  2585.   # get user input
  2586.   FILENAME="`basename "$1"`"
  2587.   FILENAME_ONLY="$FILENAME"
  2588.   DIRNAME="`dirname "$1"`"
  2589.  
  2590.   # if we don't have a file, the user probably gave a command,
  2591.   # so lets check see
  2592.   if [ ! -f "$1" ]; then
  2593.     # if we get a command, then that is our FILENAME
  2594.     cmd=`which "$1"`
  2595.     # if $cmd found, set FILENAME to $cmd (FILENAME now includes full path)
  2596.     [ "$cmd" != '' ] && FILENAME=$cmd && FILENAME_ONLY=`basename "$FILENAME"`
  2597.   fi
  2598.  
  2599.   # dont use relative paths
  2600.   [ "`echo "$DIRNAME" | grep "^\."`" != '' ] && DIRNAME=''
  2601.  
  2602.   # try user installed pkgs contents of $PKGS_DIR/*.files
  2603.  
  2604.   # try '$dir/$file', returns filename (no path) of matching *.files
  2605.   [ "$PKG_FILE_LIST" = '' ] && PKG_FILE_LIST="`grep -l "$DIRNAME/$FILENAME\$"  "$PACKAGE_FILE_LIST_DIR"/*.files 2>/dev/null| sed "s#$PKGS_DIR/##g" 2>/dev/null`"
  2606.  
  2607.   # try '$dir/$file_*' if needed
  2608.   [ "$PKG_FILE_LIST" = '' ] && PKG_FILE_LIST="`grep -l "$DIRNAME/${FILENAME}_" "$PACKAGE_FILE_LIST_DIR"/*.files 2>/dev/null| sed "s#$PKGS_DIR/##g" 2>/dev/null`"
  2609.  
  2610.   # try '$dir/$file-*' if needed
  2611.   [ "$PKG_FILE_LIST" = '' ] && PKG_FILE_LIST="`grep -l "$DIRNAME/${FILENAME}\-" "$PACKAGE_FILE_LIST_DIR"/*.files 2>/dev/null| sed "s#$PKGS_DIR/##g" 2>/dev/null`"
  2612.  
  2613.   # if we found a package, set the package name to the name of the *.files file that we got
  2614.   [ "$PKG_FILE_LIST" != '' ] && PKGNAME="`basename "$PKG_FILE_LIST" .files`"
  2615.  
  2616.   # maybe we got nothing from *.files.. check builtins/$1* for ' $FILENAME_ONLY',
  2617.   # returns PKGNAME of the matching builtin
  2618.  
  2619.   # if needed, search inside builtin file lists (EXCEPT busybox) for ' $FILENAME_ONLY'
  2620.   [ "$PKG_FILE_LIST" = '' ] && [ "$PKGNAME" = "" ] \
  2621.     && PKGNAME="$(basename `grep -l "^ $FILENAME_ONLY\$" $builtins_without_busybox | sed "s#$BUILTIN_FILE_LIST_DIR/##g"` 2>/dev/null)"
  2622.  
  2623.   # if that didn't work, search inside builtin file lists of busybox for ' $FILENAME_ONLY'
  2624.   [ "$PKG_FILE_LIST" = '' ] && [ "$PKGNAME" = "" ] \
  2625.     && PKGNAME="$(basename `grep -l "^ $FILENAME_ONLY\$" "$BUILTIN_FILE_LIST_DIR/busybox" | sed "s#$BUILTIN_FILE_LIST_DIR/##g"` 2>/dev/null)"
  2626.  
  2627.   # apps in ADRV sfs dont have their files listed in their $PKGS_DIR/builtin_files/$pkgname.. so..
  2628.  
  2629.  
  2630.   # now check pkg names (not pkg contents) of builtins (EXCEPT busybox) for '$file'.. cos maybe we didnt find the file inside the file lists
  2631.   [ "$PKGNAME" = "" ] \
  2632.     && PKGNAME="`echo "$builtins_without_busybox" | grep -m1 "^${FILENAME_ONLY}\$" | sed "s#$BUILTIN_FILE_LIST_DIR/##g" 2>/dev/null`"
  2633.  
  2634.   # now look for '$file' in user installed repo list
  2635.   [ "$PKGNAME" = "" ] \
  2636.     && 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'|'`"
  2637.  
  2638.   # also look for '$file' in user/woof/devx installed repo list
  2639.   [ "$PKGNAME" = "" ] \
  2640.     && 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'|')"
  2641.  
  2642.  
  2643.   # we searched for an exact match in builtins, user installed, woof installed and devx pkgs, now try some fuzzier matches
  2644.  
  2645.   # try /$file_* in builtins
  2646.   [ "$PKGNAME" = "" ] \
  2647.     && PKGNAME="$(basename `grep -l " ${FILENAME_ONLY}_" "$PKGS_BUILTIN_DIR"/* | sed "s#$PKGS_BUILTIN_DIR/##g"` 2>/dev/null)"
  2648.  
  2649.   # try $file-* in builtins
  2650.   [ "$PKGNAME" = "" ] \
  2651.     && PKGNAME="$(basename `grep -l " ${FILENAME_ONLY}-" "$PKGS_BUILTIN_DIR"/* | sed "s#$PKGS_DIR/builtin_files/##g"` 2>/dev/null)"
  2652.  
  2653.   # try $file.* in builtins
  2654.   [ "$PKGNAME" = "" ] \
  2655.     && PKGNAME="$(basename `grep -l " ${FILENAME_ONLY}\." "$PKGS_BUILTIN_DIR"/* | sed "s#$PKGS_BUILTIN_DIR/##g"` 2>/dev/null)"
  2656.  
  2657.   # try *file in builtin pkgs
  2658.   [ "$PKGNAME" = "" ] \
  2659.     && PKGNAME="$(basename `grep -l "${FILENAME_ONLY}"\$ "$PKGS_BUILTIN_DIR"/* | sed "s#$PKGS_BUILTIN_DIR/##g"` 2>/dev/null)"
  2660.  
  2661.  
  2662.   # if we still didnt find it, look in user installed package lists, for FILENAME
  2663.   [ "$PKGNAME" = "" ] \
  2664.     && PKGNAME=`cut -f1,2,8 -d '|' "$USER_INST_PKGS_FILE" \
  2665.       | sed -e "s/^/|/" -e "s/\$/|/" 2>/dev/null \
  2666.       | grep -m1 "|${FILENAME_ONLY}|" \
  2667.       | cut -f2 -d'|'` 2>/dev/null
  2668.  
  2669.   # if we still didnt find it, look in all installed package lists, for FILENAME
  2670.   [ "$PKGNAME" = "" ] \
  2671.     && PKGNAME=`cut -f1,2,8 -d '|' "${SPEC_DB_PATHS[@]}" \
  2672.       | sed -e "s/^/|/" -e "s/\$/|/" 2>/dev/null \
  2673.       | grep -m1 "|${FILENAME_ONLY}|" \
  2674.       | cut -f2 -d'|'` 2>/dev/null
  2675.  
  2676.   # if we still didnt find it, look in all installed package lists, for *FILENAME
  2677.   [ "$PKGNAME" = "" ] \
  2678.     && PKGNAME=`cut -f1,2,8 -d '|' "${SPEC_DB_PATHS[@]}" \
  2679.       | sed -e "s/^/|/" -e "s/\$/|/" 2>/dev/null \
  2680.       | grep -m1 "${FILENAME_ONLY}|" \
  2681.       | cut -f2 -d'|'` 2>/dev/null
  2682.  
  2683.   # if we still didnt find it, look in all installed package lists, for FILENAME*
  2684.   [ "$PKGNAME" = "" ] \
  2685.     && PKGNAME=`cut -f1,2,8 -d '|' "${SPEC_DB_PATHS[@]}" \
  2686.       | sed -e "s/^/|/" -e "s/\$/|/" \
  2687.       | grep -m1 "|${FILENAME_ONLY}" \
  2688.       | cut -f2 -d'|'` 2>/dev/null
  2689.  
  2690.  
  2691.   # clean up
  2692.   [ "$PKGNAME" = '.files' ] && PKGNAME=''
  2693.   PKGNAME="`echo "$PKGNAME" | head -1`"
  2694.  
  2695.   # now print pkgs which contain the given file ($1)
  2696.   if [ "$PKGNAME" != "" ]; then
  2697.     echo "$PKGNAME"
  2698.     return 0
  2699.   else
  2700.     echo "File '$FILENAME' not found in any installed or built-in pkgs."
  2701.     exit 1
  2702.   fi
  2703. }
  2704.  
  2705.  
  2706.  
  2707. # pkg info funcs
  2708.  
  2709. pkg_contents(){                   # list package ($1) contents FUNCLIST
  2710.  
  2711.   # if no valid options, quit
  2712.   [ ! "$1" -o "$1" = "-" -o "$1" = " " ] && print_usage contents && exit 1
  2713.  
  2714.   # get settings
  2715.   . ${PKGRC}
  2716.  
  2717.   local PKGFILE
  2718.   local ext
  2719.   local PKGNAME
  2720.   local PKGNAME_ONLY
  2721.   local pkg_is_local_file
  2722.   local pkg_is_builtin
  2723.   local pkg_is_installed
  2724.   local PKGFLIST=''
  2725.  
  2726.   # get pkg extension
  2727.   ext=`get_pkg_ext "$1"`
  2728.  
  2729.   # get pkg name (includes version), but no extension or path
  2730.   PKGNAME="$(basename "$1" .$ext)"
  2731.   PKGNAME=`get_pkg_name "$PKGNAME"`
  2732.  
  2733.   # get pkg name without version
  2734.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  2735.  
  2736.   pkg_is_local_file=`is_local_pkg "$1"`
  2737.   pkg_is_builtin=`is_builtin_pkg "$PKGNAME_ONLY"`
  2738.   pkg_is_installed=`is_installed_pkg "$PKGNAME_ONLY"`
  2739.  
  2740.   # now we check various ways to find out the contents of
  2741.   # a pkg (either by *.files, user installed, woof, builtin, etc)
  2742.  
  2743.   [ "$pkg_is_local_file" = true ] && PKGFILE="$1"
  2744.  
  2745.   # try a file in the current dir if needed
  2746.   [ ! -f "$PKGFILE" ] && PKGFILE="${CURDIR}/${PKGNAME}.$ext"
  2747.   # check $WORKDIR if needed
  2748.   [ ! -f "$PKGFILE" ] && PKGFILE="${WORKDIR}/${PKGNAME}.$ext"
  2749.  
  2750.   # if the pkg is a local file
  2751.   if [ -f "$PKGFILE" ]; then
  2752.     #list contents based on extension
  2753.     case "$ext" in
  2754.     sfs)
  2755.       unsquashfs -l "$PKGFILE"  | cut -f2-99 -d'/'| sed -e 's#^#/#g' 2>/dev/null | grep -v -E 'unsquashfs:|/squashfs-root|) to write' | grep -v "^/$"
  2756.     ;;
  2757.     deb)
  2758.       dpkg-deb -c "$PKGFILE" 2>/dev/null | grep -v "^/$"
  2759.      ;;
  2760.     pet|tar|tar**tcz|*txz|*tgz|*xz|*gz)
  2761.       # remove leading pkg name from each line of file list
  2762.       # (shown if listing *some* pkg file contents)
  2763.       tar -tf "$PKGFILE" 2>/dev/null \
  2764.         | sed -e "s#^./${PKGNAME}/#/#g" -e "s#^${PKGNAME}/#/#g" -e "s#^./##g" 2>/dev/null \
  2765.         | grep -v ^$ 2>/dev/null 2>/dev/null \
  2766.         | grep -v "^/$"
  2767.     ;;
  2768.     rpm)
  2769.       busybox rpm -qlp "$PKGFILE" 2>/dev/null | grep -v "^/$"
  2770.     ;;
  2771.     esac
  2772.     exit $?
  2773.   fi
  2774.  
  2775.   # if we are here, the pkg is not a downloaded pkg, so we build a
  2776.   # list of files by checking  *.files, woof, builtins ..
  2777.  
  2778.   # check if we need to (and can) get our pkg contents from builtins/PKGNAME
  2779.   local  need_to_check_builtins=`echo "$PKG_FLIST" | grep -m1 'packages/builtin_files/'`
  2780.  
  2781.   # if the pkg is a builtin, we will re-format the output of LIST to match the others (full paths on each line)
  2782.   if [ "$need_to_check_builtins" != '' -a "$pkg_is_builtin" = true ]; then
  2783.  
  2784.     # reset the list of pkg files, we will re-build it
  2785.     rm $TMPDIR/pkg_file_list 2>/dev/null
  2786.  
  2787.     # first we get the package contents from $PKGS_DIR/builtin_files/$PKGNAME_ONLY
  2788.     cat "$PKG_FLIST" 2>/dev/null | while read line
  2789.     do
  2790.       # parse it, so we get a full path to file on each line
  2791.       if [ "`echo "$line" | grep '^/'`" != '' ]; then
  2792.         # set the dir to use in our file list
  2793.         dir="$line"
  2794.       else
  2795.         # keep previous dir
  2796.         dir="$dir"
  2797.       fi
  2798.  
  2799.       # create our new line (a full file path) to $LIST
  2800.       line_to_add="$dir/`echo "$line" | cut -f2 -d' '`"
  2801.  
  2802.       # if only a dir, skip it
  2803.       [ "$line" = "$dir" ] && continue
  2804.  
  2805.       # if its already added, skip it
  2806.       [ "`grep -m1 "$line_to_add" ${TMPDIR}/pkg_file_list 2>/dev/null `" != '' ] && continue
  2807.  
  2808.       # if its not a file on the system (it should be), skip it
  2809.       [ ! -f "$line_to_add" ] && continue
  2810.  
  2811.       # all should be ok, add the line to our list
  2812.       echo "$line_to_add" >> $TMPDIR/pkg_file_list
  2813.  
  2814.     done
  2815.  
  2816.     # now get our pkg contents list, it might have been re-formatted (if a builtin)
  2817.     PKG_FLIST="`cat ${TMPDIR}/pkg_file_list 2>/dev/null`"
  2818.  
  2819.     # last resort, if we lost our file list or still dont have, try the basics again
  2820.     [ "$PKG_FLIST" = '' ] && PKG_FLIST="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${PKGNAME}"'*.files' 2>/dev/null)"
  2821.  
  2822.     # clean up the list a bit
  2823.     [ "$PKG_FLIST" != '' ] && PKG_FLIST="`echo "$PKG_FLIST" | grep -v ' ' | grep -v "^\$" | sort | uniq`"
  2824.  
  2825.     # and clean up the tmp files
  2826.     rm ${TMPDIR}/pkg_file_list 2>/dev/null
  2827.   fi
  2828.  
  2829.   # try PKGNAME_ONLY.files (exact match)
  2830.   PKG_FLIST="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${PKGNAME_ONLY}.files" 2>/dev/null)"
  2831.  
  2832.   # try finding PKGNAME.files (exact match)
  2833.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${PKGNAME}.files" 2>/dev/null)"
  2834.  
  2835.   # try the builtins files (exact match)
  2836.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$(find "$BUILTIN_FILE_LIST_DIR" -maxdepth 1 -type f -name "$PKGNAME_ONLY")"
  2837.  
  2838.   # try PKGNAME (exact match) in user installed pkgs
  2839.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$PKGS_DIR/`cut -f1 -d'|' $USER_INST_PKGS_FILE | grep -m1 ^"$PKGNAME\$" 2>/dev/null`.files"
  2840.  
  2841.   # try PKGNAME (exact match) in all installed pkgs
  2842.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$PKGS_DIR/`cut -f1 -d'|' "${SPEC_DB_PATHS[@]" | grep -m1 ^"$PKGNAME\$" 2>/dev/null`.files"
  2843.  
  2844.   # try PKGNAME_ONLY (exact match) in all installed pkgs
  2845.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$PKGS_DIR/`cut -f2 -d'|' "${SPEC_DB_PATHS[@]" | grep -m1 ^"$PKGNAME_ONLY\$" 2>/dev/null`.files"
  2846.  
  2847.  
  2848.   # no exact matches found, try fuzzy searches..
  2849.  
  2850.   # try PKGNAME*.files
  2851.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "/${PKGNAME}"'*.files'  2>/dev/null)"
  2852.  
  2853.   # try finding *PKGNAME.files
  2854.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name '*'"${PKGNAME}.files" 2>/dev/null)"
  2855.  
  2856.   # try ^PKGNAME_ONLY[_-] in user installed pkgs
  2857.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$PKGS_DIR/`cut -f1 -d'|' $USER_INST_PKGS_FILE | grep -Em1 "^$PKGNAME_ONLY[_-]" 2>/dev/null`.files"
  2858.  
  2859.   # try PKGNAME_ONLY* in user installed pkgs only
  2860.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$PKGS_DIR/`cut -f1 -d'|' $USER_INST_PKGS_FILE | grep -m1 ^"$PKGNAME_ONLY" 2>/dev/null`.files"
  2861.  
  2862.   # try PKGNAME_ONLY-* (any installed pkgs)
  2863.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$PKGS_DIR/`cut -f1 -d'|' "${SPEC_DB_PATHS[@]" | grep -Em1 ^"${PKGNAME_ONLY}[_-]" 2>/dev/null`.files"
  2864.  
  2865.   # try PKGNAME_ONLY* (any installed pkgs)
  2866.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$PKGS_DIR/`cut -f1 -d'|' "${SPEC_DB_PATHS[@]" | grep -m1 ^"$PKGNAME_ONLY" 2>/dev/null`.files"
  2867.  
  2868.   # try PKGNAME_ONLY*.files
  2869.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$(find "$PKGS_DIR" -maxdepth 1 -type f -name "/${PKGNAME_ONLY}"'*.files'  2>/dev/null)"
  2870.  
  2871.  
  2872.  
  2873.   #if we found a list of files
  2874.   if [ "$PKG_FLIST" != "" ]; then
  2875.  
  2876.     print_cmd=echo
  2877.     # PKG_FLIST just might contain a path to the pkg contents
  2878.     # themselves (a *.files, or a file in builtin_files/*).. so if
  2879.     # its a file path, we will cat it, if not, its the pkg contents
  2880.     # themselves, we echo it
  2881.     if [ "`echo "$PKG_FLIST" | grep -m1 '/builtin_files/'`" != '' -o "`echo "$PKG_FLIST" | grep -m1 "packages/$PKGNAME"`" != '' ]; then
  2882.       print_cmd=cat
  2883.     fi
  2884.  
  2885.     $print_cmd "$PKG_FLIST" | grep -v "^/$" 2>/dev/null
  2886.  
  2887.   else # if no files found
  2888.     if [ "$PKGNAME" != '' ]; then
  2889.       INST_CHECK="`is_installed_pkg $PKGNAME 2>/dev/null`"
  2890.       if [ "$INST_CHECK" != true -o "$pkg_is_local_file" = false ]; then
  2891.         error "Package must be installed, downloaded or built-in."
  2892.       else
  2893.         error "Could not get package contents, unable to get file list."
  2894.       fi
  2895.     else
  2896.       error "Could not get name of package."
  2897.     fi
  2898.   fi
  2899. }
  2900.  
  2901.  
  2902. pkg_entry(){                      # show pkg ($1) repo entry, each field on a new line FUNCLIST
  2903.  
  2904.   # exit if no valid opts
  2905.   [ ! "$1" -o "$1" = '-' ] && print_usage pkg-entry && exit 1
  2906.  
  2907.   local EX
  2908.   local PKGNAME
  2909.   local PKGNAME_ONLY
  2910.  
  2911.   # get pkg extension
  2912.   EX=`get_pkg_ext "$1"`
  2913.  
  2914.   # get pkg name with version, but no extension or path
  2915.   PKGNAME="$(basename "$1" .$EX)"
  2916.  
  2917.   # dont rely on the string the user gave us, try to get the exact match
  2918.   PKGNAME=`get_pkg_name "$PKGNAME"`
  2919.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  2920.  
  2921.   PKG_ENTRY="$(cat "$REPO_DB_FILE_DIR"/Packages-* | grep -m1 "|$PKGNAME|")"
  2922.   [ "$PKG_ENTRY" = '' ] && PKG_ENTRY="$(cat "$REPO_DB_FILE_DIR"/Packages-* | grep -m1 "^$PKGNAME|")"
  2923.   [ "$PKG_ENTRY" = '' ] && PKG_ENTRY="$(cat "$REPO_DB_FILE_DIR"/Packages-* | grep -m1 "|$PKGNAME_ONLY|")"
  2924.   if [ "$PKG_ENTRY" = '' ]; then
  2925.     echo "$1 not found in $REPOFILE"
  2926.     exit 1
  2927.   else
  2928.     echo "$PKG_ENTRY" | tr '|' '\n' | grep -v '^$'
  2929.   fi
  2930.   return 0
  2931. }
  2932.  
  2933.  
  2934. pkg_status(){                     # print package ($1) name, status, deps, etc FUNCLIST
  2935.  
  2936.   # exit if no valid options
  2937.   [ ! "$1" -o "$1" = "-" ] && print_usage pkg-status && exit 1
  2938.  
  2939.   # get current repo name
  2940.   . ${PKGRC}
  2941.  
  2942.   local EX
  2943.   local PKGNAME=''
  2944.   local PKGNAME_ONLY=''
  2945.   local PKGFILE=''
  2946.   local MSG=''
  2947.   local install_status=''
  2948.   local pkg_found=false
  2949.   local pkg_repo=''
  2950.   local pkg_repo_file=''
  2951.   local prev_repo=${REPONAME}
  2952.  
  2953.   # get pkg extension
  2954.   EX=`get_pkg_ext "$1"`
  2955.  
  2956.   # get pkg name with version, but no extension or path
  2957.   PKGNAME="$(basename "$1" .$EX)"
  2958.  
  2959.   # dont rely on the string the user gave us, try to get the exact match
  2960.   PKGNAME=`get_pkg_name "$PKGNAME"`
  2961.  
  2962.   # get pkg name without version
  2963.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  2964.  
  2965.   [ "$PKGNAME" = '' ] && error "Can't find $1" && exit 1
  2966.  
  2967.   # get install status of pkg (installed or not)
  2968.   [ "`is_installed_pkg $PKGNAME`" = true ] && install_status="installed" || install_status="not installed"
  2969.  
  2970.   # if pkg is installed, find out from where
  2971.   if [ "$install_status" = "installed" ]; then
  2972.  
  2973.     # check user installed pkgs for $PKGNAME
  2974.     pkg_found=`LANG=C is_usr_pkg "${PKGNAME}"`
  2975.     [ "$pkg_found" = true ] && install_status="installed (user)"
  2976.  
  2977.     # if pkg not found yet, check if pkg is from the devx
  2978.     if [ -f "$PKGS_DIR/devx-only-installed-packages" -a "$pkg_found" = false ]; then
  2979.       pkg_found=`LANG=C is_devx_pkg "${PKGNAME}"`
  2980.       [ "$pkg_found" = true ] && install_status="installed (in devx)"
  2981.     fi
  2982.  
  2983.     # check builtins for PKGNAME_ONLY, if it exists, its a builtin pkg
  2984.     if [ -d "$BUILTIN_FILE_LIST_DIR/" -a "$pkg_found" = false ]; then
  2985.       pkg_found=`LANG=C is_builtin_pkg "${PKGNAME_ONLY}"`
  2986.       [ "$pkg_found" = true ] && install_status="installed (builtin)"
  2987.     fi
  2988.  
  2989.     # last gasp, if pkg not found yet, check if pkg if listed in layers-installed packages
  2990.     if [ -f "$PKGS_DIR/layers-installed-packages" -a "$pkg_found" = false ]; then
  2991.       pkg_found=$(LANG=C grep -m1 "^${PKGNAME}|" "$LAYER_INST_PKGS_FILE")
  2992.  
  2993.       [ "$pkg_found" = '' ] && install_status="installed (layers)"
  2994.     fi
  2995.  
  2996.   fi
  2997.  
  2998.   # get the repo of this pkg, if needed
  2999.   [ "$pkg_repo" = '' ] && pkg_repo="`which_repo "$PKGNAME" 2>/dev/null | cut -f2 -d' '`"
  3000.  
  3001.   # if we got a repo name, get the repo file
  3002.   [ "$pkg_repo" != '' ] && pkg_repo_file="$(grep -m1 "^$pkg_repo|" ${HOME}/.pkg/sources | cut -f3 -d'|')"
  3003.  
  3004.   # if we have a repo to search for pkg info
  3005.   if [ "$pkg_repo_file" != "" ]; then
  3006.  
  3007.     # get package description from its repo entry
  3008.     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`"
  3009.  
  3010.     if [ "$pkg_desc" = '' ]; then
  3011.       pkg_desc="`LANG=C grep -m1 "|${PKGNAME_ONLY}|" "$PKGS_DIR/Packages-"* 2>/dev/null| grep -m1 "${PKGNAME}|" | cut -f10 -d'|' | head -1`"
  3012.     fi
  3013.  
  3014.     # get size of pkg
  3015.     pkg_size=`LANG=C grep -m1 "|${PKGNAME_ONLY}|" "$REPO_DB_FILE_DIR/$pkg_repo_file" 2>/dev/null | cut -f6 -d'|' | head -1`
  3016.  
  3017.     # add K to end of pkg_size, if not there already
  3018.     [ "$pkg_size" != '' ] && pkg_size="${pkg_size}K" && pkg_size="${pkg_size//KK/K}"
  3019.  
  3020.     # if called with -PS, we get the full info (where we sort deps missing or installed)
  3021.     if [ "$FULL_PKG_STATUS" = true ]; then
  3022.  
  3023.       # if pkg has many deps, checking the deps might take a while, print a 'please wait' msg
  3024.       please_wait=false
  3025.       # count deps of pkg
  3026.       [ "`has_deps "$PKGNAME"`" = true ] && please_wait=true
  3027.       [ "$please_wait" = true ] && echo -ne "Please wait.. Gathering dependency information.\n"
  3028.  
  3029.       set_current_repo $pkg_repo 1>/dev/null
  3030.       # sort deps into 2 lists: missing deps and installed deps
  3031.       find_deps "$PKGNAME"
  3032.       set_current_repo $prev_repo 1>/dev/null
  3033.  
  3034.       # create a nicely formatted, coloured list of deps, green for installed, yellow for missing
  3035.       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}"
  3036.       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}"
  3037.  
  3038.       DEPS_ENTRY="Installed deps: ${INSTALLED_DEPS_MSG:-None}
  3039. Missing deps:   ${MISSING_DEPS_MSG:-None}"
  3040.  
  3041.     else
  3042.  
  3043.       DEPS_ENTRY="Dependencies:   `list_deps "$PKGNAME" 2>/dev/null`"
  3044.  
  3045.     fi
  3046.  
  3047.     #250613 added desc
  3048.     MSG="
  3049. Name:           ${PKGNAME}
  3050. Description:    `echo ${pkg_desc:-No description} | fold -w 50 -s | sed '2,10 s/^/                /g'`
  3051. Size:           ${pkg_size:-Unknown}
  3052. Status:         ${install_status:-Unknown}
  3053. In Repo:        ${pkg_repo:-Not in any repos}
  3054. Repo file:      `basename ${pkg_repo_file:-Not in any repositories} 2>/dev/null`
  3055. $DEPS_ENTRY"
  3056.  
  3057.   fi
  3058.  
  3059.   # if not found in any repo, maybe a woof or alien (user-installed) package
  3060.   if [ "$MSG" = "" ]; then #if nothing found in any repo
  3061.  
  3062.     PKGFILE="`list_downloaded_pkgs | grep -m1 "^$PKGNAME"`"
  3063.  
  3064.     # if the pkg is a downloaded file (in WORKDIR)
  3065.     if [ -f "$WORKDIR/$PKGFILE" ]; then
  3066.  
  3067.       # get the file, and get its file size
  3068.       PKGFILE="$WORKDIR/${PKGFILE}"
  3069.       [ "$pkg_size" = '' ] && pkg_size="`du -s -k "$PKGFILE" | cut -f1`K"
  3070.       [ "$pkg_size" = '' ] && pkg_size="`grep -m1 "|${PKGNAME_ONLY}|" "${SPEC_DB_PATHS[@]" | cut -f6 -d'|' | head -1`"
  3071.  
  3072.  
  3073.       # create msg for downloaded pkgs
  3074.       MSG="Name:           ${PKGNAME}
  3075. Size:           ${pkg_size:-Unknown}
  3076. Status:         Downloaded
  3077. Note:           Downloaded package, not in any repo.
  3078. $DEPS_ENTRY"
  3079.  
  3080.     # else, search for exact match, then pkg-*, then pkg_*, then pkg* in installed pkgs
  3081.     elif [ "$(grep -m1 "|${PKGNAME_ONLY}|" "$USER_INST_PKGS_FILE" 2>/dev/null)"   \
  3082.       -o "$(grep -m1 "^${PKGNAME}|" "$USER_INST_PKGS_FILE" 2>/dev/null)"   \
  3083.       -o "`grep -m1 "^${PKGNAME}|" "${SPEC_DB_PATHS[@]" 2>/dev/null`"   \
  3084.       -o "`grep -m1 "^${PKGNAME}-" "${SPEC_DB_PATHS[@]" 2>/dev/null`" \
  3085.       -o "`grep -m1 "^${PKGNAME}_" "${SPEC_DB_PATHS[@]" 2>/dev/null`" \
  3086.       -o "`grep -m1 "^${PKGNAME}" "${SPEC_DB_PATHS[@]" 2>/dev/null`" ]; then
  3087.  
  3088.       # get deps and size from its entry in user-installed-packages
  3089.       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`"
  3090.       [ "$DEPS_ENTRY" = '' ] && DEPS_ENTRY="Dependencies:   `grep -m1 "^$PKGNAME" "${SPEC_DB_PATHS[@]" | cut -f9 -d'|' | sed -e 's/,+/,/' -e 's/^+//' -e 's/,$//' 2>/dev/null`"
  3091.       pkg_size=`LANG=C grep -m1 "|$PKGNAME_ONLY|" "${SPEC_DB_PATHS[@]" | cut -f6 -d'|' | head -1`
  3092.       # add K to end of pkg_size, if not there already
  3093.       [ "$pkg_size" != '' ] && pkg_size="${pkg_size}K" && pkg_size="${pkg_size//KK/K}"
  3094.  
  3095.       # create msg for user installed pkgs
  3096.       MSG="Name:      ${PKGNAME}
  3097. Size:           ${pkg_size:-Unknown}
  3098. Status:         ${install_status}
  3099. Repo:           Alien package, not in any repo.
  3100. $DEPS_ENTRY"
  3101.  
  3102.     else # pkg wasn't found, it's unknown to Pkg
  3103.       MSG="$PKGNAME not found.
  3104.  
  3105. Here are some packages in other repos:"
  3106.       PLIST="`which_repo ${PKGNAME}`"
  3107.       [ "$PLIST" != "" ] && MSG="$MSG
  3108. $PLIST"
  3109.       # create msg for alien pkgs
  3110.       MSG="$MSG
  3111.  
  3112. Name:           ${PKGNAME}
  3113. Status:         ${install_status:-Unknown}
  3114. Repo:           Alien package, not in $REPONAME repo.
  3115. $DEPS_ENTRY"
  3116.  
  3117.     fi
  3118.  
  3119.   fi
  3120.  
  3121.   # print message
  3122.   if [ "$MSG" != "" ]; then
  3123.     # now output the final msg
  3124.     echo -e "$MSG" | grep -v ^$
  3125.     menu_entry_msg "$PKGNAME"
  3126.     echo
  3127.   else
  3128.     not_found "${PKGNAME}"
  3129.     exit 1
  3130.   fi
  3131. }
  3132.  
  3133.  
  3134. # pkg creation funcs
  3135.  
  3136. prepare_petbuild(){               # get 01mickos petbuild system from Git, if needed FUNCLIST
  3137.  
  3138.   local gitcheck=`which git`
  3139.  
  3140.   # exit if no devx
  3141.   [ "`which gcc`" = '' ] \
  3142.     && echo "You need the devx SFS installed to compile packages." \
  3143.     && rm /tmp/pkg/petbuild_prepared 2>/dev/null \
  3144.     && exit 3
  3145.  
  3146.   # exit if no git
  3147.   [ "`$gitcheck`" = '' ] \
  3148.     && echo "You need git installed to auto-install petbuild." \
  3149.     && rm /tmp/pkg/petbuild_prepared 2>/dev/null \
  3150.     && exit 3
  3151.  
  3152.   if [ ! -d /usr/share/petbuild ]; then
  3153.     rm /tmp/pkg/petbuild_prepared 2>/dev/null && exit 1
  3154.   fi
  3155.  
  3156.   # if petbuild prepared flag not yet set
  3157.   if [ ! -f /tmp/pkg/petbuild_prepared ]; then
  3158.  
  3159.     # backup old petbuild if installed
  3160.     [ -d /usr/share/petbuild/ ] && mv /usr/share/petbuild/ /usr/share/petbuild.backup/ 2>/dev/null
  3161.  
  3162.     # remove the original petbuild dir (we have a backup)
  3163.     if [ -d /usr/share/petbuild.backup/ ]; then
  3164.       rm -rf /usr/share/petbuild 2>/dev/null
  3165.     fi
  3166.  
  3167.     # clone petbuild into /usr/share/petbuild
  3168.     git clone https://github.com/puppylinux-woof-CE/petbuilds.git /usr/share/petbuild &>/dev/null
  3169.  
  3170.     # if still no petbuild dir in /usr/share, tell user to install themselves
  3171.     if [ ! -d /usr/share/petbuild ]; then
  3172.       echo >&2
  3173.       echo "Run this command to download PetBuild:" >&2
  3174.       echo >&2
  3175.       echo -e " ${green}git clone https://github.com/puppylinux-woof-CE/petbuilds /usr/share/petbuild${endcolour}" >&2
  3176.       echo >&2
  3177.       echo "Alternatively, use a different build tool backend by changing BUILDTOOL= " >&2
  3178.       echo "to one of the options below, in the $PKGRC file:" >&2
  3179.       echo "petbuild, buildpet, sbopkg or src2pkg" >&2
  3180.       echo >&2
  3181.  
  3182.       exit 1
  3183.     fi
  3184.  
  3185.     # go into buildpet dir, check which Pup we have ($DISTRO_DB_SUBNAME)
  3186.     # and checkout the right branch for the running system
  3187.     cd /usr/share/petbuild
  3188.  
  3189.     # check the puppy running and checkout the relevant branch
  3190.     if [ "`echo $DISTRO_DB_SUBNAME | grep slacko`" != '' ]; then
  3191.       if [ "`echo $DISTRO_DB_SUBNAME | grep '14.2'`" != '' ]; then
  3192.         git checkout slacko_142
  3193.       else
  3194.         git checkout slacko_141
  3195.       fi
  3196.     elif [ "`echo $DISTRO_DB_SUBNAME | grep tahrpup`" ]; then
  3197.       git checkout tahrpup
  3198.     elif [ "`echo $DISTRO_DB_SUBNAME | grep stretch`" ]; then
  3199.       git checkout stretch || git checkout tahrpup
  3200.     elif [ "`echo $DISTRO_DB_SUBNAME | grep xenialpup`" ]; then
  3201.       git checkout xenialpup || git checkout tahrpup
  3202.     elif [ "$DISTRO_DB_SUBNAME" != '' ]; then
  3203.       git checkout tahrpup
  3204.     else
  3205.       echo "No pet builds available for your system ($DISTRO_DB_SUBNAME),"
  3206.       echo "using buildpet instead.."
  3207.       rm -rf /usr/share/petbuild 2>/dev/null
  3208.     fi
  3209.  
  3210.     # buildpet setup finished, create flag
  3211.     echo 'true' > /tmp/pkg/petbuild_prepared
  3212.  
  3213.     cd "$CURDIR"
  3214.   fi
  3215. }
  3216.  
  3217.  
  3218. pkg_build(){                      # build package ($1) from source (see BUILDTOOL in pkgrc) FUNCLIST
  3219.  
  3220.   # exit if devx not installed
  3221.   [ "`which gcc`" = "" ]   && echo "You need the devx SFS installed to compile." && exit 1
  3222.  
  3223.   # get the settins from RC file
  3224.   . ${PKGRC}
  3225.  
  3226.   # we do a different build method for each supported build tool
  3227.   case $BUILDTOOL in
  3228.  
  3229.     petbuild) # by 01micko
  3230.  
  3231.       # use git to install the latest petbuild
  3232.       prepare_petbuild
  3233.  
  3234.       # if petbuild not installed, quit
  3235.       [ ! -d /usr/share/petbuild ] && error "PetBuild not installed at /usr/share/petbuild." && exit 3
  3236.  
  3237.       # petbuild needs a package name, or exit
  3238.       [ ! "$1" -o "$1" = "-" ] && print_usage pkg-build && exit 1 #220613 #250613
  3239.  
  3240.       # get pkg extension
  3241.       EX=`get_pkg_ext "$1"`
  3242.  
  3243.       # get pkg name only, no extension or path
  3244.       PKGNAME="$(basename "$1" .$EX)"
  3245.  
  3246.       # get petbuild PKGNAME, then build using 01mickos petbuild
  3247.       BUILDSCRIPT="`find /usr/share/petbuild -iname ${PKGNAME}"*.petbuild"| grep -m1 "$PKGNAME"`"
  3248.  
  3249.       # if we got a build script
  3250.       if [ -f "$BUILDSCRIPT" ]; then
  3251.         cd `dirname "$BUILDSCRIPT"`
  3252.         # compile the pkg
  3253.         bash *.petbuild
  3254.         # after build, move any pets created to WORKDIR
  3255.         mv /usr/share/petbuild/0pets_out/*.pet "$WORKDIR" 2>/dev/null
  3256.         # back to prev dir (WORKDIR)
  3257.         cd -
  3258.       fi
  3259.  
  3260.       # if build script not found, exit
  3261.       [ ! -f "$BUILDSCRIPT" ] && echo "Build script for '$PKGNAME' not found in /usr/share/petbuild/" && exit 1
  3262.  
  3263.       # get the packages we just moved to WORKDIR
  3264.       PKGFILES="`find "$WORKDIR" -iname "$PKGNAME*.pet"`"
  3265.  
  3266.       # list any packages we just built
  3267.       [ "$PKGFILES" != '' ] && echo -e "${green}Success:${endcolour} Packages in $WORKDIR:" && echo "$PKGFILES"
  3268.     ;;
  3269.  
  3270.     buildpet) # by Tman/iguleder
  3271.  
  3272.       # exit if buildpet dir not found
  3273.       [ ! -d /usr/share/buildpet ] && error "buildpet not installed." && exit 1
  3274.  
  3275.       # buildpet expects a PKGNAME, or exit
  3276.       [ ! "$1" -o "$1" = "-" ] && print_usage pkg-build && exit 1
  3277.  
  3278.       # get pkg extension
  3279.       EX=`get_pkg_ext "$1"`
  3280.  
  3281.       # get pkg name (with version), no extension or path
  3282.       PKGNAME="$(basename "$1" .$EX)"
  3283.  
  3284.       # get the buildpet script of PKGNAME
  3285.       BUILDSCRIPT="`find /usr/share/buildpet/ -name $PKGNAME"*"`"
  3286.  
  3287.       # if build script not found, exit
  3288.       [ ! -f "$BUILDSCRIPT" ] && echo "Build script for '$PKGNAME' not found in /usr/share/buildpet/" && exit 1
  3289.  
  3290.       # now compile the package
  3291.       PKG_CONFIGURE="$PKG_CONFIGURE" PKG_CFLAGS="$PKG_CFLAGS" buildpet $BUILDSCRIPT
  3292.  
  3293.     ;;
  3294.  
  3295.     src2pkg)  # by amigo
  3296.  
  3297.       ### This code is a horribly simple wrapper to a great tool..
  3298.       ### Implement all src2pkg options here at some point..
  3299.       ##
  3300.       ### We should pass all options to src2pkg, and make it build a
  3301.       ### (petbuild|buildpet) buildscipt on successful compile
  3302.  
  3303.       # exit if src2pkg not installed
  3304.       [ "`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
  3305.  
  3306.       # exit if $1 not given or valid
  3307.       [ ! "$1" -o "$1" = "-" ]   && print_usage pkg-build && exit 1
  3308.  
  3309.       # show src2pkg help if user supplied any of the help options
  3310.       if [ "$1" = '-h'  -o "$1" = '-hh'  -o "$1" = '--help' -o "$1" = '--more-help' -o "$1" = '--list' ]; then
  3311.         src2pkg $1 | sed -e "s/  src2pkg /  $SELF -pb /g"
  3312.         echo -e "\n   See 'src2pkg' by amigo"
  3313.         exit 0
  3314.       fi
  3315.  
  3316.       # get pkg extension.. not really needed here
  3317.       EX=`get_pkg_ext "$1"`
  3318.  
  3319.       # use amigos src2pkg
  3320.       src2pkg $1
  3321.  
  3322.       # if src2pkg exited without error, copy any pets it made to WORKDIR
  3323.       [ $? -eq 0 ] && mv /tmp/*.pet "$WORKDIR" 2>/dev/null
  3324.  
  3325.     ;;
  3326.  
  3327.     sbopkg)   # slackware peeps
  3328.  
  3329.       # exit if not installed
  3330.       [ "`which sbopkg`" = '' ] && error "Sbopkg not installed." && exit 3
  3331.  
  3332.       # exit if no valid options
  3333.       [ ! "$1" -o "$1" = "-" ]  && print_usage pkg-build && exit 1
  3334.  
  3335.       sbopkg -b "$1"
  3336.  
  3337.       # move any built pkgs to WORKDIR.. need a better way to do this
  3338.       if [ "`find /tmp/* -iname $1*.t*`" != '' ]; then
  3339.         mv /tmp/*.tgz "$WORKDIR" 2>/dev/null
  3340.         mv /tmp/*.txz "$WORKDIR" 2>/dev/null
  3341.         mv /tmp/*.tar.xz "$WORKDIR" 2>/dev/null
  3342.         echo -e "DONE... The package should be in $WORKDIR."
  3343.       fi
  3344.  
  3345.     ;;
  3346.  
  3347.   *)
  3348.     # get pkg name only, no extension or path
  3349.     PKGNAME="$(basename "$1" .$EX)"
  3350.  
  3351.     echo "Cannot compile '$PKGNAME', no build system installed."
  3352.     echo
  3353.     echo "Please install one of the following:"
  3354.     echo " * petbuild by 01micko: http://murga-linux.com/puppy/viewtopic.php?t=96027"
  3355.     echo " * buildpet by Tman:    http://murga-linux.com/puppy/viewtopic.php?t=81056"
  3356.     echo " * src2pkg  by amigo:   http://distro.ibiblio.org/amigolinux/download/src2pkg/"
  3357.     echo " * sbopkg   by various: https://www.sbopkg.org/downloads.php"
  3358.     echo
  3359.     echo "Then set BUILDTOOL to either petbuild, buildpet, src2pkg or sbopkg "
  3360.     echo "in $PKGRC to enable building packages from source."
  3361.     echo
  3362.     echo "NOTE:"
  3363.     echo "01mickos petbuild should be installed to /usr/share/petbuild"
  3364.     echo "And Tmans buildpet should be installed to /usr/share/buildpet"
  3365.   ;;
  3366.   esac
  3367. }
  3368.  
  3369.  
  3370. pkg_repack(){                     # create package ($1) from its *.files list FUNCLIST
  3371.  
  3372.   # exit if no valid options
  3373.   [ ! "$1" -o "$1" = "-" ] && print_usage pkg-repack && exit 1
  3374.  
  3375.   local list=''
  3376.   local pkg_ext=''
  3377.   local PKGNAME=''
  3378.   local PKGNAME_ONLY=''
  3379.   local dir=''
  3380.   local build_number=''
  3381.  
  3382.   # get pkg extension
  3383.   pkg_ext=`get_pkg_ext "$1"`
  3384.  
  3385.   #get pkg name only, no extension or path
  3386.   PKGNAME="$(LANG=C basename "$1" .$pkg_ext)"
  3387.  
  3388.   # assume the file name from $1
  3389.   PKGFILE="${CURDIR}/${PKGNAME}.$pkg_ext"
  3390.  
  3391.   # don't rely on the given pkg name string, get the full name from repo if poss
  3392.   PKGNAME=`get_pkg_name "$PKGNAME"`
  3393.  
  3394.   # get pkg name without version
  3395.   PKGNAME_ONLY="`get_pkg_name_only "${PKGNAME}"`"
  3396.  
  3397.   # if the list is empty, pkg is not user installed or built in, cant show contents
  3398.   [ "`is_installed_pkg "$PKGNAME"`" = false ] && echo "$PKGNAME needs to be installed." && exit 1
  3399.  
  3400.   # if the package to built already exists, ask user to delete it
  3401.   [ -f "${PKGFILE}" ] && echo "$PKGNAME.$pkg_ext already exists in $CURDIR" && rm -f "${PKGFILE}" 2>/dev/null
  3402.  
  3403.   # if pkg exists, user didn't delete it
  3404.   [ -f "${PKGFILE}" ] && exit 0
  3405.  
  3406.   # get the build number, and increment by one
  3407.   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'|'`"
  3408.   build_number=${build_number:-0}
  3409.   build_number="-$(($build_number + 1))"
  3410.  
  3411.   # process the file list, copy each file to our pkg folder
  3412.   pkg_contents "$PKGNAME_ONLY" | while read line
  3413.   do
  3414.     if [ -f "$line" ]; then
  3415.       linedir="$(dirname "${line}")"
  3416.       [ ! -d "${CURDIR}/$PKGNAME/${linedir}" ] && mkdir -p "${CURDIR}/$PKGNAME/${linedir}"
  3417.       cp -p -P "$line" "${CURDIR}/${PKGNAME}/${linedir}"
  3418.     fi
  3419.   done
  3420.  
  3421.   sync
  3422.  
  3423.   # make sure we populated a pkg folder, ready to package up
  3424.   [ ! -d "${CURDIR}/${PKGNAME}/" ] && error "No '$PKGNAME' directory in $CURDIR" && exit 5
  3425.  
  3426.   # pkg folder should be populated, now package it up and print final msg
  3427.   dir2pet "${CURDIR}/${PKGNAME}/" "$build_number"
  3428.   rm -rf "${CURDIR}/${PKGNAME}/" 2>/dev/null
  3429.   rm -rf "${CURDIR}/${PKGNAME}$build_number/" 2>/dev/null
  3430.   sync
  3431. }
  3432.  
  3433.  
  3434. pkg_unpack(){                     # extract/unpack $1 to current dir FUNCLIST
  3435.  
  3436.   # exit if not valid usage
  3437.   [ ! -f "$1" ] && print_usage unpack && exit 1
  3438.  
  3439.   local PKGNAME
  3440.   local PKGFILE
  3441.   local PKGEXT
  3442.   local comp
  3443.  
  3444.   # get pkg details
  3445.   PKGFILE="$1"
  3446.   PKGNAME=`get_pkg_name "$PKGFILE"`
  3447.   PKGEXT=`get_pkg_ext "$PKGFILE"`
  3448.  
  3449.   # exit if we dont have enough info
  3450.   [ "$PKGEXT" = '' ]  && error "Cant get package extension" && exit 1
  3451.   [ ! -f "$PKGFILE" ] && error "Cant find $1" && exit 6
  3452.   [ "$PKGNAME" = '' ] && error "Cant get PKGNAME" && exit 3
  3453.  
  3454.   # support overriding the output dir with $2
  3455.   if [ "$2" != "" ]; then
  3456.     PKGNAME="$2"
  3457.   fi
  3458.  
  3459.   # determine compression utility
  3460.   case $PKGEXT in
  3461.  
  3462.     deb)
  3463.       mkdir "$PKGNAME" 2>/dev/null
  3464.       rm -rf "$PKGNAME"/* 2>/dev/null
  3465.       dpkg-deb -x "$PKGFILE" "$PKGNAME"    # extracts main pkg contents
  3466.       result=$?
  3467.       dpkg-deb -e "$PKGFILE" "$PKGNAME"/DEBIAN # extracts deb control files
  3468.       [ $result -eq 0 ] && echo -e "${green}Extracted${endcolour}: ${magenta}`basename $PKGFILE`${endcolour}" || error -e "${red}Error${endcolour}: Cannot extract $PKGFILE"
  3469.       return $result
  3470.       ;;
  3471.  
  3472.     rpm)
  3473.       mkdir "${PKGNAME}" 2>/dev/null
  3474.       rm -rf "$PKGNAME"/* 2>/dev/null
  3475.       # create dir called $PKGNAME, cd into it, extract the rpm in there
  3476.       cp "$PKGFILE" "$PKGNAME/"
  3477.       cd "$PKGNAME" 1>/dev/null
  3478.       # now extract the rpm contents into current dir
  3479.       exploderpm -x "$PKGFILE" &>/dev/null
  3480.       result=$?
  3481.       # clean up and leave
  3482.       rm -f *.rpm 2>/dev/null
  3483.       cd - 1>/dev/null
  3484.       # final msg
  3485.       [ $result -eq 0 ] && echo -e "${green}Success${endcolour}: File ${magenta}`basename $PKGFILE`${endcolour} extracted." || error "Cannot extract $PKGFILE"
  3486.       return $result
  3487.       ;;
  3488.  
  3489.     sfs)
  3490.  
  3491.       mkdir "/${PKGNAME}" 2>/dev/null
  3492.       rm -rf "$PKGNAME"/* 2>/dev/null
  3493.  
  3494.       # make mnt dir, mount sfs
  3495.       mkdir "/mnt/${PKGNAME}" 2>/dev/null
  3496.       mount -t squashfs "$PKGFILE" /mnt/"$PKGNAME" -o loop 1>/dev/null || { error "Could not mount $PKGFILE"; exit 3; }
  3497.       # copy sfs contents into ./$PKGNAME
  3498.       cp -a -Rf --remove-destination /mnt/"$PKGNAME" "$PKGNAME" || { error "Failed copying files from /mnt/$PKGNAME to ./$PKGNAME"; exit 4; }
  3499.       result=$?
  3500.       # clean up
  3501.       umount "/mnt/${PKGNAME}"
  3502.       rmdir "/mnt/${PKGNAME}"
  3503.       # final msg
  3504.       [ $result -eq 0 ] && echo -e "${green}Success${endcolour}: File ${magenta}`basename $PKGFILE`${endcolour} extracted." || { error "Cannot extract $PKGFILE"; exit 7; }
  3505.       return $result
  3506.       ;;
  3507.     pet)  file -b "$PKGFILE" | grep -i -q "^xz" && comp=xz || comp=gzip ;;
  3508.     tgz)  comp=gzip ;;
  3509.     gz)   comp=gzip ;;
  3510.     tbz)  comp=bzip2 ;;
  3511.     bz2)  comp=bzip2 ;;
  3512.     tlz)  comp=lzma ;;
  3513.     lzma) comp=lzma ;;
  3514.     txz)  comp=xz ;;
  3515.     xz)   comp=xz ;;
  3516.   esac
  3517.   sync
  3518.  
  3519.   # if pkg is extractable with tar, then unpack
  3520.   case $PKGEXT in
  3521.   pet|tgz|gz|tbz|bz2|tlz|lzma|txz|xz)
  3522.     ( 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" )
  3523.     ;;
  3524.   esac
  3525.  
  3526.   # return
  3527.   return ${result:-0}
  3528.  
  3529. }
  3530.  
  3531.  
  3532. pkg_combine(){                    # combine a pkg ($1) and deps into one single pkg FUNCLIST
  3533.  
  3534.   # exit if no valid options
  3535.   [ ! "$1" -o "$1" = "-" ] && print_usage pkg-combine && exit 1
  3536.  
  3537.   . ${PKGRC}
  3538.  
  3539.   local EX=''
  3540.   local PKGNAME=''
  3541.   local PKGNAME_ONLY=''
  3542.   local PKG_FILENAME=''
  3543.   local PKG_FILE=''
  3544.   local ALL_DEPS=''
  3545.   local PKG_DEPLIST=''
  3546.   local SUFFIX="${CP_SUFFIX}"
  3547.   local BUILD_DIR=$TMPDIR/build_pkg
  3548.   local SFS_FILE
  3549.   local please_wait=false
  3550.   local PREVDIR="$CURDIR"
  3551.   local ask_opt=$ASK
  3552.   local force_opt=$FORCE
  3553.  
  3554.   cd "$WORKDIR"
  3555.  
  3556.   # get pkg extension
  3557.   EX=`get_pkg_ext "$1"`
  3558.  
  3559.   # get reliable package names
  3560.   PKGNAME="$(basename "$1" .$EX)"
  3561.   PKGNAME=`get_pkg_name "$PKGNAME"`
  3562.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  3563.  
  3564.   PKG_FILE="${WORKDIR}/${PKGNAME}.$EX" #the file to install
  3565.  
  3566.   # we want to include all deps, except builtins, by default
  3567.   # we dont set HIDE_BUILTINS here, we leave that up to user
  3568.   # when user gives -F, HIDE_BUILTINS= false, and builtin
  3569.   # pkgs will be included in the package created
  3570.   FORCE=true
  3571.   HIDE_USER_PKGS=false
  3572.  
  3573.   # get full pkg filename (inc name-ver.ext).. try repos first
  3574.   PKG_FILENAME="`cut -f8 -d'|' "$REPO_DB_FILE_DIR"/Packages-* | grep -m1 "^$PKGNAME"`"
  3575.   # then WORKDIR if needed (dont include the combined pkgs)
  3576.   [ "$PKG_FILENAME" = "" ] && PKG_FILENAME="`ls -1 "$WORKDIR" | grep -v "${SUFFIX}" | grep -v ".sfs\$" | grep -m1 "^$PKGNAME"`"
  3577.  
  3578.   # just in case its empty, revert back to the earlier PKGNAME value
  3579.   [ "$PKG_FILENAME" = "" ] && PKG_FILENAME="$PKGNAME"
  3580.  
  3581.   [ "$PKG_FILENAME" = "" ] && error "Cant find $1" && exit 1
  3582.  
  3583.   # update PKGFILE with new pkg filename (PKG)
  3584.   PKG_FILE="${WORKDIR}/${PKG_FILENAME}"
  3585.  
  3586.   # check if the desired file has already been built
  3587.   [ "$COMBINE2SFS" = true ] && final_ext=sfs || final_ext=pet
  3588.   [ -f "${WORKDIR}/${PKGNAME}-${SUFFIX}.$final_ext" ] && echo -e "File ${magenta}${WORKDIR}/${PKGNAME}-${SUFFIX}.${final_ext}${endcolour} already exists." && continue
  3589.  
  3590.   # get list of deps for PKGNAME, if no deps, exit
  3591.   PKG_DEPLIST="`FORCE=$force_opt list_deps $PKGNAME_ONLY`"
  3592.   [ "$PKG_DEPLIST" = "" ] && echo "No need to combine, $PKGNAME has no dependencies." && exit 1
  3593.  
  3594.   # if exact match of pkgname is found in a repo list
  3595.   if [ "`is_repo_pkg "$PKGNAME"`" = true ]; then
  3596.  
  3597.     echo "Please wait.. Gathering dependency information."
  3598.  
  3599.     # get the deps of the pkg, but dont install
  3600.     FORCE=$force_opt find_deps "$PKGNAME"
  3601.     ALL_DEPS="`cat $TMPDIR/deps_installed $TMPDIR/deps_missing 2>/dev/null | sort | uniq`"
  3602.  
  3603.     # download the needed package
  3604.     if [ ! -f "$PKG_FILE" -o "$FORCE" = true ]; then
  3605.       ASK=false pkg_download "$PKGNAME"
  3606.     fi
  3607.  
  3608.     # make work dirs
  3609.     mkdir -p "$BUILD_DIR/"
  3610.     mkdir -p "$BUILD_DIR/${PKGNAME}-${SUFFIX}"
  3611.  
  3612.     if [ -f "$PKG_FILE" ]; then
  3613.       # copy the main pkg to the tmp dir
  3614.       # and remove the downloade file, we no longer need it
  3615.       cp "$PKG_FILE" "$BUILD_DIR/" 2>/dev/null && rm "$PKG_FILE"
  3616.     else
  3617.        error "Could not add the main package '$PKGNAME_ONLY'"
  3618.     fi
  3619.  
  3620.  
  3621.     # make a cleaned list to go over (sanity check)
  3622.     ALL_DEPS_LIST="`echo "$ALL_DEPS" | tr ',' '\n' | grep -v "^\$" | sort | uniq`"
  3623.  
  3624.     # go through each dep listed
  3625.     echo "$ALL_DEPS_LIST" | while read LINE
  3626.     do
  3627.  
  3628.       [ "$LINE" = "" -o "$LINE" = "-" -o "$LINE" = " " -o "$LINE" = "," -o "$LINE" = ", " ] && continue
  3629.  
  3630.       # only include builtins if HIDE_BUILTINS=true
  3631.       [ "`is_builtin_pkg "$LINE"`" = true -a "$HIDE_BUILTINS" = true ] && continue
  3632.  
  3633.       # only include devx pkgs if user gave the -f option
  3634.       [ "`is_devx_pkg "$LINE"`" = true -a "$force_opt" = false ] && continue
  3635.  
  3636.       # download the matching package(s)
  3637.       [ "`list_downloaded_pkgs "$LINE"`" = "" ] && ASK=false pkg_download "$LINE"
  3638.  
  3639.       # get the downloaded file
  3640.       PKGDEP="`find "$WORKDIR" -maxdepth 1 -type f -name "${LINE}*" | grep -v ".sfs\$" | grep -v "${SUFFIX}" | head -1`"
  3641.  
  3642.       # re-try download in other repos if needed
  3643.       if [ ! -f "$PKGDEP" ]; then
  3644.         PKGREPO=''; PKGREPO="`LANG=C which_repo $LINE | cut -f2 -d' ' | head -1`"
  3645.         [ "$PKGREPO" != "" ] && ASK=false pkg_download "$LINE"
  3646.       fi
  3647.  
  3648.       #if downloaded
  3649.       if [ -f "$PKGDEP" -o "$FORCE" = true ]; then
  3650.  
  3651.         # copy dep to the tmp dir, with the main pkg
  3652.         if [ ! -f "$BUILD_DIR/$PKGDEP" ]; then
  3653.           cp "$PKGDEP" "$BUILD_DIR/$(basename $PKGDEP)" && rm "$PKGDEP"
  3654.         else
  3655.           error "Cannot copy $PKGDEP to $BUILD_DIR/$(basename $PKGDEP)"
  3656.         fi
  3657.  
  3658.       else # dep not found, may be missing, or in another repo
  3659.         echo -e "${yellow}Warning:${endcolour} $LINE not downloaded to $WORKDIR.. Cannot add $LINE.."
  3660.         continue
  3661.       fi
  3662.     done
  3663.  
  3664.     # we should now be ready to make our combined pkg
  3665.     cd "$BUILD_DIR"
  3666.  
  3667.     PARENTPKG=${PKGNAME}
  3668.  
  3669.     # for all pkgs in the tmp dir (nto including any 'combined' pkgs)
  3670.     TMP_PKGS="`find "$BUILD_DIR/" -maxdepth 1 -type f -name "*" | grep -v $SUFFIX | grep -v ^$ | grep -v ' ' | sort | uniq`"
  3671.  
  3672.     local count=1
  3673.  
  3674.     for i in $TMP_PKGS
  3675.     do
  3676.  
  3677.       # skip if not a valid pkg file
  3678.       [ ! "$i" -o "$i" = ' ' -o "$i" = '' -o ! -f "$i" ] && continue
  3679.  
  3680.       # dont include the combined pkgs
  3681.       [ "`echo "$i" | grep -m1 "${SUFFIX}"`" != "" ] && continue
  3682.  
  3683.       # get the extensions of each file .. they might be from different repos, so cant use $EX
  3684.       base="`basename "$i" 2>/dev/null`"
  3685.       FILE_EXT=`get_pkg_ext "$base"`
  3686.  
  3687.       [ ! "$base" -o "$base" = ' ' -o "$base" = '' ] && continue
  3688.  
  3689.       # get name without version for this pkg ($i)
  3690.       name_only=`get_pkg_name_only "$i"`
  3691.  
  3692.       [ ! "$name_only" -o "$name_only" = ' ' -o "$name_only" = '' ] && continue
  3693.  
  3694.       CONFIRM=y
  3695.       if [ "$ask_opt" = true ]; then
  3696.         echo -n "Add package: $name_only  ($FILE_EXT)  (y/N):  "
  3697.         read -n 1 CONFIRM </dev/tty
  3698.         echo
  3699.       else
  3700.         echo "Adding package: $name_only  ($FILE_EXT)"
  3701.       fi
  3702.  
  3703.       # skip pkg if user wants to skip it
  3704.       if [ "$CONFIRM" != 'y' ]; then
  3705.         rm "$i"
  3706.         continue
  3707.       fi
  3708.  
  3709.       # add each file
  3710.       case $FILE_EXT in
  3711.       pet)
  3712.  
  3713.         # convert and extract
  3714.         pkg_unpack "${i}" 1>/dev/null
  3715.         sync
  3716.  
  3717.         # copy extracted contents (only the stuff inside the extracted folders)
  3718.         if [ ! -d "${i/.pet/}/" ]; then
  3719.           error "Dir '${i/.pet/}/' does not exist"
  3720.         fi
  3721.         cp -a --preserve=all -fr -L "${i/.pet/}/"* "${PARENTPKG}-${SUFFIX}/"
  3722.  
  3723.         if [ -f "${i/.pet/}/pinstall.sh" ]; then
  3724.           mv "${i/.pet/}/pinstall.sh" "${PARENTPKG}-${SUFFIX}/pinstall_${count}.sh"
  3725.         fi
  3726.  
  3727.         # now remove the pet and extract folder
  3728.         rm "${i}"
  3729.         rm -rf "${i/.pet/}/"
  3730.         sync
  3731.         ;;
  3732.  
  3733.       deb)
  3734.  
  3735.         #cp "$i" "$BUILD_DIR/${PARENTPKG}-${SUFFIX}"
  3736.         pkg_unpack "$i" 2>$TMPDIR/$SELF-cp-errlog
  3737.         dpkg-deb -e "$i" "$BUILD_DIR/${PARENTPKG}-${SUFFIX}/DEBIAN"
  3738.  
  3739.         # .deb package names often have extra stuff at the end of the file name
  3740.         # that does not exist in the root folder name, inside the package.. so
  3741.         # we need to strip off that extra stuff to get the correct unpacked
  3742.         # package dir
  3743.         local pkg_dir="${i/.deb/}/"
  3744.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/_all/}"
  3745.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/_i386/}"
  3746.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/_amd64/}"
  3747.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/_x64/}"
  3748.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/~dfsg-[0-9]/}"
  3749.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/~dsfg-[0-9]/}"
  3750.  
  3751.         if [ ! -d "$pkg_dir" ]; then
  3752.           needle="$(get_pkg_name "$i")"
  3753.           pkg_dir="$(find "${BUILD_DIR}" -type d -iname "$needle" | head -1)"
  3754.         fi
  3755.  
  3756.         if [ ! -d "$pkg_dir" ]; then
  3757.           error "Dir '$pkg_dir' does not exist!"
  3758.         fi
  3759.  
  3760.         # copy extracted contents (only the stuff inside the extracted folders)
  3761.         cp -a --preserve=all -fr -L "${pkg_dir}/"* "${PARENTPKG}-${SUFFIX}/"
  3762.  
  3763.         if [ -f "${pkg_dir}DEBIAN/postinst" ]; then
  3764.           mv "${pkg_dir}DEBIAN/postinst" "${PARENTPKG}-${SUFFIX}/DEBIAN/postinst_${count}"
  3765.         fi
  3766.  
  3767.         if [ -f "${pkg_dir}DEBIAN/preinst" ]; then
  3768.           mv "${pkg_dir}DEBIAN/preinst" "${PARENTPKG}-${SUFFIX}/DEBIAN/preinst_${count}"
  3769.         fi
  3770.  
  3771.         # now remove the deb and extracted folder
  3772.         rm "$i"
  3773.         rm -rf "$pkg_dir"
  3774.         sync
  3775.         ;;
  3776.  
  3777.       *tgz|*txz|*tar.gz|*tar.xz|*xz|*gz)
  3778.  
  3779.         pkg_unpack "$i" 1>/dev/null
  3780.  
  3781.         # now copy $BUILD_DIR/$PKGNAME/* into our output (combined pkg) dir
  3782.         cp -a --preserve=all -fr -L "${name_only}/"* "${PARENTPKG}-${SUFFIX}/" || error "Dir '${name_only}/' does not exist!"
  3783.  
  3784.         if [ -f "${PARENTPKG}-${SUFFIX}/install/doinst.sh" ]; then
  3785.           mv "${PARENTPKG}-${SUFFIX}/install/doinst.sh" "${PARENTPKG}-${SUFFIX}/install/doinst_${count}.sh"
  3786.         fi
  3787.  
  3788.         # remove the package and folder we just extracted
  3789.         rm "$i"
  3790.         rm -rf "${i/.*/}/"
  3791.         sync
  3792.         ;;
  3793.  
  3794.       rpm)
  3795.         pkg_unpack "$i" 1>/dev/null
  3796.  
  3797.         # now copy $BUILD_DIR/$PKGNAME/* into our output (combined pkg) dir
  3798.         cp -a --preserve=all -fr -L "${name_only}/"* "${PARENTPKG}-${SUFFIX}/"
  3799.  
  3800.         if [ -f "${PARENTPKG}-${SUFFIX}/install/doinst.sh" ]; then
  3801.           mv "${PARENTPKG}-${SUFFIX}/install/doinst.sh" "${PARENTPKG}-${SUFFIX}/install/doinst_${count}.sh"
  3802.         fi
  3803.  
  3804.         # remove the package and folder we just extracted
  3805.         rm "$i"
  3806.         rm -rf "${i/.rpm/}/"
  3807.         ;;
  3808.  
  3809.       esac
  3810.       count=$(($count + 1))
  3811.     done
  3812.  
  3813.     # we have now unpacked the package, lets combine the pre/post install scripts
  3814.     # into one pinstall.sh script
  3815.  
  3816.     local pinstall_file="$BUILD_DIR/${PARENTPKG}-${SUFFIX}/pinstall.sh"
  3817.  
  3818.     # append this pinstall/postinst/doinst script to a new pinstall.sh
  3819.     echo '#!/bin/sh' > "$pinstall_file"
  3820.  
  3821.     # concat the post install scripts (for puppy, slackware, arch, debian/ubuntu, etc) into 'pinstall.sh'
  3822.     for i in ${PARENTPKG}-${SUFFIX}/pinstall_* ${PARENTPKG}-${SUFFIX}/install/doinst_* ${PARENTPKG}-${SUFFIX}/DEBIAN/postinst_*
  3823.     do
  3824.       [ -z ${PARENTPKG}-${SUFFIX}/${i} ] && continue
  3825.       cd ${PARENTPKG}-${SUFFIX}/ 2>/dev/null
  3826.  
  3827.       files=`find . -type f -iname "$(basename $i)"`
  3828.  
  3829.       for file in $files
  3830.       do
  3831.         if [ -f "$file" ]; then
  3832.           echo "Appending ${file} to $pinstall_file"
  3833.           cat ${file} | grep -vE '^#!|^exit|exit 0|exit 1' >> "$pinstall_file"
  3834.           echo '' >> "$pinstall_file"
  3835.         fi
  3836.       done
  3837.       # we have combined the pre/post install stuff in $i into a new, combined file,
  3838.       # so remove the original
  3839.       rm -f ${PARENTPKG}-${SUFFIX}/${i}
  3840.     done
  3841.  
  3842.     chmod +x $BUILD_DIR/${PARENTPKG}-${SUFFIX}/pinstall.sh 2>/dev/null
  3843.  
  3844.     #xmessage "$(cat $BUILD_DIR/${PARENTPKG}-${SUFFIX}/pinstall.sh)"
  3845.  
  3846.     # remove any debian/ubuntu/slackware pre/post install stuff left in the package
  3847.     rm -rf $BUILD_DIR/${PARENTPKG}-${SUFFIX}/pinstall_*
  3848.     rm -rf $BUILD_DIR/${PARENTPKG}-${SUFFIX}/install
  3849.     rm -rf $BUILD_DIR/${PARENTPKG}-${SUFFIX}/DEBIAN
  3850.  
  3851.     # fix the symlinks to lib dirs - the linux-gnu-* dirs are symlinks in puppy,
  3852.     # so make sure we dont replace them with dirs (or programs won't load)
  3853.     for libdir in i386-linux-gnu i486-linux-gnu i586-linux-gnu i686-linux-gnu amd64-linux-gnu
  3854.     do
  3855.       if [ -d $BUILD_DIR/${PARENTPKG}-${SUFFIX}/usr/lib/${libdir}/ ]; then
  3856.         mv -n -u $BUILD_DIR/${PARENTPKG}-${SUFFIX}/usr/lib/${libdir}/* $BUILD_DIR/${PARENTPKG}-${SUFFIX}/usr/lib/
  3857.         cd $BUILD_DIR/${PARENTPKG}-${SUFFIX}/usr/lib
  3858.         rmdir ${libdir}/
  3859.         ln -s ${libdir}/ .
  3860.         cd -
  3861.       fi
  3862.     done
  3863.  
  3864.  
  3865.     # now we are ready to build our combined pkg
  3866.     [ "`pwd`" != "$BUILD_DIR" ] && cd "$BUILD_DIR/"
  3867.  
  3868.  
  3869.     # if not building SFS, build a .pet
  3870.     if [ "$COMBINE2SFS" = false ]; then #240613
  3871.       echo "Building PET package.. Please wait.."
  3872.  
  3873.       # build pet package... start with $removing PKGNAME-${SUFFIX}.pet.specs
  3874.       rm ${PKGNAME}-${SUFFIX}/*.specs 2>/dev/null
  3875.  
  3876.       # get compression type
  3877.       file -b "${PKGNAME}-${SUFFIX}" | grep -i -q "^xz" && TAREXT=xz || TAREXT=gz
  3878.  
  3879.       # tar up the folder
  3880.       tar -c -f ${PKGNAME}-${SUFFIX}.tar ${PKGNAME}-${SUFFIX} &>/dev/null
  3881.       sync
  3882.  
  3883.       case $TAREXT in
  3884.         xz)xz -z -9 -e ${PKGNAME}-${SUFFIX}.tar ;;
  3885.         gz)gzip --best ${PKGNAME}-${SUFFIX}.tar ;;
  3886.       esac
  3887.  
  3888.       TARBALL="${PKGNAME}-${SUFFIX}.tar.$TAREXT"
  3889.       FULLSIZE="`stat --format=%s ${TARBALL}`"
  3890.       MD5SUM="`md5sum $TARBALL | cut -f 1 -d ' '`"
  3891.       echo -n "$MD5SUM" >> $TARBALL
  3892.       sync
  3893.  
  3894.       mv -f $TARBALL ${PKGNAME}-${SUFFIX}.pet
  3895.       sync
  3896.  
  3897.       if [ $? -eq 1 ]; then
  3898.         echo "Cannot create PET file ${PKGNAME}-${SUFFIX} from dir."
  3899.         error "Please check `pwd`"
  3900.       fi
  3901.  
  3902.       # move our new PET to $WORKDIR
  3903.       mv ${PKGNAME}-${SUFFIX}.pet $WORKDIR/${PKGNAME}-${SUFFIX}.pet
  3904.       # get the size
  3905.       s=`LANG=C du -m "$WORKDIR/${PKGNAME}-${SUFFIX}.pet" | sed "s/\s.*//"`
  3906.  
  3907.     else #240613  build a .sfs
  3908.       echo "Building SFS package.. Please wait.."
  3909.  
  3910.       dir2sfs "${PKGNAME}-${SUFFIX}" 1>/dev/null
  3911.  
  3912.       # get the full file name, it may (or not) have been appended with '-DISTRO_VERSION'
  3913.       SFS_FILE=`find . -maxdepth 1 -type f -name "*${PKGNAME}-${SUFFIX}*.sfs" | head -1`
  3914.  
  3915.       s=`LANG=C du -m "${SFS_FILE}" | sed "s/\s.*//"`
  3916.       MD5SUM=`md5sum "$SFS_FILE" | cut -f1 -d ' '`
  3917.       mv "$SFS_FILE" "$WORKDIR/"
  3918.     fi
  3919.  
  3920.     # done, go back to work dir from WORKDIR/build_pkg
  3921.     cd "$WORKDIR"
  3922.     rm -R  "$BUILD_DIR/" 2>/dev/null
  3923.  
  3924.     # create install command
  3925.     if [ "$COMBINE2SFS" = false ]; then
  3926.       PKGEXT=pet
  3927.       PKGCMD="Install command: $SELF install ${WORKDIR}/${PKGNAME}-${SUFFIX}"
  3928.     else
  3929.       PKGEXT=sfs
  3930.       PKGCMD="Install command: sfs_loadr --cli -q \"${WORKDIR}/$(basename $SFS_FILE)\""
  3931.     fi
  3932.  
  3933.     #226013 updated output
  3934.     echo -e "Created package ${magenta}${PKGNAME}-${SUFFIX}.${PKGEXT}${endcolour} (${s}MB)"
  3935.     echo "The md5 checksum: $MD5SUM"
  3936.     echo "$PKGCMD"
  3937.  
  3938.   else # no exact PKGNAME match in repo
  3939.     not_found "${PKGNAME}"
  3940.     exit 1
  3941.   fi
  3942.  
  3943.   cd "$PREVDIR"
  3944. }
  3945.  
  3946.  
  3947. merge_pkg(){                      # merge the given comma-separated packages FUNCLIST
  3948.   # make sure at least 2 packages exist
  3949.  
  3950.   [ ! "$1" ] && print_usage merge && exit 1
  3951.   [ "$(echo "$1" |grep ',')" = "" ] && print_usage merge && exit 1
  3952.  
  3953.   cd "$WORKDIR" 1>/dev/null
  3954.  
  3955.   local PKG_LIST="$(echo "${1}" | tr ',' '\n' | sort -u | uniq)"
  3956.   local SUFFIX=''
  3957.  
  3958.   if [ "$2" = "--with-deps" ]; then
  3959.     SUFFIX="-WITHDEPS"
  3960.     echo "Gathering dependencies to include.."
  3961.     echo
  3962.     # add deps of pkgs to list of pkgs to merge
  3963.     for package in $PKG_LIST
  3964.     do
  3965.       PKG_LIST="$PKG_LIST $(pkg le $package)"
  3966.     done
  3967.     PKG_LIST="$(echo "$PKG_LIST" | tr ' ' '\n' | sort -u | uniq)"
  3968.   fi
  3969.  
  3970.   echo "Merging the following packages:"
  3971.   echo
  3972.   echo "${PKG_LIST}" | sed 's/^/  /g'
  3973.   echo
  3974.  
  3975.   # get a new package name for the new, merged package
  3976.   local PKGNAME="`get_pkg_name "$(basename "${1//,*/}")" 2>/dev/null`"
  3977.   bash -c 'read -e -r -p "Enter a package name and hit ENTER: " -i "${PKGNAME}-MERGED${SUFFIX}" NEWPKGNAME; echo $NEWPKGNAME > /tmp/pkg/NEWPKGNAME'
  3978.   echo
  3979.   NEWPKGNAME="$(cat /tmp/pkg/NEWPKGNAME)"
  3980.  
  3981.   if [ "$NEWPKGNAME" = "" ]; then
  3982.     echo "You must give a new package name!"
  3983.     return 1
  3984.   fi
  3985.  
  3986.   rm -rf "${NEWPKGNAME}/" &>/dev/null
  3987.   mkdir "${NEWPKGNAME}"
  3988.  
  3989.   # remove any old folders
  3990.   rm -rf ${WORKDIR}${NEWPKGNAME}/ &>/dev/null
  3991.  
  3992.   # for each package to merge
  3993.   for package in $PKG_LIST
  3994.   do
  3995.  
  3996.     # get the package details
  3997.     local PKGNAME="`get_pkg_name "$(basename "$package")" 2>/dev/null`"
  3998.     local PKGNAME_ONLY="`get_pkg_name_only "$PKGNAME" 2>/dev/null`"
  3999.     local PKGEXT="`get_pkg_ext "$PKGNAME"`"
  4000.     local PKGFILE=''
  4001.  
  4002.     [ "$PKGNAME" = "" ] && continue
  4003.     [ "$PKGNAME" = " " ] && continue
  4004.     [ "$PKGNAME" = "-" ] && continue
  4005.  
  4006.     # download it
  4007.     pkg_download "$PKGNAME"
  4008.  
  4009.     retval=$?
  4010.  
  4011.     if [ $retval -eq 1 ]; then
  4012.       echo -e "${yellow}Warning${endcolour}: package '$PKGNAME' not downloaded."
  4013.       continue
  4014.     fi
  4015.  
  4016.     # get the downloaded file
  4017.     PKGFILE="$(find $WORKDIR -maxdepth 1 -type f -name "${PKGNAME}*${PKGEXT}")"
  4018.  
  4019.     [ ! -f "$PKGFILE" ] && \
  4020.       PKGFILE="$(find $WORKDIR -maxdepth 1 -type f -name "${PKGNAME_ONLY}_*${PKGEXT}")"
  4021.  
  4022.     [ ! -f "$PKGFILE" ] && \
  4023.       PKGFILE="$(find $WORKDIR -maxdepth 1 -type f -name "${PKGNAME_ONLY}-*${PKGEXT}")"
  4024.  
  4025.     if [ ! -f "$PKGFILE" ]; then
  4026.       echo -e "${red}Error${endcolour}: could not find downloaded package '$PKGFILE'"
  4027.       return 1
  4028.     fi
  4029.  
  4030.     # unpack the downloaded file
  4031.     pkg_unpack "$PKGFILE"
  4032.     rm "${PKGFILE}"
  4033.  
  4034.     # move the unpacked files into our new package dir
  4035.     PKGDIR="$(find $WORKDIR -maxdepth 1 -type d -name "${PKGNAME}*" | grep -v ${NEWPKGNAME})"
  4036.  
  4037.     if [ ! -d "$PKGDIR" ]; then
  4038.       echo -e "${red}Error${endcolour}: could not find extracted package dir '$PKGDIR'"
  4039.       return 1
  4040.     fi
  4041.  
  4042.     cp -R "$PKGDIR"/* "$NEWPKGNAME"
  4043.     rm -rf "${PKGDIR}/"
  4044.   done
  4045.  
  4046.   if [ ! -d "${WORKDIR}/$NEWPKGNAME" ]; then
  4047.     echo -e "${red}Error${endcolour}: Dir ${lightblue}${NEWPKGNAME}${endcolour} NOT created!"
  4048.     return 1
  4049.   fi
  4050.  
  4051.   echo
  4052.   echo "Package contents:"
  4053.   find "${NEWPKGNAME}/"
  4054.   echo '--------------------------------'
  4055.   echo
  4056.  
  4057.   # create the merged PET package
  4058.   dir2pet "${NEWPKGNAME}/" || error "Could not create $NEWPKGNAME} PET file"
  4059.  
  4060.   rm -rf "./${NEWPKGNAME}/" &>/dev/null
  4061.   rm -rf "${WORKDIR}/${NEWPKGNAME}/" &>/dev/null
  4062.  
  4063.   # get the filename of the new PET package
  4064.   NEWPKGFILENAME="$(find . -maxdepth 1 -type f -name "${NEWPKGNAME}*.pet")"
  4065.  
  4066.   # output messages
  4067.   if [ ! -f "$NEWPKGFILENAME" ]; then
  4068.     echo
  4069.     echo -e "${red}Error${endcolour}: Package ${yellow}${NEWPKGFILENAME}${endcolour} NOT created!"
  4070.     return 1
  4071.   fi
  4072.  
  4073.   return 0
  4074. }
  4075.  
  4076.  
  4077. split_pkg(){                      # split package ($1) into dev, doc, nls packages FUNCLIST
  4078.  
  4079.   # make sure the package exists
  4080.   [ ! "$1" -o ! -f "$1" ] && print_usage split && exit 1
  4081.  
  4082.   local PKGNAME="`get_pkg_name "$(basename "$1")" 2>/dev/null`"
  4083.   local PKGNAME_ONLY="`get_pkg_name_only "$PKGNAME" 2>/dev/null`"
  4084.   local PKGEXT="pet"
  4085.  
  4086.   # get package extension
  4087.   PKGEXT="`get_pkg_ext "$1"`"
  4088.  
  4089.   pkg_unpack "$1" &>/dev/null
  4090.  
  4091.   # get the package directory we just extracted
  4092.   local PKG_PATH="$(find $(dirname "$PKGNAME") -maxdepth 1 -type d -iname "$PKGNAME" | head -1)"
  4093.  
  4094.   # get the full path to the package dir (dir with contents of pkg to be split)
  4095.   PKG_PATH="$(realpath "$PKG_PATH")"
  4096.  
  4097.   # make sure we have the right package dir
  4098.   [ ! -d "$PKG_PATH" ] && PKG_PATH="$(realpath "$PKGNAME")"
  4099.  
  4100.   # get the base name of the package directory
  4101.   local PKG_DIR_NAME=$(basename "$PKG_PATH")
  4102.  
  4103.   # get the package name
  4104.   local PKG_NAME="$PKGNAME_ONLY"
  4105.  
  4106.   # get the parent directory of the package
  4107.   local PARENT_DIR="$(dirname "$PKG_PATH")"
  4108.  
  4109.   # set the correct package naming style
  4110.   local dev_suffix='_DEV'
  4111.   local doc_suffix='_DOC'
  4112.   local nls_suffix='_NLS'
  4113.   if [ "${PKGEXT:-pet}" = "deb" ]; then
  4114.     dev_suffix='-dev'
  4115.     doc_suffix='-doc'
  4116.     nls_suffix='-nls'
  4117.   fi
  4118.  
  4119.   # get the sub-package names
  4120.   local EXE_PKG="$PKGNAME"
  4121.   local DEV_PKG="$(echo "$PKG_DIR_NAME" | sed -e "s/^${PKGNAME_ONLY}/${PKGNAME_ONLY}${dev_suffix}/")"
  4122.   local DOC_PKG="$(echo "$PKG_DIR_NAME" | sed -e "s/^${PKGNAME_ONLY}/${PKGNAME_ONLY}${doc_suffix}/")"
  4123.   local NLS_PKG="$(echo "$PKG_DIR_NAME" | sed -e "s/^${PKGNAME_ONLY}/${PKGNAME_ONLY}${nls_suffix}/")"
  4124.  
  4125.   # remove the target package dirs if they already exist
  4126.   [ -d "$DEV_PKG" ] && rm -rf "$DEV_PKG"
  4127.   [ -d "$DOC_PKG" ] && rm -rf "$DOC_PKG"
  4128.   [ -d "$NLS_PKG" ] && rm -rf "$NLS_PKG"
  4129.   # now create them fresh and empty
  4130.   mkdir -p "$DEV_PKG"
  4131.   mkdir -p "$DOC_PKG"
  4132.   mkdir -p "$NLS_PKG"
  4133.  
  4134.   # make sure the package directory begins with the given package name
  4135.   case $(basename "$PKG_PATH") in
  4136.     $PKG_NAME*)
  4137.         ;;
  4138.     *)
  4139.         echo "Error: $(basename "$PKG_PATH") must match $PKG_NAME"
  4140.         print_usage split
  4141.         exit 1
  4142.         ;;
  4143.   esac
  4144.  
  4145.   cd "$PKG_PATH" &>/dev/null || { echo "Not found: $PKG_PATH"; return 1; }
  4146.  
  4147.   for i in $(find -mindepth 1)
  4148.   do
  4149.     # if the file no longer exists, skip this iteration
  4150.     [ ! -e "$i" ] && continue
  4151.  
  4152.     local FILE_NAME=$(basename "$i")
  4153.  
  4154.     case "$FILE_NAME" in
  4155.         *.la|*.a|*.o|*.prl|pkgconfig|include|*.m4|*.h|*.c|*.cpp)
  4156.             PKGNAME="$DEV_PKG"
  4157.             ;;
  4158.         gdb)
  4159.             [ -d "$i" ] && PKGNAME="$DEV_PKG"
  4160.             ;;
  4161.         dir)
  4162.             [ -f "$i" ] && PKGNAME="$DOC_PKG"
  4163.             ;;
  4164.         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)
  4165.             PKGNAME="$DOC_PKG"
  4166.             ;;
  4167.         help)
  4168.             [ -d "$i" ] && PKGNAME="$DOC_PKG"
  4169.             ;;
  4170.         man|info) # if it's a directory named "man" or "info", move to doc
  4171.             [ -d "$i" ] && PKGNAME="$DOC_PKG"
  4172.              ;;
  4173.         locale|locales|lang|strings) # if it's a directory named "locale", move to nls
  4174.             [ -d "$i" ] && PKGNAME="$NLS_PKG"
  4175.             ;;
  4176.         i18n|nls)
  4177.             PKGNAME="$NLS_PKG"
  4178.             ;;
  4179.         system.profile-*|*.strings|normal.awt-*) # AbiWord stores its locale information in those files
  4180.             [ "$PKGNAME_ONLY" = "abiword" ] && PKGNAME="$NLS_PKG"
  4181.             ;;
  4182.         *)
  4183.             PKGNAME="$EXE_PKG"
  4184.             ;;
  4185.     esac
  4186.  
  4187.     # verbosity, output the redirection for each redirected file
  4188.     case "$PKGNAME" in
  4189.         $DEV_PKG|$DOC_PKG|$NLS_PKG)
  4190.             #local SUFFIX="$(echo $SUFFIX | tr [:lower:] [:upper:])"
  4191.  
  4192.             #echo "$FILE_NAME -> $PKG"
  4193.  
  4194.             # detect the sub_directory inside the package
  4195.             local SUB_DIR="${i%/$FILE_NAME}"
  4196.             SUB_DIR="${SUB_DIR:2}"
  4197.  
  4198.             # create the directory under the sub-package directory
  4199.             mkdir -p "$PARENT_DIR/$PKGNAME/$SUB_DIR"
  4200.  
  4201.             # move the file to the sub-package
  4202.             mv "$i" "$PARENT_DIR/$PKGNAME/$SUB_DIR"
  4203.  
  4204.             # get rid of empty directories in the EXE package
  4205.             rmdir "$SUB_DIR" &>/dev/null
  4206.             ;;
  4207.     esac
  4208.  
  4209.   done
  4210.  
  4211.   # if the EXE package is empty, remove it
  4212.   is_empty="$(find "$PKG_PATH" -maxdepth 2 -type f 2>/dev/null)"
  4213.   [ -z "$is_empty" ] && rm -rf "$PKG_PATH"
  4214.  
  4215.   # go back to where we started (where the main package lives)
  4216.   [ -d "$CURDIR" ] && cd "$CURDIR" &>/dev/null
  4217.  
  4218.   # build each package from the package dir
  4219.   for dir in "$EXE_PKG" "$DEV_PKG" "$DOC_PKG" "$NLS_PKG"
  4220.   do
  4221.     if [ -d "$dir" ]; then
  4222.       dir2${PKGEXT//./} "$dir" && rm -rf "$dir"
  4223.     fi
  4224.   done
  4225.  
  4226.   # get a list of the new packages we created
  4227.   new_pkgs="$(find . -maxdepth 1 -type f -name "${PKGNAME}*.$PKGEXT"
  4228.  find . -maxdepth 1 -type f -name "${DEV_PKG}*.$PKGEXT"
  4229.  find . -maxdepth 1 -type f -name "${DOC_PKG}*.$PKGEXT"
  4230.  find . -maxdepth 1 -type f -name "${NLS_PKG}*.$PKGEXT")"
  4231.  
  4232.   if [ "$new_pkgs" != "" ]; then
  4233.     echo
  4234.     echo -e "${green}Success${endcolour}: Package split into these new files:"
  4235.     echo "$new_pkgs" | sed 's/^.\// /'
  4236.   fi
  4237.  
  4238.   # cleanup
  4239.   [ -d "$DEV_PKG" ] && rm -rf "$DEV_PKG"
  4240.   [ -d "$DOC_PKG" ] && rm -rf "$DOC_PKG"
  4241.   [ -d "$NLS_PKG" ] && rm -rf "$NLS_PKG"
  4242.  
  4243.   return 0
  4244. }
  4245.  
  4246.  
  4247.  
  4248. # main pkg funcs
  4249.  
  4250. pkg_download(){                   # download a pkg ($1) to WORKDIR FUNCLIST
  4251.  
  4252.   # exit if no valid options
  4253.   [ ! "$1" -o "$1" = "-" ] && print_usage download && exit 1
  4254.  
  4255.   . ${PKGRC}
  4256.  
  4257.   local pkg_ext=''        # extension of pkg, empty if not a supported pkg ext
  4258.   local PKGNAME=''        # the pkgname with version
  4259.   local ORIG_PKGNAME=''   # keeps the original pkg name ($1)
  4260.   local PKG_FILENAME=''   # package file name (field 8 of repo, with extension)
  4261.   local PKGFILE=''        # full path to package file
  4262.   local NET=''            # 1 or 0 if net connection available
  4263.   local curr_repo=''      # name of current repo in the loop
  4264.   local curr_repo_ext=''  # pkg ext for the current repo in the loop
  4265.   local prev_repo_url=''  # holder for the prev checked url we know is working
  4266.   local curr_repo_url=''  # mirror that is used to download the pkg
  4267.   local curr_repo_url1='' # mirror1 for the current repo in the loop
  4268.   local curr_repo_url2='' # mirror2 for the current repo in the loop
  4269.   local curr_repo_url3='' # mirror3 for the current repo in the loop
  4270.   local curr_repo_url4='' # mirror4 for the current repo in the loop
  4271.   local pkg_in_repo=''    # true if pkg found in ANY repo, else false
  4272.  
  4273.   # get pkg extension
  4274.   pkg_ext=`get_pkg_ext "$1"`
  4275.  
  4276.   # get pkg name with version (if given!), no extension or path
  4277.   PKGNAME="$(basename "$1" .$pkg_ext)"
  4278.  
  4279.   # get full package name from given pkg name string .. vlc -> vlc-1.2.3-blah
  4280.   PKGNAME=`get_pkg_name "$PKGNAME"`
  4281.  
  4282.   # the file to save to.. not reliable till we checked the repos
  4283.   PKGFILE="${WORKDIR}/${PKGNAME}.$pkg_ext"
  4284.  
  4285.   # set download options
  4286.   [ "$FORCE" = true  ] && CONTINUE='' || CONTINUE='-c'
  4287.   [ "$ASK"  != true  ] && QTAG=''
  4288.  
  4289.   #if pkg not yet downloaded, or we are downloading all, or we are forcing downloads
  4290.   if [ ! -f "$PKGFILE" -o "$NO_INSTALL" = true -o "$FORCE" = true ]; then
  4291.  
  4292.     # mark this pkg as not yet downloaded
  4293.     DONE=false
  4294.  
  4295.     # exit if no internet connection
  4296.     #NET="`check_net`"; [ $NET -eq 1 ] && echo "You need an internet connection" && exit 2
  4297.  
  4298.     # for each repo, starting with current repo
  4299.     repo_file_list | while read repo_file
  4300.     do
  4301.  
  4302.       # the file with our repo URL info
  4303.       sources_file="${HOME}/.pkg/sources"
  4304.  
  4305.       # check if $repo_file contains the given pkg
  4306.       pkg_in_this_repo="$(LANG=C cut -f1,2,7,8 -d'|' "$REPO_DB_FILE_DIR/$repo_file" 2>/dev/null | sed -e "s/^/|/" -e "s/$/|/" | grep -m1 "|${PKGNAME}|")"
  4307.       [ "$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 | sed -e "s/^/|/" -e "s/$/|/" | grep -m1 "|${PKGNAME}")"
  4308.  
  4309.       # if package file found in current repo file
  4310.       if [ "$pkg_in_this_repo" != "" ]; then #if true, its an exact match
  4311.  
  4312.         # get name of current repo in loop
  4313.         prev_repo="$curr_repo"
  4314.         curr_repo="`LANG=C grep $repo_file $sources_file 2>/dev/null | cut -f1 -d'|'`"
  4315.  
  4316.         # get ext of cur repo, it might be different
  4317.         curr_repo_ext="`LANG=C grep $repo_file $sources_file 2>/dev/null| cut -f2 -d'|'`"
  4318.  
  4319.         # keep a copy of the PKGNAME we got from `get_pkg_name $1`
  4320.         ORIG_PKGNAME=$PKGNAME
  4321.  
  4322.         # get proper PKGNAME (inc name-ver) from the repo
  4323.         PKGNAME="`echo "$pkg_in_this_repo" | cut -f3 -d'|'`"
  4324.         # just in case it didn't work, revert back to the old PKGNAME value
  4325.         [ "$PKGNAME" = "" ] && PKGNAME="$ORIG_PKGNAME"
  4326.  
  4327.         # get full pkg FILENAME (inc name-ver.ext) from the repo
  4328.         PKG_FILENAME="`echo "$pkg_in_this_repo" | cut -f5 -d'|'`"
  4329.  
  4330.         # just in case its empty, revert back to the earlier PKGNAME value
  4331.         [ "$PKG_FILENAME" = "" ] && PKG_FILENAME="$PKGNAME"
  4332.  
  4333.         # update the filename to check/download
  4334.         PKGFILE="${WORKDIR}/${PKG_FILENAME}"
  4335.  
  4336.         # skip if downloaded already, print msg
  4337.         [ -f "$PKGFILE" -a "$FORCE" = false ] && DONE=true && echo -e "Already downloaded ${magenta}${PKG_FILENAME}${endcolour}" && continue
  4338.  
  4339.         # update the package name, based on PKG_FILENAME
  4340.         PKGNAME="$(basename "$PKG_FILENAME" .$curr_repo_ext)"
  4341.  
  4342.         # update generic name
  4343.         PKGNAME_ONLY="`get_pkg_name_only "$PKGNAME"`"
  4344.  
  4345.         if [ "${PKG_FILENAME}" = '' ]; then
  4346.           error "Cant find pkg file name, needed to download"
  4347.           return 1
  4348.         fi
  4349.  
  4350.         # skip pings and download if already downloaded.. unless forcing download
  4351.         if [ ! -f "$PKGFILE" -o "$FORCE" = true ]; then
  4352.  
  4353.           # ask user to download
  4354.           echo -en "Download ${magenta}${PKGNAME_ONLY}${endcolour} from ${lightblue}${curr_repo}${endcolour} repo$QTAG:  "
  4355.           [ "$ASK" = true ] && read -n 1 CONFIRM </dev/tty || CONFIRM=y
  4356.           [ "$CONFIRM" != 'y' ] && echo
  4357.  
  4358.           # if user answered yes, we will now download the pkgs
  4359.           if [ "$CONFIRM" = "y" ]; then #25073
  4360.  
  4361.             # only ask once
  4362.             ASK=false
  4363.  
  4364.             echo # start a new line
  4365.  
  4366.             # get the subdir (from repo line) that the package lives in
  4367.             sub_dir="`echo "$pkg_in_this_repo" | cut -f4 -d'|' | sed 's/^\.//'`"
  4368.  
  4369.             if [ "$sub_dir" = "." ]; then
  4370.               sub_dir=''
  4371.             fi
  4372.  
  4373.             #s243a: TODO: figure out what this file is for???
  4374.             # pre-woof get subdir
  4375.             if [ "$sub_dir" != "" ] && [ -f "$PKGS_DIR/${repo_file}_subdirs" ]; then
  4376.               sub_dir="`grep -m1 "^$PKGNAME|" "$PKGS_DIR/${repo_file}_subdirs"  2>/dev/null | cut -f7 -d'|'`"
  4377.             fi
  4378.             #s243a: TODO: maybe if the folowing produces a trailing double slash than remove it.
  4379.             # get repo mirrors (only url1 is required .. we also add a final /)
  4380.             curr_repo_url1="`LANG=C grep $repo_file $sources_file 2>/dev/null| cut -f4 -d'|'`/"
  4381.             curr_repo_url2="`LANG=C grep $repo_file $sources_file 2>/dev/null| cut -f5 -d'|'`/"
  4382.             curr_repo_url3="`LANG=C grep $repo_file $sources_file 2>/dev/null| cut -f6 -d'|'`/"
  4383.             curr_repo_url4="`LANG=C grep $repo_file $sources_file 2>/dev/null| cut -f7 -d'|'`/"
  4384.  
  4385.             # make a space separated list of all our repo URLs
  4386.             curr_repo_url_list="$curr_repo_url1 $curr_repo_url2 $curr_repo_url3 $curr_repo_url4"
  4387.  
  4388.             # clean up the list, remove any double slashes at the end of each URL
  4389.             curr_repo_url_list="`echo "$curr_repo_url_list" | sed -e "s|// |/ |g" -e "s|//\$|/\$|g"`"
  4390.  
  4391.             # update the ext to that of current repo
  4392.             pkg_ext="$curr_repo_ext"
  4393.  
  4394.             # get the best repo mirror
  4395.             #if [ "$prev_repo" != "$current_repo"  -a -f $TMPDIR/curr_repo_url ] || [ ! -f $TMPDIR/curr_repo_url ]; then
  4396.               #echo "Checking '$curr_repo' repo mirrors..."
  4397.               for URL in $curr_repo_url_list
  4398.               do
  4399.                 [ -z "$URL" ] && continue
  4400.                 [ "$URL" = '/' ] && continue
  4401.                 [ "$URL" = '' ] && continue
  4402.                 #s243a:TODO: The following additions to the URL doesn't seem to do anything
  4403.                 # add some system values into the repo URL (arch, distro version, etc.. from DISTRO_SPECS)
  4404.                 URL="`echo "$URL"| sed -e 's@${DBIN_ARCH}@'$DBIN_ARCH'@g'`"
  4405.                 URL="`echo "$URL"| sed -e 's@${DISTRO_COMPAT_VERSION}@'$DISTRO_COMPAT_VERSION'@g'`"
  4406.                 URL="`echo "$URL"| sed -e 's@${DDB_COMP}@'$DDB_COMP'@g'`"
  4407.                 URL="`echo "$URL"| sed -e 's@${SLACKWARE}@'$SLACKWARE'@g'`"
  4408.                 URL="`echo "$URL"| sed -e 's@${SLACKARCH}@'$SLACKARCH'@g'`"
  4409.                 URL="`echo "$URL"| sed -e 's@${lxDISTRO_COMPAT_VERSION}@'$lxDISTRO_COMPAT_VERSION'@g'`"
  4410.                 #ping -W2 -c1 -q $(echo  "${URL}" | awk -F/ '{print $3}') &>/dev/null
  4411.                 wget --quiet --timeout=1 --no-parent --spider "${URL}" && \
  4412.                 {
  4413.                   # set the current URL
  4414.                   curr_repo_url="$URL"
  4415.                   if [ ! -z "$curr_repo_url" ]; then
  4416.                     echo "$curr_repo_url" > $TMPDIR/curr_repo_url
  4417.                     break
  4418.                   fi
  4419.                 }
  4420.               done
  4421.             #else
  4422.             # curr_repo_url="`cat $TMPDIR/curr_repo_url`"
  4423.             #fi
  4424.  
  4425.             # exit if URL is not found or empty
  4426.             if [ -z "$curr_repo_url" ]; then
  4427.               error "Package URL not found"
  4428.               return 8
  4429.             fi
  4430.  
  4431.             # lets build our DOWNLOAD_URL: set it to the working repo mirror we found above
  4432.             DOWNLOAD_URL="${curr_repo_url}"
  4433.  
  4434.             # now add the subdir to DOWNLOAD_URL .. sub_dir may be empty, but we add a trailing '/' anyway
  4435.             DOWNLOAD_URL="${DOWNLOAD_URL}${sub_dir}/"
  4436.  
  4437.             # add some system values into the repo URL (arch, distro version, etc.. from DISTRO_SPECS)
  4438.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e 's@${DBIN_ARCH}@'$DBIN_ARCH'@g'`"
  4439.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e 's@${DISTRO_COMPAT_VERSION}@'$DISTRO_COMPAT_VERSION'@g'`"
  4440.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e 's@${DDB_COMP}@'$DDB_COMP'@g'`"
  4441.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e 's@${SLACKWARE}@'$SLACKWARE'@g'`"
  4442.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e 's@${SLACKARCH}@'$SLACKARCH'@g'`"
  4443.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e 's@${lxDISTRO_COMPAT_VERSION}@'$lxDISTRO_COMPAT_VERSION'@g'`"
  4444.  
  4445.             # add our package to the URL
  4446.             DOWNLOAD_URL="${DOWNLOAD_URL}${PKG_FILENAME}"
  4447.  
  4448.             # 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)
  4449.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e "s@//$PKG_FILENAME@/$PKG_FILENAME@g"`"
  4450.  
  4451.             # if sub_dir not empty, lets clean that bit too
  4452.             [ "$sub_dir" != '' ] && DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e "s@//${sub_dir}@/${sub_dir}@g" -e "s@${sub_dir}//@${sub_dir}/@g"`"
  4453.  
  4454.             # exit if URL is not found (if we get a 404 back)
  4455.             if [ -z "$DOWNLOAD_URL" ]; then
  4456.               error "Package URL not found   $DOWNLOAD_URL"
  4457.               return 8
  4458.             else
  4459.               wget -S --spider "$DOWNLOAD_URL" || { \
  4460.                 error "Package URL not found   $DOWNLOAD_URL"
  4461.                 return 8                 
  4462.               }
  4463.             fi
  4464.  
  4465.             # we may be using multiple URLs, so each time we change URL,
  4466.             #  remember the new one
  4467.             if [ "$OLDURL" != "$DOWNLOAD_URL" ]; then
  4468.               OLDURL="$DOWNLOAD_URL";
  4469.               #echo -e "URL: ${lightblue}${DOWNLOAD_URL}${endcolour}";
  4470.             fi
  4471.  
  4472.             # if --force, remove the package if it already exists
  4473.             [ "$FORCE" = true ] && rm -f "${WORKDIR}/${PKG_FILENAME}" &>/dev/null
  4474.  
  4475.             # if file not downloaded, or forcing downloads
  4476.             if [ ! -f "${WORKDIR}/${PKG_FILENAME}" -o "$FORCE" = true ]; then
  4477.  
  4478.               # BEGIN DOWNLOAD file here..
  4479.  
  4480.               # print DOWNLOADING msg
  4481.               echo -en "Downloading ${magenta}${PKG_FILENAME}${endcolour}. Please wait:     "
  4482.  
  4483.               # if called as 'gpkg', give a pop GUI (uses Xdialog) showing download progress..
  4484.               # can be used by X apps to easily start (and show!) downloads
  4485.               if [ "$SELF" = "gpkg" ]; then
  4486.  
  4487.                 download_progress "$DOWNLOAD_URL" "${WORKDIR}/${PKG_FILENAME}" 2>/dev/null
  4488.  
  4489.               else # if called as 'pkg', dont use Xdialog, output to terminal
  4490.  
  4491.                 if [ "$QUIET" = true ]; then
  4492.                   echo
  4493.                   echo -n "Downloading now..."
  4494.                   LANG=C wget \
  4495.                     --no-check-certificate \
  4496.                     --progress=dot -O "${WORKDIR}/${PKG_FILENAME}" \
  4497.                     -4 $CONTINUE "$DOWNLOAD_URL" &>/dev/null
  4498.                 else
  4499.                   # START DOWNLOAD, show percentage as we go
  4500.                   LANG=C wget \
  4501.                     --no-check-certificate \
  4502.                     --progress=dot -O "${WORKDIR}/${PKG_FILENAME}" \
  4503.                     -4 $CONTINUE "$DOWNLOAD_URL" 2>&1 \
  4504.                     | grep --line-buffered "%" \
  4505.                     | sed -u -e "s#\.##g" \
  4506.                     | awk '{printf("\b\b\b\b%4s", $2)}' #220613
  4507.                 fi
  4508.  
  4509.               fi
  4510.  
  4511.             fi # end if WORKDIR/PKG not a file or FORCE=true
  4512.  
  4513.             # clean up output
  4514.             [ "$QUIET" != true ] && echo -ne "\b\b\b\b"
  4515.             echo
  4516.  
  4517.             # if file downloaded ok
  4518.             if [ -f "${WORKDIR}/${PKG_FILENAME}" ]; then
  4519.               echo -e "${green}Downloaded:${endcolour} ${WORKDIR}/${PKG_FILENAME}"
  4520.               DONE=true
  4521.               break
  4522.  
  4523.             else # file NOT downloaded ok
  4524.  
  4525.               error "Failed to download '${WORKDIR}/${PKG_FILENAME}'."
  4526.               echo "Check '$DOWNLOAD_URL'"
  4527.  
  4528.               # remove the page we tried to get pkg from (if exists)
  4529.               rm "${WORKDIR}/index.html" &>/dev/null
  4530.               DONE=true
  4531.               exit 6
  4532.             fi
  4533.  
  4534.           fi # end if CONFIRM=y
  4535.  
  4536.           # user chose not to do anything
  4537.           DONE=true
  4538.           break
  4539.  
  4540.         else # Already downloaded, skip to next iteration
  4541.  
  4542.           #echo "Package $1 already downloaded"
  4543.           DONE=true
  4544.           break
  4545.         fi
  4546.  
  4547.       else # no repo match, nothing to download
  4548.  
  4549.         # go to next repo, try to ge tthe pkg there
  4550.         DONE=false
  4551.  
  4552.       fi # end if repo match was found
  4553.  
  4554.       # if download not successful, keep going to next repo
  4555.       [ "$DONE" = true ] && continue
  4556.  
  4557.     done #done while read list of repo files
  4558.  
  4559.   fi
  4560. }
  4561.  
  4562.  
  4563. pkg_install(){                    # install downloaded package ($1) FUNCLIST
  4564.  
  4565.   . ${PKGRC}
  4566.  
  4567.   # exit if no valid option
  4568.   [ ! "$1" -o "$1" = "-" ] && print_usage install && exit 1
  4569.  
  4570.   # skip if not installing pkgs
  4571.   [ "$NO_INSTALL" = true ] && continue
  4572.  
  4573.   local pkg_ext
  4574.   local PKGNAME
  4575.   local PKGNAME_ONLY
  4576.   local PKGFILE
  4577.   local PREVDIR="$CURDIR"
  4578.   local petspecs
  4579.  
  4580.   # get pkg extension
  4581.   pkg_ext=`get_pkg_ext "$1"`
  4582.   # get pkg name only, no extension or path
  4583.   PKGNAME="$(basename "$1" .$pkg_ext)"
  4584.   # exit if no valid option
  4585.   [ "$PKGNAME" = '' ] && print_usage install && exit 1
  4586.   # get name without version
  4587.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  4588.  
  4589.   [ "`is_installed_pkg "$PKGNAME"`" = true -a "$FORCE" = false ] && echo "Already installed: $PKGNAME" && return 1
  4590.   [ "`is_blacklisted_pkg "$PKGNAME_ONLY"`" = true ] && echo "Blacklisted package: $PKGNAME" && return 1
  4591.  
  4592.   if [ -f "$1" ]; then
  4593.     PKGFILE="$1"
  4594.     CURDIR="$(dirname "$(realpath "$1")")"
  4595.    PKGNAME=`basename "$1" .$pkg_ext`
  4596.  else
  4597.    # get the real file name (inc path) from the given PKGFILE and pkg_ext (which may be empty)
  4598.    PKGFILE=`find "$CURDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*${pkg_ext}" | head -1`
  4599.    [ ! -f "$PKGFILE" ] && PKGFILE=`find "$CURDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*${EX}" | grep -m1 $EX`
  4600.    [ -f "$PKGFILE" ] && PKGNAME=`basename "$1" .$pkg_ext`
  4601.  fi
  4602.  
  4603.  # maybe the file is not in the current dir, but in WORKDIR, so lets look there too
  4604.  if [ ! -f "$PKGFILE" ]; then
  4605.    PKGFILE=`find "$WORKDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*${pkg_ext}" | head -1`
  4606.    [ ! -f "$PKGFILE" ] && PKGFILE=`find "$WORKDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*${EX}" | grep -m1 $EX`
  4607.  
  4608.    # if we found the file in WORKDIR, make that our CURDIR
  4609.    if [ -f "$PKGFILE" ]; then
  4610.      PKGNAME=`basename "$PKGFILE" .$pkg_ext`
  4611.      CURDIR="$WORKDIR"
  4612.      cd "$WORKDIR"
  4613.    else
  4614.      # if we still didn't find it, do a broader check
  4615.      PKGFILE=`find "$WORKDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*" | head -1`
  4616.      if [ -f "$PKGFILE" ]; then
  4617.        PKGNAME=`basename "$PKGFILE" .$pkg_ext`
  4618.        CURDIR="$WORKDIR"
  4619.        cd "$WORKDIR"
  4620.      fi
  4621.    fi
  4622.  fi
  4623.  
  4624.  # if the file exists, or using --force
  4625.  if [ -f "$PKGFILE" -o "$FORCE" = true ]; then
  4626.  
  4627.    [ ! -f "$PKGFILE" ] && echo "The file $1 was not found." && return 1
  4628.  
  4629.    # get the extension of this newly found pkg (which we should have found by now)
  4630.    pkg_ext=`get_pkg_ext "$PKGFILE"`
  4631.    # get extension again, we may have been given only a pkg name, find its extension from repo files
  4632.    [ "$pkg_ext" = "" ] && pkg_ext=`get_pkg_ext "$PKGNAME"`
  4633.  
  4634.    [ "$pkg_ext" = '' ] && echo "Not installing $PKGFILE." && error "Invalid file extension ($pkg_ext)." && return 1
  4635.  
  4636.    # remove any previous PET/pkg stuff lying around from previous installs
  4637.    rm -f /pet.specs /pinstall.sh /puninstall.sh /install/doinst.sh
  4638.  
  4639.    # if pkg is not yet installed, or we are force re-installing
  4640.  
  4641.    # get filename from download file
  4642.    PKGNAME=`basename "$PKGFILE" .$pkg_ext`
  4643.  
  4644.    # ask/inform user before install
  4645.    echo -n "Install package ${PKGNAME}$QTAG:  "
  4646.    [ "$ASK" = true ] && read -n 1 CONFIRM </dev/tty || CONFIRM=y
  4647.    [ "$ASK" = true ] && echo -ne "\b\b\n"
  4648.  
  4649.    # if user answered yes, we will now download the pkgs
  4650.    if [ "$CONFIRM" = "y" ]; then
  4651.  
  4652.      # print new line if we didnt take any user input on tty
  4653.      [ "$ASK" != true ] && echo
  4654.  
  4655.      # only ask once
  4656.      ASK=false
  4657.  
  4658.      #if [ "`pkg_contents "$PKGFILE" 2>/dev/null`" = '' ]; then
  4659.      # error "$PKGFILE not a valid package."
  4660.      # exit 1
  4661.      #fi
  4662.  
  4663.      # fallback to repo pkg ext if none found
  4664.      [ "$pkg_ext" = "" ] && pkg_ext="$EX"
  4665.  
  4666.      #remove the old error log
  4667.      rm $TMPDIR/$SELF-cp-errlog 2>/dev/null
  4668.  
  4669.      # extract the archive file into a $CURDIR/$PKGNAME folder
  4670.      case "$pkg_ext" in
  4671.  
  4672.        pet)
  4673.          PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.pet"`"
  4674.          #determine the compression, extend test to 'XZ'
  4675.          file -b "${PKGFILE}" | grep -i -q "^xz" && TAREXT=xz || TAREXT=gz
  4676.          [ "$TAREXT" = 'xz' ] && taropts='-xJf' || taropts='-zxf'
  4677.  
  4678.          # convert to tar extractable archive
  4679.          pet2tgz "${PKGFILE}" 1>/dev/null
  4680.  
  4681.          # now extract the file
  4682.          tar $taropts "${CURDIR}/${PKGNAME}.tar.${TAREXT}" 2> $TMPDIR/$SELF-cp-errlog
  4683.  
  4684.          # some old types need this extra step
  4685.          if [ -f "${CURDIR}/${PKGNAME}.tar.$TAREXT" ]; then
  4686.            cd "${CURDIR}/${PKGNAME}/" 1>/dev/null
  4687.            tar --absolute-names $tarops "./${PKGNAME}.tar.$TAREXT" ${DIRECTSAVEPATH}/ 2> $TMPDIR/$SELF-cp-errlog #260713
  4688.            sync
  4689.            rm -f  "./${PKGNAME}.tar.$TAREXT" 1>/dev/null
  4690.            cd - 1>/dev/null
  4691.          fi
  4692.  
  4693.          # save the uninstall script for later
  4694.          if [ -f "${CURDIR}/${PKGNAME}/puninstall.sh" ]; then
  4695.            mv "${CURDIR}/${PKGNAME}/puninstall.sh" "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.remove"
  4696.          fi
  4697.  
  4698.          # create a tgz
  4699.          tgz2pet "${CURDIR}/${PKGNAME}.tar.$TAREXT" 1>/dev/null
  4700.          rm "${CURDIR}/${PKGNAME}.tar.$TAREXT" 2>/dev/null
  4701.        ;;
  4702.  
  4703.        sfs)
  4704.          PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.sfs"`"
  4705.          [ ! -f "$PKGFILE" ] && PKGFILE="`find ${WORKDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.sfs"`"
  4706.          echo sfs_loadr -q --cli +"$PKGFILE" #2>/dev/null
  4707.          sfs_loadr -q --cli +"$PKGFILE" #2>/dev/null
  4708.          rm -f $PACKAGE_FILE_LIST_DIR/${PKGNAME}.files 2>/dev/null
  4709.          # create $PKGS_DIR/$PKGNAME.files
  4710.          pkg_contents "$PKGFILE" | while read line
  4711.          do
  4712.            [ -f "$line" ] && echo "$line" >> $PACKAGE_FILE_LIST_DIR/${PKGNAME}.files
  4713.          done
  4714.          # exit, we dont need to unpack and copy
  4715.          return 0
  4716.        ;;
  4717.  
  4718.        deb)
  4719.  
  4720.          rmdir "${CURDIR}/${PKGNAME}" &>/dev/null
  4721.          mkdir -p "${CURDIR}/${PKGNAME}"
  4722.          [ ! -f "$PKGFILE" ] && PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.deb"`"
  4723.          cp "${PKGFILE}" "${CURDIR}/${PKGNAME}/${PKGNAME}.deb" || exit 4
  4724.          cd "${CURDIR}/${PKGNAME}/"
  4725.  
  4726.          if [ -f "${CURDIR}/${PKGNAME}/${PKGNAME}.deb" ]; then
  4727.            dpkg-deb --contents "${CURDIR}/${PKGNAME}/${PKGNAME}.deb" \
  4728.              | grep -v '/$' \
  4729.              | tr -s ' ' \
  4730.              | cut -f6 -d' ' \
  4731.              | sed -e 's/^.//g' 2>/dev/null \
  4732.              | grep -v '^$' > "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  4733.          fi
  4734.  
  4735.                     # woofce: NO_MULTIARCH_SYMLINK=1 (DISTRO_SPECS)
  4736.                     if [ -z "$NO_MULTIARCH_SYMLINK" ] ; then
  4737.            if [ -f "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" ]; then
  4738.              pkg_has_archdir="$(grep -m1 "$DISTRO_ARCHDIR" "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files")"
  4739.            fi
  4740.  
  4741.                         # Workaround to avoid overwriting the $DISTRO_ARCHDIR symlink.
  4742.                         if [ "$DISTRO_ARCHDIR" != "" -a -f "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" -a "$pkg_has_archdir" != "" ]; then
  4743.                           mkdir -p /tmp/$PKGNAME
  4744.                           rm -rf /tmp/$PKGNAME/*
  4745.                           dpkg-deb -x ${CURDIR}/${PKGNAME}.deb /tmp/$PKGNAME/ 2> $TMPDIR/$SELF-cp-errlog
  4746.                           #set -x
  4747.                           while read f
  4748.                           do
  4749.                               xpath="$(echo "$f" |  cut  -f 4-30 -d "/" | sed "s/$DISTRO_ARCHDIR\///")"
  4750.                               mkdir -p ${DIRECTSAVEPATH}/$(dirname "$xpath")
  4751.                               cp -a "$f" ${DIRECTSAVEPATH}/$(dirname "$xpath")/
  4752.                           done < <(find "/tmp/$PKGNAME" \( -type f -o -type l \))
  4753.                           #set +x
  4754.                           rm -rf /tmp/$PKGNAME &>/dev/null
  4755.                         else
  4756.                           dpkg-deb -x ${CURDIR}/${PKGNAME}.deb ${DIRECTSAVEPATH}/ 2> $TMPDIR/$SELF-cp-errlog
  4757.                         fi
  4758.                     else
  4759.                         dpkg-deb -x ${CURDIR}/${PKGNAME}.deb ${DIRECTSAVEPATH}/ 2> $TMPDIR/$SELF-cp-errlog
  4760.                     fi
  4761.  
  4762.          if [ "`cat $TMPDIR/$SELF-cp-errlog | grep 'tar: Exiting with failure status due to previous errors'`" != "" ]; then
  4763.            error "Failed to unpack $PKGNAME.deb"
  4764.            exit 1
  4765.          fi
  4766.          [ -d /DEBIAN ] && rm -rf /DEBIAN #130112 precaution.
  4767.          dpkg-deb -e "${CURDIR}/${PKGNAME}.deb" /DEBIAN #130112 extracts deb control files to dir /DEBIAN. may have a post-install script, see below.
  4768.          rm "${PKGNAME}.deb"
  4769.  
  4770.          cd - 1>/dev/null
  4771.        ;;
  4772.  
  4773.        *tbz|*tar.bz2)
  4774.          rmdir "${CURDIR}/${PKGNAME}" &>/dev/null
  4775.          mkdir -p "${CURDIR}/${PKGNAME}"
  4776.          cp "${CURDIR}/${PKGNAME}.$pkg_ext" "${CURDIR}/${PKGNAME}/${PKGNAME}.$pkg_ext" || exit 4
  4777.          cd "${CURDIR}/${PKGNAME}/"
  4778.  
  4779.          ( umask 000 ; cat "${PKGNAME}.$pkg_ext" | bzip2 -dc | tar -xf - 2> $TMPDIR/$SELF-cp-errlog )
  4780.  
  4781.          rm "${PKGNAME}.$pkg_ext"
  4782.          cd - 1>/dev/null
  4783.        ;;
  4784.  
  4785.        *tlz|*tar.lzma)
  4786.          rmdir "${CURDIR}/${PKGNAME}" &>/dev/null
  4787.          mkdir -p "${CURDIR}/${PKGNAME}"
  4788.          PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.${pkg_ext}"`"
  4789.          cp "$PKGFILE" "${CURDIR}/${PKGNAME}/`basename $PKGFILE`" || exit 4
  4790.          cd "${CURDIR}/${PKGNAME}/"
  4791.  
  4792.          ( umask 000 ; cat "${PKGNAME}.$pkg_ext" | lzma -dc | tar -xf - 2> $TMPDIR/$SELF-cp-errlog )
  4793.  
  4794.          rm "${PKGNAME}.$pkg_ext"
  4795.          cd - 1>/dev/null
  4796.        ;;
  4797.  
  4798.        *tgz|*txz|*tar.gz|*tar.xz|*xz|*gz)
  4799.          rmdir "${CURDIR}/${PKGNAME}" &>/dev/null
  4800.          mkdir -p "${CURDIR}/${PKGNAME}"
  4801.          PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.${pkg_ext}"`"
  4802.          cp "$PKGFILE" "${CURDIR}/${PKGNAME}/`basename $PKGFILE`" || exit 4
  4803.          cd "${CURDIR}/${PKGNAME}/"
  4804.  
  4805.          file -b "${PKGNAME}.$pkg_ext" | grep -i -q "^xz" && TAREXT=xz || TAREXT=gz
  4806.          [ "$TAREXT" = 'xz' ] && taropts='-xJf' || taropts='-zxf'
  4807.  
  4808.          tar $taropts "${PKGNAME}.$pkg_ext" 2> $TMPDIR/$SELF-cp-errlog
  4809.          rm "${PKGNAME}.$pkg_ext"
  4810.          cd - 1>/dev/null
  4811.        ;;
  4812.  
  4813.        rpm)
  4814.          PKGNAME="`basename ${PKGFILE} .rpm`"
  4815.          busybox rpm -qp "$PKGFILE" > /dev/null 2>&1
  4816.          [ $? -ne 0 ] && exit 1
  4817.          PFILES="`busybox rpm -qpl $PKGFILE`"
  4818.          [ $? -ne 0 ] && exit 1
  4819.          echo "$PFILES" > "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  4820.          pkg_unpack "$PKGFILE" 1>/dev/null
  4821.        ;;
  4822.  
  4823.      esac
  4824.  
  4825.      # now extract pkg contents to /
  4826.      [ ! -d "${PKGNAME}/" ] && error "Cannot enter directory '${PKGNAME}/', it doesn't exist." && exit 4
  4827.      cd "${PKGNAME}/" 1>/dev/null
  4828.  
  4829.      cp -a --remove-destination * ${DIRECTSAVEPATH}/ 2> $TMPDIR/$SELF-cp-errlog
  4830.  
  4831.      #source is a directory, target is a symlink...
  4832.      if [ -s $TMPDIR/$SELF-cp-errlog ]; then
  4833.        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 '`' |
  4834.        while read ONEDIRSYMLINK
  4835.        do
  4836.          #adding that extra trailing / does the trick...
  4837.          cp -a --remove-destination ${ONEDIRSYMLINK}/* /${ONEDIRSYMLINK}/ 2> $TMPDIR/$SELF-cp-errlog #260713
  4838.        done
  4839.      fi
  4840.      sync
  4841.      cd .. 1>/dev/null
  4842.  
  4843.      if [ ! -f "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" ] || \
  4844.         [ -z "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" ]; then
  4845.        # add ${HOME}/.packages/${PKGNAME}.files, need to find regular files and links separately... then images..
  4846.        find "./${PKGNAME}/" -mount -mindepth 2 -type f | sed -e "s/\.\/${PKGNAME}//g" -e 's/\/root0\//\/root\//g' > "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  4847.        find "./${PKGNAME}/" -mount -mindepth 2 -type l | sed -e "s/\.\/${PKGNAME}//g" -e 's/\/root0\//\/root\//g' >> "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  4848.  
  4849.        for ONEIMAGEFILE in `ls -1 /*[0-9].xpm 2>/dev/null`
  4850.        do
  4851.         BASEONEIMAGE="`basename $ONEIMAGEFILE`"
  4852.         echo "/usr/local/lib/X11/pixmaps/$BASEONEIMAGE" >> "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  4853.        done
  4854.        for ONEIMAGEFILE in `ls -1 /*[0-9].png 2>/dev/null`
  4855.        do
  4856.         BASEONEIMAGE="`basename $ONEIMAGEFILE`"
  4857.         echo "/usr/local/lib/X11/pixmaps/$BASEONEIMAGE" >> "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  4858.        done
  4859.        for ONEIMAGEFILE in `ls -1 /*[^0-9].xpm 2>/dev/null`
  4860.        do
  4861.         BASEONEIMAGE="`basename $ONEIMAGEFILE`"
  4862.         echo "/usr/local/lib/X11/mini-icons/$BASEONEIMAGE" >> "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  4863.        done
  4864.        for ONEIMAGEFILE in `ls -1 /*[^0-9].png 2>/dev/null` #v2.16
  4865.        do
  4866.         BASEONEIMAGE="`basename $ONEIMAGEFILE`"
  4867.         echo "/usr/local/lib/X11/mini-icons/$BASEONEIMAGE" >> "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  4868.        done
  4869.      fi
  4870.  
  4871.      # move the *.files to $PKGS_DIR
  4872.      FILES=''
  4873.      FILES="`find "$CURDIR" -mount -maxdepth 1 -iname "*.files" | head -1`"
  4874.      [ -f "`echo $FILES`" ] && mv "$FILES" "$PACKAGE_FILE_LIST_DIR/" 2>/dev/null #260713
  4875.  
  4876.      #pkgname.files may need to be fixed...
  4877.      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"
  4878.      mv "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files2" "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  4879.  
  4880.      sort -u "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" > "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files2"
  4881.      mv "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files2" "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files"
  4882.  
  4883.      # some pets add images and icons at / so move them
  4884.      mv /{*.xpm,*.png} /usr/share/pixmaps/ 2>/dev/null
  4885.      mv /*.ico /usr/share/pixmaps/ 2>/dev/null
  4886.  
  4887.      # run the post install scripts (for puppy, slackware, arch, debian/ubuntu, etc)
  4888.      for i in /pinstall.sh /install/doinst.sh /DEBIAN/postinst
  4889.      do
  4890.        [ -z /${i} -o ! -e /${i} ] && continue
  4891.        chmod +x /${i}
  4892.        cd /
  4893.        nohup sh ${i} &>/dev/null
  4894.        sleep 0.2
  4895.        rm -f ${i}
  4896.        cd ${CURDIR}/
  4897.      done
  4898.      rm -rf /install
  4899.      rm -rf /DEBIAN
  4900.  
  4901.      #130314 run arch linux pkg post-install script...
  4902.      if [ -f /.INSTALL -a /usr/local/petget/ArchRunDotInstalls ]; then #precaution. see 3builddistro, script created by noryb009.
  4903.        #this code is taken from below...
  4904.        dlPATTERN='|'"`echo -n "${PKGNAME}" | sed -e 's%\\-%\\\\-%'`"'|'
  4905.        archVER="`cat $TMPDIR/petget_missing_dbentries-Packages-* 2>/dev/null | grep "$dlPATTERN" | head -n 1 | cut -f 3 -d '|'`"
  4906.        if [ "$archVER" ]; then #precaution.
  4907.          cd /
  4908.          mv -f .INSTALL .INSTALL1-${archVER}
  4909.          cp -a /usr/local/petget/ArchRunDotInstalls ArchRunDotInstalls
  4910.          LANG=$LANG_USER ./ArchRunDotInstalls
  4911.          rm -f ArchRunDotInstalls
  4912.          rm -f .INSTALL*
  4913.          cd ${CURDIR}/
  4914.        fi
  4915.      fi
  4916.  
  4917.      PKGCAT=''
  4918.      PKGSIZE=''
  4919.      PKGDESC=''
  4920.      DEPLIST=''
  4921.  
  4922.      # clean up pet installs
  4923.      if [ "$pkg_ext" = "pet" ]; then
  4924.        cd /
  4925.  
  4926.        # get pkg info from pet.spec before we delete it, because the PET may not be a repo pkg
  4927.        petspecs="`cat /pet.specs 2>/dev/null`"
  4928.  
  4929.        if [ "$petspecs" != '' ]; then
  4930.          # now get pkg info
  4931.          PKGCAT="`echo $petspecs|  cut -f5  -d'|'`"
  4932.          PKGSIZE="`echo $petspecs| cut -f6  -d'|'`"
  4933.          PKGDESC="`echo $petspecs| cut -f10 -d'|'`"
  4934.          DEPSLIST="`echo $petspecs|cut -f9  -d'|'`"
  4935.        fi
  4936.  
  4937.        cd ${CURDIR}/
  4938.      fi
  4939.  
  4940.      # cleanup a bit
  4941.      rm -R "${PKGNAME}/" 2>/dev/null || echo "${yellow}Warning:${endcolour} Cannot remove directory '${PKGNAME}/'." #150213
  4942.  
  4943.      #120102 install may have overwritten a symlink-to-dir...
  4944.      #tar defaults to not following symlinks, for both dirs and files, but i want to follow symlinks
  4945.      #for dirs but not for files. so, fix here... (note, dir entries in .files have / on end)
  4946.      cat "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" | grep '[a-zA-Z0-9]/$' | sed -e 's%/$%%' | grep -v '^/mnt' |
  4947.      while read ONESPEC
  4948.      do
  4949.       if [ -d "${DIRECTSAVEPATH}${ONESPEC}" ]; then
  4950.        if [ ! -h "${DIRECTSAVEPATH}${ONESPEC}" ]; then
  4951.         DIRLINK=""
  4952.         if [ -h "/initrd${PUP_LAYER}${ONESPEC}" ]; then #120107
  4953.          DIRLINK="`readlink -m "/initrd${PUP_LAYER}${ONESPEC}" | sed -e "s%/initrd${PUP_LAYER}%%"`" #PUP_LAYER: see /etc/rc.d/PUPSTATE. 120107
  4954.          xDIRLINK="`readlink "/initrd${PUP_LAYER}${ONESPEC}"`" #120107
  4955.         fi
  4956.         if [ ! "$DIRLINK" ]; then
  4957.          if [ -h "/initrd${SAVE_LAYER}${ONESPEC}" ]; then #120107
  4958.           DIRLINK="`readlink -m "/initrd${SAVE_LAYER}${ONESPEC}" | sed -e "s%/initrd${SAVE_LAYER}%%"`" #SAVE_LAYER: see /etc/rc.d/PUPSTATE. 120107
  4959.           xDIRLINK="`readlink "/initrd${SAVE_LAYER}${ONESPEC}"`" #120107
  4960.          fi
  4961.         fi
  4962.         if [ "$DIRLINK" ]; then
  4963.          if [ -d "$DIRLINK"  ]; then
  4964.           if [ "$DIRLINK" != "${ONESPEC}" ]; then #precaution.
  4965.            mkdir -p "${DIRECTSAVEPATH}${DIRLINK}" #120107
  4966.            cp -a -f --remove-destination ${DIRECTSAVEPATH}"${ONESPEC}"/* "${DIRECTSAVEPATH}${DIRLINK}/" #ha! fails if put double-quotes around entire expression.
  4967.            rm -rf "${DIRECTSAVEPATH}${ONESPEC}"
  4968.            if [ "$DIRECTSAVEPATH" = "" ]; then
  4969.             ln -s "$xDIRLINK" "${ONESPEC}"
  4970.            else
  4971.             DSOPATH="`dirname "${DIRECTSAVEPATH}${ONESPEC}"`"
  4972.             DSOBASE="`basename "${DIRECTSAVEPATH}${ONESPEC}"`"
  4973.             rm -f "${DSOPATH}/.wh.${DSOBASE}" #allow underlying symlink to become visible on top.
  4974.            fi
  4975.           fi
  4976.          fi
  4977.         fi
  4978.        fi
  4979.       fi
  4980.      done
  4981.  
  4982.             # woofce: NO_MULTIARCH_SYMLINK=1 (DISTRO_SPECS)
  4983.             if [ -z "$NO_MULTIARCH_SYMLINK" ] ; then
  4984.        #121217 it seems that this problem is occurring in other modes (13 reported)...
  4985.        #121123 having a problem with multiarch symlinks in full-installation...
  4986.        #it seems that the symlink is getting replaced by a directory.
  4987.        if [ "$DISTRO_ARCHDIR" ]; then #in /etc/rc.d/DISTRO_SPECS. 130112 change test from DISTRO_ARCHDIR. 130114 revert DISTRO_ARCHDIR_SYMLINKS==yes.
  4988.          if [ -d /usr/lib/${DISTRO_ARCHDIR} ]; then
  4989.            if [ ! -h /usr/lib/${DISTRO_ARCHDIR} ]; then
  4990.              cp -a -f --remove-destination /usr/lib/${DISTRO_ARCHDIR}/* /usr/lib/
  4991.              sync
  4992.              rm -r -f /usr/lib/${DISTRO_ARCHDIR}
  4993.              ln -s ./ /usr/lib/${DISTRO_ARCHDIR}
  4994.            fi
  4995.          fi
  4996.          if [ -d /lib/${DISTRO_ARCHDIR} ]; then
  4997.            if [ ! -h /lib/${DISTRO_ARCHDIR} ]; then
  4998.              cp -a -f --remove-destination /lib/${DISTRO_ARCHDIR}/* /lib/
  4999.              sync
  5000.              rm -r -f /lib/${DISTRO_ARCHDIR}
  5001.              ln -s ./ /lib/${DISTRO_ARCHDIR}
  5002.            fi
  5003.          fi
  5004.          if [ -d /usr/bin/${DISTRO_ARCHDIR} ]; then
  5005.            if [ ! -h /usr/bin/${DISTRO_ARCHDIR} ]; then
  5006.              cp -a -f --remove-destination /usr/bin/${DISTRO_ARCHDIR}/* /usr/bin/
  5007.              sync
  5008.              rm -r -f /usr/bin/${DISTRO_ARCHDIR}
  5009.              ln -s ./ /usr/bin/${DISTRO_ARCHDIR}
  5010.            fi
  5011.          fi
  5012.        fi
  5013.      fi
  5014.  
  5015.      #flush unionfs cache, so files in pup_save layer will appear "on top"...
  5016.      if [ "$DIRECTSAVEPATH" != "" ]; then
  5017.       #but first, clean out any bad whiteout files...
  5018.       # 22sep10 shinobar: bugfix was not working clean out whiteout files
  5019.       find /initrd/pup_rw -mount -type f -name .wh.\*  -printf '/%P\n'|
  5020.       while read ONEWHITEOUT
  5021.       do
  5022.        ONEWHITEOUTFILE="`basename "$ONEWHITEOUT"`"
  5023.        ONEWHITEOUTPATH="`dirname "$ONEWHITEOUT"`"
  5024.        if [ "$ONEWHITEOUTFILE" = ".wh.__dir_opaque" ]; then
  5025.         [ "$(grep "$ONEWHITEOUTPATH" "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files")" != "" ] && rm -f "/initrd/pup_rw/$ONEWHITEOUT"
  5026.         continue
  5027.        fi
  5028.        ONEPATTERN="`echo -n "$ONEWHITEOUT" | sed -e 's%/\\.wh\\.%/%'`"'/*' ;#echo "$ONEPATTERN" >&2
  5029.        [ "$(grep -x "$ONEPATTERN" "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files")" != "" ] && rm -f "/initrd/pup_rw/$ONEWHITEOUT"
  5030.       done
  5031.       #111229 /usr/local/petget/removepreview.sh when uninstalling a pkg, may have copied a file from sfs-layer to top, check...
  5032.       cat "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" |
  5033.       while read ONESPEC
  5034.       do
  5035.        [ "$ONESPEC" = "" ] && continue #precaution.
  5036.        if [ ! -d "$ONESPEC" ]; then
  5037.         [ -e "/initrd/pup_rw${ONESPEC}" ] && rm -f "/initrd/pup_rw${ONESPEC}"
  5038.        fi
  5039.       done
  5040.       #now re-evaluate all the layers...
  5041.       busybox mount -t aufs -o remount,udba=reval unionfs / #remount with faster evaluation mode.
  5042.       [ $? -ne 0 ] && logger -s -t "pkg" "Failed to remount aufs / with udba=reval"
  5043.       sync
  5044.      fi
  5045.  
  5046.      # get pkg size, category and description
  5047.      [ "$PKGCAT" = ''  ] && PKGCAT=`LANG=C  grep -m1 "|${PKGNAME_ONLY}|" "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | cut -f5 -d'|' | head -1`
  5048.      [ "$PKGCAT" = ''  ] && PKGCAT=`LANG=C  grep -m1 "^${PKGNAME}|" "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null | cut -f5 -d'|' | head -1`
  5049.      [ "$PKGDESC" = '' ] && PKGDESC=`LANG=C grep -m1 "|${PKGNAME_ONLY}|" "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null| cut -f10 -d'|' | head -1`
  5050.      [ "$PKGDESC" = '' ] && PKGDESC=`LANG=C grep -m1 "^${PKGNAME}|" "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null| cut -f10 -d'|' | head -1`
  5051.      [ "$PKGSIZE" = '' ] && PKGSIZE=`LANG=C du -s -k ${CURDIR}/${PKG} 2>/dev/null | cut -f1`
  5052.      # get pkg version
  5053.      PKGVER="`LANG=C  echo "$PKGNAME" | sed -e 's/^[^0-9]*-//g'`"
  5054.      PKGARCH="`LANG=C echo "$PKGNAME" | cut -f3 -d'-' | grep -E 'i[3-9]|x[8-9]|noarch'`"
  5055.  
  5056.      # last check for pkg info
  5057.      [ "$DEPSLIST" = '' ] && DEPSLIST="`grep -m1 "^${PKGNAME}|" "$REPO_DB_FILE_DIR"/Packages-* 2>/dev/null | cut -f9 -d'|' | grep '+' | head -1`"
  5058.      [ "$PKGDESC" = ''  ] && PKGDESC="No description"
  5059.  
  5060.      #080917 - build woof compatible repo line .. #090817
  5061.      if [ "`cut -f1 -d'|' "$USER_INST_PKGS_FILE" 2>/dev/null | grep -m1 ^${PKGNAME}`" = '' ]; then
  5062.  
  5063.        # get the package db entry to add to user-installed-packages...
  5064.        # first, get it from petspecs if possible, or from the pkgs repo, or make one
  5065.        if [ -f /pet.specs ]; then
  5066.          DB_ENTRY="`cat /pet.specs | head -n 1`"
  5067.        elif [ "`grep -m1 "^${PKGNAME}|" "$REPO_DB_FILE_DIR/$REPOFILE"`" != "" ]; then
  5068.          DB_ENTRY="`grep -m1 "^${PKGNAME}|" "$REPO_DB_FILE_DIR/$REPOFILE"`"
  5069.        else
  5070.          DB_ENTRY="${PKGNAME}|${PKGNAME_ONLY}|${PKGVER}|${BUILD}|${PKGCAT}|${PKGSIZE}|${PKGDIR}|${PKGNAME}.${pkg_ext}|${DEPSLIST}|${PKGDESC}|${DISTRO_BINARY_COMPAT}|${DISTRO_COMPAT_VERSION}"
  5071.        fi
  5072.  
  5073.        # Fix for Debian executables showing as shared libs in ROX.
  5074.        if [ "$DISTRO_FILE_PREFIX" = "stretch" -o "$DISTRO_FILE_PREFIX" = "ascii" ]; then
  5075.          if [ "$(ps aux | grep ROX |grep -v grep)" ]; then # Other managers are OK
  5076.            if [ "$(which elfedit)" ]; then
  5077.              grep -E '/bin/|/sbin/' "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" |
  5078.              while read FLINE
  5079.              do
  5080.                [ "$(file "$FLINE" | grep -i 'shared object')" ] && elfedit --input-type=dyn --output-type=exec $FLINE
  5081.              done
  5082.            else
  5083.              echo -e "${yellow}Warning${endcolour} Recent Debian executables show as shared libraries in ROX,"
  5084.              echo "which causes ROX to fail to open or execute them. To fix that during package "
  5085.              echo "installation you should install elfutils or have devx loaded."
  5086.            fi
  5087.          fi
  5088.        fi
  5089.  
  5090.        # now add the db entry to user-installed-packages
  5091.        echo "$DB_ENTRY" >> "$USER_INST_PKGS_FILE" #130913
  5092.  
  5093.      fi
  5094.  
  5095.      # now delete the petspecs file(s), we already added our PKG info to installed-packages
  5096.      rm /pet.specs &>/dev/null
  5097.      rm /*.pet.specs &>/dev/null
  5098.  
  5099.      # make sure /tmp has correct permission, a pkg may have overwritten it
  5100.      ls -dl /tmp | grep -q '^drwxrwxrwt' || chmod 1777 /tmp #130305 rerwin.
  5101.  
  5102.      #090817
  5103.      if [ "$(is_installed_pkg $PKGNAME)" = true -a "$(cat "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files")" != '' ]; then
  5104.        echo -e "${green}Installed:${endcolour} $PKGNAME"
  5105.        # show if pkg has menu entry
  5106.        menu_entry_msg "$PKGNAME_ONLY"
  5107.        #080413 do fixmenus, if menu entry found #200713 moved here
  5108.        if [ "$(grep -m1 '/usr/share/applications' "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" 2>/dev/null | grep desktop$)" != "" ]; then
  5109.          [ ! -f /tmp/pkg/update_menus_busy ] && update_menus &
  5110.        fi
  5111.        [ "`which logger`" != '' ] && logger "$0 Package $PKGNAME installed by $APP $APPVER"
  5112.      else
  5113.        error "$PKGNAME may not have installed correctly."
  5114.      fi
  5115.  
  5116.      #100817 clean up user-installed-packages (remove duplicates and empty lines
  5117.      cat "$USER_INST_PKGS_FILE" | grep -v "^\$" | uniq > "$PKGS_DIR/user-installed-packages_clean"
  5118.      mv "$PKGS_DIR/user-installed-packages_clean" "$USER_INST_PKGS_FILE"
  5119.  
  5120.      # puppy specific fixes for installed package
  5121.      postinstall_hacks "$PKGNAME" "$PKGNAME_ONLY" &
  5122.  
  5123.      #100622 slackware 13.1: just in case any got through, remove c-shell scripts...
  5124.      rm -f /etc/profile.d/*.csh* 2>/dev/null
  5125.  
  5126.      #120523 precise puppy needs this... (refer also rc.update and 3builddistro)
  5127.      if [ "`grep '/usr/share/glib-2.0/schemas' "$PACKAGE_FILE_LIST_DIR/${PKGNAME}*.files" 2>/dev/null`" != "" ]; then
  5128.        [ -e /usr/bin/glib-compile-schemas ] && /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas &>/dev/null
  5129.      fi
  5130.  
  5131.      if [ "`grep '/usr/lib/gio/modules' "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.files" 2>/dev/null`" != "" ]; then
  5132.        [ -e /usr/bin/gio-querymodules ] && /usr/bin/gio-querymodules /usr/lib/gio/modules &>/dev/null
  5133.      fi
  5134.  
  5135.      sync
  5136.  
  5137.    fi #if CONFIRM = y #100213,0.9  moved down to here, fixes install, and output msgs
  5138.  
  5139.  else # file doesn't exists, user must download it first
  5140.  
  5141.    if [ "$PKGNAME" != '' ]; then
  5142.  
  5143.      echo "Package '$PKGNAME' not yet downloaded."
  5144.  
  5145.      matching_local_pkgs="`list_downloaded_pkgs $PKGNAME`"
  5146.  
  5147.      # if no matching pkgs downloaded
  5148.      if [ "$matching_local_pkgs" != "" ]; then
  5149.        echo "You could install one of the following packages with \`$SELF -i PKGNAME\`:"
  5150.        echo "`list_downloaded_pkgs $(basename "$PKGNAME" .$pkg_ext 2>/dev/null) 2>/dev/null`"
  5151.  
  5152.      # else if not downloaded, and in the current repo, list the matching repo pkgs
  5153.      elif [ "$matching_local_pkgs" = "" -a "`grep -m1 "^${PKGNAME}|" "$REPO_DB_FILE_DIR/${REPOFILE}" 2>/dev/null`" != "" ]; then
  5154.        echo "You must first download one of the following packages with \`$SELF -d PKGNAME\`:"
  5155.        echo "`$PKGSEARCH $(basename $PKGNAME .$pkg_ext)`"
  5156.  
  5157.      else
  5158.        not_found "${PKGNAME}"
  5159.      fi
  5160.  
  5161.    else # PKGNAME is empty
  5162.  
  5163.      echo "Package could not be identified."
  5164.  
  5165.    fi
  5166.  
  5167.    exit 1
  5168.  
  5169.  fi
  5170.  
  5171.  # go back to the dir we started in
  5172.  cd "$PREVDIR"
  5173.  
  5174. }
  5175.  
  5176.  
  5177. postinstall_hacks(){              # fix pkgs after installation
  5178.  local PKGNAME="$1"
  5179.  local PKGNAME_ONLY="$2"
  5180.  
  5181.  # remove %u, %U, %f (etc) from Exec lines
  5182.  DESKTOPFILE="`find /usr/share/applications -iname "${PKGNAME_ONLY}*.desktop"`"
  5183.  [ -f "$DESKTOPFILE" ] && \
  5184.    sed -i 's/ %u//' $DESKTOPFILE && \
  5185.    sed -i 's/ %U//' $DESKTOPFILE && \
  5186.    sed -i 's/ %f//' $DESKTOPFILE && \
  5187.    sed -i 's/ %F//' $DESKTOPFILE
  5188.  
  5189.  case $PKGNAME in
  5190.   0ad-*|0ad_*)
  5191.    bbe -e 's/geteuid/getppid/' /usr/games/pyrogenesis > /usr/games/pyrogenesis1 2>/dev/null
  5192.    mv /usr/games/pyrogenesis1 /usr/games/pyrogenesis
  5193.    chmod 755 /usr/games/pyrogenesis
  5194.   ;;
  5195.   openclonk-*|openclonk_*)
  5196.    bbe -e 's/geteuid/getppid/' /usr/games/openclonk > /usr/games/openclonk1 2>/dev/null
  5197.    mv /usr/games/openclonk1 /usr/games/openclonk
  5198.    chmod 755 /usr/games/openclonk
  5199.   ;;
  5200.   vlc_*|vlc-*)
  5201.    VLCDESKTOP="`find /usr/share/applications -mindepth 1 -maxdepth 1 -iname 'vlc*.desktop'`"
  5202.    [ -f $VLCDESKTOP ] && sed -i 's/ --started-from-file %U//' $VLCDESKTOP
  5203.    [ -f $VLCDESKTOP ] && sed -i 's/ --started-from-file//' $VLCDESKTOP
  5204.  
  5205.    #120907 vlc in debian/ubuntu configured to not run as root (it is a pre-compile configure option to enable running as root).
  5206.    #this hack will fix it...
  5207.    #note, this code is also in FIXUPHACK in 'vlc' template.
  5208.    if [ -f /usr/bin/bbe ]; then #bbe is a sed-like utility for binary files.
  5209.     if [ -f /usr/bin/vlc  ]; then
  5210.       bbe -e 's/geteuid/getppid/' /usr/bin/vlc > /tmp/vlc-temp1
  5211.       mv -f /tmp/vlc-temp1 /usr/bin/vlc
  5212.       chmod 755 /usr/bin/vlc
  5213.     fi
  5214.    fi
  5215.   ;;
  5216.   google-chrome-*) #130221 pemasu. 130224 pemasu: limit cache size...
  5217.    if [ -f /usr/bin/bbe ]; then #bbe is a sed-like utility for binary files.
  5218.     if [ -f /opt/google/chrome/chrome  ]; then
  5219.    bbe -e 's/geteuid/getppid/' /opt/google/chrome/chrome > /tmp/chrome-temp1
  5220.    mv -f /tmp/chrome-temp1 /opt/google/chrome/chrome
  5221.    chmod 755 /opt/google/chrome/chrome
  5222.    [ -e /usr/bin/google-chrome ] && rm -f /usr/bin/google-chrome
  5223.    echo '#!/bin/sh
  5224.  exec /opt/google/chrome/google-chrome --user-data-dir=/root/.config/chrome --disk-cache-size=10000000 --media-cache-size=10000000 "$@"' > /usr/bin/google-chrome
  5225.    chmod 755 /usr/bin/google-chrome
  5226.    ln -s google-chrome /usr/bin/chrome
  5227.    ln -s /opt/google/chrome/product_logo_48.png /usr/share/pixmaps/google-chrome.png
  5228.    ln -s /opt/google/chrome/product_logo_48.png /usr/share/pixmaps/chrome.png
  5229.    CHROMEDESKTOP="`find /usr/share/applications -mindepth 1 -maxdepth 1 -iname '*chrome*.desktop'`"
  5230.    if [ "$CHROMEDESKTOP" = "" ]; then #precaution.
  5231.     echo '[Desktop Entry]
  5232. Encoding=UTF-8
  5233. Version=1.0
  5234. Name=Google Chrome web browser
  5235. GenericName=Google Chrome
  5236. Comment=Google Chrome web browser
  5237. Exec=google-chrome
  5238. Terminal=false
  5239. Type=Application
  5240. Icon=google-chrome.png
  5241. Categories=WebBrowser;' > /usr/share/applications/google-chrome.desktop
  5242.    fi
  5243.     fi
  5244.    fi
  5245.   ;;
  5246.  chromium*) #130221 pemasu. 130224 pemasu: limit cache size...
  5247.    if [ -f /usr/bin/bbe ]; then #bbe is a sed-like utility for binary files.
  5248.     if [ -f /usr/lib/chromium/chromium  ]; then
  5249.    bbe -e 's/geteuid/getppid/' /usr/lib/chromium/chromium > /tmp/chrome-temp1
  5250.    mv -f /tmp/chrome-temp1 /usr/lib/chromium/chromium
  5251.    chmod 755 /usr/lib/chromium/chromium
  5252.    [ -e /usr/bin/chromium ] && rm -f /usr/bin/chromium
  5253.    echo '#!/bin/sh
  5254.  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
  5255.    chmod 755 /usr/bin/chromium
  5256.    ln -s /usr/share/icons/hicolor/48x48/apps/chromium.png /usr/share/pixmaps/chromium.png
  5257.    CHROMEDESKTOP="`find /usr/share/applications -mindepth 1 -maxdepth 1 -iname '*chromium-br*.desktop'`"
  5258.    if [ "$CHROMEDESKTOP" = "" ]; then #precaution.
  5259.     echo '[Desktop Entry]
  5260. Encoding=UTF-8
  5261. Version=1.0
  5262. Name=Chromium web browser
  5263. GenericName=Chromium
  5264. Comment=Chromium web browser
  5265. Exec=chromium
  5266. Terminal=false
  5267. Type=Application
  5268. Icon=chromium.png
  5269. Categories=WebBrowser;' > /usr/share/applications/chromium.desktop
  5270.    fi
  5271.     fi
  5272.    fi
  5273.   ;;
  5274.   jwm_theme_*)
  5275.    #120924 DejaVu font no good for non-Latin languages...
  5276.    #see also langpack_* pinstall.sh (template is in /usr/share/doc/langpack-template/pinstall.sh, read by momanager).
  5277.    LANGUSER="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
  5278.    case $LANGUSER in
  5279.     zh*|ja*|ko*) #chinese, japanese, korean
  5280.    sed -i -e 's%DejaVu Sans%Sans%' /etc/xdg/templates/_root_*
  5281.    sed -i -e 's%DejaVu Sans%Sans%' /root/.jwm/themes/*-jwmrc
  5282.    sed -i -e 's%DejaVu Sans%Sans%' /root/.jwm/jwmrc-theme
  5283.     ;;
  5284.    esac
  5285.    #130326 font size fix for 96 dpi...
  5286.    if [ "$PKGNAME_ONLY" ]; then
  5287.     JWMTHEMEFILE="$(grep '^/root/\.jwm/themes/.*-jwmrc$' "$PACKAGE_FILE_LIST_DIR/${PKGNAME_ONLY}.files" | head -n 1)"
  5288.     [ "$JWMTHEMEFILE" ] && hackfontsize "JWMTHEMES='${JWMTHEMEFILE}'"
  5289.    fi
  5290.   ;;
  5291.   openbox*)
  5292.    #120924 DejaVu font no good for non-Latin languages...
  5293.    #see also langpack_* pinstall.sh (template is in /usr/share/doc/langpack-template/pinstall.sh, read by momanager).
  5294.    LANGUSER="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
  5295.    case $LANGUSER in
  5296.     zh*|ja*|ko*) #chinese, japanese, korean
  5297.    sed -i -e 's%DejaVu Sans%Sans%' /etc/xdg/openbox/*.xml
  5298.    sed -i -e 's%DejaVu Sans%Sans%' /root/.config/openbox/*.xml
  5299.     ;;
  5300.    esac
  5301.   ;;
  5302.   gtk_theme_*)
  5303.    #120924 DejaVu font no good for non-Latin languages...
  5304.    #see also langpack_* pinstall.sh (template is in /usr/share/doc/langpack-template/pinstall.sh, read by momanager).
  5305.    LANGUSER="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
  5306.    case $LANGUSER in
  5307.     zh*|ja*|ko*) #chinese, japanese, korean
  5308.    GTKRCFILE="$(find /usr/share/themes -type f -name gtkrc | tr '\n' ' ')"
  5309.    for ONEGTKRC in $GTKRCFILE
  5310.    do
  5311.     sed -i -e 's%DejaVu Sans%Sans%' $ONEGTKRC
  5312.    done
  5313.     ;;
  5314.    esac
  5315.    #130326 font size fix for 96 dpi...
  5316.    if [ "$PKGNAME_ONLY" ]; then
  5317.     GTKTHEMEFILE="$(grep '^/usr/share/themes/.*/gtk-2\.0/gtkrc$' "$PACKAGE_FILE_LIST_DIR/${PKGNAME_ONLY}.files" | head -n 1)"
  5318.     [ "$GTKTHEMEFILE" ] && hackfontsize "GTKRCS='${GTKTHEMEFILE}'"
  5319.    fi
  5320.   ;;
  5321.   seamonkey*|firefox*)
  5322.    #120924 DejaVu font no good for non-Latin languages...
  5323.    #see also langpack_* pinstall.sh (template is in /usr/share/doc/langpack-template/pinstall.sh, read by momanager).
  5324.    LANGUSER="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
  5325.    case $LANGUSER in
  5326.     zh*|ja*|ko*) #chinese, japanese, korean
  5327.    MOZFILE="$(find /root/.mozilla -type f -name prefs.js -o -name '*.css' | tr '\n' ' ')"
  5328.    for ONEMOZ in $MOZFILE
  5329.    do
  5330.     sed -i -e 's%DejaVu Sans%Sans%' $ONEMOZ
  5331.    done
  5332.     ;;
  5333.    esac
  5334.   ;;
  5335.   mc_*) #121206 midnight commander
  5336.    #in ubuntu, won't run from the menu. this fixes it...
  5337.    [ -f /usr/share/applications/mc.desktop ] && sed -i -e 's%^Exec=.*%Exec=TERM=xterm mc%' /usr/share/applications/mc.desktop
  5338.   ;;
  5339.   xsane*) #130122
  5340.    #xsane puts up a warning msg at startup if running as root, remove it...
  5341.    #this code is also in file FIXUPHACK in xsane template (in Woof).
  5342.    #WARNING: this may only work for x86 binary.
  5343.    if [ -f /usr/bin/bbe ]; then #bbe is a sed-like utility for binary files.
  5344.     if [ -f /usr/bin/xsane  ]; then
  5345.    bbe -e 's/\x6b\x00getuid/\x6b\x00getpid/' /usr/bin/xsane > /tmp/xsane-temp1
  5346.    mv -f /tmp/xsane-temp1 /usr/bin/xsane
  5347.    chmod 755 /usr/bin/xsane
  5348.     fi
  5349.    fi
  5350.   ;;
  5351.   kompozer*) #130507
  5352.    [ -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
  5353.   ;;
  5354.  esac
  5355. }
  5356.  
  5357.  
  5358. choose_pkg(){                     # given partial name ($1), choose from a list of matching packages FUNCLIST
  5359.  
  5360.  
  5361.  # exit if no valid options
  5362.  [ ! "$1" -o "$1" = "" -o "$1" = "-" ] && print_usage get && exit 1
  5363.  
  5364.  # get $REPONAME and $EX
  5365.  . ${PKGRC}
  5366.  
  5367.  local REPOEX    # extension of pkgs in the current repo $REPONAME (from rc file)
  5368.  local PKGEX     # pkg extension we get from $1, defaults to $REPOEXT is empty
  5369.  local PKGNAME   # the name of the pkg, we get this from $1
  5370.  local PKGNAME_ONLY  # the pkg name without version, we get this from PKGNAME
  5371.  local PKGS      # the list of PKGS returned matching $1/$PKGNAME
  5372.  local INT=1     # used to provide numbered lists
  5373.  
  5374.  REPOEX=$EX
  5375.  # get pkg extension
  5376.  PKGEX=`get_pkg_ext "$1"`
  5377.  
  5378.  # if no extension, set to extension of current repo
  5379.  [ "$PKGEX" = '' ] && PKGEX=$REPOEX
  5380.  
  5381.  # get pkg name with version, no extension or path
  5382.  PKGNAME="$(basename "$1" .$PKGEX)"
  5383.  
  5384.  # get the full pkg name, to compare against repo pkgs we find
  5385.  PKGNAME_FULL="`get_pkg_name "$PKGNAME"`"
  5386.  
  5387.  # get pkg name only .. without version or suffix
  5388.  PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  5389.  
  5390.  # remove any previous user choice lists
  5391.  rm $TMPDIR/PKGLIST &>/dev/null
  5392.  rm $TMPDIR/USRPKGLIST &>/dev/null
  5393.  
  5394.  # get all pkgs that match the given pkgname
  5395.  # returns full pkg names (field1 of repo, pkgname with ver but no extension) each on a new line
  5396.  PKGS="`$PKGSEARCH "${PKGNAME}"`"
  5397.  
  5398.  if [ "$FORCE" = false -a "`is_installed_pkg "$PKGNAME_FULL"`" = true -a "$HIDE_INSTALLED" = true ]; then
  5399.    # remove it from choices
  5400.    PKGS="`echo "$PKGS" | grep -v ^$PKGNAME_FULL\$`"
  5401.  fi
  5402.  
  5403.  # extra steps for ubuntu and debian repos, if multiple choices returned.. remove non-compatible archs, remove dev and dbg pkgs...
  5404.  if [ "$PKGEX" = "deb" -a "`echo "$PKGS" | wc -l`" != "1" ]; then
  5405.  
  5406.    ARCH="`uname -m`"
  5407.    #remove x64 pkgs from choices if not using an x64 cpu
  5408.    [ "$ARCH" != "x86_64" ] && PKG="`echo "$PKGS" | grep -v -E 'amd64|x86_64'`"
  5409.  
  5410.    # set any pkgs matching current arch to top of list
  5411.    for LINE in `echo "$PKGS"  | sort -r` #first pkg found (newest) added to top, then the next, etc, sort use sort -r to keep newest at top of new list
  5412.    do
  5413.      # if not searching for -dev or -dgb pkg, move it to bottom of the list
  5414.      if [ "`is_blacklisted_pkg "$LINE"`" = false -a "`echo "$PKGNAME" | grep -E "\-dbg_|\-dev_"`" = "" -a "`echo "$LINE" | grep -E "\-dbg_|\-dev_"`" != "" ]; then
  5415.        PKGS="`echo "$PKGS" | grep -v  "$LINE"`"
  5416.        PKGS="$PKGS
  5417. $LINE"
  5418.      fi
  5419.      # if pkg is for current cpu arch, move it to top of the list
  5420.      if [ "`echo "$LINE" | grep -m1 "$ARCH"`" != "" ]; then
  5421.        PKGS="`echo "$PKGS" | grep -v  "$LINE"`"
  5422.        PKGS="$LINE
  5423. $PKGS"
  5424.      fi
  5425.    done
  5426.    #remove debug and dev pkgs
  5427.    #PKGS="`echo "$PKGS" | grep -v "\-dbg_"`"
  5428.    #PKGS="`echo "$PKGS" | grep -v "\-dev_"`"
  5429.  fi
  5430.  
  5431.  # get the user to choose which packages they want to install
  5432.  if [ "$ASK" = true -a "$PKGS" != "" ]; then
  5433.    echo "Please choose the package number. For the first package,"
  5434.    echo "enter '1', without quotes. To install multiple packages,"
  5435.    echo "enter the numbers, separated by a comma. Example:  1,3,4"
  5436.    echo
  5437.  fi
  5438.  
  5439.  # if using ubuntu/debian packages, put pkg_* before pkg-* .. else dont
  5440.  [ "$PKGEX" = 'deb' ] && sort='sort -r' || sort='sort'
  5441.  
  5442.  # go through each actual pkg that matches the pkgname search, make it a numbered list
  5443.  echo "$PKGS" | $sort -u | while read LINE
  5444.  do
  5445.    if [ "$LINE" != "" -a "$LINE" != "," -a "$LINE" != " " ]; then
  5446.      [ "$ASK" = true ] && echo "${INT}. $LINE"
  5447.      echo "${INT}. $LINE" >> $TMPDIR/PKGLIST
  5448.      INT=$(($INT + 1))
  5449.    fi
  5450.  done
  5451.  
  5452.  # if pkg list was made
  5453.  if [ -f $TMPDIR/PKGLIST -a "`cat $TMPDIR/PKGLIST 2>/dev/null`" != "" ]; then
  5454.  
  5455.    # set to first pkg only as default
  5456.    if [ "$ASK" = false ]; then
  5457.      USRPKGLIST="$(echo "$PKGS" | $sort | head -1)"
  5458.      echo "$USRPKGLIST" > $TMPDIR/USRPKGLIST
  5459.    fi
  5460.  
  5461.    # user can now input which actual pkgs to get, chosen by number
  5462.    if [ "$ASK" = true ]; then
  5463.      # only ask once
  5464.      ASK=false
  5465.  
  5466.      echo
  5467.      echo "Give the numbers of the packages you want to install,"
  5468.      echo -n "separated by a comma, or hit ENTER only to skip: "
  5469.      read USRPKGLIST1 </dev/tty
  5470.  
  5471.      # if user chose nothing (hit ENTER only), just skip
  5472.      [ "$USRPKGLIST1" = '' ] && continue
  5473.  
  5474.      # split the results into newlines, create the list of chosen pkgs (used by other funcs)
  5475.      echo "${USRPKGLIST1}" | tr ',' '\n' | while read LINE
  5476.      do
  5477.        # set chosen pkg choice(s)
  5478.        echo "`grep "^$LINE. " "$TMPDIR/PKGLIST" 2>/dev/null | cut -f2 -d' '`" >> $TMPDIR/USRPKGLIST
  5479.      done
  5480.    fi
  5481.  
  5482.    # remove temp file.. but keep $TMPDIR/USRPKGLIST, it contains our users choices, and is used by pkg_get() and get_deps()
  5483.    rm $TMPDIR/PKGLIST &>/dev/null
  5484.  fi
  5485. }
  5486.  
  5487.  
  5488. pkg_get(){                        # find, download and install $1 and its deps FUNCLIST
  5489.  
  5490.  # The function `choose_pkg` is run just before this one. It gives us $TMPDIR/USRPKGLIST,
  5491.  # which contains a list of packages the user wants to install or download.
  5492.  # In this func, we will go through the list and download/install the package, as well
  5493.  # as its dependencies (depending on what the user chose to do).
  5494.  
  5495.  . ${PKGRC}
  5496.  
  5497.  # exit if no valid options
  5498.  [ ! "$1" -o "$1" = "" -o "$1" = "-" ] && print_usage get && exit 1
  5499.  
  5500.  local PREVDIR="$CURDIR"
  5501.  local pkg_ext=`get_pkg_ext "$1"`; pkg_ext="${pkg_ext:-$EX}" # fall back to repo extension
  5502.  local PKGNAME="`get_pkg_name $(basename "$1" .$pkg_ext)`"
  5503.  local PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  5504.  local PKGLIST="${PKGNAME}"
  5505.  local pkg_builtin=''
  5506.  
  5507.  # dont ask to download or install pkg or deps, as choose_pkg() already asked
  5508.  ASK=false
  5509.  
  5510.  # exit if no valid pkg name
  5511.  [ ! "$PKGNAME" -o "$PKGNAME" = "" -o "$PKGNAME" = "-" ] && print_usage get && exit 1
  5512.  
  5513.  # we want to download all pkgs to same place
  5514.  cd "$WORKDIR"
  5515.  CURDIR="$WORKDIR"
  5516.  
  5517.  # use the list of pkgs user has chosen to install, or the given PKGNAME
  5518.  if [ "`cat "$TMPDIR/USRPKGLIST" 2>/dev/null`" != "" ]; then
  5519.    PKGLIST="`cat "$TMPDIR/USRPKGLIST" | grep -v "^\$"`"
  5520.  fi
  5521.  
  5522.  if [ -z "$PKGLIST" ]; then
  5523.    echo "No packages to get, exiting."
  5524.    exit 1
  5525.  fi
  5526.  
  5527.  # skip if any pkgs previously installed during our current loop (of getting 'pkg + recursive deps')
  5528.  echo "$PKGLIST" | while read pkg_in_list
  5529.  do
  5530.    name_only=`get_pkg_name_only "$pkg_in_list"`
  5531.    [ -f $TMPDIR/PKGSDONE -a "`grep "^${name_only}\$" $TMPDIR/PKGSDONE 2>/dev/null`" != '' ] && continue
  5532.  done
  5533.  
  5534.  # list the pkgs and ask to download (and maybe install)
  5535.  echo "$PKGLIST" | while read pkg
  5536.  do
  5537.  
  5538.    [ "$pkg" = '' -o ! "$pkg" ] && continue
  5539.  
  5540.    local pkg_name=`get_pkg_name "$pkg" 2>/dev/null`
  5541.    [ "$pkg_name" = '' -o ! "$pkg_name" ] && continue
  5542.  
  5543.    local pkg_name_only=`get_pkg_name_only "$pkg" 2>/dev/null`
  5544.    [ "`is_blacklisted_pkg "$pkg_name_only"`" = true ] && continue
  5545.  
  5546.    local pkg_already_done=`grep -m1 "^$pkg_name_only\$" $TMPDIR/PKGSDONE 2>/dev/null`
  5547.    [ "$pkg_already_done" != '' ] && continue
  5548.  
  5549.    local pkg_in_repo=`is_repo_pkg $pkg_name_only`
  5550.    local PKGFILE=''
  5551.  
  5552.    # dont even try to download if no matches found
  5553.    if [ "$pkg_in_repo" = true -a "$pkg_name_only" != "" ]; then
  5554.  
  5555.      local pkg_is_builtin=`is_builtin_pkg "$pkg_name_only"`
  5556.      local pkg_is_in_devx=`is_devx_pkg "$pkg_name_only"`
  5557.  
  5558.      # skip getting pkg if its a builtin, unless HIDE_BUILTINS=false
  5559.      if [ "$pkg_is_builtin" = true -a "${HIDE_BUILTINS}" = true ]; then
  5560.        echo "Skipping $pkg_name_only (already built-in).."
  5561.        continue
  5562.      fi
  5563.  
  5564.      # skip getting pkg if its in the devx, unless HIDE_BUILTINS=false
  5565.      if [ "$pkg_is_in_devx" = true -a "${HIDE_BUILTINS}" = true ]; then
  5566.        echo "Skipping $pkg_name_only (already in devx).."
  5567.        continue
  5568.      fi
  5569.  
  5570.      # if we are intending to install the pkg
  5571.      if [ "$NO_INSTALL" = false -a "$FORCE" = false ]; then
  5572.        # skip if package is already installed, unless --force given
  5573.        if [ "$FORCE" = false -a "`is_installed_pkg $pkg_name`" = true ]; then
  5574.          echo -e "Package ${magenta}${pkg_name_only}${endcolour} already installed."
  5575.          echo -e "Use the -f option to force installation: $SELF add $pkg_name_only -f"
  5576.          continue
  5577.        fi
  5578.      fi
  5579.  
  5580.      # get deps early (if any)
  5581.      list_deps "$pkg_name" > ${TMPDIR}/${pkg_name}_dep_list &
  5582.  
  5583.      # DOWNLOAD PKG
  5584.      pkg_download "$pkg_name"
  5585.  
  5586.      [ ! -f "$PKGFILE" ] && PKGFILE="`find "$CURDIR" -maxdepth 1 -type f -name "${pkg_name}.*${pkg_ext}"`"
  5587.      # try grabbing $CURDIR/pkgname.pkg_ext
  5588.      [ ! -f "$PKGFILE" ] && PKGFILE="`find "$CURDIR" -maxdepth 1 -type f -name "${pkg_name}*.${pkg_ext}"`"
  5589.      # now try grabbing $CURDIR/pkgname.*
  5590.      [ ! -f "$PKGFILE" ] && PKGFILE="`find "$CURDIR" -maxdepth 1 -type f -name "${pkg_name}.*"`"
  5591.      # maybe try $CURDIR/pkgname-*.*
  5592.      [ ! -f "$PKGFILE" ] && PKGFILE="`find "$CURDIR" -maxdepth 1 -type f -name "${pkg_name}-*.*"`"
  5593.      # maybe try $CURDIR/pkgname_*.*
  5594.      [ ! -f "$PKGFILE" ] && PKGFILE="`find "$CURDIR" -maxdepth 1 -type f -name "${pkg_name}_*.*"`"
  5595.      # maybe try $CURDIR/pkgname*.*
  5596.      [ ! -f "$PKGFILE" ] && PKGFILE="`find "$CURDIR" -maxdepth 1 -type f -name "${pkg_name}*.*"`"
  5597.  
  5598.      # add extension if Pkg returned an erroneous or user dir
  5599.      [ -d "$PKGFILE" ] && PKGFILE="${PKGFILE}.$pkg_ext"
  5600.  
  5601.      # if we found the package to install in CURDIR
  5602.      if [ -f "${PKGFILE}" ]; then
  5603.  
  5604.        # check if we install or not
  5605.        if [ "${NO_INSTALL}" = false ]; then
  5606.  
  5607.          # if a valid pkg, with files to extract
  5608.          if [ "`pkg_contents "$PKGFILE" 2>/dev/null`" != '' ]; then
  5609.  
  5610.  
  5611.            #INSTALL PKG
  5612.            [ "`is_local_pkg "$PKGFILE"`" = true ] && pkg_install "${PKGFILE}"
  5613.  
  5614.  
  5615.          fi
  5616.  
  5617.        fi
  5618.  
  5619.        # if pkg was installed, or Pkg is simply downloading all deps
  5620.        if [ "`is_installed_pkg "$pkg_name_only"`" = true -o "${NO_INSTALL}" = true ]; then
  5621.  
  5622.          # get the dependencies for this package
  5623.          get_deps "${pkg_name}"
  5624.          #rm /tmp/pkg/list_deps_busy #s243a: we might want to remove list_deps_busy here
  5625.        fi
  5626.  
  5627.      else # PKGFILE not a file
  5628.        echo "Can't find ${PKGNAME} or not a valid pkg.."
  5629.      fi
  5630.  
  5631.    else # no matches in repo found
  5632.      echo "Cannot find ${PKGNAME}.."
  5633.    fi
  5634.  
  5635.    # done with this pkg, add it to done list, will be skipped it seen again, until loop is finished
  5636.    echo "$pkg_name_only" >> $TMPDIR/PKGSDONE
  5637.  done
  5638. }
  5639.  
  5640.  
  5641. pkg_update(){                     # update installed pkgs, $1 is optional filter FUNCLIST
  5642.  
  5643.  # exit if no valid options
  5644.  #[ ! "$1" -o "$1" = "-" ] && print_usage pkg-update && exit 1
  5645.  
  5646.  # get rc file settings
  5647.  . ${PKGRC}
  5648.  
  5649.  local PKGNAME=''
  5650.  local PKGLIST=''
  5651.  local pkg_ext=$EX
  5652.  local BUILTINS=''
  5653.  local separator=''
  5654.  
  5655.  if [ "$1" != '' ]; then
  5656.  
  5657.    PKGNAME=$(basename "$1")
  5658.    PKGNAME=`get_pkg_name "$PKGNAME"`
  5659.    PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  5660.  
  5661.    # get installed packages
  5662.    PKGLIST="`HIDE_BUILTINS=false list_installed_pkgs "$PKGNAME_ONLY"`"
  5663.  
  5664.    #is_builtin=`is_builtin_pkg "$PKGNAME"`
  5665.  
  5666.    # dont update builtins unless -F was given
  5667.    #if [ "$is_builtin" = true -a "$HIDE_BUILTINS" = true ]; then
  5668.    #  echo -e "Package ${magenta}${PKGNAME}${endcolour} is built in, not updating."
  5669.    #  #echo -e "Use `$SELF -F --pkg-update $PKGNAME` to update it anyway."
  5670.    #  exit 0
  5671.    #fi
  5672.  else
  5673.    # get installed packages
  5674.    PKGLIST="`HIDE_BUILTINS=false list_installed_pkgs`"
  5675.  fi
  5676.  
  5677.  # iterate over the list
  5678.  echo "$PKGLIST"  | grep -v ^$ | sort -u | while read installed_pkg; do
  5679.  
  5680.    [ ! "$installed_pkg" -o "$installed_pkg" = '' ] && continue
  5681.  
  5682.    # if this pkg ($installed_pkg) is not from a known repo, we cant compare its version to anything else, skip it
  5683.    #[ "`is_repo_pkg "$installed_pkg"`" = false ] && echo -e "Package ${magenta}$installed_pkg${endcolour} not found in any repos." && continue
  5684.  
  5685.    local PNAME=''
  5686.    local latest_repo_pkg=''
  5687.    local ASKREPLY='y'
  5688.    # get pkg name (without version) and version of current PKG
  5689.  
  5690.    PNAME=`get_pkg_name_only $installed_pkg`
  5691.  
  5692.    [ "$PNAME" = '' ] && continue
  5693.  
  5694.    case $DISTRO_BINARY_COMPAT in
  5695.      ubuntu|trisquel|debian|devuan)
  5696.        separator='_'
  5697.        ;;
  5698.      *)
  5699.        separator='-'
  5700.        ;;
  5701.    esac
  5702.  
  5703.    latest_repo_pkg="`grep -m1 "^${PNAME}${separator}" "${ALL_REPO_DB_PATHS[@]}" |cut -f1 -d'|' | head -1 | cut -f2 -d':'`"
  5704.  
  5705.    # skip if we didn't find the right package
  5706.    [ "`echo "$latest_repo_pkg" | grep "^${PNAME}${separator}"`" = '' ] && continue
  5707.  
  5708.    latest_version=`get_pkg_version "$latest_repo_pkg"`
  5709.    installed_version=`get_pkg_version "$installed_pkg"`
  5710.  
  5711.    # get latest versions
  5712.  
  5713.    # check pkg version against installed version
  5714.    if [ "$latest_version" != "" -a "$installed_version" != "" ]; then
  5715.      vercmp $latest_version gt $installed_version 2>/dev/null
  5716.      RESULT=$?
  5717.  
  5718.      if [ "$RESULT" = 0 ]; then #newer version available
  5719.  
  5720.        if [ "$ASK" = true ]; then
  5721.          echo "Do you want to update to ${PNAME}${separator}${latest_version}? [y/N]   "
  5722.          read -n 1 ASKREPLY </dev/tty
  5723.          [ "$ASK" = true ] && echo -ne "\b\b\n"
  5724.        fi
  5725.  
  5726.        if [ "$ASK" = false -o "$ASKREPLY" = "y" ]; then
  5727.          echo -e "${yellow}Update${endcolour}: ${magenta}$PNAME${endcolour} from ${bold}$installed_version${endcolour} to ${bold}$latest_version${endcolour}"
  5728.          cd "$WORKDIR"
  5729.          ASK=$ASK FORCE=$FORCE pkg_get "$latest_repo_pkg"
  5730.          cd "$CURDIR"
  5731.        fi
  5732.  
  5733.      else #280613 inform user if no update found
  5734.        echo -e "${green}Latest${endcolour}: $PNAME${separator}$installed_version"
  5735.      fi #end if vercmp XX gt YY = 0
  5736.  
  5737.    elif [ "$PNAME" = '' ]; then
  5738.      error "${installed_pkg} not found."
  5739.    else #280613
  5740.      error "$installed_pkg package versions could not be compared: ${latest_version:-unknown latest} => ${installed_version:-unknown installed version}"
  5741.      #return 1
  5742.    fi #end if PKGVER != ""
  5743.  done
  5744. }
  5745.  
  5746.  
  5747. pkg_uninstall(){                  # remove an installed package ($1) FUNCLIST
  5748.  
  5749.  # quit if no valid options
  5750.  [ ! "$1" -o "$1" = "-" ] && print_usage uninstall && exit 1
  5751.  
  5752.  local PKGNAME
  5753.  local PKGNAME_ONLY
  5754.  local PKGFILE
  5755.  local pkg_ext
  5756.  
  5757.  # get pkg extension
  5758.  pkg_ext=`get_pkg_ext "$1"`
  5759.  
  5760.  #get pkg name with version, but no extension or path
  5761.  PKGNAME="$(basename "$1" .$pkg_ext)"
  5762.  PKGNAME="$(basename "$PKGNAME" .pet)"
  5763.  PKGNAME="$(basename "$PKGNAME" .deb)"
  5764.  PKGNAME="$(basename "$PKGNAME" .sfs)"
  5765.  PKGNAME="$(basename "$PKGNAME" .tar.gz)"
  5766.  PKGNAME="$(basename "$PKGNAME" .tar.xz)"
  5767.  PKGNAME="$(basename "$PKGNAME" .tgz)"
  5768.  PKGNAME="$(basename "$PKGNAME" .txz)"
  5769.  PKGNAME=`get_pkg_name "$PKGNAME"`
  5770.  
  5771.  # get pkg name only .. without versions or suffix
  5772.  PKGNAME_ONLY="`get_pkg_name_only "$PKGNAME"`"
  5773.  
  5774.  local pkg_is_builtin=`is_builtin_pkg "$PKGNAME_ONLY"`
  5775.  local is_installed=`is_installed_pkg "$PKGNAME"`
  5776.  
  5777.  # skip pkg if its a builtin
  5778.  [ "$pkg_is_builtin" = true ] && return 1
  5779.  
  5780.  local pkg_is_sfs_file="`sfs_loadr -q -i | grep -v ^sfs_loadr | grep ^$PKGNAME`"
  5781.  
  5782.  # if pkg is SFS, "uninstall" it here
  5783.  if [ "$pkg_is_sfs_file" != "" -o "$pkg_ext" = "sfs" ]; then
  5784.    PKGNAME="$(sfs_loadr -q -i | grep -v ^sfs_loadr | grep ^$PKGNAME | head -1)"
  5785.    sfs_loadr -q --cli -"${CURDIR}/${PKGNAME}" 2>/dev/null
  5786.    is_installed=true # we want to continue
  5787.  fi
  5788.  
  5789.  
  5790.  if [ "$is_installed" = true -o "$FORCE" = true ]; then #250713
  5791.  
  5792.    # get the list of files to be deleted, exact match
  5793.    PKGFILE="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "$PKGNAME.files")"
  5794.  
  5795.    # if no exact match, search for pkgname*
  5796.    [ ! -f "$PKGFILE" ] && PKGFILE="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${PKGNAME}"'*.files')"
  5797.    [ ! -f "$PKGFILE" ] && PKGFILE="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${PKGNAME_ONLY}_"'*.files')"
  5798.    [ ! -f "$PKGFILE" ] && PKGFILE="$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${PKGNAME_ONLY}-"'*.files')"
  5799.    [ ! -f "$PKGFILE" ] && PKGFILE="${PKGNAME}.files"
  5800.    [ ! -f "$PKGFILE" ] && PKGFILE="${PKGNAME}.sfs.files"
  5801.    [ ! -f "$PKGFILE" ] && PKGFILE="${PKGNAME}-${CP_SUFFIX}.sfs.files"
  5802.  
  5803.    #if PKG.files not found, then remove it from alien packages list
  5804.    if [ ! -f "$PKGFILE" ]; then
  5805.  
  5806.      #error "'${PKGFILE}' not found.. Cleaning up.."
  5807.  
  5808.      # backup the original list of user installed pkgs
  5809.      cp "$USER_INST_PKGS_FILE" $TMPDIR/user-installed-packages.backup
  5810.  
  5811.      # remove $PKGNAME from list of installed pkgs
  5812.      cat "$USER_INST_PKGS_FILE" 2>/dev/null | grep -v "^${PKGNAME}" > "$PKGS_DIR/user-installed-packages.updated"
  5813.  
  5814.      # if we created a new file ok
  5815.      if [ -f "$PKGS_DIR/user-installed-packages.updated" ]; then
  5816.        # replace the user-installed-packages file with our new one
  5817.        mv "$PKGS_DIR/user-installed-packages.updated" "$USER_INST_PKGS_FILE" 2>/dev/null
  5818.      fi
  5819.  
  5820.      # clean up user-installed-packages (remove duplicates and empty lines)
  5821.      cat "$USER_INST_PKGS_FILE" | grep -v "^\$" | uniq > "$PKGS_DIR/user-installed-packages.clean"
  5822.      mv "$PKGS_DIR/user-installed-packages.clean" "$USER_INST_PKGS_FILE"
  5823.  
  5824.      # no *.files to process, so if not forcing full uninstall, we can exit here
  5825.      [ "$FORCE" = false ] && return 1
  5826.    fi
  5827.  
  5828.    # if we are here, we have a $PKGS_DIR/***.files to work with (or using --force)
  5829.  
  5830.    # get pkgs that depend on $PKGNAME
  5831.    [ "$FORCE" = false ] && dependents="`list_dependents "$PKGNAME"`" || dependents=''
  5832.  
  5833.    # if we have dependents, we should not uninstall and just exit, unless --force was given
  5834.    if [ "$dependents" != "" -a "`echo "$dependents" | grep 'not installed'`" = '' -a "$FORCE" != true ]; then
  5835.  
  5836.      # inform user of dependent pkgs
  5837.      echo -e "${yellow}Warning${endcolour}: $PKGNAME_ONLY is needed by:  "
  5838.      echo -e "${magenta}$dependents${endcolour}"
  5839.      echo "Uninstall the packages above first, or use:"
  5840.      echo -e "${bold}pkg --force uninstall $PKGNAME${endcolour}."
  5841.      echo
  5842.      return 1
  5843.  
  5844.    fi
  5845.  
  5846.    # ask/inform user before uninstall
  5847.    echo -n "Uninstall the package ${PKGNAME}$QTAG:  "
  5848.    [ "$ASK" = true ] && read -n 1 CONFIRM </dev/tty || CONFIRM=y
  5849.    [ "$ASK" = true ] && echo -ne "\b\b\n"
  5850.  
  5851.    # if user answered yes, we will now uninstall the pkgs
  5852.    if [ "$CONFIRM" = "y" ]; then
  5853.  
  5854.      # print new line if we didnt take any user input on tty
  5855.      [ "$ASK" != true ] && echo
  5856.  
  5857.      # execute uninstall script.
  5858.      if [ -x "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.remove" ]; then
  5859.        "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.remove" &>/dev/null
  5860.        rm -f "$PACKAGE_FILE_LIST_DIR/${PKGNAME}.remove" &>/dev/null
  5861.      fi
  5862.  
  5863.      # if we have no pkg file (listing of pkg contents), we cant cat/grep it
  5864.      if [ ! -f "$PKGFILE" ]; then
  5865.        echo "Not found: $PKGS_DIR/$PKGNAME.files"
  5866.        return 1
  5867.      fi
  5868.  
  5869.      # check if has menu entry
  5870.      [ "`cat "$PKGFILE" | grep -m1 ".desktop\$"`" != "" ] && HASMENUENTRY=true || HASMENUENTRY=false
  5871.  
  5872.      # remove files listed in *.files
  5873.      cat "$PKGFILE" | while read LINE
  5874.      do
  5875.        # some symlinks may not get removed. '-e' will not work if symlink
  5876.        # is pointing to a non-existent file. So, check for symlink...
  5877.        REMFILE=""
  5878.        [ -h "$LINE" ] && REMFILE="yes"
  5879.        [ -e "$LINE" ] && REMFILE="yes"
  5880.        if [ "$REMFILE" = "yes" ]; then
  5881.          if [ ! -d "$LINE" ]; then
  5882.            if [ -e "/initrd/pup_ro2$LINE" ]; then
  5883.              # deleting the file on the top layer places a ".wh" whiteout file, that hides the original file.
  5884.              # what we want is to remove the installed file, and restore the original pristine file...
  5885.              cp -af "/initrd/pup_ro2${LINE}" "$LINE"
  5886.            else
  5887.              rm -f "$LINE" &>/dev/null
  5888.            fi
  5889.            #delete empty dirs...
  5890.            DELDIR="`dirname "$LINE" 2>/dev/null`"
  5891.            [ "`ls -1 "$DELDIR"`" = "" ] && rmdir "$DELDIR" &>/dev/null
  5892.          fi
  5893.        fi
  5894.      done
  5895.  
  5896.      # go through again and remove any empty dirs...
  5897.      cat "$PKGFILE" 2>/dev/null  | while read LINE
  5898.      do
  5899.        DELDIR="`dirname "$LINE" 2>/dev/null`"
  5900.        [ -d "$DELDIR" ] && [ "`ls -1 "$DELDIR"`" = "" ] && rmdir "$DELDIR"
  5901.        #check one level up... but do not delete top dir, like /opt...
  5902.        DELLEVELS=`echo -n "$DELDIR" | sed -e 's/[^/]//g' | wc -c | sed -e 's/ //g'`
  5903.        if [ $DELLEVELS -gt 2 ]; then
  5904.          DELDIR="`dirname "$DELDIR" 2>/dev/null`"
  5905.          [ -d "$DELDIR" ] && [ "`ls -1 "$DELDIR"`" = "" ] && rmdir $DELDIR
  5906.        fi
  5907.      done
  5908.  
  5909.      # remove $PKGNAME from user-installed-packages
  5910.      NEWUSERPKGS="$(grep -v "^${PKGNAME}" "$USER_INST_PKGS_FILE")"
  5911.      [ "$NEWUSERPKGS" != "" ] && echo "$NEWUSERPKGS" > "$USER_INST_PKGS_FILE"
  5912.  
  5913.      # clean up user-installed-packages (remove duplicates and empty lines)
  5914.      cat "$USER_INST_PKGS_FILE" | grep -v "^\$" | uniq > "$PKGS_DIR/user-installed-packages_clean"
  5915.      mv "$PKGS_DIR/user-installed-packages_clean" "$USER_INST_PKGS_FILE"
  5916.  
  5917.      # remove $PKGS_DIR/$PKGNAME.files
  5918.      rm $PKGFILE ${PKGFILE} 2>/dev/null
  5919.  
  5920.      # do fixmenus, if menu entry found
  5921.      if [ "$HASMENUENTRY" = true ]; then
  5922.        [ ! -f /tmp/pkg/update_menus_busy ] && update_menus &
  5923.      fi
  5924.  
  5925.      # UNINSTALL DONE .. print message
  5926.      echo -e "${green}Uninstalled:${endcolour} $PKGNAME"
  5927.  
  5928.      # log uninstall with the system logs
  5929.      [ "`which logger`" != '' ] && logger "$0 Package $PKGNAME uninstalled by $APP $APPVER"
  5930.  
  5931.    fi #end if $CONFIRM=yes
  5932.  
  5933.  else # $PKGNAME is not installed
  5934.  
  5935.    # if any installed pkg matches $PKGNAME
  5936.    if [ "`list_installed_pkgs $PKGNAME`" != "" ]; then #290613
  5937.      # list the matching pkgs
  5938.      echo "These installed packages match '$PKGNAME':"
  5939.      echo "`list_installed_pkgs $PKGNAME`"
  5940.    fi
  5941.  
  5942.    return 1
  5943.  
  5944.  fi # endif installed or not
  5945. }
  5946.  
  5947.  
  5948. pkg_remove(){                     # remove pkg ($1) and its leftover deps FUNC_LIST
  5949.  
  5950.  # quit if no valid options
  5951.  [ ! "$1" -o "$1" = "-" ] && print_usage remove && exit 1
  5952.  
  5953.  local PKGNAME
  5954.  local PKGNAME_ONLY
  5955.  local PKGFILE
  5956.  local pkg_ext
  5957.  
  5958.  # get pkg extension
  5959.  pkg_ext=`get_pkg_ext "$1"`
  5960.  
  5961.  #get pkg name with version, but no extension or path
  5962.  PKGNAME="$(basename "$1" .$pkg_ext)"
  5963.  PKGNAME=`get_pkg_name "$PKGNAME"`
  5964.  
  5965.  # get pkg name only .. without versions or suffix
  5966.  PKGNAME_ONLY="`get_pkg_name_only "$PKGNAME"`"
  5967.  
  5968.  if [ "`is_installed_pkg "$PKGNAME"`" = false ]; then
  5969.    echo "Package '$1' not installed."
  5970.    return 1
  5971.  fi
  5972.  
  5973.  pkg_uninstall "$PKGNAME"
  5974.  
  5975.  # now we will remove any left over dependencies
  5976.  
  5977.  file="`find $TMPDIR -iname "${1}*_dep_list"`"
  5978.  
  5979.  if [ -f "$file" ]; then
  5980.  
  5981.    # if we have a deps list file, go over it three times, uninstalling all deps
  5982.    # that have no packages that depend on them ... fugly solution, but works ok..
  5983.  
  5984.    for x in 1 2 3 4
  5985.    do
  5986.      cat "$file" | tr ' ' '\n' | while read user_installed_dep
  5987.      do
  5988.        [ "$user_installed_dep" = '' ] && continue
  5989.        ASK=false pkg_uninstall $user_installed_dep &>/dev/null && \
  5990.          echo -e "${green}Uninstalled:${endcolour} $(get_pkg_name $user_installed_dep)"
  5991.      done
  5992.    done
  5993.  
  5994.  fi
  5995.  
  5996.  return 0
  5997. }
  5998.  
  5999.  
  6000. clean_pkgs(){                     # delete downloaded pkg files of installed pkgs FUNCLIST
  6001.  local pkg_to_rm
  6002.  [ "$ASK" = true ] && ASKOPT='--ask'
  6003.  # list all (matching) installed pkgs
  6004.  list_installed_pkgs | while read line;
  6005.  do
  6006.    # get all pkgs except the combined (user-created) pkgs
  6007.    pkg_to_rm="`list_downloaded_pkgs $line | grep -v $CP_SUFFIX`"
  6008.    # if it has a downloaded pkg in $WORKDIR, (offer to) delete it
  6009.    [ "$pkg_to_rm" != '' ] && rm -v "${WORKDIR}/$pkg_to_rm";
  6010.  done
  6011. }
  6012.  
  6013.  
  6014. # dependency funcs
  6015.  
  6016. get_deps_entry(){                 # $1 is PKGNAME, returns dep entry from repo db
  6017.  [ "$1" = '' -o "$1" = "-" ] && print_usage list-deps && exit 1
  6018.  
  6019.  # get current $REPOFILE, $DEPSEARCH
  6020.  #. ${PKGRC}
  6021.  
  6022.  local PKGNAME="$1"
  6023.  local pkg_ext=''
  6024.  local repo_files=''
  6025.  local deps_list=''
  6026.  local deps=''
  6027.  local repo_of_pkg=''
  6028.  local repo_file_of_pkg=''
  6029.  
  6030.  # get pkg extension
  6031.  pkg_ext=`get_pkg_ext "$1"`
  6032.  
  6033.  
  6034.  # get repo file to search from RC file
  6035.  repo_file_of_pkg="$REPOFILE"
  6036.  
  6037.  # if searching dependencies in all repos
  6038.  if [ "$DEPSEARCH" = "list_all_pkg_names" ]; then
  6039.  
  6040.    # get the repo that $PKGNAME lives in
  6041.    repo_of_pkg=`which_repo "$PKGNAME" | cut -f2 -d' ' | head -1`
  6042.    # then get the repo file for that repo.. that is where this pkg lists its deps
  6043.    repo_file_of_pkg=`grep -m1 ^"$repo_of_pkg" ~/.pkg/sources-all | cut -f3 -d'|'`
  6044.  
  6045.  fi
  6046.  
  6047.  # add the full path to the repo file
  6048.  repo_file_of_pkg="$REPO_DB_FILE_DIR/$repo_file_of_pkg"
  6049.  
  6050.  # search for deps entry in repo file.. look for |pkgname.ext|
  6051.  deps_list="`LANG=C grep -m1 "|${PKGNAME}.$pkg_ext|" $repo_file_of_pkg 2>/dev/null | cut -f9 -d'|' | grep '+' | sed -e "s/:any//g" -e "s/&[geql][eqt][0-9a-z._-]*//g" | grep -vE '/ge|/le'`"
  6052.  
  6053.  # try again if needed.. look for ^pkgname|
  6054.  [ "$deps_list" = "" -a "$PKGNAME" != '' ] && deps_list="`LANG=C grep -m1 "^${PKGNAME}|" $repo_file_of_pkg 2>/dev/null | cut -f9 -d'|' | grep '+' | sed -e "s/:any//g" -e "s/&[geql][eqt][0-9a-z._-]*//g" | grep -v '/ge'`"
  6055.  
  6056.  # try again if needed.. look for |pkgname|
  6057.  [ "$deps_list" = "" -a "$PKGNAME" != '' ] && deps_list="`LANG=C grep -m1 "|${PKGNAME}|" $repo_file_of_pkg 2>/dev/null | cut -f9 -d'|' | grep '+' | sed -e "s/:any//g" -e "s/&[geql][eqt][0-9a-z._-]*//g" | grep -vE '/ge|/le'`"
  6058.  
  6059.  # try again, look for dep-*
  6060.  [ "$deps_list" = "" -a "$PKGNAME" != '' ] && deps_list="`LANG=C grep -m1 "^${PKGNAME}-" $repo_file_of_pkg 2>/dev/null | cut -f9 -d'|' | grep '+' | sed -e "s/:any//g" -e "s/&[geql][eqt][0-9a-z._-]*//g" | grep -vE '/ge|/le'`"
  6061.  
  6062.  # try again if needed.. with underscore.. dep_*
  6063.  [ "$deps_list" = "" -a "$PKGNAME" != '' ] && deps_list="`LANG=C grep -m1 "^${PKGNAME}_" $repo_file_of_pkg 2>/dev/null | cut -f9 -d'|' | grep '+' | sed -e "s/:any//g" -e "s/&[geql][eqt][0-9a-z._-]*//g" | grep -vE '/ge|/le'`"
  6064.  
  6065.  # if no deps, exit with error
  6066.  [ "$deps_list" != "" ] && echo "$deps_list"
  6067. }
  6068. list_all_installed_pkgs_old(){        # list inc builtins if HIDE_BUILTINS=false
  6069.  # reset list of installed pkgs
  6070.  echo -n '' > $TMPDIR/installed_pkgs
  6071.  
  6072.  #if [ "${HIDE_INSTALLED}" = true -a "$FORCE" = false ]; then
  6073.  #  # add user installed pkgs to list of pkgs to remove from final output
  6074.    cut -f2 -d'|' "$USER_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6075.  #fi
  6076.  
  6077.  #if [ "${HIDE_BUILTINS}" = true ]; then
  6078.  #  # add builtins to list of pkgs to remove from final output
  6079.    cut -f2 -d'|' "$WOOF_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6080.  #fi
  6081.  
  6082.  #if [ -f "$DEVX_INST_PKGS_FILE"  ]; then
  6083.  #  # add devx pkgs to list of pkgs to remove from final output
  6084.    cut -f2 -d'|' "$DEVX_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6085.  #fi
  6086.  
  6087.  #if [ -f "$LAYER_INST_PKGS_FILE" ]; then
  6088.  #  # add layers list of pkgs to remove from final output
  6089.    cut -f2 -d'|' "$LAYER_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6090.  #fi
  6091.  
  6092.  if [ -f $TMPDIR/installed_pkgs -a ! -z $TMPDIR/installed_pkgs ]; then
  6093.    sort -u $TMPDIR/installed_pkgs | grep -v ^$ > $TMPDIR/installed_pkgs__sorted
  6094.    mv $TMPDIR/installed_pkgs__sorted $TMPDIR/installed_pkgs
  6095.  fi
  6096. }
  6097.  
  6098. list_all_installed_pkgs(){        # list inc builtins if HIDE_BUILTINS=false
  6099.  # reset list of installed pkgs
  6100.  echo -n '' > $TMPDIR/installed_pkgs
  6101.  
  6102.  if [ "${HIDE_INSTALLED}" != true -o -z "${HIDE_INSTALLED}" ] && [ "$FORCE" != false ]; then
  6103.  #  # add user installed pkgs to list of pkgs to remove from final output
  6104.    cut -f2 -d'|' "$USER_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6105.  fi
  6106.  
  6107.  if [ "${HIDE_BUILTINS}" != true -o -z "${HIDE_INSTALLED}" ]; then
  6108.  #  # add builtins to list of pkgs to remove from final output
  6109.    cut -f2 -d'|' "$WOOF_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6110.  fi
  6111.  
  6112.  if [ -f "$DEVX_INST_PKGS_FILE"  ]; then
  6113.  #  # add devx pkgs to list of pkgs to remove from final output
  6114.    cut -f2 -d'|' "$DEVX_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6115.  fi
  6116.  
  6117.  if [ -f "$LAYER_INST_PKGS_FILE" ]; then
  6118.  #  # add layers list of pkgs to remove from final output
  6119.    cut -f2 -d'|' "$LAYER_INST_PKGS_FILE" >> $TMPDIR/installed_pkgs
  6120.  fi
  6121.  
  6122.  if [ -f $TMPDIR/installed_pkgs -a ! -z $TMPDIR/installed_pkgs ]; then
  6123.    sort -u $TMPDIR/installed_pkgs | grep -v ^$ > $TMPDIR/installed_pkgs__sorted
  6124.    mv $TMPDIR/installed_pkgs__sorted $TMPDIR/installed_pkgs
  6125.  fi
  6126.  ln -s $TMPDIR/installed_pkgs $TMPDIR/installed_pkgs_finished
  6127. }
  6128.  
  6129.  
  6130. has_deps(){                       # return true if $1 has deps, else false
  6131.  [ "$1" = '' -o "$1" = "-" -o ! "$1" ] && echo false
  6132.  [ "`get_deps_entry $1`" != '' ] && echo true || echo false
  6133. }
  6134.  
  6135.  
  6136. list_deps(){                      # list all deps of PKG ($1), space separated on one line FUNCLIST
  6137.  list_deps_count=$(( list_deps_count + 1 ))
  6138.  [ "$1" = '' -o "$1" = "-" ] && print_usage list-deps && exit 1
  6139.  
  6140.  echo true > /tmp/pkg/list_deps_busy
  6141.  
  6142.  # create list of installed pkgs ($TMPDIR/installed_pkgs) for later
  6143.  [ ! -f $TMPDIR/installed_pkgs ] && list_all_installed_pkgs
  6144.  
  6145.  # get current $REPOFILE, $DEPSEARCH
  6146.  #. ${PKGRC}
  6147.  
  6148.  local PKGNAME=''
  6149.  local PKGNAME_ONLY=''
  6150.  local pkg_ext=''
  6151.  local repo_files=''
  6152.  local deps_list=''
  6153.  local deps=''
  6154.  local repo_of_pkg=''
  6155.  local repo_file_of_pkg=''
  6156.  dep_id=$((dep_id +1))
  6157.  local ldepID=dep_id
  6158.  # get pkg extension
  6159.  pkg_ext=`get_pkg_ext "$1"`
  6160.  
  6161.  #pkg name only ,no path, no extension
  6162.  PKGNAME="$(LANG=C basename "$1" .$pkg_ext)"
  6163.  PKGNAME=`get_pkg_name "$PKGNAME"`
  6164.  PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  6165.  
  6166.  blacklisted_pkgs_list="$(echo $PKG_NAME_IGNORE | sed -e 's/ /|/g')"
  6167.  
  6168.  # get the deps of the pkg, note, in the repo deps are NOT listed by proper pkg names, .. they are +dbus,+glib,+SDL
  6169.  
  6170.  # search for deps entry in repo file.. look for |pkgname.ext|
  6171.  deps_list="`LANG=C get_deps_entry "$PKGNAME"`"
  6172.  
  6173.  # if no deps, exit with error
  6174.  [ "$deps_list" = "" ] && rm /tmp/pkg/list_deps_busy 2>/dev/null && return 1
  6175.  
  6176.  # remove the '+' from the start of each dep
  6177.  deps="${deps_list/+/}" # remove first + at start of line
  6178.  deps="${deps//,+/ }"   # remove others .. DEPS will now be just 'dep1 dep2 dep3'
  6179.  deps="${deps//  / }"   # remove double spaces
  6180.  
  6181.  if [ "$deps" != '' -a "$deps" != ' ' ]; then
  6182.  
  6183.    # put all deps of pkg in a tmp file
  6184.    echo "$deps" | tr ' ' '\n' | tr ',' '\n' | sort -u > $TMPDIR/all_deps_${ldepID}_0
  6185.    #Wait for installed_packages list to be created in list_all_installed_pkgs.
  6186.    while [ ! -f "${TMPDIR}/installed_pkgs_finished" ];do #TODO: s243a: consider adding readlink here
  6187.      echo -n '.'
  6188.      sleep 0.1
  6189.    done
  6190.    # remove any deps in installed pkgs list from all deps.. to leave only missing deps
  6191.    comm -23 ${TMPDIR}/all_deps_${ldepID}_0 ${TMPDIR}/installed_pkgs | sort -u | grep -v ^$ > ${TMPDIR}/all_deps_${ldepID}_1
  6192.  
  6193.    # remove all blacklisted packages from the list
  6194.    grep -vE "'$blacklisted_pkgs_list'" ${TMPDIR}/all_deps_${ldepID}_1 2>/dev/null | sort -u > ${TMPDIR}/all_deps_${ldepID}_1_without_blacklisted_packages
  6195.    mv ${TMPDIR}/all_deps_${ldepID}_1_without_blacklisted_packages ${TMPDIR}/all_deps_${ldepID}_1
  6196.  
  6197.    rm -f ${TMPDIR}/DEP_DONE 2>/dev/null
  6198.  
  6199.    if [ -f $TMPDIR/all_deps_${ldepID}_1 -a ! -z $TMPDIR/all_deps_${ldepID}_1 ]; then
  6200.      # recursive search deps of deps
  6201.      for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  6202.      do
  6203.        deps_list_file="${TMPDIR}/all_deps_${ldepID}_${i}"
  6204.  
  6205.  
  6206.        if [ -f $deps_list_file ]; then
  6207.  
  6208.          # remove all blacklisted packages from the list
  6209.          grep -vE "'$blacklisted_pkgs_list'" "$deps_list_file" > ${TMPDIR}/deps_list_file_without_blacklisted_pkgs
  6210.          mv ${TMPDIR}/deps_list_file_without_blacklisted_pkgs  $deps_list_file
  6211.  
  6212.          # remove done packages from the list
  6213.          if [ -f ${TMPDIR}/DEP_DONE ]; then
  6214.            deps_done_list="$(cat ${TMPDIR}/DEP_DONE | tr '\n' '|' | grep -v ^$)"
  6215.            grep -vE "'$deps_done_list'" $deps_list_file > ${TMPDIR}/deps_list_file_without_completed_pkgs
  6216.            mv ${TMPDIR}/deps_list_file_without_completed_pkgs $deps_list_file
  6217.          fi
  6218.  
  6219.          next_deps_list_file="${TMPDIR}/all_deps_${ldepID}_$(($i + 1))"
  6220.  
  6221.          # for each dep in $deps, get their deps too
  6222.          for subdep in `sort -u $deps_list_file | grep -v ^$`
  6223.          do
  6224.            [ "$subdep" = '' -o "$subdep" = ' ' ] && continue
  6225.  
  6226.            local subdeps_entry=''
  6227.            local subdeps=''
  6228.            local subdeps_list=''
  6229.            local is_builtin
  6230.            local is_in_devx
  6231.  
  6232.            local already_done=`grep -m1 "^${subdep}\$" ${TMPDIR}/DEP_DONE 2>/dev/null`
  6233.            [ "$already_done" != '' ] && continue
  6234.  
  6235.            if [ "$HIDE_BUILTINS" = true ] || [ -z "$HIDE_BUILTINS" ]; then
  6236.              is_builtin=`is_builtin_pkg "$subdep"`
  6237.              [ "$is_builtin" = true ] && continue
  6238.              is_in_devx=`is_devx_pkg "$subdep"`
  6239.              [ "$is_in_devx" = true ] && continue
  6240.            fi
  6241.  
  6242.            subdeps_entry="`get_deps_entry "$subdep"`"
  6243.            subdeps="${subdeps_entry/+/}" # remove first + at start of line
  6244.            subdeps="${subdeps//,+/ }"    # remove others .. DEPS will now be just 'dep1 dep2 dep3'
  6245.            subdeps="${subdeps//  / }"    # remove double spaces
  6246.  
  6247.            if [ "$subdeps" != '' ] && [ "$subdeps" != ' ' ]; then
  6248.              # remove everything after the + (if the + is followed by alphanumeric chars), then add to tmp files
  6249.              echo "$subdep" >> ${TMPDIR}/DEP_DONE
  6250.  
  6251.              # create the next deps list file to parse, containing the deps of this $subdep
  6252.              subdeps_list="`echo "${subdeps}" | tr ' ' '\n' | grep -v ^$ | sed -e 's/++/+++/g' -e 's/+[a-z0-9].*//g' | sort -u`"
  6253.              echo "$subdeps_list" >> $next_deps_list_file
  6254.            fi
  6255.          done
  6256.        fi
  6257.      done
  6258.  
  6259.      # add all deps together, sorted, duplicated removed
  6260.      sort -u ${TMPDIR}/all_deps_${ldepID}_* | grep -v ^$ > ${TMPDIR}/all_deps_${ldepID}_sorted
  6261.      mv ${TMPDIR}/all_deps_${ldepID}_sorted ${TMPDIR}/all_deps_${ldepID}
  6262.  
  6263.    fi
  6264.  
  6265.  fi
  6266.  
  6267.  # remove any deps in installed pkgs list from all deps.. to leave only missing deps
  6268.  if [ -f ${TMPDIR}/installed_pkgs -a ! -z  ${TMPDIR}/installed_pkgs ]; then
  6269.    comm -23 ${TMPDIR}/all_deps_${ldepID} ${TMPDIR}/installed_pkgs | sort -u | grep -v ^$ > ${TMPDIR}/missing_deps
  6270.  fi
  6271.  
  6272.  # remove all blacklisted packages from the list
  6273.  grep -vE "'$blacklisted_pkgs_list'" ${TMPDIR}/missing_deps 2>/dev/null | sort -u > ${TMPDIR}/missing_deps_without_blacklisted_pkgs
  6274.  mv ${TMPDIR}/missing_deps_without_blacklisted_pkgs ${TMPDIR}/missing_deps
  6275.  
  6276.  [ ! -f ${TMPDIR}/missing_deps -o -z ${TMPDIR}/missing_deps ] && rm /tmp/pkg/list_deps_busy 2>/dev/null && return 1
  6277.  [ ! -f ${TMPDIR}/missing_deps ] && return 1
  6278.  
  6279.  # get fixed deps list
  6280.  deps="`LANG=C sort -u ${TMPDIR}/missing_deps 2>/dev/null | tr ' ' '\n' | while read dep
  6281.  do
  6282.    echo $(get_pkg_name_only "$dep")
  6283.  done`"
  6284.  
  6285.  [ "$deps" != "" ] && echo "$deps" | sed -e 's/^ //g' | tr ' ' '\n' | sort -u | tr '\n' ' '
  6286.  
  6287.  # clean up
  6288.  if [ $list_deps_count -eq 1 ]; then
  6289.  rm "${TMPDIR}/installed_pkg"* 2>/dev/null
  6290.  fi
  6291.  rm "${TMPDIR}/missing_dep"*  "${TMPDIR}/all_dep"* "${TMPDIR}/DEP_DONE" "/tmp/pkg/list_deps_busy" 2>/dev/null
  6292.  
  6293. }
  6294.  
  6295.  
  6296. find_deps(){                      # given a package name ($1), makes 2 lists: DEPS_INSTALLED and DEPS_MISSING FUNCLIST
  6297.  
  6298.  # This function takes 1 argument: PKGNAME
  6299.  #
  6300.  # PKGNAME should be the name of a package in one of your repos,
  6301.  # such as 'vlc' or 'hardinfo'.
  6302.  #
  6303.  # This function creates 2 files: $TMPDIR/deps_missing and $TMPDIR/deps_installed.
  6304.  # Each file is list the dependencies for PKGNAME, each dependency on a new line.
  6305.  # get_deps() can then go through those files and download/install the missing deps.
  6306.  #
  6307.  # If --force was given, all deps will be treated as missing.
  6308.  
  6309.  # get current repo ($REPOFILE)
  6310.  . ${PKGRC}
  6311.  
  6312.  local PKGNAME=''            # the pkg given ($1), may be full name, or generic name (no version), etc
  6313.  local PKGNAME_ONLY=''       # the pkg given ($1), without version (vlc,htop,etc)
  6314.  local pkg_ext=''            # the pkg extension, blank if not a valid extension or none given
  6315.  local repo_files=''         # the list of repo files to check, may be current only or all
  6316.  local deps_list=''          # deps of PKGNAME in comma-delimited format: dep1,dep2,dep3
  6317.  local deps_on_new_lines=''  # as above, but each dep on a new line
  6318.  local deps_missing=''       # comma separated list of missing deps
  6319.  local deps_installed=''     # comma separated list of deps already installed
  6320.  local dep=''                # dep name scraped from deps_list, usually the same as dep_name_only
  6321.  local dep_match=''          # used to find matches in the repo for $dep
  6322.  local dep_name_only=''      # short (generic) pkg name, no version (vlc,htop,etc)
  6323.  local dep_full_name=''      # pkg name with version (vlc-2.3.3-i686_s700, etc)
  6324.  local loading_indicator     # blank if only a few deps to parse, or .
  6325.  dep_id=$((dep_id +1))
  6326.  local ldepID=dep_id
  6327.  
  6328.  # get pkg extension
  6329.  pkg_ext=`get_pkg_ext "$1"`
  6330.  
  6331.  # pkg name with version, but no path, no extension
  6332.  PKGNAME="$(basename "$1" .$pkg_ext)"
  6333.  
  6334.  # we can't rely on the user input, try to get the right pkg names
  6335.  PKGNAME=`get_pkg_name "$PKGNAME"`       # vlc -> 'vlc-2.3-blah_etc
  6336.  PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`   # vlc-2.3-blah_etc -> vlc
  6337.  
  6338.  # if PKGNAME still empty, we cant find its deps, move on
  6339.  [ -z "$PKGNAME" ] && return 1
  6340.  
  6341.  # didn't exit yet, so remove the old tmp dirs
  6342.  rm $TMPDIR/deps_installed &>/dev/null
  6343.  rm $TMPDIR/deps_missing   &>/dev/null
  6344.  rm $TMPDIR/deps_missing1  &>/dev/null
  6345.  
  6346.  # loop through all repo files, or current repo only, depending on PKGSEARCH in RC file
  6347.  # add the full path to the file(s) while we are getting the list of repo files
  6348.  [ "$DEPSEARCH" = "list_all_pkg_names" ] && repo_files="`repo_file_list | sed -e "s|^|$PKGS_DIR/|g" | tr '\n' ' '`" || repo_files="$REPO_DB_FILE_DIR/$REPOFILE"
  6349.  
  6350.  # get the list of deps from the repo entry of this pkg
  6351.  deps_list="`LANG=C list_deps "$PKGNAME"`"
  6352.  
  6353.  # remove builtins from list unless HIDE_BUILTINS=false
  6354.  if [ "${HIDE_BUILTINS}" = true ]; then
  6355.    echo "$deps_list" | tr ' ' '\n'  | grep -v ^$ > ${TMPDIR}/all_deps_${ldepID}
  6356.    # add woof pkgs to list of pkgs to remove from final output
  6357.    cut -f2 -d'|' "$WOOF_INST_PKGS_FILE" | grep -v ^$ | sort | uniq >> $TMPDIR/installed_pkgs
  6358.    # remove any deps in installed pkgs list from all deps.. to leave only missing deps
  6359.  
  6360.    sort -u ${TMPDIR}/all_deps_${ldepID} > ${TMPDIR}/all_deps_${ldepID}_sorted
  6361.    mv ${TMPDIR}/all_deps_${ldepID}_sorted ${TMPDIR}/all_deps_${ldepID}
  6362.  
  6363.    sort -u ${TMPDIR}/installed_pkgs > ${TMPDIR}/installed_pkgs_sorted
  6364.    mv ${TMPDIR}/installed_pkgs_sorted ${TMPDIR}/installed_pkgs
  6365.  
  6366.    comm -23 ${TMPDIR}/all_deps_${ldepID} ${TMPDIR}/installed_pkgs | grep -v ^$ > ${TMPDIR}/missing_deps
  6367.    deps_list="`cat "${TMPDIR}/missing_deps" 2>/dev/null | grep -v ^$ | sort | tr '\n' ' ' `"
  6368.  fi
  6369.  
  6370.  # so now, $deps_list='dep1 dep2 dep3'
  6371.  
  6372.  # now get the deps list with each dep on its own line
  6373.  deps_on_new_lines="`echo "$deps_list" | tr ' ' '\n'`"
  6374.  
  6375.  # exit if no deps to parse
  6376.  [ "$deps_on_new_lines" = '' ] && return 1
  6377.  
  6378.  # set a loading bar (appending dots....) if we have numerous deps to search
  6379.  [ `echo "$deps_on_new_lines" | wc -l` -gt 4 ] && loading_indicator='.' || loading_indicator=''
  6380.  
  6381.  # now.. we go through ALL deps listed, and create a list for installed or not .. $dep will be 'gtkdialog3', for example
  6382.  echo "$deps_on_new_lines" | grep -v ^$ | while read dep
  6383.  do
  6384.    # append dots as we go, like a loading spinner
  6385.    [ "$loading_indicator" = '.' ] && echo -n "$loading_indicator"
  6386.  
  6387.    # $dep is currently
  6388.    dep_full_name=`get_pkg_name "$dep"`     #vlc-2.3.3-i586_s700
  6389.    dep_name_only=`get_pkg_name_only "$dep"`  #vlc
  6390.  
  6391.    # skip these non pkgs
  6392.    [ "`is_repo_pkg "$dep_name_only"`" = false ] && continue
  6393.  
  6394.    # if we added this dep to the list of PKGs already done, skip it
  6395.    [ "`grep -m1 "^$dep_name_only\$" $TMPDIR/PKGSDONE 2>/dev/null`" != '' ] && continue
  6396.  
  6397.    # lets check if the $dep is installed or not
  6398.  
  6399.    # if dep was found in a repo and not installed, add to missing deps list
  6400.    if [ "$FORCE"  = true -o "`is_installed_pkg "$dep_full_name"`" = false ]; then
  6401.      grep -m1 "^$dep_name_only\$" $TMPDIR/deps_missing 2>/dev/null || echo "$dep_name_only" | grep -v "^$PKGNAME" >> $TMPDIR/deps_missing
  6402.  
  6403.    else
  6404.      # else if the dep is already installed, add to installed deps list
  6405.      grep -m1 "^$dep_name_only\$" $TMPDIR/deps_installed 2>/dev/null || echo "$dep_name_only" | grep -v "^$PKGNAME" >> $TMPDIR/deps_installed
  6406.    fi
  6407.  
  6408.    # clean up deps_installed, remove duplicates, etc
  6409.    if [ -f $TMPDIR/deps_installed ]; then
  6410.      cat $TMPDIR/deps_installed 2>/dev/null| sort | uniq >> $TMPDIR/deps_installed1
  6411.      [ -f $TMPDIR/deps_installed1 ] && mv $TMPDIR/deps_installed1 $TMPDIR/deps_installed 2>/dev/null
  6412.    fi
  6413.  
  6414.  done # end of while $dep
  6415.  
  6416.  # make a comma separated list from newlines
  6417.  deps_installed="`cat $TMPDIR/deps_installed 2>/dev/null | sort | uniq | tr '\n' ',' | sed -e 's/,,/,/' -e 's/,$//' -e 's/^,//'`"
  6418.  deps_missing="`cat $TMPDIR/deps_missing 2>/dev/null | sort | uniq | tr '\n' ',' | sed -e 's/,,/,/' -e 's/,$//' -e 's/^,//'`"
  6419.  
  6420.  #120213, fixed, force all deps to be in the download list, if --force was given
  6421.  if [ "$FORCE" = true ]; then
  6422.    # dont skip installed deps (except builtins.. handled elsewhere)
  6423.    if [ "$deps_installed" != "" ]; then
  6424.      deps_missing="${deps_installed},${deps_missing}"
  6425.      deps_missing="`echo "${deps_missing//,,/}" | sed -e 's/,$//' -e 's/^,//'`"
  6426.    fi
  6427.  fi
  6428.  
  6429.  # later, get_deps() will use $DEPS_MISSING and $DEPS_INSTALLED
  6430.  DEPS_MISSING="$deps_missing"
  6431.  DEPS_INSTALLED="$deps_installed"
  6432.  
  6433.  # end appending dots... msg, by printing a new line
  6434.  [ "$loading_indicator" != '' ] && echo
  6435.  
  6436. }
  6437.  
  6438.  
  6439. get_deps(){                       # find, get and install the deps of pkgname ($1) FUNCLIST
  6440.  
  6441.  [ -z "$1" -o ! "$1" -o "$1" = "-" ] && print_usage deps && exit 1
  6442.  
  6443.  . ${PKGRC} #150813
  6444.  
  6445.  local EX
  6446.  local PKGNAME
  6447.  local DEPCONFIRM
  6448.  
  6449.  # get pkg extension
  6450.  local EX=`get_pkg_ext "$1"`
  6451.  
  6452.  # get pkg name with version, but no path, no extension
  6453.  local PKGNAME="$(LANG=C basename "$1" .$EX)"
  6454.  
  6455.  # don't rely on user input, get the name w version from repos
  6456.  local PKGNAME=`get_pkg_name "$PKGNAME" 2>/dev/null`
  6457.  local PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME" 2>/dev/null`
  6458.  
  6459.  local pkg_is_builtin=`is_builtin_pkg "$PKGNAME_ONLY" 2>/dev/null`
  6460.  local pkg_already_done=`LANG=C grep -m1 "^$PKGNAME_ONLY\$" $TMPDIR/PKGSDONE 2>/dev/null`
  6461.  
  6462.  local pkg_is_blacklisted=`is_blacklisted_pkg "$PKG_NAME_ONLY" 2>/dev/null`
  6463.  
  6464.  # if pkg is builtin, skip it, we dont need to get its dependencies
  6465.  [ "$pkg_is_builtin" = true -a "$HIDE_BUILTINS" = true ] && continue
  6466.  
  6467.  # if pkg is blacklisted, also skip it
  6468.  [ "$pkg_is_blacklisted" = true ] && continue
  6469.  
  6470.  # skip if already processed, it should be listed in $TMPDIR/PKGSDONE
  6471.  [ "$pkg_already_done" != "" -a "$FORCE" = false ] && continue
  6472.  
  6473.  echo -n "Resolving dependencies.." # find deps will append ... as it goes
  6474.  
  6475.  # wait until list_deps() is finished (if it's running at all...)
  6476.  while [ -f /tmp/pkg/list_deps_busy ];do
  6477.    echo -n '.'
  6478.    sleep 0.75
  6479.  done
  6480.  
  6481.  echo
  6482.  
  6483.  # if list_deps() created a file listing the deps, get it from the file created, else, run list deps to be sure
  6484.  [ -f ${TMPDIR}/${pkg_name}_dep_list ] && DEPS_MISSING="`cat ${TMPDIR}/${pkg_name}_dep_list 2>/dev/null`" || DEPS_MISSING="`list_deps "$PKGNAME" 2>/dev/null`"
  6485.  
  6486.  # if we have missing deps, or are downloading them all regardless (INSTALLDEPS=false), or using --force
  6487.  if [ "$DEPS_MISSING" != "" -o "${NO_INSTALL}" = true -o "$FORCE" = true ]; then
  6488.  
  6489.    # ask to download (and maybe install) the deps
  6490.    DEPCONFIRM=y
  6491.    if [ "$ASK" = true ]; then
  6492.      echo "Missing deps: $DEPS_MISSING"
  6493.      echo -n "Download the missing dependencies$QTAG:  "
  6494.      read -n 1 DEPCONFIRM </dev/tty
  6495.      echo
  6496.      # skip if user chose --ask and didn't answer 'y'
  6497.      [ "$DEPCONFIRM" != "y" ] && return
  6498.    fi
  6499.  
  6500.    # if user answered yes, we will now download (and maybe install) the deps
  6501.    if [ "$DEPCONFIRM" = "y" -o "$FORCE" = true ]; then
  6502.  
  6503.      # only ask once
  6504.      ASK=false
  6505.  
  6506.      # make a list of the deps, each on a new line for each dep/newline
  6507.      WARNLIBS=''; SEP='';
  6508.  
  6509.      # if more than one missing dep, set separator to a comma
  6510.      [ ! -z "$DEPS_MISSING" -a "`echo "$DEPS_MISSING" | wc -l`" != "1" -a "`echo "$DEPS_MISSING" | wc -l`" != "0" ] && SEP=','
  6511.  
  6512.      # clean up our deps list, and make space separated only (no commas)
  6513.      DEPS_MISSING="`LANG=C echo "${DEPS_MISSING//,/ }" | grep -v '^,' | grep -v "^\$"`"
  6514.  
  6515.      # show deps info, if any available
  6516.      [ "${DEPS_MISSING}" != ""  ]   && echo "Dependencies to get: ${DEPS_MISSING//,/, }"
  6517.      [ "$FORCE" = false -a "${DEPS_INSTALLED}" != "" ] && echo "Dependencies installed: `LANG=C grep -v "^\$" $TMPDIR/deps_installed 2>/dev/null | head -1`"
  6518.  
  6519.      # for each missing dep  (or simply for each dep, if $FORCE is true)
  6520.      for DEP in $DEPS_MISSING
  6521.      do
  6522.  
  6523.        [ "$DEP" = "" -o "$DEP" = "${PKGNAME_ONLY}" -o "$DEP" = "${PKGNAME}" ] && continue #skip if the dep is the main package
  6524.  
  6525.        local DEPNAME=`get_pkg_name "$DEP" 2>/dev/null`
  6526.        local DEPNAME_ONLY=`get_pkg_name_only "$DEPNAME" 2>/dev/null`
  6527.        local DEPFILE=''
  6528.  
  6529.        # skip if already processed, it should be listed in $TMPDIR/PKGSDONE
  6530.        local dep_already_done=`LANG=C grep -m1 "^$DEPNAME_ONLY\$" $TMPDIR/PKGSDONE 2>/dev/null`
  6531.        [ "$dep_already_done" != "" -a "$FORCE" = false ] && continue
  6532.  
  6533.        local dep_is_blacklisted=`is_blacklisted_pkg "$DEPNAME_ONLY" 2>/dev/null`
  6534.        local dep_is_builtin=`is_builtin_pkg "$DEPNAME_ONLY" 2>/dev/null`
  6535.        local dep_is_usr_pkg=`is_usr_pkg "$DEPNAME_ONLY" 2>/dev/null`
  6536.        local dep_is_in_devx=`is_devx_pkg "$DEPNAME_ONLY" 2>/dev/null`
  6537.  
  6538.        # skip getting pkg if its blacklisted
  6539.        if [ "$dep_is_blacklisted" = true ]; then
  6540.          echo "Skipping $DEPNAME_ONLY (blacklisted).."
  6541.          continue
  6542.        fi
  6543.  
  6544.        # skip getting pkg if its a builtin, unless HIDE_BUILTINS=false
  6545.        if [ "$dep_is_builtin" = true -a "${HIDE_BUILTINS}" = true ]; then
  6546.          echo "Skipping $DEPNAME_ONLY (already built-in).."
  6547.          continue
  6548.        fi
  6549.  
  6550.        # skip getting pkg if its user installed and not using --force
  6551.        if [ "$dep_is_usr_pkg" = true -a "${FORCE}" != true ]; then
  6552.          echo "Skipping $DEPNAME_ONLY (already installed).."
  6553.          continue
  6554.        fi
  6555.  
  6556.        # skip getting pkg if its in the devx, unless HIDE_BUILTINS=false
  6557.        if [ "$dep_is_in_devx" = true -a "${HIDE_BUILTINS}" = true ]; then
  6558.          echo "Skipping $DEPNAME_ONLY (already built-in).."
  6559.          continue
  6560.        fi
  6561.  
  6562.        #DOWNLOAD THE PKG
  6563.        pkg_download "$DEPNAME" 2>/dev/null
  6564.  
  6565.        # skip install unless NO_INSTALL=true
  6566.        if [ "${NO_INSTALL}" = false ]; then
  6567.  
  6568.          # get the actual file we just downloaded to WORKDIR
  6569.          DEPFILE="`find "$WORKDIR" -maxdepth 1 -type f -name "$DEPNAME*" 2>/dev/null`"
  6570.  
  6571.          #INSTALL THE DEP, if it was downloaded
  6572.          [ "`is_local_pkg "$DEPFILE" 2>/dev/null`" = true ] && pkg_install "$DEPFILE" 2>/dev/null
  6573.  
  6574.          # mark the pkg as done!
  6575.          echo "$DEPNAME_ONLY" >> $TMPDIR/PKGSDONE
  6576.  
  6577.        else # if not installing,
  6578.          # skip dep checking of the deps that dont get installed
  6579.          continue
  6580.        fi
  6581.  
  6582.  
  6583.        # we finished with this dep, mark it done,
  6584.        # .. so we can skip it if it appears again (in a recursive dep check loop for example)
  6585.        echo "$DEPNAME_ONLY" >> $TMPDIR/PKGSDONE #260713
  6586.  
  6587.  
  6588.      done #done for DEP in DEPS_MISSING
  6589.  
  6590.    fi #endif DEPCONFIM=y
  6591.  
  6592.  else
  6593.    echo "No missing dependencies."
  6594.  fi # endif DEPS_MISSING != ''
  6595.  
  6596.  # if some deps were missing (listed but not found), print msg
  6597.  [ "$INSTALLDEPS" = true ] && actioned=installed || actioned=downloaded
  6598.  [ "$WARNLIBS" != "" ]     && echo -e "${yellow}Warning:${endcolour} Not $actioned from repo: $WARNLIBS"
  6599.  #exit 0 #110913
  6600. }
  6601.  
  6602.  
  6603. pkg_ldd_msg(){                    # check given package ($1) for missing deps FUNCLIST
  6604.  
  6605.  # exit if no valid usage
  6606.  [ ! "$1" -o "$1" = "-" ] && print_usage deps-check && exit 1
  6607.  
  6608.  . ${PKGRC}
  6609.  
  6610.  local PKGNAME
  6611.  local FNDFILES
  6612.  local LIST
  6613.  local RES
  6614.  local MISSING
  6615.  
  6616.  # get pkg name
  6617.  PKGNAME=`get_pkg_name "$1"`
  6618.  rm $TMPDIR/pkg-$PKGNAME-MISSING.txt &>/dev/null
  6619.  
  6620.  [ "`is_installed_pkg "$PKGNAME"`" = false ] && print_usage deps-check && exit 1
  6621.  
  6622.  echo "Searching for missing dependencies.. Please wait."
  6623.  
  6624.  local list="$PKGNAME `list_deps $PKGNAME`"
  6625.  
  6626.  for pkg_name in $list
  6627.  do
  6628.    local pkg_name_only=`get_pkg_name_only "$pkg_name"`
  6629.    # get the *.files for this pkg
  6630.    FNDFILES="$(find $PACKAGE_FILE_LIST_DIR/ -iname "${pkg_name}.files" 2>/dev/null)"
  6631.    [ ! -f "$FNDFILES" ] && FNDFILES="$(find "$PACKAGE_FILE_LIST_DIR" -iname "${pkg_name}_*.files" 2>/dev/null)"
  6632.    [ ! -f "$FNDFILES" ] && FNDFILES="$(find "$PACKAGE_FILE_LIST_DIR" -iname "${pkg_name}-*.files" 2>/dev/null)"
  6633.    [ ! -f "$FNDFILES" ] && FNDFILES="$(find "PACKAGE_FILE_LIST_DIR" -iname "${pkg_name}*.files"  2>/dev/null)"
  6634.    [ ! -f "$FNDFILES" ] && continue
  6635.  
  6636.    # get list of ldd-able file
  6637.    cat "$FNDFILES" | grep -E '/lib/|/lib64/|/bin/|/games/|/sbin/' > ${TMPDIR}/ldd_file_list_${pkg_name}
  6638.    [ -z ${TMPDIR}/ldd_file_list_${pkg_name} ] && continue
  6639.  
  6640.    #loop through list
  6641.    for file in `cat ${TMPDIR}/ldd_file_list_${pkg_name}`
  6642.    do
  6643.      [ ! -x "$file" ] && continue
  6644.      RES="`ldd $file 2>/dev/null`"
  6645.      MISSING="`echo "$RES" | grep found`"
  6646.      [ "$MISSING" != "" -a "$MISSING" != " " ] && echo "  $file:
  6647.  $MISSING" >> $TMPDIR/${pkg_name}-MISSINGLIBS.txt
  6648.    done
  6649.  
  6650.    #print message
  6651.    if [ -f $TMPDIR/${pkg_name}-MISSINGLIBS.txt ]; then
  6652.      echo -e "${yellow}WARNING${endcolour}: ${magenta}${pkg_name_only}${endcolour} has missing dependencies: "
  6653.      echo -e "`cat $TMPDIR/${pkg_name}-MISSINGLIBS.txt`"
  6654.    else
  6655.      echo -e "${green}OK:${endcolour} ${pkg_name} has no missing dependencies."
  6656.    fi
  6657.    rm -f ${TMPDIR}/ldd_file_list* $TMPDIR/${pkg_name}-MISSINGLIBS.txt 2>/dev/null
  6658.  done
  6659. }
  6660.  
  6661.  
  6662. get_all_deps(){                   # try to install all missing deps FUNCLIST
  6663. echo "Checking $(list_installed_pkgs | wc -l) installed packages for missing dependencies. Please wait..."
  6664. rm $TMPDIR/pkg_get_all_deps 2>/dev/null
  6665.  
  6666. list_installed_pkgs | while read LINE; do
  6667.  DEPLIST="`LANG=C list_deps "$LINE"`"
  6668.  [ "$DEPLIST" != "" -a "$LINE" != "" ] && echo "$LINE|$DEPLIST" >> $TMPDIR/pkg_get_all_deps
  6669. done
  6670.  
  6671. [ ! -f $TMPDIR/pkg_get_all_deps ]   && echo "No missing dependencies." && exit 0
  6672. ASKOPT='';   [ "$ASK" = true ]   && ASKOPT='--ask ';
  6673. FORCEOPT=''; [ "$FORCE" = true ] && FORCEOPT='--force ';
  6674.  
  6675. cat $TMPDIR/pkg_get_all_deps 2>/dev/null | while read LINE
  6676. do
  6677.  [ "$LINE" = "" -o "`echo "$LINE" | grep -v '|'`" = "" ] && continue
  6678.  # get pkg gname from field 1
  6679.  PKGNAME="${LINE%%|*}"
  6680.  DEPS="${LINE##*|}"
  6681.  
  6682.  echo "Checking $PKGNAME..."
  6683.  
  6684.  for DEP in $DEPS;
  6685.  do
  6686.    # try to get the pkg name
  6687.    DEPPKG="`list_all_pkg_names $DEP- | head -1`" || \
  6688.    DEPPKG="`list_all_pkg_names $DEP_ | head -1`" || \
  6689.    DEPPKG="`list_all_pkg_names $DEP  | head -1`"
  6690.    # if dep not in any repos, skip it
  6691.    [ "$DEPPKG" = "" ] && continue
  6692.    # skip if the dep is already installed
  6693.    [ "`is_installed_pkg $DEPPKG`" = true ] && continue
  6694.    # ask to get dep
  6695.    echo -n "Get the missing package: ${DEPPKG}$QTAG:  "
  6696.    echo
  6697.    [ "$ASK" = true ] && read -n 1 CONFIRM </dev/tty || CONFIRM=y
  6698.    [ "$ASK" = true ] && echo -ne "\b\b\n"
  6699.    # if user answered yes, we will now download the pkgs
  6700.    if [ "$CONFIRM" = "y" ]; then
  6701.      NO_INSTALL=false pkg_get "$DEPPKG" # get the missing dep (and its deps)
  6702.    fi
  6703.  done
  6704. done
  6705. rm -f $TMPDIR/pkg_get_all_deps 2>/dev/null
  6706. }
  6707.  
  6708.  
  6709. list_dependents(){                # list user installed pkgs that depend on $1 FUNCLIST
  6710.  
  6711.  local PKGNAME=''
  6712.  local PKGNAME_ONLY=''
  6713.  
  6714.  if [ "$1" = '' ]; then
  6715.    print_usage what-needs
  6716.    exit 1
  6717.  fi
  6718.  
  6719.  # try to get correct pkg names
  6720.  PKGNAME=`get_pkg_name "$1"`
  6721.  PKGNAME_ONLY=`get_pkg_name_only "$1"`
  6722.  
  6723.  # if pkg is not installed, then nothing depends on it
  6724.  if [ "`is_installed_pkg "$PKGNAME"`" = false ]; then
  6725.    echo "Package $1 not installed, nothing depends on it."
  6726.    exit 1
  6727.  fi
  6728.  
  6729.  # list all pkgs (from all repos) with $PKGNAME_ONLY as a dep
  6730.  cut -f1,2,9 -d'|' "$REPO_DB_FILE_DIR"/Packages-* \
  6731.    | sed -e "s/:any//g" -e "s/&[geql][eqt][0-9a-z._-]*//g" 2>/dev/null \
  6732.    | grep -E "+${PKGNAME_ONLY},|+${PKGNAME_ONLY}\|" 2>/dev/null \
  6733.    | cut -f2 -d'|' 2>/dev/null \
  6734.    | grep -v "^\$" \
  6735.    | grep -v "^#" \
  6736.    | grep -v "^$PKGNAME_ONLY\$" \
  6737.    | sort -u \
  6738.    >> ${TMPDIR}/dependents_list
  6739.  
  6740.  if [ "$HIDE_BUILTINS" = false ]; then
  6741.    # list all builtin and devx packages with $PKGNAME_ONLY as a dependency..
  6742.    cut -f1,2,9 -d'|' "$WOOF_INST_PKGS_FILE"  "$DEVX_INST_PKGS_FILE" \
  6743.      "$DEVX_INST_PKGS_FILE" \
  6744.      | grep -E "+${PKGNAME_ONLY},|+${PKGNAME_ONLY}\|" \
  6745.      | cut -f2 -d'|' \
  6746.      | grep -v "^\$" \
  6747.      | grep -v "^#" \
  6748.      | grep -v "^$PKGNAME_ONLY\$" \
  6749.      | sort -u \
  6750.      >> ${TMPDIR}/dependents_list
  6751.  fi
  6752.  
  6753.  # remove duplicates
  6754.  sort -u ${TMPDIR}/dependents_list > ${TMPDIR}/dependents_list__sorted
  6755.  mv ${TMPDIR}/dependents_list__sorted ${TMPDIR}/dependents_list_tmp
  6756.  rm ${TMPDIR}/dependents_list
  6757.  
  6758.  # only keep the user installed pkgs in the list
  6759.  for pkg in `list_installed_pkgs | grep -v ^$PKGNAME`
  6760.  do
  6761.    [ "`grep -m1 "^$(get_pkg_name_only $pkg)\$" ${TMPDIR}/dependents_list_tmp`" != '' ] && echo "$pkg" >> ${TMPDIR}/dependents_list
  6762.  done
  6763.  
  6764.  # print the list if we have one
  6765.  [ -f ${TMPDIR}/dependents_list -a ! -z ${TMPDIR}/dependents_list ] && cat ${TMPDIR}/dependents_list
  6766.  rm ${TMPDIR}/dependents_lis* 2>/dev/null
  6767. }
  6768.  
  6769.  
  6770. # file conversion
  6771.  
  6772. deb2pet(){                        # $1 must be valid deb file or repo pkg FUNCLIST
  6773.  [ ! -f "$1" ] && print_usage deb2pet && exit 1
  6774.  
  6775.  local DEB="${CURDIR}/$(basename "${1}")"
  6776.  local DIRNAME="${CURDIR}"
  6777.  #create file name we work with
  6778.  [ -f "$1" ] && DEB="$1" && DIRNAME="`dirname "$1"`"
  6779.  
  6780.  #keep old file, download it if needed
  6781.  #[ -f "$(basename "$1" .deb)" ] || ASK=$ASK FORCE=$FORCE pkg_download "$(basename "$1" .deb)"
  6782.  
  6783.  # if the deb exists
  6784.  if [ -e "$DEB" ]; then
  6785.    for i in "$DEB" # for each deb given
  6786.    do
  6787.      #remove the extensions
  6788.      #example, will be something like FOLDR=$HOME/Desktop/atari800_3.1.0-2+b2_i386
  6789.      FOLDR="$(echo "$i"|sed 's/\.deb$//')"
  6790.    done
  6791.  
  6792.    #make the new dir, copy the deb file into it, and ci into it
  6793.    mkdir -p "$FOLDR"; cp "$DEB" "$FOLDR"; cd "$FOLDR";
  6794.  
  6795.    #get the new full path and filename
  6796.    DEB="`ls | grep ".deb"`"
  6797.  
  6798.    # extract into current dir and remov ethe copied deb file
  6799.    pkg_unpack "$DEB" 1>/dev/null
  6800.  
  6801.    #this will be something like  PKGNAME=atari800_3.1.0-2+b2_i386
  6802.    PKGNAME="`basename "$DEB" .deb`"
  6803.  
  6804.    #now we package up the stuff into a pet
  6805.    [ -d "${FOLDR}/$PKGNAME" ] && dir2pet "${FOLDR}/$PKGNAME" || dir2pet "$PKGNAME"
  6806.  
  6807.    [ ! -f "$FOLDR.pet" -a ! -f "${CURDIR}/${PKGNAME}.pet" ] && error "$FOLDR.deb NOT converted to PET package!"
  6808.  
  6809.    #clean up
  6810.    rm -rf "$FOLDR"
  6811.  
  6812.  else
  6813.    echo "Package '$(basename $DEB)' not found in '$CURDIR'."
  6814.    return 1
  6815.  fi
  6816. }
  6817.  
  6818.  
  6819. dir2pet(){                        # dir to PET, $1 must be valid dir FUNCLIST
  6820.  
  6821.  # exit if no options
  6822.  [ ! -d "$1" ] && print_usage dir2pet && exit 1
  6823.  
  6824.  DIR="$1"
  6825.  SUFFIX=''
  6826.  
  6827.  [ "$2" != "" ] && SUFFIX="$2"
  6828.  
  6829.  #[ -d "$DIR" ] && cd `dirname "$DIR"`
  6830.  
  6831.  # get pkg name only,
  6832.  DIR="$(basename "${DIR}")"
  6833.  [ "$SUFFIX" != "" ] && cp -R "$DIR" "${DIR}${SUFFIX}" && DIR="${DIR}${SUFFIX}"
  6834.  
  6835.  echo -e "Converting directory ${lightblue}$DIR${endcolour} to .pet package."
  6836.  
  6837.  # we must have a dir with same name as a valid pkg
  6838.  if [ -d "$DIR" ]; then
  6839.    # move it to the build dir, to work on
  6840.    mkdir -p "$CURDIR/build_pkg/"
  6841.    [ ! -e "$CURDIR/build_pkg/$DIR" ] && cp -R "$DIR" "$CURDIR/build_pkg/"
  6842.  
  6843.    ARCHINDEPENDENT='yes'
  6844.    for ONEEXEC in `find $CURDIR/build_pkg/${DIR}/ -maxdepth 6 -type f -perm -o+x`
  6845.    do
  6846.      [ -f $ONEEXEC ] && [ "`file $ONEEXEC | grep ' ELF '`" != "" ] && ARCHINDEPENDENT='no'
  6847.    done
  6848.  
  6849.    # if it's a _DEV pkg.. it can't be ARCHINDEPENDENT
  6850.    case "${BASEPKG}" in *"_DEV"*) ARCHINDEPENDENT='no' ;; esac
  6851.    [ "`find $CURDIR/build_pkg/${DIR}/ -maxdepth 6 -type f -name '*.a' -o -type f -name 'lib*.so*' -o -type f -name '*.la'`" != "" ] && ARCHINDEPENDENT='no'
  6852.    [ "$ARCHINDEPENDENT" = "no" ] && COMPAT=$DISTRO_BINARY_COMPAT V=$DISTRO_COMPAT_VERSION
  6853.  
  6854.        #...borrowed from dir2pet script
  6855.    #w482 directory may already have a pet.specs, reuse it...
  6856.    NAMEONLY=""
  6857.    PUPMENUDESCR=""
  6858.    PUPOFFICIALDEPS=""
  6859.    PUPCATEGORY=""
  6860.    PUPPATH="" #100201
  6861.    ARCHDEPENDENT="yes" #100201
  6862.    DEFREPO="" #100201
  6863.    if [ -f $CURDIR/build_pkg/${DIR}/pet.specs ]; then #160803
  6864.     #new: pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|
  6865.     #optionally on the end: compileddistro|compiledrelease|repo| (fields 11,12,13)
  6866.     PETSPECS="`cat $CURDIR/build_pkg/${DIR}/pet.specs | head -n 1`" #160803
  6867.     while IFS="|" read -r F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 <&3
  6868.     do
  6869.       DB_pkgname="$F1"
  6870.       DB_nameonly="$F2"
  6871.       NAMEONLY="$DB_nameonly"
  6872.       DB_version="$F3"
  6873.       DB_pkgrelease="$F4"
  6874.       DB_category="$F5"
  6875.       PUPCATEGORY="$DB_category"
  6876.       DB_size="$F6"
  6877.       DB_path="$F7"
  6878.       PUPPATH="$DB_path" #100201
  6879.       DB_fullfilename="$F8"
  6880.       DB_dependencies="$F9"
  6881.       PUPOFFICIALDEPS="$DB_dependencies"
  6882.       DB_description="$F10"
  6883.       PUPMENUDESCR="$DB_description"
  6884.       DB_compileddistro="$F11"
  6885.       DB_compiledrelease="$F12"
  6886.       ARCHDEPENDENT="${DB_compileddistro}|${DB_compiledrelease}"
  6887.       DB_repo="$F13"
  6888.       DEFREPO="$DB_repo"
  6889.      done 3< $CURDIR/build_pkg/${DIR}/pet.specs
  6890.    else
  6891.      echo -e "\nCategories:
  6892.  BuildingBlock, Desktop, System, Setup, Utility,
  6893.  Filesystem, Graphic, Document, Business, Personal,
  6894.  Network, Internet, Multimedia, Fun\n"
  6895.      read -p 'Type one of the categories above, then hit ENTER: ' CAT_ENTRY </dev/tty;
  6896.  
  6897.      echo
  6898.      read -p "Add a short description, then hit ENTER: " DESC_ENTRY </dev/tty;
  6899.  
  6900.      echo -e "\nAdd dependencies in this format:  ${bold}+libglib,+ffmpeg${endcolour}\n"
  6901.      read -p 'Enter dependencies (if any), then hit ENTER: ' DEPS_ENTRY </dev/tty;
  6902.  
  6903.      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
  6904.    fi
  6905.  
  6906.    # build .pet.specs
  6907.  
  6908.    BASEPKG="`basename $DIR`"
  6909.    DIRPKG="`dirname $DIR`"
  6910.    [ "$DIRPKG" = "/" ] && DIRPKG=""
  6911.  
  6912.    #difficult task, separate package name from version part...
  6913.    #not perfect, some start with non-numeric version info...
  6914.    [ "$NAMEONLY" = "" ] && NAMEONLY=`get_pkg_name_only "$BASEPKG"`
  6915.  
  6916.    # get pet details from the pre-existing pet.specs (if any): deps, category, descr, version, nameonly, arch, distro version
  6917.    PUPOFFICIALDEPS="$DB_dependencies"
  6918.  
  6919.    TOPCAT="$PUPCATEGORY"
  6920.    [ -z "$TOPCAT" ] && TOPCAT=BuildingBlock
  6921.  
  6922.    PUPMENUDESCR="$DB_description"
  6923.    [ -z "${PUPMENUDESCR}" ] && PUPMENUDESCR="No description provided"
  6924.  
  6925.    VERSION="`get_pkg_version "$BASEPKG"`"
  6926.  
  6927.    # build pet spec
  6928.    if [ ! -f $CURDIR/build_pkg/${DIR}/pet.specs ]; then
  6929.      echo "$BASEPKG|${NAMEONLY}|$VERSION|$DB_pkgrelease|$TOPCAT|$DB_size|$REPO_SUBDIR|${BASEPKG}.pet|$PUPOFFICIALDEPS|$PUPMENUDESCR|$COMPAT|$V||" > $CURDIR/build_pkg/${DIR}/pet.specs
  6930.    fi
  6931.  
  6932.    # delete the slackware package management files
  6933.    #if [ -d "$CURDIR/build_pkg/install" ]; then
  6934.    # rm -r "$CURDIR/build_pkg/install"; rmdir "$CURDIR/build_pkg/install";
  6935.    #fi
  6936.  
  6937.    # delete arch pkg stuff
  6938.    #rm -r "$CURDIR/build_pkg/.INSTALL" &>/dev/null
  6939.    #rm "$CURDIR/build_pkg/.PKGINFO" &>/dev/null
  6940.  
  6941.    # now tar up the folder, ready to make into tar.gz, then into .pet
  6942.    cd $CURDIR/build_pkg/
  6943.  
  6944.    # we need to choose xz or gzip pets
  6945.    arch=`uname -m`
  6946.    if [ "${arch:0:3}" = "arm" ]; then
  6947.      TAREXT="gz"
  6948.    else
  6949.      TAREXT="xz"
  6950.    fi
  6951.  
  6952.    # if in a pre-woof puppy then we dont use xz
  6953.    if [ ! -f "$WOOF_INST_PKGS_FILE" -o ! -f /etc/DISTRO_SPECS ]; then
  6954.      # pre woof format
  6955.      echo "PETMENUDESCR='${DIR}'" >  $CURDIR/build_pkg/${DIR}/${DIR}.pet.specs
  6956.      echo "PETOFFICIALDEPS=''"    >> $CURDIR/build_pkg/${DIR}/${DIR}.pet.specs
  6957.      echo "PETREGISTER='yes'"     >> $CURDIR/build_pkg/${DIR}/${DIR}.pet.specs
  6958.      compression=xz
  6959.      TAREXT="xz"
  6960.    fi
  6961.    tar -c -f ${DIR}.tar ${DIR} 1>/dev/null
  6962.    sync
  6963.    [ "`which xz`" = "" ] && TAREXT=gz
  6964.    case "$TAREXT" in
  6965.      xz) xz -z -9 -e ${DIR}.tar; ;;
  6966.      gz) gzip --best ${DIR}.tar; ;;
  6967.    esac
  6968.  
  6969.    # now get info needed to make a pet file
  6970.    TARBALL="${DIR}.tar.$TAREXT"
  6971.    FULLSIZE="`stat --format=%s ${TARBALL}`"
  6972.    MD5SUM="`md5sum $TARBALL | cut -f 1 -d ' '`"
  6973.  
  6974.    # add the info to the file
  6975.    echo -n "$MD5SUM" >> $TARBALL
  6976.    sync
  6977.  
  6978.    # now rename it to .pet
  6979.    mv -f $TARBALL ${DIR}.pet
  6980.    sync
  6981.  
  6982.    # move the created pet out of build_pkg, into WORK_DIR
  6983.    mv ${DIR}.pet $CURDIR/${DIR}.pet
  6984.    cd $CURDIR
  6985.  
  6986.    # clean up
  6987.    rm -f -R $CURDIR/build_pkg/
  6988.    echo
  6989.    echo -e "Package ${magenta}${DIR}.pet${endcolour} created."
  6990.    echo "The md5sum is: `md5sum ${DIR}.pet | cut -f1 -d' '`."
  6991.  else
  6992.    echo "Directory '$DIR' not found."
  6993.    exit 1
  6994.  fi
  6995. }
  6996.  
  6997.  
  6998. dir2sfs(){                        # $1 must be dir of pkg contents FUNCLIST
  6999.  
  7000.  if [ ! -d "$1" ]; then
  7001.    print_usage dir2sfs
  7002.    exit 1
  7003.  fi
  7004.  
  7005.  # if found, we will append it to the SFS filename
  7006.  [ "$DISTRO_VERSION" != "" ] && SFS_SUFFIX="_${DISTRO_VERSION}" || SFS_SUFFIX=''
  7007.  
  7008.  # get sfs name only, no extension or path
  7009.  SFS_DIR="`basename "$1" .sfs`"
  7010.  
  7011.  # if a valid dir
  7012.  if [ -d "$SFS_DIR" ]; then
  7013.    rm "${SFS_DIR}/"*pet*specs 2>/dev/null #240613
  7014.    #start building the sfs file
  7015.    echo "Please wait... building SFS file.."
  7016.    local SFS_NAME="${SFS_DIR/$SFS_SUFFIX/}${SFS_SUFFIX}.sfs"
  7017.    LANG=C mksquashfs "$SFS_DIR" "$SFS_NAME" -noappend &>/dev/null && SUCCESS="y" || SUCCESS=""
  7018.    if [ -z "$SUCCESS" -a ! -f "${SFS_DIR}${SFS_SUFFIX}.sfs" ]; then
  7019.      echo "Failed to create $SFS_NAME."
  7020.      exit 1
  7021.    fi
  7022.    chmod 644 "$SFS_NAME" #shinobar
  7023.    sync
  7024.    #get size, and output msg
  7025.    s=`LANG=C du -m "$SFS_NAME" | sed "s/\s.*//"`
  7026.    MD5SUM=`md5sum "$SFS_NAME" | cut -f1 -d ' '`
  7027.    echo -e "Created ${magenta}$SFS_NAME${endcolour} ( $s MB )"
  7028.    echo -e "The md5 checksum: $MD5SUM"
  7029.  else
  7030.    echo "Cannot create SFS:  '${SFS_DIR}' is not a directory."
  7031.    exit 1
  7032.  fi
  7033.  
  7034.  # clean up
  7035.  #rm -rf "${SFS_DIR}" &>/dev/null
  7036.  #rm -f "${SFS}${SFS_SUFFIX}"*.sfs-md5.txt &>/dev/null
  7037.  rm -f "${SFS}${SFS_SUFFIX}" &>/dev/null
  7038. }
  7039.  
  7040.  
  7041. dir2tgz(){                        # requires $1 as valid dir FUNCLIST
  7042.  
  7043.  [ ! -d "$1" ] && print_usage dir2tgz && exit 1
  7044.  
  7045.  DIR="${1/.t*gz/}"
  7046.  
  7047.  # remove trailing slash
  7048.  DIRNAME="`echo -n $DIR | sed -e 's%/$%%'`"
  7049.  DIRNAME="`basename ${DIRNAME}`" # get name only, no path
  7050.  
  7051.  # make sure any pet specs are named correctly - this new tar file might later be converted to a .pet
  7052.  echo "`ls -1 ${DIRNAME} | grep 'pet.specs'`" | while read LINE
  7053.  do
  7054.    mv "${DIRNAME}/$LINE" "${DIRNAME}/pet.specs" 2>/dev/null;
  7055.  done
  7056.  
  7057.  # create the tar file
  7058.  ##echo "Adding directory to '${DIRNAME}.tar.gz'.."
  7059.  tar -c -f "${DIRNAME}.tar" "${DIRNAME}/"
  7060.  gzip "${DIRNAME}.tar"
  7061.  sync
  7062.  
  7063.  # print message
  7064.  if [ -f "${DIRNAME}.tar.gz" ]; then
  7065.    echo -e "Package ${magenta}${DIRNAME}.tar.gz${endcolour} created."
  7066.    echo "The md5sum is: `md5sum ${DIRNAME}.tar.gz | cut -f1 -d' '`."
  7067.  else
  7068.    echo "Package '${DIRNAME}.tar.gz' NOT created."
  7069.    return 1
  7070.  fi
  7071. }
  7072.  
  7073.  
  7074. pet2sfs(){                        # convert pet to sfs, $1 must be file or repo pkg FUNCLIST
  7075.  
  7076.  # require valid option or quit
  7077.  [ ! -f "$1" ] && print_usage pet2sfs && exit 1
  7078.  
  7079.  pkg_ext=`get_pkg_ext "$1"`
  7080.  
  7081.  [ "$pkg_ext" != pet ] && print_usage pet2sfs && exit 1
  7082.  
  7083.  #110213,0.9.1 we want the file in $WORKDIR
  7084.  #[ ! -f "${WORKDIR}/$(basename "$1" .pet).pet" ] && cp -f "${1/.pet/}.pet" "${WORKDIR}/$(basename "$1" .pet)" 2>/dev/null
  7085.  
  7086.  PKGNAME="$(basename "$1" .pet)" #get pkg name only, no extension or path
  7087.  PKGFILE="${CURDIR}/${PKGNAME}.pet" #the file to install
  7088.  
  7089.  # determine the compression, extend test to 'XZ'
  7090.  file -b "${PKGFILE}" | grep -i -q "^xz" && TAREXT=xz || TAREXT=gz
  7091.  [ "$TAREXT" = 'xz' ] && taropts='-xJf' || taropts='-zxf'
  7092.  
  7093.  pet2tgz "${PKGFILE}" 1>/dev/null
  7094.  
  7095.  # now extract the files into $CURDIR/$PKGNAME/
  7096.  tar $taropts "${PKGFILE//.pet/.tar.$TAREXT}" 2> $TMPDIR/$SELF-cp-errlog
  7097.  
  7098.  # if DISTRO_VERSION found, the sfs will prob have a suffix
  7099.  [ "$DISTRO_VERSION" != "" ] && SFS_SUFFIX="_${DISTRO_VERSION}" || SFS_SUFFIX=''
  7100.  
  7101.  # remove the old one, if needed
  7102.  rm "${PKGNAME}${SFS_SUFFIX}.sfs" 2>/dev/null
  7103.  
  7104.  # create the new one
  7105.  dir2sfs "${CURDIR}/${PKGNAME}" && rm -rf "${CURDIR}/${PKGNAME}"
  7106.  
  7107.  # remove the .tar.$TAREXT file
  7108.  rm -f "${PKGNAME}.tar.$TAREXT"
  7109.  
  7110.  # if pkg put the pet in $CURDIR (not user), remove it
  7111.  #[ -f "${PKGFILE}" -a "`dirname "$1"`" != "$CURDIR" ] && rm -f "${PKGFILE}" 2>/dev/null
  7112.  
  7113.  # print message
  7114.  if [ ! -f "${PKGNAME}.sfs" -a ! -f "${PKGNAME}${SFS_SUFFIX}.sfs" ]; then
  7115.    echo "Package '${PKGNAME}.pet' not converted."
  7116.    return 1
  7117.  fi
  7118. }
  7119.  
  7120.  
  7121. pet2tgz(){                        # convert to tar.gz, $1 must be valid file FUNCLIST
  7122.  
  7123.  # if $1 is not a valid pkg name or a file that exists then exit..
  7124.  [ ! -f "$1" ] && print_usage pet2tgz && exit 1
  7125.  
  7126.  pkg_ext=`get_pkg_ext "$1"`
  7127.  [ "$pkg_ext" != "pet" ] && print_usage pet2tgz && exit 1
  7128.  
  7129.  PKGNAME="$(basename "$1" .pet)" # get pkg name only, no extension or path
  7130.  PKGFILE="$1"          # build the pkg file path
  7131.  
  7132.  # backup the package, to restore it later
  7133.  cp -f "$PKGFILE" "$TMPDIR/`basename $PKGFILE`.backup" 1>/dev/null
  7134.  
  7135.  chmod +w "$PKGFILE" # make it writable.
  7136.  FOOTERSIZE="32"
  7137.  
  7138.  # determine the compression, extend test to 'XZ'
  7139.  finfo=`file -b "$PKGFILE"`
  7140.  case $finfo in
  7141.    gz*|GZ*) EXT=gz ;;
  7142.    xz*|XZ*) EXT=xz ;;
  7143.    *) error "Unsupported compression type, or corrupted package." && exit 1 ;;
  7144.  esac
  7145.  
  7146.  # get the md5
  7147.  MD5SUM="`tail -c $FOOTERSIZE \"$PKGFILE\"`"
  7148.  NEWNAME="`echo -n \"$PKGFILE\" | sed -e "s/\\.pet$/\\.tar\\.$EXT/g"`" #131122
  7149.  head -c -$FOOTERSIZE "$PKGFILE" > $NEWNAME
  7150.  NEWMD5SUM="`md5sum \"$NEWNAME\" | cut -f 1 -d ' '`"
  7151.  sync
  7152.  [ ! "$MD5SUM" = "$NEWMD5SUM" ] && exit 1
  7153.  
  7154.  # restore original pet pkg
  7155.  [ -f "$TMPDIR/`basename $PKGFILE`.backup" ] && mv -f "$TMPDIR/`basename $PKGFILE`.backup" "$PKGFILE"
  7156.  
  7157.  # print message
  7158.  if [ -f "$NEWNAME" ]; then
  7159.    echo -e "${green}Success${endcolour}: Package ${magenta}`basename ${NEWNAME}`${endcolour} created."
  7160.    echo "The md5sum is `md5sum "$NEWNAME" | cut -f1 -d' '`."
  7161.  else
  7162.    error "Package ${PKGNAME}.pet not converted!"
  7163.    return 1
  7164.  fi
  7165. }
  7166.  
  7167.  
  7168. pet2txz(){                        # calls pet2tgz FUNCLIST
  7169.  pet2tgz "$1"
  7170. }
  7171.  
  7172.  
  7173. sfs2pet(){                        # convert sfs to pet, requires $1 as a valid sfs FUNCLIST
  7174.  
  7175.  # exit if no valid options
  7176.  [ ! -f "$1" ] && print_usage sfs2pet && exit 1
  7177.  
  7178.  # exit if user did not give an SFS file, or didnt give a file at all
  7179.  [ "`file "$1" | grep  Squashfs`" = '' ] && print_usage sfs2pet && exit 1
  7180.  
  7181.  # we want the file in $CURDIR
  7182.  #[ ! -f "${WORKDIR}/$(basename "${1}")" ] && cp -f "$1" "${WORKDIR}/$(basename "${1}")" 2>/dev/null
  7183.  
  7184.  SFSNAME="$(basename "$1")"
  7185.  SFSDIR="$(dirname "$1")"
  7186.  SFSEXT="${SFSNAME##*.}"
  7187.  
  7188.  # if DISTRO_VERSION found, the sfs will prob have a suffix
  7189.  [ "$DISTRO_VERSION" != "" ] && SFS_SUFFIX="_${DISTRO_VERSION}" || SFS_SUFFIX=''
  7190.  
  7191.  # create the name without path or extension (may include a $SUFFIX)
  7192.  ROOTNAME=`basename "$SFSNAME" ".$SFSEXT"`
  7193.  
  7194.  # build the file name we will work on
  7195.  SFS="${SFSDIR}/${SFSNAME}"
  7196.  
  7197.  if [ -f "$SFS" -a "$SFS" != "" ]; then
  7198.  
  7199.    echo "Unsquashing $SFSNAME.. Please wait.."
  7200.  
  7201.    # remove any older dirs, and unsquash
  7202.    rm -rf "${CURDIR}/squashfs-root/"
  7203.    LANG=C unsquashfs "$SFS"  &>/dev/null
  7204.  
  7205.    # lets remove the SFS suffix, to get back the the 'valid' PET name
  7206.    ROOTNAME_NOSUFFIX=`echo "$ROOTNAME" | sed -e "s/$SFS_SUFFIX//"`
  7207.  
  7208.    # create the folder we will package up
  7209.    mv squashfs-root "${ROOTNAME_NOSUFFIX}/"
  7210.  
  7211.    dir2tgz "${ROOTNAME_NOSUFFIX}" 1>/dev/null
  7212.  
  7213.    tgz2pet "${ROOTNAME_NOSUFFIX}".tar.gz 1>/dev/null
  7214.  
  7215.    # remove the dir and the tgz we just created
  7216.    rm -rf "${ROOTNAME_NOSUFFIX}"
  7217.    rm -f "${ROOTNAME_NOSUFFIX}".tar.gz
  7218.  
  7219.    if [ -f "${ROOTNAME_NOSUFFIX}.pet" ]; then
  7220.      echo -e "${green}Success${endcolour}: Package '${magenta}${ROOTNAME_NOSUFFIX}.pet${endcolour}' created."
  7221.      echo "The md5sum is: `md5sum ${ROOTNAME_NOSUFFIX}.pet | cut -f1 -d' '`"
  7222.    else
  7223.      echo "Package '${ROOTNAME_NOSUFFIX}.pet' not converted."
  7224.      return 1
  7225.    fi
  7226.  fi
  7227. }
  7228.  
  7229.  
  7230. tgz2pet(){                        # convert $1 (a tar.gz or tgz) to .pet FUNCLIST
  7231.  
  7232.  [ ! -f "$1" ] && print_usage tgz2pet && exit 1
  7233.  sync
  7234.  TARBALL="$1"
  7235.  [ ! -f "$TARBALL" ] && echo "The archive '$TARBALL' could not be found." && exit 1
  7236.  
  7237.  cp -f $TARBALL "$TMPDIR/`basename $TARBALL`.backup" 1>/dev/null
  7238.  
  7239.  TARBALL="${CURDIR}/$(basename "${TARBALL}")"
  7240.  chmod 644 "$TARBALL" #make it writable.
  7241.  echo "Converting `basename ${TARBALL}`.."
  7242.  
  7243.  #only accept .tgz or .tar.gz .tar.xz files...
  7244.  EXT=''
  7245.  case ${TARBALL} in
  7246.    *.tar.gz) EXT='.tar.gz' ;;
  7247.    *.tgz)    EXT='.tgz' ;;
  7248.    *.tar.xz) EXT='.tar.xz' ;;
  7249.    *.txz)    EXT='.txz' ;;
  7250.    *) echo "${1##*/}: File extension not allowed" >&2 ; exit 1 ;;
  7251.  esac
  7252.  [ "$EXT" = "" ] && error "$TARBALL must be a .tgz, .tar.gz, .txz or .tar.xz file" && exit 1
  7253.  
  7254.  #split TARBALL path/filename into components...
  7255.  BASEPKG="`basename $TARBALL $EXT`"
  7256.  DIRPKG="$CURDIR"
  7257.  [ "$DIRPKG" = "/" ] && DIRPKG=""
  7258.  case $EXT in
  7259.  *gz)OPT=-z;;
  7260.  *xz)OPT=-J;;
  7261.  esac
  7262.  
  7263.  # move pkg, update extensions (tgz -> .tar.gz, txz -> .tar.xz)
  7264.  # make code later more readable
  7265.  case $EXT in
  7266.  *tgz)mv -f $TARBALL $DIRPKG/${BASEPKG}.tar.gz
  7267.   TARBALL="$DIRPKG/${BASEPKG}.tar.gz"
  7268.   EXT='.tar.gz';;
  7269.  *txz)mv -f $TARBALL $DIRPKG/${BASEPKG}.tar.xz
  7270.   TARBALL="$DIRPKG/${BASEPKG}.tar.xz"
  7271.   EXT='.tar.xz';;
  7272.  esac
  7273.  
  7274.  # if tarball expands direct to '/' want to wrap around it (slackware pkg)... 100628 add -z ...
  7275.  # man bad bug here... the thing isn't expanded! #131122
  7276.  if [ "`tar ${OPT} --list -f ${TARBALL} | head -n 1`" = "./" ]; then
  7277.    tar --one-top-level=${BASEPKG} -xf ${TARBALL}
  7278.    tar --remove-files -c -f ${DIRPKG}/${BASEPKG}.tar ${BASEPKG}/
  7279.    case $EXT in
  7280.    *gz) gzip --force --best ${DIRPKG}/${BASEPKG}.tar ;;
  7281.    *xz) xz --force -z -9 -e ${DIRPKG}/${BASEPKG}.tar ;;
  7282.    esac
  7283.  fi
  7284.  
  7285.  FULLSIZE="`stat --format=%s ${TARBALL}`"
  7286.  MD5SUM="`md5sum $TARBALL | cut -f 1 -d ' '`"
  7287.  echo -n "$MD5SUM" >> $TARBALL
  7288.  sync
  7289.  mv -f $TARBALL $DIRPKG/${BASEPKG}.pet
  7290.  sync
  7291.  
  7292.  # restore original tar file
  7293.  [ -f "$TMPDIR/`basename $TARBALL`.backup" ] && mv -f "$TMPDIR/`basename $TARBALL`.backup" "$TARBALL"
  7294.  
  7295.  # print msg
  7296.  if [ -f "${DIRPKG}/${BASEPKG}.pet" ]; then
  7297.    echo -e "${green}Success:${endcolour} Created ${magenta}${BASEPKG}.pet${endcolour}."
  7298.    echo "This is the md5sum: `md5sum ${DIRPKG}/${BASEPKG}.pet | cut -f1 -d' '`"
  7299.  else
  7300.    error "Could not convert ${DIRPKG}/${BASEPKG}.$EXT"
  7301.    return 1
  7302.  fi
  7303. }
  7304.  
  7305.  
  7306. txz2pet(){                        # convert txz to pet, requires $1 as valid file FUNCLIST
  7307.  [ ! -f "$1" ] && print_usage txz2pet && exit 1
  7308.  FILE="$1"   # full path, filename and extension
  7309.  #keep old file, download it if needed
  7310.  #[ -f "$(basename "$1" .txz)" ] || pkg_download "$(basename "$1" .txz)" #  200813 try to download it
  7311.  # we want $FILE in $CURDIR
  7312.  [ ! -f "${CURDIR}/$(basename "${FILE}")" ] && cp -f "$FILE" "${CURDIR}/$(basename "${FILE}")" 2>/dev/null
  7313.  FILE="${CURDIR}/$(basename "${FILE}")"
  7314.  #set vars
  7315.  FILENAME="`basename "$FILE"`" # filename and extension only
  7316.  BARENAME="${FILENAME/.t*xz/}" # filename, no extension..
  7317.  PETFILE="${CURDIR}/${BARENAME}.pet" # the full path and filename of the pet file
  7318.  PETNAME="`basename "$PETFILE"`" # the filename of the pet file
  7319.  # create the pet directory, if needed
  7320.  mkdir -p "${CURDIR}/${BARENAME}"
  7321.  [ ! -d "${CURDIR}/${BARENAME}" ] && { echo "PET directory not created or found."; exit 1; }
  7322.  # unpack the file
  7323.  [ -f "$FILENAME" ] && tar -Jxvf "$FILENAME" -C "${BARENAME}/" 1>/dev/null || echo "Cannot untar $FILENAME"
  7324.  # create the pet file
  7325.  dir2pet "$BARENAME" || echo "PET working directory not found"
  7326.  
  7327.  if [ -f "$PETFILE" ]; then # if pet file was created
  7328.    # remove the pet file working directory
  7329.    rm -R "${CURDIR}/${BARENAME}/" 2>/dev/null
  7330.    rmdir "${CURDIR}/${BARENAME}/" 2>/dev/null
  7331.    # report the file was created #nope, no need, dir2pet will do it
  7332.    #echo "Package '$PETNAME' created successfully."
  7333.  else
  7334.    echo "Package "$PETFILE" file not created"
  7335.    return 1
  7336.  fi
  7337.  # delete the original txz file
  7338.  rm -f "$FILE" &>/dev/null
  7339. }
  7340.  
  7341.  
  7342. # other
  7343.  
  7344. menu_entry_msg(){                 # show menu entry details of installed pkg FUNCLIST
  7345.  
  7346.  # exit if not valid usage
  7347.  [ ! "$1" -o "$1" = "-" ] && exit 1
  7348.  
  7349.  . ${PKGRC}
  7350.  
  7351.  local MENUFILE=''
  7352.  local pkg_file_list=''
  7353.  local no_display=''
  7354.  local terminal_only=''
  7355.  local pkg_command=''
  7356.  
  7357.  # get the menu file, if it exists
  7358.  pkg_file_list=$(find "$PACKAGE_FILE_LIST_DIR" -maxdepth 1 -type f -name "${1}*.files")
  7359.  
  7360.  [ -f "$pkg_file_list" ] && MENUFILE="`LANG=C grep -m1 '/usr/share/applications/' "$pkg_file_list" | grep -i -m1 ".desktop\$"`" || return 1
  7361.  
  7362.  # if pkg has a .desktop file
  7363.   if [ -f "$MENUFILE" ]; then
  7364.  
  7365.    # exit if the menu item is set to NoDisplay
  7366.    no_display="`grep -m1 ^NoDisplay=true "$MENUFILE" 2>/dev/null`"
  7367.    terminal_only="`grep -m1 ^Terminal=true "$MENUFILE" 2>/dev/null`"
  7368.  
  7369.    # fix menu entries
  7370.    sed -i 's/ %u//g' "$MENUFILE"
  7371.    sed -i 's/ %U//g' "$MENUFILE"
  7372.    sed -i 's/ %f//g' "$MENUFILE"
  7373.    sed -i 's/ %F//g' "$MENUFILE"
  7374.    sed -i 's/ %d//g' "$MENUFILE"
  7375.    sed -i 's/ %D//g' "$MENUFILE"
  7376.  
  7377.    update_menus &
  7378.  
  7379.    # get the details for a final msg
  7380.    MENU_CAT="`LANG=C grep -m1 "^Categories=" $MENUFILE 2>/dev/null | cut -f2 -d'=' | sed -e 's/\(.*\);/\1/g' -e 's/.*;//g' | head -1`"
  7381.    APATTERN="[ ,]${MENU_CAT}" #MHHP
  7382.    TOPMENU="`LANG=C grep -m1 "${APATTERN}" /etc/xdg/menus/hierarchy | cut -f1 -d' ' | head -1`"
  7383.    if [ "$TOPMENU" = "" ]; then
  7384.      APATTERN="[ ,]${MENU_CAT};"
  7385.      TOPMENU="`LANG=C grep -m1 "${APATTERN}" /etc/xdg/menus/hierarchy | cut -f1 -d' ' | head -1`"
  7386.    fi
  7387.    MENU_NAME="`LANG=C grep -m1 "^Name=" $MENUFILE | cut -f2 -d'=' | head -1`"
  7388.  
  7389.    [ "$TOPMENU" = '' ] && TOPMENU="$MENU_CAT"
  7390.  
  7391.    # replace the Categories= line with one which has only the top, main category..
  7392.    # .. this should prevent duplicate menu entries
  7393.    #cat "$MENUFILE" | grep -v ^Categories > ${TMPDIR}/fixed.desktop
  7394.    #echo "Categories=${TOPMENU};" >> ${TMPDIR}/fixed.desktop
  7395.    #mv ${TMPDIR}/fixed.desktop $MENUFILE
  7396.  
  7397.    # print msg
  7398.    if [ "$no_display" != "" -o "$terminal_only" != '' ]; then
  7399.      pkg_command="`grep -m1 ^Exec= "$MENUFILE" | cut -f2 -d'='`"
  7400.      [ "$pkg_command" != '' ] && echo -e "To run, type:  ${bold}${pkg_command}${endcolour}"
  7401.    else
  7402.      echo -e "Menu entry:     ${lightblue}${TOPMENU:-[None]}${endcolour} -> ${lightblue}${MENU_NAME:-[None]}${endcolour}"
  7403.    fi
  7404.  
  7405.    return 0
  7406.  fi
  7407.  return 1
  7408. }
  7409.  
  7410.  
  7411. update_menus() {                  # update menus, calls fixmenus, refreshes WM
  7412.  echo started > /tmp/pkg/update_menus_busy
  7413.  fixmenus &>/dev/null
  7414.  # refresh JWM menus if using JWM window manager
  7415.  [ "`which jwm`" != "" -a "`ps -e | grep jwm`" != "" ] && jwm -reload &>/dev/null
  7416.  rm /tmp/pkg/update_menus_busy 2>/dev/null
  7417.  return 0
  7418. }
  7419.  
  7420.  
  7421. check_net(){                      # check net connection (before downloading) FUNCLIST
  7422.  [ -f $TMPDIR/internetsuccess ] && echo 0 && exit 0 #220613
  7423.  [ ! "$1" -o "$1" = "-" ] && URL="8.8.8.8" || URL="`echo  ${1} | awk -F/ '{print $3}'`"
  7424.  LANG=C ping -4 -c1 -q "$URL" &>/dev/null #220613
  7425.  REPLY=$?
  7426.  [ $REPLY -eq 0 ] && echo -n "ok" > $TMPDIR/internetsuccess #220613
  7427.  echo 0
  7428. }
  7429.  
  7430.  
  7431. get_stdin(){                      # read from stdin (when - is last option) FUNCLIST
  7432.  read -t 0.1 STDINVAR
  7433. }
  7434.  
  7435.  
  7436. not_found(){                      # list alternative packages when no match in repos FUNCLIST
  7437.  PKGNAME="${1/.pet/}"
  7438.  #echo "$APPTITLE"
  7439.  #echo "Package '${PKGNAME}' not found in current repo..  "
  7440.  if [ "$(ls "${WORKDIR}/"| grep "${PKGNAME}")" != "" ]; then
  7441.    [ "`list_downloaded_pkgs "${PKGNAME}"`" != "" ] && echo "These downloaded packages match your search:
  7442. `list_downloaded_pkgs "${PKGNAME}"`"
  7443.  fi
  7444.    [ "`$PKGSEARCH "${PKGNAME}"`" != "" ] && echo "These packages in the repos match your search:
  7445. `$PKGSEARCH "${PKGNAME}"`"
  7446.  return 1
  7447. }
  7448.  
  7449.  
  7450. first_run (){                     # welome message on first run
  7451.  
  7452.  # quit if not the first run
  7453.  [ ! -f ~/.pkg/firstrun ] && return 0
  7454.  # print msg
  7455.  echo '============================================================'
  7456.  echo -e "  ${bold}$APPNAME $APPVER${endcolour} - a command-line package manager"
  7457.  echo '============================================================'
  7458.  echo
  7459.  echo "  pkg repo-update      # update the contents of each installed repo"
  7460.  echo "  pkg repo-list        # list all available repos"
  7461.  echo "  pkg repo <name>      # change to the chosen repo"
  7462.  echo "  pkg show-config      # show current Pkg settings"
  7463.  echo "  pkg workdir <DIR>    # change where to keep downloaded packages"
  7464.  echo
  7465.  echo "  pkg search <term>    # search all fields in the current repo"
  7466.  echo "  pkg names <pkgname>  # search package names in the current repo"
  7467.  echo "  pkg status <pkgname> # print info about the given package"
  7468.  echo "  pkg add <pkgname>    # install & download packages and dependencies"
  7469.  echo
  7470.  echo " You can run the commands below to learn more about Pkg:"
  7471.  echo
  7472.  echo "  pkg help             # help info, all options and some tips"
  7473.  echo "  pkg help-all         # full help info, including advanced usage"
  7474.  echo "  pkg usage [cmd]      # show every command, or info on the given one"
  7475.  echo "  pkg examples         # show lots more example Pkg commands"
  7476.  echo
  7477.  echo " HINT: Use TAB to auto-complete package and repo names (and more).."
  7478.  echo
  7479.  echo '============================================================'
  7480.  
  7481.  # get the right list of repos
  7482.  update_sources &>/dev/null &
  7483.  
  7484.  # delete first run flag
  7485.  rm -f ~/.pkg/firstrun 2>/dev/null
  7486.  
  7487.  # delete other tmp stuff, make it like a first run
  7488.  rm $TMPDIR/func_list        &>/dev/null
  7489.  rm $TMPDIR/curr_repo_url    &>/dev/null
  7490.  rm $TMPDIR/internetsuccess  &>/dev/null
  7491.  rm $TMPDIR/pkg_aliases      &>/dev/null
  7492.  rm $TMPDIR/pkglist          &>/dev/null
  7493.  rm $TMPDIR/pkglist_*        &>/dev/null
  7494.  rm $TMPDIR/USRPKGLIST       &>/dev/null
  7495.  rm $TMPDIR/PKGSDONE         &>/dev/null
  7496.  rm $TMPDIR/deps_installed   &>/dev/null
  7497.  rm $TMPDIR/deps_missing     &>/dev/null
  7498.  rm $TMPDIR/$SELF-cp-errlog  &>/dev/null
  7499.  rm $TMPDIR/*-MISSING.txt    &>/dev/null
  7500.  
  7501.  exit 0
  7502. }
  7503.  
  7504.  
  7505. #====================  main functions  ======================#
  7506.  
  7507.  
  7508.  
  7509. #=====================  final checks  =======================#
  7510.  
  7511. # try to set a default repo if none was found
  7512. if [ ! -f "$REPO_DB_FILE_DIR/$REPOFILE" -o ! -f "$HOME/.pkg/sources" ]; then
  7513.  
  7514.  mkdir -p $HOME/.pkg 2>/dev/null
  7515.  mkdir -p $PKGS_DIR 2>/dev/null
  7516.  [ ! -f "$HOME/.pkg/sources" ] && touch $HOME/.pkg/sources
  7517.  # try to get repo file from /root if it exists
  7518.  if [ -f "$REPO_DB_FILE_DIR/Packages-puppy-noarch-official" ]; then
  7519.    # copy any available Puppy repo files in root to $PKGS_DIR
  7520.    mv "$REPO_DB_FILE_DIR"/Packages-puppy-* $REPO_DB_FILE_DIR/
  7521.  fi
  7522.  
  7523.  ## if repo file was NOT added to home dir, fata error, exit
  7524.  #if [ ! -f $PKGS_DIR/Packages-puppy-noarch-official ]; then
  7525.  #  error "Repo files not found. Check ${magenta}$PKGS_DIR${endcolour}"
  7526.  #  exit 1
  7527.  #fi
  7528.  
  7529.  # if we got here, the noarch repo file is in $PKGS_DIR, lets use it as the default/only repo
  7530.  echo -e "${yellow}Warning:${endcolour} No repo files found. Setting the default repo to 'noarch'."
  7531.  update_sources 1>/dev/null
  7532.  set_current_repo noarch 1>/dev/null
  7533.  # if repos added ok
  7534.  if [ $? -eq 0 ]; then
  7535.    echo -e "${green}Success:${endcolour} 'noarch' added."
  7536.    echo "Re-running: $SELF $@"
  7537.    echo
  7538.    $SELF $@
  7539.    exit 0
  7540.  fi
  7541.  
  7542. fi
  7543.  
  7544. # create the user-installed pkgs file if it doesnt exists
  7545. [ ! -f $PKGS_DIR/user-installed-packages ] && touch $PKGS_DIR/user-installed-packages
  7546.  
  7547. first_run
  7548.  
  7549.  
  7550. #=====================  final checks  =======================#
  7551.  
  7552.  
  7553.  
  7554. #====================  main interface  ======================#
  7555.  
  7556.  
  7557. while [ $# != 0 ]; do # get all options ($# means all options)
  7558.   I=1
  7559.   while [ ! -z $# -a $I -le `echo $# | wc -c` ]; do
  7560.  
  7561.    # accept from stdin, (using dash (-) as final option)
  7562.    for x in "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
  7563.    do
  7564.      case "$x" in
  7565.        -)
  7566.          while true; do
  7567.            read ALINE
  7568.            [ "$ALINE" ] || break
  7569.            [ "$(echo $ALINE| cut -b1)" = '#' ] && continue # skip comment
  7570.            [ "$ALINE" = ':' ] && continue  # skip colon
  7571.            if [ "$ALINE" = "- " -o "$ALINE" = " -" -o "$ALINE" != "-" ]; then
  7572.              OPTS="${@%-}" #get all opts, without last dash
  7573.              OPTS="${OPTS% }" #get all opts, strip last space
  7574.              $SELF $OPTS "$ALINE"
  7575.            fi
  7576.          done
  7577.          break
  7578.        ;;
  7579.        --ask|-a)         ASK=true ; QTAG="?  (y/N)";;
  7580.        --quiet|-q)       QUIET=true ;;
  7581.        --force|-f)       FORCE=true ;;
  7582.        --no-color)       green='' red='' magenta='' lightblue='' yellow='' bold='' endcolour='' ;;
  7583.        --no-colour|-nc)  green='' red='' magenta='' lightblue='' yellow='' bold='' endcolour='' ;;
  7584.        '--configure='*)  PKG_CONFIGURE="$(echo "$x" | sed -e 's/^--configure=//')" ;;
  7585.        '--cflags='*)     PKG_CFLAGS="$(echo "$x" | sed -e 's/^--cflags=//')"       ;;
  7586.      esac
  7587.    done
  7588.  
  7589.    # enable passing multiple pkgs separated by comma, space,
  7590.    # works with or without double quotes around pkgs
  7591.    opt="`shift; echo "$@"`"
  7592.    opt="${opt//,/ }"
  7593.    opt="${opt//|/ }"
  7594.    # only keep params up to the next option
  7595.    opt="${opt// -*/}"
  7596.  
  7597.    ## main options
  7598.    case $1 in
  7599.  
  7600.      -) # accept from stdin, pkg -i (defaults to pkg add $1 or pkg -u $1)
  7601.        if [ "$2" = "" ]; then
  7602.          while true; do
  7603.            read ALINE
  7604.            [ "$ALINE" -a "$ALINE" != '-' ] || break
  7605.            [ "$(echo $ALINE| cut -b1)" = '#' ] && continue # skip comment
  7606.            ALINE=`echo $ALINE` #strip spaces
  7607.            if [ "$ALINE" != "-" -a "`$PKGSEARCH "$ALINE"`" != "" ]; then
  7608.              [ "`is_installed_pkg $ALINE`" = false ] && pkg_get "$ALINE" || \
  7609.              pkg_uninstall "$ALINE"
  7610.            fi
  7611.          done
  7612.          shift
  7613.        fi
  7614.      ;;
  7615.  
  7616.      --ask|-a)         ASK=true ; QTAG="?  (y/N)";;
  7617.      --quiet|-q)       QUIET=true ;;
  7618.      --force|-f)       FORCE=true ;;
  7619.      --no-color)       green='' red='' magenta='' lightblue='' yellow='' bold='' endcolour='' ;;
  7620.      --no-colour|-nc)  green='' red='' magenta='' lightblue='' yellow='' bold='' endcolour='' ;;
  7621.      '--configure='*)  PKG_CONFIGURE="$(echo "$1" | sed -e 's/^--configure=//')" ;;
  7622.      '--cflags='*)     PKG_CFLAGS="$(echo "$1" | sed -e 's/^--cflags=//')"       ;;
  7623.  
  7624.      # package search
  7625.      --all-pkgs|--all)                 [ ! "$2" ] && cat "$REPO_DB_FILE_DIR/${REPOFILE}" || cat "$REPO_DB_FILE_DIR/${REPOFILE}" | grep "$2"; exit 0;;
  7626.      --search|-s|search|s)             [ ! "$2" ] && search_pkgs          || for x in "$opt"; do [ "$x" -a "$x" != "-" ] && search_pkgs "$x";          done; ;;
  7627.      -ss)                              [ ! "$2" ] && search_fast          || for x in "$opt"; do [ "$x" -a "$x" != "-" ] && search_fast "$x";          done; ;;
  7628.      --search-all|-sa|search-all|sa)   [ ! "$2" ] && search_all_pkgs      || for x in "$opt"; do [ "$x" -a "$x" != "-" ] && search_all_pkgs "$x";      done; ;;
  7629.      -ssa|ssa)                         [ ! "$2" ] && search_all_fast      || for x in "$opt"; do [ "$x" -a "$x" != "-" ] && search_all_fast "$x";      done; ;;
  7630.      --names|-n|names|n)               [ ! "$2" ] && list_pkg_names       || for x in $opt; do [ "$x" -a "$x" != "-" ] && list_pkg_names "$x";         done; ;;
  7631.      --names-exact|-ne|names-exact|ne) [ ! "$2" ] && list_pkg_names       || for x in $opt; do [ "$x" -a "$x" != "-" ] && list_pkg_names "$x\$";       done; ;;
  7632.      --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; ;;
  7633. --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 ;;
  7634. --list-downloaded|list-downloaded|-ld|ld) [ ! "$2" ] && list_downloaded_pkgs || list_downloaded_pkgs "$2"; exit 0 ;;
  7635.      -LI|LI)                           [ ! "$2" ] && HIDE_BUILTINS=false list_installed_pkgs || HIDE_BUILTINS=false list_installed_pkgs "$2"; exit 0 ;; # list all installed pkgs inc builtins
  7636.  --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 ;;
  7637.  
  7638.      # get, download, install, remove, pkgs
  7639.      --download|-d|download|d)
  7640.        AUTOCLEAN_override='no'  
  7641.        [ ! "$2" ] && pkg_download || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_download "$x";  done;;
  7642.      --install|-i|install|i)           [ ! "$2" ] && pkg_install  || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_install "$x";   done;;
  7643.      --uninstall|-u|uninstall|u)       [ ! "$2" ] && pkg_uninstall|| for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_uninstall "$x"; done;;
  7644.      --remove|-rm|remove|rm)           [ ! "$2" ] && pkg_remove   || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_remove "$x";    done;;
  7645.      --unpack|--extract|unpack|extract)[ ! "$2" ] && pkg_unpack   || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_unpack "$x";    done;;
  7646.      --clean|clean)                    [ ! "$2" ] && clean_pkgs   || for x in $opt; do [ "$x" -a "$x" != "-" ] && clean_pkgs "$x";    done;;
  7647.      --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 ;;
  7648.      --get-only|-go|get-only|go)       [ ! "$2" ] && print_usage get-only || for x in $opt; do if [ "$x" -a "$x" != "-" ]; then choose_pkg "$x"; NO_INSTALL=true  pkg_get "$x"; fi; done ;;
  7649.      --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
  7650.      --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;;
  7651.      --install-all|-ia)                list_downloaded_pkgs | while read pkg; do [ "$pkg" -a "$pkg" != "-" ] && pkg_install "$pkg";   done;;
  7652.      --uninstall-all|-ua)              list_installed_pkgs  | while read pkg; do [ "$pkg" -a "$pkg" != "-" ] && pkg_uninstall "$pkg"; done;;
  7653.  
  7654.      # pkg status, info
  7655.      --pkg-installed|-pi|installed|pi)   [ ! "$2" ] && is_installed_pkg || for x in $opt; do [ "$x" -a "$x" != "-" ] && is_installed_pkg "$x"; done;;
  7656.      --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
  7657.      --pkg-status|-ps|status|ps)         [ ! "$2" ] && pkg_status  || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_status "$x";   done;;
  7658.      --contents|-c|contents|c)           [ ! "$2" ] && pkg_contents|| for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_contents "$x" 2>/dev/null; done;;
  7659.      --which|-w|which|w)                 [ ! "$2" ] && which_pkg   || for x in $opt; do [ "$x" -a "$x" != "-" ] && which_pkg "$x";    done;;
  7660.      --which-repo|-wr|which-repo|wr)     [ ! "$2" ] && which_repo  || for x in $opt; do [ "$x" -a "$x" != "-" ] && which_repo "$x";   done;;
  7661.      --pkg-repack|-pr|repack|pr)         [ ! "$2" ] && pkg_repack  || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_repack "$x";   done;;
  7662.      --pkg-update|-pu|update|pu)         [ ! "$2" ] && pkg_update  || for x in $opt; do [ "$x" -a "$x" != "-" ] && pkg_update "$x";   done;;
  7663.      --pkg-combine|-pc|pkg-combine|pc)   [ ! "$2" ] && pkg_combine || for x in $opt; do [ "$x" -a "$x" != "-" ] && COMBINE2SFS=false pkg_combine "$x"; done;;
  7664.      --pkg-split|split)                  [ ! "$2" ] && split_pkg   || for x in $opt; do [ "$x" -a "$x" != "-" ] && split_pkg "$x"; done;;
  7665.      --pkg-merge|merge)                  [ ! "$2" ] && merge_pkg   || merge_pkg "$2" "$3"; exit 0;;
  7666.      --sfs-combine|-sc|sfs-combine|sc)   [ ! "$2" ] && pkg_combine || for x in $opt; do [ "$x" -a "$x" != "-" ] && COMBINE2SFS=true  pkg_combine "$x"; done;;
  7667.      -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
  7668.  
  7669.      # pkg compiling
  7670.      --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;;
  7671.   --pkg-build-list|-pbl|build-list|pbl)  [ ! "$2" ] && list_build_scripts || for x in $opt; do [ "$x" -a "$x" != "-" ] && list_build_scripts "$x"; done;;
  7672.  
  7673.      # dependencies
  7674.      --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
  7675.      --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
  7676.      --deps|-e|deps|e)                   [ ! "$2" ] && get_deps;  for x in $opt; do [ "$x" -a "$x" != "-" ] && get_deps  "$x";                done ;;
  7677.    --deps-download|-ed|deps-download|ed) [ ! "$2" ] && get_deps;  for x in $opt; do [ "$x" -a "$x" != "-" ] && NO_INSTALL=true get_deps "$x"; done ;;
  7678.      --has-deps|-he|has-deps|he)         [ ! "$2" ] && has_deps;  for x in $opt; do [ "$x" -a "$x" != "-" ] && has_deps  "$x";                done ;;
  7679.      --deps-all|-ea|deps-all|ea)         [ ! "$2" ] && get_deps;  [ "$2" != "-" ] && NO_INSTALL=false get_all_deps "$2" ;;
  7680.      --deps-check|-ec|deps-check|ec|ldd) [ ! "$2" ] && print_usage deps-check || pkg_ldd_msg "$2" ;;
  7681.      -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
  7682.  
  7683.      # repo
  7684.      --repo|-r|repo|r)                     [ ! "$2" ] && echo $REPONAME || set_current_repo "$2" ;;
  7685.      --repo-convert|-rc|repo-convert|rc)   [ ! "$2" ] && print_usage repo-convert || for x in $opt; do [ "$x" -a "$x" != "-" ]  && convert_repofile "$x"; done ;;
  7686.      --repo-list|-rl|repo-list|rl)         [ "$2" != "-" ] && repo_list "$2"      ;;
  7687.      --repo-info|-ri|repo-info|ri)         [ "$2" != "-" ] && print_repo_info "$2";;
  7688.  --repo-file-list|-rfl|repo-file-list|rfl) [ "$2" != "-" ] && repo_file_list "$2" ;;
  7689.      --repo-update|-ru|repo-update|ru)     update_repo "$2";;
  7690.      --add-source|add-source)              [ "`echo $2|grep '|'`" = '' -o "$2" = '-' ] && print_usage add-source || add_source "$2";;
  7691.      --add-repo|add-repo)                  [ ! "$2" -o "$2" = '-'  ] && print_usage add-repo || shift; add_repo "$@"; exit 0;;
  7692.      --rm-repo|rm-repo)                    [ ! "$2" -o "$2" = '-'  ] && print_usage rm-repo  || rm_repo "$2"; exit 0;;
  7693.      --update-sources|update-sources)      update_sources ;;
  7694.  
  7695.      # set repo settings
  7696.      --repo-pkg-scope|-rps|rps|repo-pkg-scope)  if [ "$2" -a "$2" != "-" ]; then set_pkg_scope "$2";      else echo "$PKGSCOPE"; fi; exit 0;;
  7697.      --repo-dep-scope|-rds|repo-dep-scope|rds)  if [ "$2" -a "$2" != "-" ]; then set_dep_scope "$2";      else echo "$DEPSCOPE"; fi; exit 0;;
  7698.      --bleeding-edge|-be|bleeding-edge|be)      if [ "$2" -a "$2" != "-" ]; then set_bleeding_edge "$2";  else echo "$BLEDGE";   fi; exit 0;;
  7699.      --rdep-check|-rdc|rdep-check|rdc)          if [ "$2" -a "$2" != "-" ]; then set_recursive_deps "$2"; else echo "$RDCHECK";  fi; exit 0;;
  7700.  
  7701.      # convert
  7702.      --deb2pet|deb2pet) [ ! "$2" ] && deb2pet || for x in $opt; do deb2pet "$x"; done ;;
  7703.      --dir2pet|dir2pet) [ ! "$2" ] && dir2pet || for x in $opt; do dir2pet "$x"; done ;;
  7704.      --dir2sfs|dir2sfs) [ ! "$2" ] && dir2sfs || for x in $opt; do dir2sfs "$x"; done ;;
  7705.      --dir2tgz|dir2tgz) [ ! "$2" ] && dir2tgz || for x in $opt; do dir2tgz "$x"; done ;;
  7706.      --pet2sfs|pet2sfs) [ ! "$2" ] && pet2sfs || for x in $opt; do pet2sfs "$x"; done ;;
  7707.      --pet2tgz|pet2tgz) [ ! "$2" ] && pet2tgz || for x in $opt; do pet2tgz "$x"; done ;;
  7708.      --pet2txz|pet2txz) [ ! "$2" ] && pet2txz || for x in $opt; do pet2txz "$x"; done ;;
  7709.      --sfs2pet|sfs2pet) [ ! "$2" ] && sfs2pet || for x in $opt; do sfs2pet "$x"; done ;;
  7710.      --tgz2pet|tgz2pet) [ ! "$2" ] && tgz2pet || for x in $opt; do tgz2pet "$x"; done ;;
  7711.      --txz2pet|txz2pet) [ ! "$2" ] && txz2pet || for x in $opt; do txz2pet "$x"; done ;;
  7712.    --dir2repo|dir2repo) [ ! "$2" ] && dir2repo|| for x in $opt; do dir2repo "$x"; done ;;
  7713.  
  7714.      # other
  7715.      --func-list|func-list)  [ -f $TMPDIR/func_list ] && cat $TMPDIR/func_list || func_list; exit 0;;
  7716.      --workdir|workdir)      [ "$2" != "-" ] && set_workdir "$2"; exit $?;;
  7717.      --autoclean|autoclean)  [ "$2" = "yes" -o "$2" = "no" ] && set_autoclean "$2" || { echo "$AUTOCLEAN"; exit 1; } ;;
  7718.  
  7719.      # usage, help funcs, version info, etc
  7720.      --welcome|welcome)          touch $HOME/.pkg/firstrun; first_run; exit 0;;
  7721.      --show-config|show-config)  show_config; exit 0;;
  7722.      --version|-v|version|v)     echo "$APPNAME $APPVER"; exit 0;;
  7723.      --usage|usage)              print_usage "$2"; exit 0;;
  7724.  
  7725.      --examples|-ex|examples|ex)
  7726.        . /usr/share/pkg/docs/examples.txt;
  7727.        echo -e "$EXAMPLES\n"
  7728.        exit 0
  7729.        ;;
  7730.      --help-all|-H|help-all|H)
  7731.          . /usr/share/pkg/docs/env_vars.txt
  7732.          . /usr/share/pkg/docs/help.txt
  7733.          . /usr/share/pkg/docs/examples.txt
  7734.          . /usr/share/pkg/docs/help-all.txt
  7735.          echo -e "$HELP_ALL\n\n" | less -R
  7736.          exit 0
  7737.          ;;
  7738.  
  7739.      --help|-h|help|h)
  7740.          . /usr/share/pkg/docs/env_vars.txt
  7741.          . /usr/share/pkg/docs/help.txt
  7742.          echo -e "$HELP\n"
  7743.          exit 0
  7744.          ;;
  7745.  
  7746.      # any other options
  7747.      -?*|--?*)
  7748.          # exit if bad options
  7749.          echo "Unknown option '$1'" && exit 1
  7750.        ;;
  7751.  
  7752.      ?*)
  7753.          # all other options .. might be an internal func, if so, execute it with all given options
  7754.          [ "`func_list | tr '\t' ' ' | cut -f2 -d' ' | grep -m1 "^${1}"'()'`" != '' ] && "$@" && exit 0
  7755.        ;;
  7756.  
  7757.      esac
  7758.  
  7759.      shift
  7760.      I=$(($I + 1))
  7761.    done
  7762. done
  7763.  
  7764.  
  7765.  
  7766. #====================  main interface  ======================#
  7767. code=$?
  7768. [ ! -z "$AUTOCLEAN_override" ] && AUTOCLEAN="$AUTOCLEAN_override"
  7769. # if AUTOCLEAN=true silently delete all pkgs in WORKDIR that are already installed
  7770. if [ "$AUTOCLEAN" = 'yes' -a "`HIDE_BUILTINS=true list_installed_pkgs`" != '' ]; then
  7771.   clean_pkgs &>/dev/null
  7772. fi
  7773.  
  7774.  
  7775.  
  7776. # remove tmp dir used by pkg_get and get_deps
  7777. rm $TMPDIR/PKGSDONE 2>/dev/null
  7778.  
  7779. # reset lang options as we found them
  7780. LANG=$USER_LANG
  7781. LC_ALL=$USER_LC_ALL
  7782.  
  7783. exit $code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement