Advertisement
s243a

/usr/sbin/pkg (3)

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