Advertisement
s243a

pkg (return dl name via global)

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