SHARE
TWEET

pkg (fix symlink removal)

s243a Dec 3rd, 2019 (edited) 129 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/ash
  2. #
  3. #  pkg - a command line package manager for Puppy Linux
  4. #
  5. #  By Scott Jarvis (sc0ttman)
  6. #
  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. TMPDIR=/tmp/pkg/${whoami}  # set the tmp dir
  41.  
  42. # set colours true by default
  43. green="\e[32m"; red="\e[91m"; magenta="\e[95m"; lightblue="\e[36m"; yellow="\e[93m"; bold="\e[1m"; endcolour="\e[0m"
  44.  
  45. # ENVIRONMENT VARS, which may be overridden later
  46. [ -z "$PKGRC" ] && export PKGRC=${HOME}/.pkg/pkgrc      # config file for this script
  47. [ -z "$ASK"   ] && ASK=false                            # if true, ask user before doing stuff. Overridden by --ask
  48. [ -z "$QUIET" ] && QUIET=false                          # if true, hide output that doesnt log well in Xdialog, Gtkdialog, etc
  49. [ -z "$FORCE" ] && FORCE=false                          # if true, force (re)install/remove pkgs. Overridden by --force
  50. [ -z "$HIDE_INSTALLED" ] && export HIDE_INSTALLED=false # if true, hide installed pkgs from pkg searches (-n, -na, -ss, -ssa)
  51. [ -z "$HIDE_BUILTINS"  ] && export HIDE_BUILTINS=true   # if true, remove builtins pkgs from dep lists, dont download/install them
  52. [ -z "$HIDE_USER_PKGS" ] && export HIDE_USER_PKGS=true  # if true, hide user installed pkgs from dep lists, dont download/install them
  53. [ -z "$NO_ALIASES"     ] && export NO_ALIASES=false     # if true, skip searching pkg alias names for pkg alternatives
  54. [ -z "$NO_INSTALL"     ] && export NO_INSTALL=false     # if true, skip installing of pkgs
  55. [ -z "$PKG_NO_COLOURS" ] && export PKG_NO_COLOURS=false # if true, disable coloured output or not
  56. [ -z "$PKG_CONFIGURE"  ] && export PKG_CONFIGURE=''     # reset
  57. [ -z "$PKG_CFLAGS"     ] && export PKG_CFLAGS=''        # reset
  58. [ -z "$PKG_PPA2PUP_FN" ] && if [ ! -z "`which gawk`" ]; then PKG_PPA2PUP_FN=ppa2pup_gawk; else PKG_PPA2PUP_FN=ppa2pup; fi
  59.  
  60. # but disable colours if called as gpkg, or the ENV variable PKG_NO_COLOURS is true
  61. if [ "`basename $0`" != "pkg" -o "$PKG_NO_COLOURS" = true ];then
  62.   green='' red='' magenta='' lightblue='' yellow='' bold='' endcolour=''
  63. fi
  64.  
  65. # report errors better than just echo
  66. error(){
  67.   echo -e 1>&2 "${red}Error:${endcolour} $1"
  68. }
  69.  
  70. # for correct error output, trap commands early
  71. cleanup(){
  72.   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
  73.   rm -rf /tmp/pkg/build_pkg/ &>/dev/null
  74.   #Delete Broken symlinks. See: https://unix.stackexchange.com/a/49470
  75.   find ~/pkg -type l -exec test ! -e {} \; -print | xargs rm
  76.  
  77.  
  78.   # prevent double msg output if error=130 (user Ctrl-C)
  79.   [ -f /tmp/pkg/error130 -a "$2" = '130' ] && exit "$2"
  80.   # make the tmp dir we'll use
  81.   mkdir -p /tmp/pkg &>/dev/null
  82.   # only add line numbers if it was given
  83.   [ "$1" != 'none' ] && lineno="line $1, " || lineno=''
  84.   # get the right error msg
  85.   case "$2" in
  86.   130) echo; msg="User cancelled operation!"; touch /tmp/pkg/error130;;
  87.   8)   msg="Package URL invalid.";;
  88.   7)   msg="Package extraction failed.";;
  89.   6)   msg="Missing package file.";;
  90.   5)   msg="Missing directory.";;
  91.   4)   msg="Copy failed.";;
  92.   3)   msg="Setup failed.";;
  93.   2)   msg="Code error!";;
  94.   1)   exit 1 ;; # user gave wrong options or pkg name, not serious
  95.   *)   msg="Error code = $2" ;;
  96.   esac
  97.   # print msg
  98.   [ "$2" != "0" ]  && echo -e 1>&2 "${red}Error${endcolour} ${2}: ${lineno}$msg"
  99.   exit $2
  100. }
  101. trap 'cleanup ${LINENO:-none} $?' EXIT INT TERM
  102.  
  103. #run as root only
  104. whoami=$(whoami)
  105. [ "$whoami" != "root" ] && echo "You must be root to run Pkg." && exit 1
  106.  
  107. #080413 make sure all config files are present, copy from /etc if need be
  108. if [ ! -f ${PKGRC} ];then
  109.   echo "Restoring default settings from /etc/pkg"
  110.   [ ! -d /etc/pkg/ ] && error "Default config files  in /etc/pkg/ not found." && exit 3
  111.   mkdir -p "$HOME/.pkg"
  112.   [ ! -d "$HOME/.pkg" ] && error "Default user config dir '$HOME/.pkg/' could not be created." && exit 3
  113.   cp --force -a /etc/pkg/* "$HOME/.pkg/" || exit 3
  114. fi
  115.  
  116. # get puppy distro env vars
  117. [ -f /etc/rc.d/PUPSTATE ]                   && . /etc/rc.d/PUPSTATE                    #this has PUPMODE and SAVE_LAYER.
  118. [ -f /etc/DISTRO_SPECS ]                    && . /etc/DISTRO_SPECS                     #has DISTRO_BINARY_COMPAT, DISTRO_COMPAT_VERSION
  119. [ -f /etc/rc.d/BOOTCONFIG ]                 && . /etc/rc.d/BOOTCONFIG                  #has EXTRASFSLIST PREVUNIONRECORD, LASTUNIONRECORD (sfs stuff)
  120. [ -f /etc/xdg/menus/hierarchy ]             && . /etc/xdg/menus/hierarchy              #w478 has PUPHIERARCHY variable.
  121. [ -f $HOME/.packages/DISTRO_PKGS_SPECS ]    && . $HOME/.packages/DISTRO_PKGS_SPECS     #has lot sof essential info
  122. [ -f $HOME/.packages/PKGS_MANAGEMENT ]      && . $HOME/.packages/PKGS_MANAGEMENT       #has PKG_NAME_IGNORE, PKG_PET_THEN_BLACKLIST_COMPAT_KIDS, PKG_REPOS_ENABLED
  123. [ -f $HOME/.packages/DISTRO_COMPAT_REPOS ]  && . $HOME/.packages/DISTRO_COMPAT_REPOS   #has repo URL related vars
  124.  
  125. # set the package name suffix appended to combined pkgs (pkg+deps)
  126. CP_SUFFIX="WITHDEPS_${DISTRO_FILE_PREFIX}"
  127.  
  128. # set correct arch for repo URLs (used by some slack-pup repos)
  129. case "$DISTRO_TARGETARCH" in
  130.   x86)    DBIN_ARCH=i486                ;;
  131.   x86_64) DBIN_ARCH=x86_64 ; DSUFFIX=64 ;;
  132.   *)      DBIN_ARCH=i486                ;;
  133. esac
  134.  
  135. # needed for some debian based repos
  136. case $DISTRO_COMPAT_VERSION in
  137.   wheezy) DDB_COMP=bz2 ;; # older versions
  138.   *)      DDB_COMP=xz  ;;
  139. esac
  140.  
  141. # now create 'layers-installed': will contain builtins (and devx packages, if devx is loaded)
  142. #130511 need to include devx-only-installed-packages, if loaded...
  143. if which gcc &>/dev/null;then
  144.   [ ! -f /tmp/ppm-layers-installed-packages ] && cp -f $HOME/.packages/woof-installed-packages /tmp/ppm-layers-installed-packages &>/dev/null
  145.   cat $HOME/.packages/devx-only-installed-packages >> /tmp/ppm-layers-installed-packages &>/dev/null
  146.   sort -u /tmp/ppm-layers-installed-packages > $HOME/.packages/layers-installed-packages &>/dev/null
  147. else
  148.   cp -f $HOME/.packages/woof-installed-packages $HOME/.packages/layers-installed-packages &>/dev/null
  149. fi
  150.  
  151. # set $DIRECTSAVEPATH (where we want to install pkgs)
  152. if [ $PUPMODE -eq 3 -o $PUPMODE -eq 7 -o $PUPMODE -eq 13 ];then
  153.   DIRECTSAVEPATH="/initrd${SAVE_LAYER}" #SAVE_LAYER is in /etc/rc.d/PUPSTATE.
  154. elif [ "$PUPMODE" = "2" ]; then
  155.   DIRECTSAVEPATH=""
  156. fi
  157.  
  158. # get repo details, workdir, search settings and so on.. you could
  159. # you can also add any ENVIRONMENT VARS above to PKGRC, to override the defaults
  160. . ${PKGRC}
  161.  
  162. # set and create workdir if no valid dir set
  163. [ ! -d "$WORKDIR" ] && mkdir -p "$WORKDIR"
  164. if [ ! -d "$WORKDIR" ];then
  165.   error "Can't create $WORKDIR. Please create it."
  166.   exit 3
  167. fi
  168. WORKDIR=~/pkg
  169.  
  170. # add to tab completion settings to bashrc and print hint
  171. if [ -z "$PKG_TAB_COMPLETION" ];then
  172.   if [ "$(grep 'export PKG_TAB_COMPLETION=true' ~/.bashrc)" = "" ];then
  173.     # add to bashrc
  174.     echo "" >> ~/.bashrc
  175.     echo "# enable $APPNAME $APPVER TAB completion" >> ~/.bashrc
  176.     echo "export PKG_TAB_COMPLETION=true" >> ~/.bashrc
  177.     echo ". /etc/bash_completion.d/pkg 2>/dev/null" >> ~/.bashrc
  178.   fi
  179. fi
  180.  
  181. # make tmp dir writable by everyone if needed
  182. if [ ! -d "$TMPDIR" ];then
  183.   mkdir -p "$TMPDIR" 2>/dev/null
  184.   chmod -R 1777 /tmp/pkg/ 2>/dev/null
  185. fi
  186.  
  187. # if no tmp dir created or accessible, exit
  188. [ ! -d "$TMPDIR" ] && error "Cannot create temp dir ${lightblue}$TMPDIR${endcolour}" && exit 3
  189.  
  190. # support aliases, create ALIASES tmp file
  191. 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'
  192. [ ! -f "$TMPDIR/pkg_aliases" ] && echo "$PKG_NAME_ALIASES" | tr ' ' '\n' > $TMPDIR/pkg_aliases
  193.  
  194. # remove error code flag if we got here
  195. rm -f /tmp/pkg/error130 &>/dev/null
  196.  
  197. # if not first run, and no options given, print the help
  198. [ ! -f ~/.pkg/firstrun -a "$1" = "" ] && $SELF -H && exit 1 #200913 more help by default
  199. }
  200. #==================  setup script vars  =====================#
  201.  
  202.  
  203. {
  204. #====================  main functions  ======================#
  205.  
  206. set -a
  207.  
  208. # utility funcs
  209.  
  210. print_usage(){                    # print usage text. Requires $1, a valid Pkg cmd FUNCLIST
  211.  
  212.   local examples_file=/usr/share/pkg/docs/examples.txt
  213.  
  214.   [ -f $examples_file ] && source $examples_file
  215.  
  216.   # get and print the usage info from its usage file
  217.   if [ -f /usr/share/pkg/docs/usage/$1 -a "$1" ];then
  218.     source /usr/share/pkg/docs/usage/$1
  219.     echo -e "\n$USAGE"
  220.     # show examples, taken from $EXAMPLES, if they exist
  221.     if [ "`echo "$EXAMPLES" | grep "$SELF" | grep "\-$1"`" != '' ];then
  222.       echo -e "\n${bold}Usage Examples${endcolour}:\n"
  223.       echo -e "$EXAMPLES" | grep "$SELF " | grep "$1 "
  224.     fi
  225.   else
  226.     echo -e "Usage: ${bold}$SELF usage CMD${endcolour}"
  227.     echo
  228.     echo "Commands:"
  229.     echo -e "add-repo       get-only         repo-convert
  230. add-source     help             repo-dep-scope
  231. all-pkgs       help-all         repo-file-list
  232. add            remove           repo-info
  233. ask            install          repo-list
  234. autoclean      install-all      repo-pkg-scope
  235. bleeding-edge  list-deps        repo-update
  236. clean          list-downloaded  search
  237. contents       list-installed   search-all
  238. deb2pet        names            sfs2pet
  239. delete         names-all        sfs-combine
  240. delete-all     names-exact      show-config
  241. deps           names-exact-all  split
  242. deps-all       pet2sfs          tgz2pet
  243. deps-check     pet2tgz          uninstall
  244. deps-download  pet2txz          uninstall-all
  245. dir2pet        build            unpack
  246. dir2sfs        build-list       update-sources
  247. dir2tgz        pkg-combine      version
  248. download       installed        which
  249. examples       repack           which-repo
  250. extract        status           rdep-check
  251. force          update           what-needs
  252. func-list      workdir          rm-repo
  253. txz2pet        dir2repo
  254.  
  255. Usage: ${bold}$SELF usage CMD${endcolour}"
  256.     #cd /usr/share/pkg/docs/usage/; echo `ls` | fold -w 70 -s
  257.   fi
  258.   exit 1
  259. }
  260.  
  261.  
  262. func_list(){                      # list all functions available in this script FUNCLIST
  263.   [ -f $TMPDIR/func_list ] && cat $TMPDIR/func_list | sort && exit 0
  264.   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
  265.   cat $TMPDIR/func_list | sort
  266.   exit 0
  267. }
  268.  
  269.  
  270. get_pkg_ext(){                    # return file extension of $1 FUNCLIST
  271.  
  272.   # get valid usage or exit
  273.   [ ! "$1" ] && echo 'Usage: get_pkg_ext PKGNAME' && exit 1
  274.  
  275.   local pkg_installed=''
  276.   local pkg_filename=''
  277.   local pkg_ext=''
  278.   local pkg_name=''
  279.  
  280.   # check given file type, see if it matches our supported pkg types
  281.   case "$1" in
  282.     *.bz2)        echo bz2        ;;
  283.     *.pet)        echo pet        ;;
  284.     *.deb)        echo deb        ;;
  285.     *.pkg.tar.gz) echo pkg.tar.gz ;;
  286.     *.pkg.tar.xz) echo pkg.tar.xz ;;
  287.     *.pkg.tgz)    echo pkg.tgz    ;;
  288.     *.pkg.txz)    echo pkg.txz    ;;
  289.     *.tar.bz2)    echo tar.bz2    ;;
  290.     *.tar.lzma)   echo tar.lzma   ;;
  291.     *.tar.gz)     echo tar.gz     ;;
  292.     *.tar.xz)     echo tar.xz     ;;
  293.     *.tbz)        echo tbz        ;;
  294.     *.tgz)        echo tgz        ;;
  295.     *.tlz)        echo tlz        ;;
  296.     *.txz)        echo txz        ;;
  297.     *.gz)         echo gz         ;;
  298.     *.xz)         echo xz         ;;
  299.     *.rpm)        echo rpm        ;;
  300.     *.sfs)        echo sfs        ;;
  301.     #*.tcz)        echo tcz       ;;
  302.     #*.tpkg)       echo tpkg      ;;
  303.     #*.apk)        echo apk       ;;
  304.     *)
  305.       # if it's an installed pkg, get the extension from $HOME/.packages/*
  306.       pkg_installed=`list_installed_pkgs "$1" | grep -m1 "^$1"`
  307.  
  308.       # if $pkg_check not empty, then $1 is an installed pkg
  309.       if [ "$pkg_installed" != '' ];then
  310.         pkg_filename=`cut -f8 -d'|' $HOME/.packages/user-installed-packages |grep -m1 ^$1`
  311.         [ "$pkg_filename" = '' ] && pkg_filename=`cut -f8 -d'|' $HOME/.packages/*-installed-packages |grep -m1 ^$1`
  312.         [ "$pkg_filename" != '' ] && pkg_ext=`get_pkg_ext "$pkg_filename"`
  313.       else
  314.         # if it's a repo package, get it's extension from $HOME/.packages/*
  315.         pkg_filename=`grep -m1 "^$1" $HOME/.packages/Packages-* |cut -f8 -d'|'`
  316.        [ "$pkg_filename" != '' ] &&  pkg_ext=`get_pkg_ext "$pkg_filename"`
  317.       fi
  318.       [ "$pkg_ext" != '' ] && echo $pkg_ext
  319.       ;;
  320.   esac
  321. }
  322.  
  323.  
  324. get_pkg_version(){                # returns version of PKGNAME ($1) FUNCLIST
  325.  
  326.   # exit if no pkg given
  327.   [ ! "$1" ] && exit 1
  328.  
  329.   local PKGNAME=`get_pkg_name "$1"`
  330.   local PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  331.   local pkg_ext=`get_pkg_ext "$PKGNAME"`
  332.   local pkg_version=''
  333.  
  334.   PKGNAME=$(basename "$PKGNAME" .$pkg_ext)
  335.  
  336.   pkg_version=`echo $PKGNAME | sed -e "s/^${PKGNAME_ONLY}_//g"`
  337.   # if $pkg_version is same as $PKGNAME, try cutting again, using '-' instead of '_'
  338.   [ "$pkg_version" = "$PKGNAME" ] && pkg_version=`echo $PKGNAME | sed -e "s/^${PKGNAME_ONLY}-//g"`
  339.  
  340.   # now trim off the other unneeded bits
  341.   pkg_version=`echo  $pkg_version | sed \
  342.     -e "s/^-//g" \
  343.     -e "s/^_//g" \
  344.     -e "s/^DEV-//g" \
  345.     -e "s/^DOC-//g" \
  346.     -e "s/^NLS-//g" \
  347.     -e "s/^dev-//g" \
  348.     -e "s/^doc-//g" \
  349.     -e "s/^nls-//g" \
  350.     -e "s/+deb.*//g" \
  351.     -e "s/+ubuntu.*//g" \
  352.     -e "s/-i[346].*//g" \
  353.     -e "s/-x[86].*//g" \
  354.     -e "s/-pup.*//g" \
  355.     -e "s/-q[0-9].*//g" \
  356.     -e "s/-WITHDEPS.*//g" \
  357.     -e "s/-PLUSDEPS.*//g" \
  358.     -e 's@\\\@@g'`
  359.  
  360.   # get $REPONAME and $EX
  361.   . ${PKGRC}
  362.  
  363.   [ "$pkg_ext" = '' ] && pkg_ext=$EX
  364.  
  365.   echo $pkg_version
  366.   return 0
  367. }
  368.  
  369.  
  370. get_pkg_name_only_from_ext(){     # return package name from $1 (must include file extension)
  371.  
  372.   # get valid usage or exit
  373.   [ ! "$1" ] && echo 'Usage: get_pkg_name_only_from_ext <PKG_FILENAME>' && exit 1
  374.  
  375.   # get the extension of the given package name, if any
  376.   local pkg_ext=`get_pkg_ext "$1"`
  377.   # get the name without path and extension
  378.   local pkg_name=`basename "$1" .$pkg_ext`
  379.   local PKGNAME=''
  380.   local PKGNAME_ONLY=''
  381.   local DB_pkgrelease=''
  382.   local prPATTERN=''
  383.   local DB_version=''
  384.   local xDB_version=''
  385.   local xPATTERN=''
  386.  
  387.   #split PKGMAIN, ex: FULLPKGNAME=xvidtune-1.0.1-i486-1.tgz has PKGNAME=xvidtune-1.0.1
  388.   case $pkg_ext in
  389.     deb)
  390.      #deb ex: xsltproc_1.1.24-1ubuntu2_i386.deb  xserver-common_1.5.2-2ubuntu3_all.deb
  391.      PKGNAME_ONLY="`echo -n "$pkg_name" | cut -f 1 -d '_'`"
  392.      DB_pkgrelease="`echo -n "$pkg_name" | rev | cut -f 2 -d '_' | cut -f 1 -d '-' | rev`"
  393.      prPATTERN="s%\\-${DB_pkgrelease}.*%%"
  394.      PKGNAME="`echo -n "$pkg_name" | sed -e "$prPATTERN" 2>/dev/null`"
  395.     ;;
  396.     pet)
  397.      PKGNAME="$pkg_name"
  398.      DB_version="`echo -n "$PKGNAME" | grep -o '\\-[0-9].*' | sed -e 's%^\-%%'`"
  399.      xDB_version="`echo -n "$DB_version" | sed -e 's%\\-%\\\\-%g' -e 's%\\.%\\\\.%g'`"
  400.      xPATTERN="s%${xDB_version}%%"
  401.      PKGNAME_ONLY="`echo -n "$PKGNAME" | sed -e "$xPATTERN" -e 's%\\-$%%' 2>/dev/null`"
  402.     ;;
  403.     tgz|txz)
  404.      #slack ex: xvidtune-1.0.1-i486-1.tgz  printproto-1.0.4-noarch-1.tgz
  405.      PKGNAME="`echo -n "$pkg_name" | sed -e 's%\\-i[3456]86.*%%' -e 's%\\-noarch.*%%'`"
  406.      DB_version="`echo -n "$PKGNAME" | grep -o '\\-[0-9].*' | sed -e 's%^\\-%%'`"
  407.      xDB_version="`echo -n "$DB_version" | sed -e 's%\\-%\\\\-%g' -e 's%\\.%\\\\.%g'`"
  408.      xPATTERN="s%${xDB_version}%%"
  409.      PKGNAME_ONLY="`echo -n "$PKGNAME" | sed -e "$xPATTERN" -e 's%\-$%%' 2>/dev/null`"
  410.     ;;
  411.     tar.gz)
  412.      #arch ex: xproto-7.0.14-1-i686.pkg.tar.gz  trapproto-3.4.3-1.pkg.tar.gz
  413.      PKGNAME="`echo -n "$pkg_name" | sed -e 's%\\-i[3456]86.*%%' -e 's%\\.pkg$%%' | rev | cut -f 2-9 -d '-' | rev`"
  414.      DB_version="`echo -n "$PKGNAME" | grep -o '\\-[0-9].*' | sed -e 's%^\\-%%'`"
  415.      xDB_version="`echo -n "$DB_version" | sed -e 's%\\-%\\\\-%g' -e 's%\\.%\\\\.%g'`"
  416.      xPATTERN="s%${xDB_version}%%"
  417.      PKGNAME_ONLY="`echo -n "$PKGNAME" | sed -e "$xPATTERN" -e 's%\\-$%%' 2>/dev/null`"
  418.     ;;
  419.     rpm) #110523
  420.      #exs: hunspell-fr-3.4-1.1.el6.noarch.rpm
  421.      PKGNAME="$pkg_name"
  422.      DB_version="`echo -n "$PKGNAME" | grep -o '\\-[0-9].*' | sed -e 's%^\-%%'`"
  423.      xDB_version="`echo -n "$DB_version" | sed -e 's%\\-%\\\\-%g' -e 's%\\.%\\\\.%g'`"
  424.      xPATTERN="s%${xDB_version}%%"
  425.      PKGNAME_ONLY="`echo -n "$PKGNAME" | sed -e "$xPATTERN" -e 's%\\-$%%' 2>/dev/null`"
  426.     ;;
  427.   esac
  428.   [ "$PKGNAME_ONLY" != '' ] && echo $PKGNAME_ONLY || echo "$1"
  429. }
  430.  
  431.  
  432. get_pkg_name_only(){              # return pkg name only from $1, with no version or suffix FUNCLIST
  433.  
  434.   # exit if no valid options
  435.   [ ! "$1" ] && exit 1
  436.  
  437.   # get config settings, inc current repo file
  438.   #. ${PKGRC}
  439.  
  440.   local pkg_name=''
  441.   local pkg_name_only=''
  442.   local name_only=''
  443.   local pkg_ext=`get_pkg_ext "$1"`
  444.   local repo_of_pkg=''
  445.   local repo_pkg_with_ext=''
  446.   local repo_ext=$EX      # from $PKGRC
  447.  
  448.   # check the repos
  449.   local pkg_name_only="`grep -m1 "|${1}|" ${HOME}/.packages/Packages-* 2>/dev/null | cut -f2 -d'|'`"
  450.   [ "$pkg_name_only" = '' ] && pkg_name_only="`grep -m1 "^${1}|" ${HOME}/.packages/Packages-* 2>/dev/null | cut -f2 -d'|' | head -1`"
  451.   [ "$pkg_name_only" = '' ] && pkg_name_only="`grep -m1 "|${1}.${pkg_ext}|" ${HOME}/.packages/Packages-* 2>/dev/null | cut -f2 -d'|' | head -1`"
  452.  
  453.   pkg_name_only=${pkg_name_only// */}
  454.  
  455.   # if we found it, print it and exit
  456.   if [ "$pkg_name_only" != '' ];then
  457.       echo ${pkg_name_only}
  458.       return 0
  459.   fi
  460.  
  461.   # if not, but we have an extension, try  get_pkg_name_only_from_ext()
  462.   case $DISTRO_BINARY_COMPAT in ubuntu|trisquel|debian|devuan)
  463.     name_only=`get_pkg_name_only_from_ext "$(basename "${1}.${repo_ext}")"| head -1`
  464.     name_only=${name_only// */}
  465.     [ "$name_only" != '' -a "$name_only" != "$1" ] && echo $name_only && return 0
  466.     ;;
  467.   esac
  468.  
  469.   ## ...if we didn't find the name using the above, do the horrible checks below
  470.  
  471.   # replace the dash before the version number with an @ symbol.
  472.   # INFO: sed /-[^-][^+][^a-zA-Z][0-9.]*/ (hopefully?) replaces '-$VER' with '@'
  473.   # not including when $VER starts with a number/letter
  474.   pkg_name_only="`echo "$(basename "${1}")" | sed -e 's/-[^-][^+][^a-zA-Z][0-9.]*/@/'`"
  475.  
  476.   # do 'ioquake3+u20130504' => 'oquake3'
  477.   pkg_name_only="`echo "${pkg_name_only}" | sed -e 's/+[a-z]/@/'`"
  478.  
  479.   # note, we haven't cut away version numbers preceeded with underscores (_) yet
  480.  
  481.   # if the version number was preceeded by an underscore, the version
  482.   # will still be in the $pkg_name_only - pkgname_2.3.4 - so cut it out
  483.   if [ "`echo "$pkg_name_only" | grep -o "_[0-9]\."`" != '' ];then
  484.     pkg_name_only="`echo "$pkg_name_only" | sed -e 's/_[^a-z][^A-Z][0-9.]*/@/'`"
  485.  
  486.   # maybe the underscore preceeds a version that starts with a letter
  487.   elif [ "`echo "$pkg_name_only" | grep -o "_[a-zA-Z][0-9]\."`" != '' ];then
  488.     pkg_name_only="`echo "$pkg_name_only" | sed -e 's/_[a-zA-Z][0-9.]*/@/'`"
  489.   fi
  490.  
  491.   # now cut away everything after the @, that will leave only the package name
  492.   pkg_name_only1="${pkg_name_only/@*/}"
  493.   pkg_name_only="$pkg_name_only1"
  494.  
  495.   # we might still have foo_bar_v1.2.3, so lets chop off * after the last _
  496.   if [ "`echo "$pkg_name_only" | grep -o "_[a-zA-Z][0-9]\."`" != '' ];then
  497.     pkg_name_only="${pkg_name_only/_[a-zA-Z][0-9]*/}"
  498.   fi
  499.  
  500.   # another chop, we might have foo_bar_vv1.2.3, so lets chop off
  501.   # everything after the last underscore, if we still have version numbers
  502.   if [ "`echo "$pkg_name_only" | grep -o "_[a-zA-Z][a-zA-Z][0-9]\."`" != '' ];then
  503.     pkg_name_only="${pkg_name_only/_[a-zA-Z][a-zA-Z][0-9]*/}"
  504.   fi
  505.  
  506.   # we might still have foo_bar_vvv1.2.3, so lets chop off
  507.   # everything after the last underscore, if we still have version numbers
  508.   if [ "`echo "$pkg_name_only" | grep -o "_[a-zA-Z*][0-9]\."`" != '' ];then
  509.     pkg_name_only="${pkg_name_only/_[a-zA-Z*][0-9]*/}"
  510.   fi
  511.  
  512.   # chop again, we might have abc_xwy-zzz1.2.3-blah-etc, so lets chop off
  513.   # everything after the last dash-before-number
  514.   if [ "`echo "$pkg_name_only" | grep -o "\-[a-zA-Z*]*[0-9]\."`" != '' ];then
  515.     pkg_name_only="${pkg_name_only/-[a-zA-Z*]*[0-9]*/}"
  516.   fi
  517.  
  518.   # another fix, if we still have PKGNAME-1.2, remove the '-1.2'
  519.   if [ "`echo "$pkg_name_only" | grep -o "\-[0-9]"`" != '' ];then
  520.     pkg_name_only="${pkg_name_only/-[0-9]*/}"
  521.   fi
  522.  
  523.   # the sed bit changes 'liblzma5+20120614' to 'liblzma5'
  524.   [ "$pkg_name_only" != '' ] && echo $pkg_name_only | sed -e 's/++/+++/g' -e 's/+[a-z0-9].*//g' || return 1
  525. }
  526.  
  527.  
  528. get_pkg_name(){                   # return full pkg name (with version) from $1 FUNCLIST
  529.  
  530.   # get config settings, inc current repo file
  531.   #. ${PKGRC}
  532.  
  533.   local pkg_ext=''
  534.   local pkg_name=''
  535.   local full_name=''
  536.   local supported_repo_files=''
  537.   local repo_contents=''
  538.  
  539.   # get the extension of the given package name, if any
  540.   local pkg_ext=`get_pkg_ext "$1"`
  541.   # get the name without path and extension
  542.   local pkg_name="`basename "$1" .$pkg_ext`"
  543.  
  544.   # get the repos files in the correct (fall back) order, then add file paths
  545.   supported_repo_files="`repo_file_list | sed -e "s#^#$HOME/.packages/#g"`"
  546.  
  547.   # get the relevant repo contents (fields 1,2,8) from all repos,
  548.   # then add pipes to line start/end for easier parsing later, then do
  549.   # a basic search, for $pkg* (we'll refine it later), to avoid 'Argument list too long'..
  550.   cut -f1,2,8 $HOME/.packages/user-installed-packages $HOME/.packages/woof-installed-packages $supported_repo_files | sed -e "s/^/|/g" -e "s/$/|/g" 2>/dev/null | grep -i "|$pkg_name" > ${TMPDIR}repo_contents
  551.  
  552.   # get fields 1,2 and 8 (the 3 name fields) from all supported repo files, then
  553.   # add '|' to start and end of lines, then find exact match of $pkg_name..
  554.   # if no exact match, look for $pkg_name-*, then $pkg_name_*
  555.   if [ -f ${TMPDIR}repo_contents ];then
  556.     full_name=`cat ${TMPDIR}repo_contents 2>/dev/null| grep -m1 "|${pkg_name}|"` \
  557.       || full_name=`cat ${TMPDIR}repo_contents 2>/dev/null|grep -m1 "|${pkg_name//-*/}|"` \
  558.       || full_name=`cat ${TMPDIR}repo_contents 2>/dev/null|grep -m1 "|${pkg_name//_*/}|"` \
  559.       || full_name=`cat ${TMPDIR}repo_contents 2>/dev/null|grep -m1 "|${pkg_name}-"` \
  560.       || full_name=`cat ${TMPDIR}repo_contents 2>/dev/null|grep -m1 "|${pkg_name}_"` \
  561.       || full_name=`cat ${TMPDIR}repo_contents 2>/dev/null|grep -m1 "|${pkg_name}"` \
  562.       || full_name=`cat ${TMPDIR}repo_contents 2>/dev/null|grep -m1 "^${pkg_name}|"` \
  563.       || full_name=`cat ${TMPDIR}repo_contents 2>/dev/null|grep -m1 "^${pkg_name}-"` \
  564.       || full_name=`cat ${TMPDIR}repo_contents 2>/dev/null|grep -m1 "^${pkg_name}_"`
  565.  
  566.     rm ${TMPDIR}repo_contents &>/dev/null
  567.   fi
  568.  
  569.   # if we found pkg in repo, keep only the package name ('|pkg-123|pkg|blah|' -> 'pkg-123')
  570.   [ "$full_name" != '' ] && full_name=`echo $full_name| cut -f2 -d '|' | tr -d '|'` || full_name=$pkg_name
  571.  
  572.   # if we have a package name, return it here and leave the func
  573.   [ "$full_name" != '' ] && echo $full_name && return 0
  574.  
  575.   # if no pkg found in repos, we cant translate/lookup its longer name,
  576.   # so just return what we got, without path, without pkg extension
  577.   [ "$full_name" = '' ] && echo "`basename "$pkg_name" .$pkg_ext`"
  578. }
  579.  
  580.  
  581. is_blacklisted_pkg(){             # return true if PKGNAME ($1) is blacklisted FUNCLIST
  582.   # exit if no valid options
  583.   [ ! "$1" -o "$1" = "-" ] && print_usage pkg-installed && exit 1
  584.  
  585.   local EX=''
  586.   local PKGNAME=''
  587.   local PKGNAME_ONLY=''
  588.  
  589.   # get pkg extension
  590.   EX=`get_pkg_ext "$1"`
  591.  
  592.   # strip away extension and path
  593.   PKGNAME="$(basename "$1" .$EX)"
  594.   #PKGNAME=`get_pkg_name "$PKGNAME"`
  595.  
  596.   # get the package name without version
  597.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  598.  
  599.   [ "`echo "$PKG_NAME_IGNORE" | grep "$PKGNAME_ONLY "`" != '' ] && echo true || echo false
  600. }
  601.  
  602.  
  603. is_installed_pkg(){               # return true if PKGNAME ($1) is installed, else false FUNCLIST
  604.  
  605.   # exit if no valid options
  606.   [ ! "$1" -o "$1" = "-" ] && print_usage pkg-installed && exit 1
  607.  
  608.   local EX=''
  609.   local PKGNAME=''
  610.   local PKGNAME_ONLY=''
  611.   local check=''
  612.  
  613.   # get pkg extension
  614.   EX=`get_pkg_ext "$1"`
  615.  
  616.   # strip away extension and path
  617.   PKGNAME="$(basename "$1" .$EX)"
  618.   PKGNAME=`get_pkg_name "$PKGNAME"`
  619.  
  620.   # we cant rely on the strings the user passes us, so...
  621.  
  622.   # get the package name without version
  623.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  624.  
  625.   # exit if no valid package name
  626.   [ "$PKGNAME" = '' -o "$PKGNAME_ONLY" = '' ] && print_usage pkg-installed && exit 1
  627.  
  628.   # check the $HOME/.packages/*.files
  629.   check="`find ${HOME}/.packages/ -name "$PKGNAME.files"`"
  630.   [ "$check" = '' ] && check="`find ${HOME}/.packages/ -name "${PKGNAME}_*.files"`"
  631.   [ "$check" = '' ] && check="`find ${HOME}/.packages/ -name "${PKGNAME}-*.files"`"
  632.   [ "$check" = '' ] && check="`find ${HOME}/.packages/ -name "${PKGNAME}*.files"`"
  633.  
  634.   # slow but reliable & simple: separately check fields 1,2,8 of the various files listing installed pkgs
  635.   [ "$check" = '' ] && check="`cut -f1 -d'|' ${HOME}/.packages/*-installed-packages 2>/dev/null| grep -m1 "^$PKGNAME\$"`"
  636.   [ "$check" = '' ] && check="`cut -f1,2 -d'|' ${HOME}/.packages/*-installed-packages 2>/dev/null| grep -m1 "^$PKGNAME|" | grep -m1 "|$PKGNAME_ONLY\$"`"
  637.   [ "$check" = '' ] && check="`cut -f8 -d'|' ${HOME}/.packages/*-installed-packages 2>/dev/null| grep -m1 "^$PKGNAME"`"
  638.   [ "$check" = '' ] && check="`HIDE_BUILTINS=false list_installed_pkgs | grep -m1 ^"$PKGNAME"`"
  639.  
  640.   # if any checks above returned a result, $check will not be empty (ash didn't like grep -q, not sure why?)
  641.   [ "$check" != '' ] && echo true || echo false
  642. }
  643.  
  644.  
  645. is_builtin_pkg (){                # return true if $1 is a builtin pkg, else false FUNCLIST
  646.  
  647.   # if $1 is an exact match of a pkg short name,
  648.   # long name or file name in woof-installed-packages,
  649.   # we will return true, else we return false
  650.  
  651.   # exit if no valid input
  652.   [ ! "$1"  ] && exit 1
  653.  
  654.   local pkg_builtin=''
  655.   local pkg_name_only=''
  656.  
  657.   pkg_builtin="`cut -f1,2,8 -d '|' ${HOME}/.packages/woof-installed-packages 2>/dev/null\
  658.    | sed -e 's@^@|@' -e 's@$@|@' \
  659.    | grep -m1 "|$1|"`"
  660.  
  661.   # try again.. if user gave only partial name (geany-1.27), the above would fail
  662.   if [ "$pkg_builtin" = '' ];then
  663.     pkg_name_only=`get_pkg_name_only "$1"`
  664.  
  665.     # so search for pkg_name* AND pkg_name_only (exact match, should be in field 2)
  666.     if [ "$pkg_name_only" != '' ];then
  667.       pkg_builtin="`cut -f1,2,8 -d '|' ${HOME}/.packages/woof-installed-packages 2>/dev/null\
  668.        | sed -e 's@^@|@' -e 's@$@|@' \
  669.        | grep "|$1" \
  670.        | grep -m1 "|$pkg_name_only|"`"
  671.     fi
  672.   fi
  673.  
  674.   # print result
  675.   [ "$pkg_builtin" != '' ] && echo true || echo false
  676. }
  677.  
  678.  
  679. is_devx_pkg (){                   # return true if $1 is a pkg in the devx, else false FUNCLIST
  680.  
  681.   # if $1 is an exact match of a pkg short name,
  682.   # long name or file name in devx-only-installed-packages,
  683.   # we will return true, else we return false
  684.  
  685.   # exit if no valid input
  686.   [ ! "$1"  ] && exit 1
  687.  
  688.   local devx_pkg=''
  689.   local pkg_name_only=''
  690.  
  691.   devx_pkg="`cut -f1,2,8 -d '|' ${HOME}/.packages/devx-only-installed-packages 2>/dev/null\
  692.    | sed -e 's@^@|@' -e 's@$@|@' \
  693.    | grep -m1 "|$1|"`"
  694.  
  695.   # try again.. if user gave only partial name (geany-1.27), the above would fail
  696.   if [ "$devx_pkg" = '' ];then
  697.     pkg_name_only=`get_pkg_name_only "$1"`
  698.  
  699.     # so search for pkg_name* AND |pkg_name_only| (exact match, should be in field 2)
  700.     if [ "$pkg_name_only" != '' ];then
  701.       devx_pkg="`cut -f1,2,8 -d '|' ${HOME}/.packages/devx-only-installed-packages 2>/dev/null\
  702.        | sed -e 's@^@|@' -e 's@$@|@' \
  703.        | grep "|$1" \
  704.        | grep -m1 "|$pkg_name_only|"`"
  705.     fi
  706.   fi
  707.  
  708.   # print result
  709.   [ "$devx_pkg" != '' ] && echo true || echo false
  710. }
  711.  
  712.  
  713. is_usr_pkg (){                    # return true if $1 is a user installed pkg, else false FUNCLIST
  714.  
  715.   # if $1 is an exact match of a pkg short name,
  716.   # long name or file name in user-installed-packages,
  717.   # we will return true, else we return false
  718.  
  719.   # exit if no valid input
  720.   [ ! "$1"  ] && exit 1
  721.  
  722.   local usr_pkg=''
  723.   local pkg_name_only=''
  724.  
  725.   usr_pkg="`cut -f1,2,8 -d '|' ${HOME}/.packages/user-installed-packages 2>/dev/null\
  726.    | sed -e 's@^@|@' -e 's@$@|@' \
  727.    | grep -m1 "|$1|"`"
  728.  
  729.   # try again.. if user gave only partial name (geany-1.27), the above would fail
  730.   if [ "$usr_pkg" = '' ];then
  731.     pkg_name_only=`get_pkg_name_only "$1"`
  732.  
  733.     # so search for pkg_name* AND |pkg_name_only| (exact match, should be in field 2)
  734.     if [ "$pkg_name_only" != '' ];then
  735.       usr_pkg="`cut -f1,2,8 -d '|' ${HOME}/.packages/user-installed-packages 2>/dev/null\
  736.        | sed -e 's@^@|@' -e 's@$@|@' \
  737.        | grep "|$1" \
  738.        | grep -m1 "|$pkg_name_only|"`"
  739.     fi
  740.   fi
  741.  
  742.   # print result
  743.   [ "$usr_pkg" != '' ] && echo true || echo false
  744. }
  745.  
  746.  
  747. is_repo_pkg (){                   # return true if $1 is a pkg in a supported repo, else false FUNCLIST
  748.  
  749.   # if $1 is an exact match of a pkg short name,
  750.   # long name or file name in Packages-*-
  751.   # we will return true, else we return false
  752.  
  753.   # exit if no valid input
  754.   [ ! "$1"  ] && exit 1
  755.  
  756.   local repo_pkg=''
  757.   local pkg_name_only=''
  758.   local all_supported_repofiles
  759.  
  760.   # all repo files, full paths
  761.   all_supported_repofiles=`repo_file_list | sed "s#^#$HOME/.packages/#" | tr '\n' ' '`
  762.  
  763.   # search all repo files for the $1
  764.   repo_pkg="`cut -f1,2,8 -d '|' $all_supported_repofiles 2>/dev/null\
  765.    | sed -e 's/ //g' -e 's@^@|@' -e 's@$@|@' \
  766.    | grep -m1 "|${1}|"`"
  767.  
  768.   # search all repo files for the $1*
  769.   [ "$repo_pkg" = '' ] \
  770.     && repo_pkg="`cut -f1,2,8 -d '|' $all_supported_repofiles 2>/dev/null\
  771.    | sed -e 's/ //g' -e 's@^@|@' -e 's@$@|@' \
  772.    | grep -m1 "|${1}-\?_\?[0-9.a-zA-Z]*|"`"
  773.  
  774.   # try again.. if user gave only partial name (geany-1.27), the above would fail
  775.   if [ "$repo_pkg" = '' ];then
  776.     pkg_name_only=`get_pkg_name_only "$1"`
  777.  
  778.     # so search for pkg_name* AND |pkg_name_only| (exact match, should be in field 2)
  779.     if [ "$pkg_name_only" != '' ];then
  780.       repo_pkg="`cut -f1,2,8 -d '|' $all_supported_repofiles 2>/dev/null\
  781.        | sed -e 's@^@|@' -e 's@$@|@' \
  782.        | grep "|$1" \
  783.        | grep -m1 "|$pkg_name_only|"`"
  784.     fi
  785.   fi
  786.  
  787.   # print result
  788.   [ "$repo_pkg" != '' ] && echo true || echo false
  789. }
  790.  
  791.  
  792. is_current_repo_pkg(){            # takes $PKGNAME ($1), returns true or false FUNCLIST
  793.  
  794.   # exit if no valid input
  795.   [ ! "$1"  ] && exit 1
  796.  
  797.   # get current repo ($REPOFILE)
  798.   . ${PKGRC}
  799.  
  800.   local pkg_in_repo
  801.  
  802.   # check if given pkg ($1) is in the current repo
  803.   pkg_in_repo="`LANG=C cut -f1,2,7,8 -d'|' ${HOME}/.packages/$REPOFILE 2>/dev/null | sed -e "s/^/|/" -e "s/$/|/" | grep -m1 "|${1}|"`"
  804.  
  805.   # print msg
  806.   [ "$pkg_in_repo" != '' ] && echo true || echo false
  807. }
  808.  
  809.  
  810. is_local_pkg(){                   # returns true if $1 is local package file FUNCLIST
  811.  
  812.   # exit if no valid input
  813.   [ ! "$1"  ] && exit 1
  814.  
  815.   . ${PKGRC}
  816.  
  817.   # if $1 is a local file with a supported package extension
  818.   # then we return true, else, we return false
  819.  
  820.   local is_file=''
  821.   local is_local_pkg=false
  822.  
  823.   # first, check we have a local file
  824.   if [ -f "$1" ];then
  825.  
  826.     # file *probably* has an extension, lets try to get it..
  827.     # the func get_pkg_ext() will return empty if not a valid package extension
  828.     file_ext=`get_pkg_ext "$1"`
  829.  
  830.     # if not empty, it must be a local file, with valid pkg ext (a local pkg)
  831.     [ "$file_ext" != '' ] && is_local_pkg=true
  832.  
  833.   elif [ -f "$CURDIR/$1" ];then
  834.  
  835.     file_ext=`get_pkg_ext "$CURDIR/$1"`
  836.     [ "$file_ext" != '' ] && is_local_pkg=true
  837.  
  838.   elif [ -f "$WORKDIR/$1" ];then
  839.  
  840.     file_ext=`get_pkg_ext "$CURDIR/$1"`
  841.     [ "$file_ext" != '' ] && is_local_pkg=true
  842.  
  843.   fi
  844.  
  845.   # print output
  846.   echo $is_local_pkg
  847. }
  848.  
  849.  
  850.  
  851. # RC file funcs
  852.  
  853. set_workdir(){                    # set new WORKDIR in rc file location FUNCLIST
  854.  
  855.   # get latest rc file values
  856.   . ${PKGRC}
  857.  
  858.   # make sure $1 was given and doesn't already exist
  859.   [ ! "$1"  ] && print_usage workdir && exit 1
  860.   [ -e "$1" ] && echo "Error: directory already exists. Choose a new one." && exit 1
  861.  
  862.   # create the dir
  863.   mkdir -p "$1" 2>/dev/null
  864.  
  865.   # if dir not created, exit with error
  866.   [ $? -eq 1  ] && echo "Error: Could not create $1" && exit 1
  867.   [ ! -d "$1" ] && echo "Error: Could not create directory:  $1" && exit 1
  868.  
  869.   # dir was created, so lets copy our pkgs in there
  870.   list_downloaded_pkgs | while read pkg_file
  871.   do
  872.     cp -v "$WORKDIR/$pkg_name" "$1/"
  873.   done
  874.  
  875.   # if copying everything to new dir failed
  876.   if [ $? -eq 1 ];then
  877.     # print msg
  878.     echo -e "${yellow}Warning:${endcolour}Could not copy packages from $WORKDIR to $1.."
  879.     echo "You should copy all packages in $WORKDIR into $1."
  880.   fi
  881.  
  882.   # update the RC file
  883.   WORKDIR="$1"
  884.   set_config
  885.   echo "Success. Work directory updated."
  886.   exit 0
  887. }
  888.  
  889.  
  890. set_pkg_scope(){                  # one|all set search for pkgs in current repo or all FUNCLIST
  891.  
  892.   # get old values, any we dont set here are not overwritten
  893.   . ${PKGRC}
  894.  
  895.   [ "$1" != "" ] && PKGSCOPE="$1" || PKGSCOPE="$PKGSCOPE"
  896.  
  897.   case "$1" in
  898.     all)
  899.     # set pkg search to all
  900.     PKGSEARCH="list_all_pkg_names"
  901.     PKGSEARCHEXACT="$SELF -nea"
  902.     echo -e "${green}Success:${endcolour} Find packages in all repos."
  903.     ;;
  904.     one)
  905.     # set pkg search to current only
  906.     PKGSEARCH="list_pkg_names"
  907.     PKGSEARCHEXACT="$SELF -ne"
  908.     echo -e "${green}Success:${endcolour} Find packages in current repo ($REPONAME) only."
  909.     ;;
  910.     *)
  911.     PKGSCOPE="one"
  912.     # set pkg search to current only
  913.     PKGSEARCH="list_pkg_names"
  914.     PKGSEARCHEXACT="$SELF -ne"
  915.     echo "Find packages in current repo ($REPONAME) only."
  916.     ;;
  917.   esac
  918.  
  919.   set_config #170213
  920. }
  921.  
  922.  
  923. set_dep_scope(){                  # all|one set search for deps in current repo or all FUNCLIST
  924.  
  925.    # get RC file values, so any we dont set here are not overwritten
  926.    . ${PKGRC}
  927.  
  928.   # make sure we have a valid value
  929.   [ "$1" != "" ] && DEPSCOPE="$1" || DEPSCOPE="$DEPSCOPE"
  930.  
  931.   case "$1" in
  932.     all)
  933.     # set pkg search to all
  934.     DEPSEARCH="list_all_pkg_names"
  935.     DEPSEARCHEXACT="$SELF -nea"
  936.     echo -e "${green}Success:${endcolour} Find dependencies in all repos."
  937.     ;;
  938.     one)
  939.     # set pkg search to one
  940.     DEPSEARCH="list_pkg_names"
  941.     DEPSEARCHEXACT="$SELF -ne"
  942.     echo -e "${green}Success:${endcolour} Find dependencies in current repo ($REPONAME) only."
  943.     ;;
  944.   esac
  945.  
  946.   set_config
  947. }
  948.  
  949.  
  950. set_recursive_deps(){             # yes|no search for deps of deps or not FUNCLIST
  951.   [ "`echo $1 | grep -E "^yes\$|^no\$"`" = "" ] && print_usage recursive-dep-check && exit 1
  952.  
  953.   # get old values, any we dont set here are not overwritten
  954.   . ${PKGRC}
  955.  
  956.   # make sure we have a valid value
  957.   RDCHECK="$1"
  958.   [ "$RDCHECK" = "yes" -o "$RDCHECK" = "no" ] || RDCHECK=no
  959.  
  960.   set_config
  961.  
  962.   # print final msg
  963.   if [ "$1" = "yes" ];then
  964.     echo -e "${green}Success:${endcolour} 'Recursive dependency checking' enabled."
  965.   else
  966.     echo -e "${green}Success:${endcolour} 'Recursive dependency checking' disabled"
  967.   fi
  968. }
  969.  
  970.  
  971. set_bleeding_edge(){              # yes|no get latest pkgs, ignore fall backs FUNCLIST
  972.   [ "`echo $1 | grep -E "^yes\$|^no\$"`" = "" ] && echo usage bleeding-edge && exit 1
  973.  
  974.   #get old values, any we dont set here are not overwritten
  975.   . ${PKGRC}
  976.  
  977.   # make sure we have a valid value
  978.   BLEDGE="$1"
  979.   [ "$BLEDGE" = "yes" -o "$BLEDGE" = "no" ] || BLEDGE=no
  980.  
  981.   set_config
  982.  
  983.   # print final msg
  984.   if [ "$1" = "yes" ];then
  985.     echo -e "${green}Success:${endcolour} 'Bleeding edge' package search enabled."
  986.   else
  987.     echo -e "${green}Success:${endcolour} 'Bleeding edge' package search disabled."
  988.   fi
  989. }
  990.  
  991.  
  992. set_autoclean(){                  # yes|no auto delete installed packages from WORKDIR FUNCLIST
  993.   [ "`echo $1 | grep -E "^yes\$|^no\$"`" = "" ] && print_usage autoclean && exit 1
  994.  
  995.   #get old values, any we dont set here are not overwritten
  996.   . ${PKGRC}
  997.  
  998.   # make sure we have a valid value
  999.   AUTOCLEAN="$1"
  1000.   [ "$AUTOCLEAN" = "yes" -o "$AUTOCLEAN" = "no" ] || AUTOCLEAN=no
  1001.  
  1002.   set_config
  1003.  
  1004.   # print final msg
  1005.   if [ "$1" = "yes" ];then
  1006.     echo -e "${green}Success:${endcolour} Auto-remove installed packages ENABLED."
  1007.   else
  1008.     echo -e "${green}Success:${endcolour} Auto-remove installed packages DISABLED."
  1009.   fi
  1010. }
  1011.  
  1012.  
  1013. set_config(){                     # update all options in config file FUNCLIST
  1014.   echo "WORKDIR=$WORKDIR"                    > ${PKGRC}
  1015.   echo "REPONAME=$REPONAME"                 >> ${PKGRC}
  1016.   echo "EX=$EX"                             >> ${PKGRC}
  1017.   echo "REPOFILE=$REPOFILE"                 >> ${PKGRC}
  1018.   echo "REPOURL1=$REPOURL1"                 >> ${PKGRC}
  1019.   echo "REPOURL2=$REPOURL2"                 >> ${PKGRC}
  1020.   #140213 add all urls
  1021.   echo "REPOURL3=$REPOURL3"                 >> ${PKGRC}
  1022.   echo "REPOURL4=$REPOURL4"                 >> ${PKGRC}
  1023.   # seach settings
  1024.   echo "PKGSEARCH=\"$PKGSEARCH\""           >> ${PKGRC}
  1025.   echo "PKGSEARCHEXACT=\"$PKGSEARCHEXACT\"" >> ${PKGRC}
  1026.   echo "DEPSEARCH=\"$DEPSEARCH\""           >> ${PKGRC}
  1027.   echo "DEPSEARCHEXACT=\"$DEPSEARCHEXACT\"" >> ${PKGRC}
  1028.   # multiple repo settings
  1029.   echo "REPOFALLBACKS=\"$REPOFALLBACKS\""   >> ${PKGRC}
  1030.   echo "PKGSCOPE=\"$PKGSCOPE\""             >> ${PKGRC}
  1031.   echo "DEPSCOPE=\"$DEPSCOPE\""             >> ${PKGRC}
  1032.   echo "BLEDGE=\"$BLEDGE\""                 >> ${PKGRC}
  1033.   echo "RDCHECK=\"$RDCHECK\""               >> ${PKGRC} #150813
  1034.   echo "AUTOCLEAN=\"$AUTOCLEAN\""           >> ${PKGRC}
  1035.   echo "BUILDTOOL=$BUILDTOOL"               >> ${PKGRC}
  1036. }
  1037.  
  1038.  
  1039. show_config(){                    # show config settings from ~/.pkg/pkgrc FUNCLIST
  1040.   . ${PKGRC}
  1041.  
  1042.   # set default values
  1043.   PKGSCOPETXT="Search for packages in all repos."
  1044.   DEPSCOPETXT="Search for dependencies in all repos."
  1045.   FALLBACKTXT="Accessing other repos in this order:"
  1046.   FALLBACKTXT="$FALLBACKTXT\n`repo_list | grep -v "^$REPONAME" | tr '\n' ' ' | sed -e "s/ /, /g" -e "s/, $//" | fold -w 54 -s`"
  1047.   RDCHECKTXT="Recursive dependency search is NOT enabled."
  1048.  
  1049.   # update with values from PKGRC file
  1050.   [ "$PKGSCOPE" = "one" ] && PKGSCOPETXT="Search for packages in current repo only."
  1051.   [ "$DEPSCOPE" = "one" ] && DEPSCOPETXT="Search for dependencies in current repo only."
  1052.   [ "$BLEDGE"   = "yes" ] && FALLBACKTXT="Bleeding-edge enabled - searching all repos, for the latest packages versions.."
  1053.   [ "$RDCHECK"  = "yes" ] && RDCHECKTXT="Recursive dependency search is enabled."
  1054.  
  1055.   # print current Pkg config
  1056.   echo "==========================="
  1057.   echo "$APPNAME $APPVER"
  1058.   echo "==========================="
  1059.   echo "Config file:  $PKGRC"
  1060.   echo "Packages dir: ${WORKDIR}/"
  1061.   echo "Autoclean:    $AUTOCLEAN"
  1062.   echo
  1063.   echo "Search settings:"
  1064.   #echo "- $HIDEINSTALLEDTXT"
  1065.   echo "- $PKGSCOPETXT"
  1066.   echo "- $DEPSCOPETXT"
  1067.   echo "- $RDCHECKTXT"
  1068.   echo
  1069.   echo "Package Compiling backend:"
  1070.   echo "- $BUILDTOOL"
  1071.   echo
  1072.   echo "Repo details:"
  1073.   echo "- Current Repo: $REPONAME"
  1074.   echo "- Package type: $EX"
  1075.   echo "- Packages:     `cat ${HOME}/.packages/${REPOFILE} | wc -l`"
  1076.   echo "- Mirror 1:     `echo $REPOURL1 | cut -f1-3 -d'/' `"
  1077.   [ "$REPOURL2" != "" ] && echo "- Mirror 2:     `echo $REPOURL2 | cut -f1-3 -d'/' `"
  1078.   [ "$REPOURL3" != "" ] && echo "- Mirror 3:     `echo $REPOURL3 | cut -f1-3 -d'/' `"
  1079.   [ "$REPOURL4" != "" ] && echo "- Mirror 4:     `echo $REPOURL4 | cut -f1-3 -d'/' `"
  1080.   echo
  1081.   echo -e "$FALLBACKTXT"
  1082. }
  1083.  
  1084.  
  1085. # repo and source funcs
  1086.  
  1087. get_repo_info(){                  # return details of given repo name ($1) FUNCLIST
  1088.   [ ! "$1" -o "$1" = "-" ] && print_usage repo-info && exit 1
  1089.  
  1090.   REPOLINE="`list_all_sources $1 | sort -u | uniq`" #170214 always get latest info, not info from rcfile
  1091.  
  1092.   # on first run, this might be needed to set the repo correctly
  1093.   [ "$REPOLINE" = '' ] && REPOLINE="`list_all_sources noarch`"
  1094.  
  1095.   REPONAME="`echo $REPOLINE | cut -f1 -d'|'`"
  1096.         EX="`echo $REPOLINE | cut -f2 -d'|'`"
  1097.   REPOFILE="`echo $REPOLINE | cut -f3 -d'|'`"
  1098.   REPOURL1="`echo $REPOLINE | cut -f4 -d'|'`"
  1099.   REPOURL2="`echo $REPOLINE | cut -f5 -d'|'`"
  1100.   REPOURL3="`echo $REPOLINE | cut -f6 -d'|'`"
  1101.   REPOURL4="`echo $REPOLINE | cut -f7 -d'|'`"
  1102.   REPOFALLBACKS="`echo "$REPOLINE"| cut -f8 -d'|'`"
  1103. }
  1104.  
  1105.  
  1106. set_current_repo(){               # set the current repo to use, give repo name as $1 FUNCLIST
  1107.  
  1108.   # print usage if no valid options
  1109.   [ ! "$1" -o "$1" = "-" -o "$1" = "-h" -o "$1" = "--help" ] && print_usage repo && exit 1
  1110.  
  1111.   # get repo details from rc file
  1112.   . ${PKGRC}
  1113.  
  1114.   # remove the default mirror tmp file, we're changing repo and mirrors
  1115.   rm $TMPDIR/CURREPOURL 2>/dev/null
  1116.  
  1117.   # remove old repo files list (used in list_source_files)
  1118.   rm -f ${TMPDIR}source_files 2>/dev/null
  1119.  
  1120.   # if not updating the scopes or bleeding-edge,leave them as set in rcfile
  1121.   [ "$PKGSCOPE" = "" ] && PKGSCOPE="$PKGSCOPE"
  1122.   [ "$DEPSCOPE" = "" ] && DEPSCOPE="$DEPSCOPE"
  1123.   [ "$BLEDGE" = "" ]   && BLEDGE="$BLEDGE"
  1124.  
  1125.   # if nothing was found in rcfile, we need to set to default
  1126.   [ "`echo $PKGSCOPE | grep -E "^one\$|^all\$"`" = "" ] && PKGSCOPE="one"
  1127.   [ "`echo $DEPSCOPE | grep -E "^one\$|^all\$"`" = "" ] && DEPSCOPE="one"
  1128.   [ "$BLEDGE" = "" ] && BLEDGE="no"
  1129.  
  1130.   # check if $1 is valid repo
  1131.   if [ "$(LANG=C list_sources "$1")" != "" ];then
  1132.     # set repo details
  1133.     LANG=C get_repo_info "$1"
  1134.     LANG=C set_config #170213
  1135.     LANG=C update_sources 1>/dev/null #update the order of sources to match new fallback list of new current repo
  1136.     LANG=C print_repo_info "$1" #output msg, repo info
  1137.   else
  1138.     # not a valid repo, print message
  1139.     echo "The name '$1' is not a valid repo name. These are:"
  1140.     LANG=C repo_list #250613
  1141.   fi
  1142. }
  1143.  
  1144.  
  1145. repo_list(){                      # list names of all avail repos [optionally matching $1] FUNCLIST
  1146.  
  1147.   # we need to list these repos in the order defined in the RC file
  1148.   # so Pkg can 'fall back' to other repos in that order
  1149.  
  1150.   # get current config
  1151.   . ${PKGRC}
  1152.  
  1153.   # set vars for this func
  1154.   local list=''
  1155.   local repo_file=''
  1156.   local repo_names=`cut -f1 -d'|' ${HOME}/.pkg/sources`
  1157.   local current_fallback_list=`grep "^$REPONAME|" ${HOME}/.pkg/sources | cut -f8 -d'|' | grep -v "^\$"`
  1158.  
  1159.   # get current repo fallback list order.. note if repo not listed in fallback list, it will be appended after
  1160.   for line in $current_fallback_list
  1161.   do
  1162.     # get the repo file
  1163.     repo_file="`grep "^$line|" ${HOME}/.pkg/sources | cut -f3 -d'|'`"
  1164.     # add it to the list
  1165.     [ "$repo_file" != "" ] && list="$list$line "
  1166.   done
  1167.  
  1168.   # now add all other avail repos to the end of fallback list
  1169.   for repo_name in $repo_names
  1170.   do
  1171.     #if not blank, not already in the repo list and not the current repo ($REPONAME from rc file) then add to list
  1172.     [ "$repo_name" != "" -a "`echo "$list" | grep "$repo_name "`" = ""  -a "$repo_name" != "$REPONAME" ] && list="$list$repo_name "
  1173.   done
  1174.  
  1175.   # list the current repo first
  1176.   echo $REPONAME
  1177.   # then the other repos
  1178.   echo "$list" | tr ' ' '\n' | grep -v "^\$"
  1179. }
  1180.  
  1181.  
  1182. repo_file_list(){                 # list available repo files, $1 optional FUNCLIST
  1183.  
  1184.   # we need to list these repos in the order defined in the RC file
  1185.   # so Pkg can 'fall back' to other repos in that order
  1186.  
  1187.   # get current config
  1188.   . ${PKGRC}
  1189.  
  1190.   # set vars for this func
  1191.   local list=''
  1192.   local repo_file=''
  1193.   local repo_files=`cut -f3 -d'|' ${HOME}/.pkg/sources`
  1194.   local current_fallback_list=`grep "^$REPONAME|" ${HOME}/.pkg/sources | cut -f8 -d'|' | grep -v "^\$"`
  1195.  
  1196.   # get current repo fallback list order.. note if repo not listed in fallback list, it will be appended after
  1197.   for line in $current_fallback_list
  1198.   do
  1199.     # get the repo file
  1200.     repo_file="`grep "^$line|" ${HOME}/.pkg/sources | cut -f3 -d'|'`"
  1201.     # add it to the list
  1202.     [ "$repo_file" != "" ] && list="$list$repo_file "
  1203.   done
  1204.  
  1205.   # now add all other avail repos to the end of fallback list
  1206.   for repo_file in $repo_files
  1207.   do
  1208.     #if not blank, not already in the repo list and not the current repo ($REPONAME from rc file) then add to list
  1209.     [ "$repo_file" != "" -a "`echo "$list" | grep "$repo_file "`" = ""  -a "$repo_file" != "$REPOFILE" ] && list="$list$repo_file "
  1210.   done
  1211.  
  1212.   # list the current repo first
  1213.   echo $REPOFILE
  1214.   # then the other repos
  1215.   echo "$list" | tr ' ' '\n' | grep -v "^\$"
  1216. }
  1217.  
  1218.  
  1219. dir2repo(){                       # create a native-Puppy repo from a dir of packages FUNCLIST
  1220.  
  1221.   # exit if not a valid directory
  1222.   if [ ! -d "$1" ];then
  1223.     print_usage dir2repo && exit 1
  1224.   fi
  1225.  
  1226.   local filename=''
  1227.   local filepath=''
  1228.   local fileext=''
  1229.   local prevext=''
  1230.   local repo_name
  1231.  
  1232.   # get the repo directory
  1233.   local repo_dir="$(realpath "$1")"
  1234.  
  1235.   # create a temp repo_file name
  1236.   local repo_file="${repo_dir}/Packages-$DISTRO_BINARY_COMPAT-$DISTRO_COMPAT_VERSION"
  1237.  
  1238.   # remove any old files
  1239.   rm "${repo_dir}/install" "${repo_dir}/Packages-"* "$repo_file" &>/dev/null
  1240.  
  1241.   echo
  1242.   echo "Creating repo from contents of: $repo_dir"
  1243.   echo
  1244.  
  1245.   # exit if we encounter multiple file extensions
  1246.   for file in $(find "$repo_dir" -maxdepth 5 -type f)
  1247.   do
  1248.     filename=$(basename $file)
  1249.     fileext="$(get_pkg_ext $filename)"
  1250.     if [ "$fileext" != "$prevext" ] && [ "$prevext" != "" ];then
  1251.       echo "All packages must have the same extension."
  1252.       echo "Extensions '$fileext' and '$prevext' don't match."
  1253.       echo "Package '$filename' needs to be converted to $prevext or removed."
  1254.       exit 1
  1255.     fi
  1256.     prevext="$fileext"
  1257.   done
  1258.  
  1259.  
  1260.   # get the packages in $repo_dir
  1261.   local package_list="$(find "$repo_dir" -maxdepth 5 -type f | grep -v ^Packages | grep -v "^install$")"
  1262.  
  1263.   if [ "$package_list" = "" ];then
  1264.     echo "No packages found in ${repo_dir}!"
  1265.     exit 1
  1266.   fi
  1267.  
  1268.   # for each file in the repo dir
  1269.   for file in $package_list
  1270.   do
  1271.     # skip if not a recognised package type
  1272.     [ "$(is_local_pkg "$file")" != true ] && continue
  1273.  
  1274.     # get the package info
  1275.     filename=$(basename $file)
  1276.     fileext="$(get_pkg_ext $filename)"
  1277.     filepath="$(realpath $(dirname $file))"
  1278.     pathname="$(dirname ${filepath})"
  1279.     repopath="$(echo $filepath | sed -e "s#${repo_dir}##" -e "s/^\///")"
  1280.     pkgname=$(basename $filename .$fileext)
  1281.     pkgnameonly="$(get_pkg_name_only $pkgname)"
  1282.     pkgversion="$(get_pkg_version $pkgname)"
  1283.     pkgcat="$(grep -m1 "|$pkgnameonly|" ~/.packages/Packages-* | cut -f5 -d'|')"
  1284.     pkgsize="$(du "$file" 2>/dev/null | cut -f1)K"
  1285.     [ "$pkgsize" = "" ] && pkgsize="$(grep -m1 "|$pkgnameonly|" ~/.packages/Packages-* | cut -f6 -d'|')"
  1286.     pkgdeps="$(grep -m1 "|$pkgnameonly|" ~/.packages/Packages-* | cut -f9 -d'|')"
  1287.     # dont include deps if we're adding a combined package + plus deps (they have $CP_SUFFIX in the name)
  1288.     [ "$(echo "$filename" | grep "$CP_SUFFIX")" != "" ] && pkgdeps=''
  1289.     pkgdesc="$(grep -m1 "|$pkgnameonly|" ~/.packages/Packages-* | cut -f10 -d'|')"
  1290.     pkgdistro="$DISTRO_BINARY_COMPAT"
  1291.     pkgdistroversion="$DISTRO_COMPAT_VERSION"
  1292.  
  1293.     # if we don't have the required package info, dont add it to the repo file
  1294.     [ "$pkgname" = "" -o "$pkgnameonly" = "" -o "$filename" = "" ] && continue
  1295.  
  1296.     # add this package to the repo file
  1297.     echo "$pkgname|$pkgnameonly|$pkgversion||${pkgcat:-BuildingBlock}|$pkgsize|$repopath|$filename|$pkgdeps|${pkgdesc:-No description}|$pkgdistro|$pkgdistroversion|" >> "$repo_file"
  1298.   done
  1299.  
  1300.   if [ ! -f "$repo_file" ];then
  1301.     echo "Error: file '$repo_file' not created."
  1302.     exit 1
  1303.   fi
  1304.  
  1305.   echo "Step 1 of 3: CHOOSE A REPO NAME"
  1306.   echo "(something like 'distroversion-username-repo', such as 'bionic-bob-main' or 'stretch-sc0ttman-games')"
  1307.   echo
  1308.   bash -c 'read -e -r -p "Type a repo name and hit ENTER: " repo_name; echo "$repo_name" > /tmp/pkg/repo_name'
  1309.   repo_name="$(cat /tmp/pkg/repo_name)"
  1310.  
  1311.   # rename the repo file to the new name
  1312.   mv "$repo_file"   "${repo_dir}/Packages-${DISTRO_BINARY_COMPAT:-puppy}-$repo_name"
  1313.   repo_file="${repo_dir}/Packages-${DISTRO_BINARY_COMPAT:-puppy}-$repo_name"
  1314.  
  1315.   # build repo entry
  1316.   local repo_entry="$repo_name|$fileext|$repo_file|$repo_url||||$repo_fallbacks"
  1317.  
  1318.   # get the URL where the repo file and packages will live
  1319.   echo
  1320.   echo "Step 2 of 3: ADD THE REPO URL"
  1321.   echo "(the full URL where you will upload your repo file and packages)"
  1322.   echo
  1323.   bash -c 'read -e -r -i "http://" -p "Type a repo URL and hit ENTER: " mirror1; echo "$mirror1" > /tmp/pkg/mirror1'
  1324.   mirror1="$(cat /tmp/pkg/mirror1)"
  1325.  
  1326.   # get fallback repos list
  1327.   echo
  1328.   echo "Step 3 of 3: ADD FALLBACK REPOS"
  1329.   echo "(the other repos to fall back to when looking for dependencies)"
  1330.   echo
  1331.   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'
  1332.   fallback_repos="$(cat /tmp/pkg/fallback_repos)"
  1333.  
  1334.   # remove the tmp files which store user input, they're no longer needed
  1335.   rm /tmp/pkg/repo_name /tmp/pkg/mirror1 /tmp/pkg/fallback_repos
  1336.  
  1337.   # add a trailing slash to the URL
  1338.   if [ "$(echo "${mirror1}" | grep "/$")" = "" ];then
  1339.     mirror1="${mirror1}/"
  1340.   fi
  1341.  
  1342.   # create repo installer file in $repo_dir
  1343.   echo "REPONAME=$repo_name
  1344. EX=$fileext
  1345. REPOFILE=$(basename $repo_file)
  1346. URL1=${mirror1}
  1347. URL2=''
  1348. URL3=''
  1349. URL4=''
  1350. FALLBACKS='$fallback_repos'" > "${repo_dir}/install"
  1351.  
  1352.   # print final message
  1353.   echo
  1354.   echo -e "${green}Success${endcolour}: Repo ${yellow}$repo_name${endcolour} created."
  1355.   echo
  1356.   echo -e "You should upload everything in ${lightblue}$repo_dir${endcolour} to:"
  1357.   echo
  1358.   echo "  $mirror1"
  1359.   echo
  1360.   echo "You (and anyone else) can then install the repo using:"
  1361.   echo
  1362.   echo "  pkg add-repo ${mirror1}"
  1363.   echo
  1364.   echo -e "NOTE: You can edit the ${yellow}install${endcolour} and ${yellow}$(basename $repo_file)${endcolour}"
  1365.   echo "files in a text editor, before you upload your new repo."
  1366.   echo
  1367. }
  1368.  
  1369.  
  1370. add_pkg_repo() {                  # add a Pkg-created repo, called by add_repo() FUNCLIST
  1371.   [ ! "$1" ] && return 1
  1372.   mkdir -p /tmp/pkg/
  1373.   # this is probably a Pkg created Puppy repo, get info from 'install' file
  1374.   rm /tmp/pkg/repo_info &>/dev/null
  1375.  
  1376.   # check if the 'install' file we need exists
  1377.   wget --no-check-certificate -O "/tmp/pkg/repo_info" -4 "$1" &>/dev/null
  1378.   if [ ! -f /tmp/pkg/repo_info ];then
  1379.     echo "Error: Repo installer file not downloaded!"
  1380.     return 1
  1381.   elif [ "$(grep -m1 "^REPONAME" /tmp/pkg/repo_info 2>/dev/null)" = "" ];then
  1382.     echo "Error: invalid repo installer file"
  1383.     return 1
  1384.   fi
  1385.  
  1386.   # build a valid repo entry for the ~/.pkg/sources* files
  1387.   # (must have some fallbacks, strip quotes, newlines, no trailing pipes)
  1388.   local repo_entry="$(cat /tmp/pkg/repo_info \
  1389.    | sed -e "s/FALLBACKS=''/FALLBACKS='noarch common32'/" -e 's/.*=//g' \
  1390.    | tr -d '"' \
  1391.    | tr -d "'" \
  1392.    | tr '\n' '|' \
  1393.    | head -c -1)"
  1394.  
  1395.   if [ "$repo_entry" = "" ];then
  1396.     echo "Error: repo entry not created."
  1397.     return 1
  1398.   fi
  1399.  
  1400.   # get repo info
  1401.   local repo_name="$(echo "$repo_entry" | cut -f1 -d'|')"
  1402.   local repo_filename="$(echo "$repo_entry" | cut -f3 -d'|')"
  1403.   local repo_file_url="$(echo ${1} | sed 's#/install$##')/${repo_filename}"
  1404.  
  1405.   # download the repo file
  1406.   wget --no-check-certificate -O "/tmp/pkg/$repo_filename" -4 "$repo_file_url" &>/dev/null
  1407.   if [ -z /tmp/pkg/$repo_filename ] || [ ! -f /tmp/pkg/$repo_filename ];then
  1408.     echo "Error: Repo file $repo_filename not downloaded!"
  1409.     return 1
  1410.   fi
  1411.  
  1412.   # install the repo file
  1413.   mv /tmp/pkg/$repo_filename ~/.packages/$repo_filename
  1414.   RETVAL=$?
  1415.  
  1416.   # add repo entry to ~/.pkg/sources
  1417.   echo
  1418.   add_source "${repo_entry}"
  1419.   echo
  1420.  
  1421.   # refresh list of available repos
  1422.   update_sources
  1423.   echo
  1424.   echo "Repo info:"
  1425.   print_repo_info $repo_name
  1426.   echo
  1427.  
  1428.   if [ "$(cat ~/.pkg/sources 2>/dev/null | grep -m1 "^$repo_name|")" != "" ];then
  1429.     echo "Success! Repo added and available to use."
  1430.     echo
  1431.     echo "To use this repo, simply type the following and hit ENTER:"
  1432.     echo "  pkg repo $repo_name"
  1433.     echo
  1434.  
  1435.     # register as a user installed repo, so we can remove it later (using rm_repo)
  1436.     touch ~/.pkg/sources-user
  1437.     echo "$repo_entry" >> ~/.pkg/sources-user
  1438.     local user_repos="$(sort -u ~/.pkg/sources-user | uniq)"
  1439.     echo "$user_repos" > ~/.pkg/sources-user
  1440.  
  1441.   fi
  1442.  
  1443.   return $RETVAL
  1444. }
  1445.  
  1446.  
  1447. add_repo(){                       # add Pkg/Ubuntu/Debian/Slackware third-party repos FUNCLIST
  1448.  
  1449.   local slack_repo_url
  1450.  
  1451.   if [ ! "$1" -o "$1" = "-" -o "$1" = "-h" -o "$1" = "--help" ];then
  1452.     print_usage add_repo && exit 1
  1453.   fi
  1454.  
  1455.   # work out which kind of repo we are processing
  1456.   case "$1" in
  1457.     'http'*'/PACKAGES.TXT.gz'|'http'*'/PACKAGES.TXT')
  1458.       # strip trailing /PACKAGES.TXT[.gz]
  1459.       slack_repo_url="${1//PACKAGES.TXT.gz/}"
  1460.       slack_repo_url="${slack_repo_url//\/.gz/}"
  1461.       slack_repo_url="${slack_repo_url//.gz\//}"
  1462.       slack_repo_url="${slack_repo_url//PACKAGES.TXT/}"
  1463.       # add trailing slash, if needed
  1464.       if [ "$(echo $slack_repo_url | grep '/$')" = '' ];then
  1465.         slack_repo_url="${slack_repo_url}/"
  1466.       fi
  1467.       slack2pup "$@"
  1468.       retval=$?
  1469.       if [ $retval -eq 0 ];then
  1470.         # register repo, so we know to update it in update_sources()
  1471.         touch ~/.pkg/sources-user
  1472.         echo "$repo_entry" >> ~/.pkg/sources-user
  1473.         local user_repos="$(sort -u ~/.pkg/sources-user | uniq)"
  1474.         echo "$user_repos" > ~/.pkg/sources-user
  1475.  
  1476.         # register in a slack supported file too
  1477.         mkdir -p /etc/slackpkg/
  1478.         touch /etc/slackpkg/mirrors
  1479.         echo "$slack_repo_url" >> /etc/slackpkg/mirrors
  1480.         local slack_repos="$(sort -u /etc/slackpkg/mirrors | uniq)"
  1481.         echo "$slack_repos" > /etc/slackpkg/mirrors
  1482.       fi
  1483.       ;;
  1484.  
  1485.     'http'*'/install')
  1486.       add_pkg_repo "$1"
  1487.       ;;
  1488.  
  1489.     'ppa:'*|'http://'*|'https://'*)
  1490.       wget --quiet --timeout=2 --no-parent --spider "${1}install" 2>/dev/null && \
  1491.       {
  1492.         add_pkg_repo "${1}install"
  1493.         exit 0
  1494.       } || exit 1
  1495.  
  1496.       ppa2pup "$@"
  1497.       retval=$?
  1498.       if [ $retval -eq 0 ];then
  1499.         # register repo, so we know to update it in update_sources()
  1500.         touch ~/.pkg/sources-user
  1501.         echo "$repo_entry" >> ~/.pkg/sources-user
  1502.         local user_repos="$(sort -u ~/.pkg/sources-user | uniq)"
  1503.         echo "$user_repos" > ~/.pkg/sources-user
  1504.  
  1505.         # register in an apt-supported file too
  1506.         mkdir -p /etc/apt/
  1507.         touch /etc/apt/sources.list
  1508.         echo "deb $@" >> /etc/apt/sources.list
  1509.         local deb_repos="$(sort -u /etc/apt/sources.list | uniq)"
  1510.         echo "$deb_repos" > /etc/apt/sources.list
  1511.       fi
  1512.       ;;
  1513.  
  1514.     *)
  1515.       print_usage add_repo && exit 1
  1516.       ;;
  1517.  
  1518.   esac
  1519.  
  1520. }
  1521.  
  1522.  
  1523. rm_repo(){                        # remove an installed repo by name ($1) FUNCLIST
  1524.   if [ ! "$1" -o "$1" = "-" -o "$1" = "-h" -o "$1" = "--help" ];then
  1525.     print_usage rm_repo
  1526.     exit 1
  1527.   fi
  1528.  
  1529.   # $1 must be a valid, installed repo name
  1530.   if [ "$(repo_list | grep -m1 "^${1}$")" = "" ];then
  1531.     print_usage rm-repo
  1532.     exit 1
  1533.   fi
  1534.  
  1535.   # get the repo file and URL of the given repo
  1536.   local repo_file="$(grep "^$1" ~/.pkg/sources-all | cut -f3 -d'|' )"
  1537.   local repo_url="$(grep "^$1" ~/.pkg/sources-all | cut -f4 -d'|')"
  1538.  
  1539.   # dont allow removal of 'built in' repos, only user-added repos
  1540.   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)"
  1541.   if [ "${matching_user_added_repos}" = "" ];then
  1542.     echo "You can only remove the repositories that you installed yourself."
  1543.     echo -e "Use the command  ${bold}pkg repo-list${endcolour}  to see which repos are installed."
  1544.     exit 1
  1545.   fi
  1546.  
  1547.   # dont allow removal of current repo
  1548.   if [ "$REPONAME" = "$1" ];then
  1549.     echo "Can't remove current repo."
  1550.     echo -e "Switch to another repo first using:  ${bold}pkg repo REPONAME${endcolour}"
  1551.     exit 1
  1552.   fi
  1553.  
  1554.   # remove the repo file
  1555.   echo "Removing '$1'.. Please wait.."
  1556.   [ -f "/root/.packages/$repo_file" ] && rm "/root/.packages/$repo_file"
  1557.  
  1558.   # remove from sources
  1559.   grep -v "^$1|" ~/.pkg/sources > /tmp/pkg/sources
  1560.   grep -v "^$1|" ~/.pkg/sources-all > /tmp/pkg/sources-all
  1561.   [ -f /tmp/pkg/sources-all ] && mv /tmp/pkg/sources-all ~/.pkg/sources-all
  1562.   [ -f /tmp/pkg/sources     ] && mv /tmp/pkg/sources ~/.pkg/sources
  1563.  
  1564.   # remove from third-party lists
  1565.   grep -v  "|${repo_url}|"  ~/.pkg/sources-user > /tmp/pkg/sources-user
  1566.   grep -vE "$repo_url|${repo_name//*-/}" /etc/apt/sources.list > /tmp/pkg/sources.list
  1567.   grep -v  "$repo_url" /etc/slackpkg/mirrors > /tmp/pkg/mirrors
  1568.   [ -f /tmp/pkg/sources-user ] && mv /tmp/pkg/sources-user ~/.pkg/sources-user
  1569.   [ -f /tmp/pkg/sources.list ] && mv /tmp/pkg/sources.list /etc/apt/sources.list
  1570.   [ -f /tmp/pkg/mirrors ] && mv /tmp/pkg/mirrors /etc/slackpkg/mirrors
  1571.  
  1572.   if [ "$(repo_list | grep -m1 "^${1}$")" = "" ];then
  1573.     update_sources &>/dev/null
  1574.     echo -e "${green}Success${endcolour}: Repo removed."
  1575.   fi
  1576.  
  1577. }
  1578.  
  1579.  
  1580. add_source(){                     # add a new repo to your repo 'sources' list FUNCLIST
  1581.   [ ! "$1" \
  1582.     -o "$1" = "-" \
  1583.     -o "$1" = "-h" \
  1584.     -o "$1" = "--help" \
  1585.     -o "`echo "$1" |  grep '|'`" = "" \
  1586.     -o "`echo "$1" | cut -f2 -d'|'`" = "" \
  1587.     -o "`echo "$1" | cut -f4 -d'|'`" = "" \
  1588.     -o "`echo "$1" | cut -f8 -d'|'`" = "" ] && \
  1589.     print_usage add_source && exit 1
  1590.  
  1591.   # get repo file to add to sources
  1592.   REPOTOADD="`echo $1 | cut -f1 -d'|'`"
  1593.   REPOFILETOADD="`echo $1 | cut -f3 -d'|'`"
  1594.  
  1595.   # do checks before adding repo (dont add duplicate, make sure file exists, etc)
  1596.  
  1597.   # check if repo name already in sources-all
  1598.   #if [ "`grep "^$REPOTOADD\$" ~/.pkg/sources-all`" != "" ] || \
  1599.   #   [ "`repo_list | grep "^$REPOTOADD\$"`" != "" ];then
  1600.   #  echo "Repo with the name '$REPOTOADD' already in the list"
  1601.   #fi
  1602.  
  1603.   # check if repo filename already  exists in sources-all
  1604.   #if [ "`repo_file_list | grep -m1 "^$REPOFILETOADD\$"`" != "" ];then
  1605.   #  echo "Repo with database file $HOME/.packages/'$REPOFILETOADD' already in the list"
  1606.   #fi
  1607.  
  1608.   # check if the repo file exists in $HOME/.packages
  1609.   #if [ ! -f "`find $HOME/.packages/ -name "$REPOFILETOADD"`" ];then
  1610.   #  echo "The repo database file '$HOME/.packages/$REPOFILETOADD' not found."
  1611.   #fi
  1612.  
  1613.   # add the repo to sources-all, if not already there
  1614.   if [ "$(grep -m1 "^$REPOTOADD|" ${HOME}/.pkg/sources-all)" = "" ];then
  1615.     # all good, so add repo entry to sources-all
  1616.     echo "$1" >> ${HOME}/.pkg/sources-all
  1617.   fi
  1618.  
  1619.   # update users repo sources to get the new repo
  1620.   update_sources 1>/dev/null || { echo "Could not update repo sources."; exit 2; }
  1621.  
  1622.   # print msg
  1623.   echo "Repo '$REPOTOADD' added successfully."
  1624. }
  1625.  
  1626.  
  1627. update_sources(){                 # create the list of available repos FUNCLIST
  1628.  
  1629.   # get current repo values and Pkg settings
  1630.   . ${PKGRC}
  1631.  
  1632.   #list current repo first in sources
  1633.   get_repo_info "${REPONAME:-noarch}"
  1634.  
  1635.  
  1636.   # only add the current repo to the list of available sources if it exists
  1637.   if [ "`find $HOME/.packages/ -iname "$REPOFILE" `" != '' ];then
  1638.     echo "$REPONAME|$EX|$REPOFILE|$REPOURL1|$REPOURL2|$REPOURL3|$REPOURL4|$REPOFALLBACKS" > ${HOME}/.pkg/sources
  1639.   elif [ "`find /var/packages/ -iname "$REPOFILE" `" != '' ];then
  1640.     echo "$REPONAME|$EX|$REPOFILE|$REPOURL1|$REPOURL2|$REPOURL3|$REPOURL4|$REPOFALLBACKS" > ${HOME}/.pkg/sources
  1641.   fi
  1642.  
  1643.  
  1644.   # get repos in order of fallbacks, pkg will then 'fall back' to each repo in that order
  1645.   FALLBACKS="`grep -m1 "^${REPONAME}|" ${HOME}/.pkg/sources-all 2>/dev/null | grep -v ^'#' | cut -f8 -d'|'`"
  1646.  
  1647.   # for each repo in fallback list
  1648.   for FBACK in $REPOFALLBACKS; do
  1649.     # dont add current repo, its already added
  1650.     [ "$FBACK" = "$REPONAME" ] && continue
  1651.  
  1652.     # check if repo is supported (has entries in sources-all)
  1653.     LINE="`grep -m1 "^$FBACK|" ${HOME}/.pkg/sources-all 2>/dev/null | grep -v ^'#' | grep -v "^${REPONAME}|"`"
  1654.     [ "$LINE" = "" ] && continue
  1655.  
  1656.     # if repo is valid, add to users list of in-use repos (only valid repos from sources-all => sources)
  1657.     if [ -f "${HOME}/.packages/`echo "$LINE" | cut -f3 -d'|'`" ];then
  1658.  
  1659.       if [ "$(grep -m1 "^$FBACK|" ${HOME}/.pkg/sources)" = "" ];then
  1660.         echo "Adding repo: `echo "$LINE"|cut -f1 -d'|'`.."
  1661.         echo "$LINE" >> ${HOME}/.pkg/sources
  1662.       fi
  1663.     fi
  1664.   done
  1665.  
  1666.   cleaned_repo_list="`cat ${HOME}/.pkg/sources-all 2>/dev/null | grep -v ^'#'| grep -v ^$`"
  1667.  
  1668.   # now add any other repos to the list (repos that are installed, but not added from fallback list)
  1669.   echo "$cleaned_repo_list" | uniq | while read repo_entry
  1670.   do
  1671.     # dont add current repo, its already added
  1672.     [ "`echo "$repo_entry" | cut -f1 -d'|'`" = "$REPONAME" ] && echo "Adding repo: $REPONAME.." && continue
  1673.  
  1674.     # get the repo name
  1675.     repo_name="`echo "$repo_entry"|cut -f1 -d'|'`"
  1676.  
  1677.     # build the repo file (full path)
  1678.     repo_file=${HOME}/.packages/`echo "$repo_entry" | cut -f3 -d'|'`
  1679.  
  1680.     # set a flag true if repo already in repo, false if not
  1681.     already_in_repo=false
  1682.     [ "`grep -m1 "^$repo_entry" ${HOME}/.pkg/sources 2>/dev/null`" != "" ] && already_in_repo=true
  1683.  
  1684.     if [ -f "$repo_file" -a "$already_in_repo" = false ];then
  1685.       echo "Adding repo: $repo_name.."
  1686.       echo "$repo_entry" >> ${HOME}/.pkg/sources
  1687.     fi
  1688.   done
  1689.  
  1690.   # finished, print message
  1691.   [ -f ${HOME}/.pkg/sources ] && echo "Sources updated." && . ${PKGRC}
  1692. }
  1693.  
  1694. set_ppa2pup_fn(){
  1695.   #Paranoid precaution, so that one can't do code injection
  1696.   case "$PKG_PPA2PUP_FN" in
  1697.   ppa2pup_gawk) ppa2pup_fn=ppa2pup_gawk; ;;
  1698.   ppa2pup) ppa2pup_fn=ppa2pup ;;
  1699.   *)
  1700.     echo "Invalid value: PKG_PPA2PUP_FN=${PKG_PPA2PUP_FN}"
  1701.     echo "Valid values: ppa2pup or ppa2pup_gawk"
  1702.     exit 1 ;;
  1703.   esac
  1704. }
  1705.  
  1706. update_repo(){                    # update the current repo from a file stored online FUNCLIST
  1707.  
  1708.   # check internet, net connection required
  1709.   NET="`check_net`"; [ $NET -eq 1 ] && echo "You need an internet connection" && exit 1
  1710.  
  1711.   # remove the repo update tmp file
  1712.   rm -f $TMPDIR/update_repo_results 2>/dev/null
  1713.  
  1714.   echo "Updating system repositories, please wait.."
  1715.   echo
  1716.  
  1717.   # use petget for now .. not ideal, petget wont accept $1 and only do that repo,
  1718.   # also petget uses loads of other files pkg doesnt have/need (in $HOME/.packages)
  1719.   # also, we're limited to updating only the repos that petget supports, not all the ones Pkg supports..
  1720.   # .. on the plus side petget code is way faster than mine
  1721.   mkdir -p /var/local/petget/
  1722.   chmod 777 /var/local/petget/
  1723.   echo 'false' > /var/local/petget/db_verbose
  1724.  
  1725.   # now call petget 0setup.. the ENV options prevent popup windows, and need for user input
  1726.   #DISPLAY='' SETUPCALLEDFROM=ppm /usr/local/petget/0setup &>$TMPDIR/update_repo_results
  1727.  
  1728.   # if the repos updated ok
  1729.   if [ $? -eq 0 ];then
  1730.     [ "`which logger`" != '' ] && logger "$0 Repo files updated by $APP $APPVER"
  1731.     echo -e "Repo files updated:"
  1732.     grep ^Processing $TMPDIR/update_repo_results | cut -f2 -d' '
  1733.     # remove the repo update tmp file
  1734.     rm -f $TMPDIR/update_repo_results 2>/dev/null
  1735.   else
  1736.     # repo did not update ok
  1737.     error "Repos NOT updated."
  1738.     cat $TMPDIR/update_repo_results | tail -20
  1739.     exit 2
  1740.   fi
  1741.  
  1742.   # update Pkg created, user-added (third-party) repos
  1743.   if [ -f ~/.pkg/sources-user ] || [ -f /etc/apt/sources.list ] || [ -f /etc/slackpkg/mirrors ];then
  1744.     echo
  1745.     echo "Updating third-party repos.. This may take a while.."
  1746.     echo
  1747.   fi
  1748.  
  1749.   # update Pkg created repos
  1750.   if [ -f ~/.pkg/sources-user ];then
  1751.     pkg_repos="$(sort -u ~/.pkg/sources-user | grep -v ^$ | uniq | cut -f1 -d'|')"
  1752.     for pkg_repo in $pkg_repos
  1753.     do
  1754.       local pkg_repo_url="$(cat ~/.pkg/sources-user | grep -m1 "^$pkg_repo|" | cut -f4 -d'|')"
  1755.       echo "Processing:  $pkg_repo_url"
  1756.  
  1757.       ANSWER=y
  1758.       if [ "$ASK" = true ];then
  1759.         bash -c 'read -r -N 1 -p "Update repo $pkg_repo? [y/n] " ANSWER; echo $ANSWER > /tmp/pkg/ANSWER'
  1760.         echo
  1761.         ANSWER="$(cat /tmp/pkg/ANSWER)"
  1762.       fi
  1763.  
  1764.       if [ "$ANSWER" = 'y' ] || [ "$ANSWER" = 'Y' ];then
  1765.         echo "Please wait..."
  1766.         add_pkg_repo ${pkg_repo_url}install 1>/dev/null && echo -e "${green}Success${endcolour}: Updated repo '$pkg_repo'"
  1767.       fi
  1768.     done
  1769.   fi
  1770.  
  1771.   # update third-party non-native repos.. These repos comes 'from source',
  1772.   # and are not in native Puppy format - they need to be downloaded, converted
  1773.   # into the Puppy format, then installed... so this will be slow..
  1774.   if [ -f /etc/apt/sources.list ] || [ -f /etc/slackpkg/mirrors ];then
  1775.  
  1776.     # update third-party Ubuntu/Debian repos
  1777.     # search inside /etc/apt/sources.list /etc/apt/sources.list.d/*.list
  1778.     # ...look for lines starting with 'deb ', ignore others
  1779.     # ...(use grep -h, to remove the preppended filenames if grepping multiple files)
  1780.     # ...remove any arch stuff from the entries, ppa2pup will handle that,
  1781.     # ...convert spaces to | chars, so we can process each line as a whole later
  1782.     local apt_sources_list="$(grep -h '^deb ' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null \
  1783.      | sed \
  1784.        -e "s/^deb //g" \
  1785.        -e "s/^tor+//g" \
  1786.        -e 's/\[arch=[a-z,0-9].*\] //g' \
  1787.        -e 's/ /|/g'\
  1788.    )"
  1789.  
  1790.     # for each repo in $apt_sources_list, use `ppa2pup` to update the repo
  1791.     for line in $apt_sources_list
  1792.     do
  1793.       [ "$line" = "" ] && continue
  1794.       [ "$line" = "\n" ] && continue
  1795.  
  1796.       local ppa_repo_url="$(echo ${line//|/ } | cut -f1 -d" ")"
  1797.       local ppa_repo_name=$(cat ~/.pkg/sources-all | grep -m1 $ppa_repo_url | cut -f1 -d'|')
  1798.  
  1799.       # if a PPA repo, get a user-friendly repo name from the /etc/sources.list entry
  1800.       if [ "$ppa_repo_name" = "" ];then
  1801.         ppa_repo_name="$(echo ${line} | cut -f3 -d'|')-$(echo $line | cut -f2 -d':' | cut -f1 -d '/' | tr -d '-' | tr -d '_')"
  1802.       fi
  1803.  
  1804.       echo
  1805.       echo "Processing:  ${line//|/ }"
  1806.  
  1807.       ANSWER=y
  1808.       if [ "$ASK" = true ];then
  1809.         # ask user to update repo
  1810.         bash -c 'read -r -N 1 -p "Update repo $ppa_repo_name? [y/n] " ANSWER; echo $ANSWER > /tmp/pkg/ANSWER'
  1811.         echo
  1812.         ANSWER="$(cat /tmp/pkg/ANSWER)"
  1813.       fi
  1814.      
  1815.       if [ "$ANSWER" = 'y' ] || [ "$ANSWER" = 'Y' ];then
  1816.         echo "Please wait..."
  1817.         set_ppa2pup_fn
  1818.         $ppa2pup_fn ${line//|/ } 1>/dev/null && echo -e "${green}Success${endcolour}: Updated repo '$ppa_repo_name'"
  1819.         retval=$?
  1820.       fi
  1821.     done
  1822.  
  1823.     # update third-party Slackware repos
  1824.     if [ -f /etc/slackpkg/mirrors ];then
  1825.       local slack_repos="$(sort -u /etc/slackpkg/mirrors | uniq)"
  1826.       local slack_repo_url=""
  1827.  
  1828.       for slack_repo in $slack_repos
  1829.       do
  1830.         slack_repo_url="${slack_repo//PACKAGES.TXT.gz/}"
  1831.         slack_repo_url="${slack_repo_url//\/.gz/}"
  1832.         slack_repo_url="${slack_repo_url//.gz\//}"
  1833.         slack_repo_url="${slack_repo_url//PACKAGES.TXT/}"
  1834.  
  1835.         # add trailing slash, if needed
  1836.         if [ "$(echo $slack_repo_url | grep '/$')" = '' ];then
  1837.           slack_repo_url="${slack_repo_url}/"
  1838.         fi
  1839.  
  1840.         local slack_repo_name=$(cat ~/.pkg/sources-all | grep -m1 $slack_repo_url | cut -f1 -d'|')
  1841.  
  1842.         echo
  1843.         echo "Processing:  $slack_repo_url"
  1844.  
  1845.         ANSWER=y
  1846.         if [ "$ASK" = true ];then
  1847.           # ask user to update repo
  1848.           bash -c 'read -r -N 1 -p "Update repo '"$slack_repo_name"'? [y/n] " ANSWER; echo $ANSWER > /tmp/pkg/ANSWER'
  1849.           echo
  1850.           ANSWER="$(cat /tmp/pkg/ANSWER)"
  1851.         fi
  1852.  
  1853.         if [ "$ANSWER" = 'y' ] || [ "$ANSWER" = 'Y' ];then
  1854.           echo "Please wait..."
  1855.           slack2pup "${slack_repo_url}/PACKAGES.TXT" $slack_repo_name 1>/dev/null && echo -e "${green}Success${endcolour}: Updated repo '$slack_repo_name'"
  1856.         fi
  1857.       done
  1858.     fi
  1859.   fi
  1860.  
  1861.   echo
  1862.   exit 0
  1863.  
  1864. }
  1865.  
  1866.  
  1867. list_sources(){                   # return available (matching) repos (~/.pkg/sources) FUNCLIST
  1868.   grep -m1 "^$1" ${HOME}/.pkg/sources ${HOME}/.pkg/sources-user | cut -f2-99 -d':' | uniq
  1869. }
  1870.  
  1871.  
  1872. list_all_sources(){               # return all (or matching) repos (~/.pkg/sources-all) FUNCLIST
  1873.   grep -m1 "^$1" ${HOME}/.pkg/sources-all ${HOME}/.pkg/sources-user | cut -f2-99 -d':' | uniq
  1874. }
  1875.  
  1876.  
  1877. convert_repofile(){               # convert repo files formats (pre-woof/post-woof) FUNCLIST
  1878.  
  1879.   # get the file name (FILENAME) and full path (FILE)
  1880.   FILENAME="`basename "$1"`"
  1881.   FILE="${CURDIR}/${FILENAME}"
  1882.  
  1883.   # check for valid options
  1884.   [ ! -f "$FILE" ] && print_usage repo-convert && exit 1
  1885.  
  1886.   # dont replace repo unless -f was given
  1887.   [ "$FORCE" != true -a -f "${HOME}/.packages/${FILENAME}" ] && echo "File '${HOME}/.packages/$FILENAME' already exists."  && exit 1
  1888.  
  1889.   # remove tmp files
  1890.   rm $TMPDIR/$FILENAME &>/dev/null
  1891.   rm $TMPDIR/${FILENAME}_subdirs &>/dev/null
  1892.  
  1893.   # check repo file format (woof or pre-woof)
  1894.   if [ -f "$FILE" -a "`cat "$FILE" | head -1 | grep -m1 '^"'`" = "" ];then #if is a new format  #'
  1895.  
  1896.     # convert woof repo file to pre-woof repo file.. takes ages..
  1897.     echo "Converting '${FILE}' to pre-woof format.. This might take a while.."
  1898.     cat "$FILE" | while read LINE
  1899.     do
  1900.       PKGNAME="`echo $LINE| cut -f1 -d'|'`"
  1901.       PKGNAME1=''
  1902.       # we need to get the package name, try lots of different extensions
  1903.       [ "`echo $PKGNAME1 | grep ".deb\$"`" != "" ]  && PKGNAME1="`echo $LINE| cut -f8 -d'|'`"
  1904.       [ "`echo $PKGNAME1 | grep ".deb\$"`" != "" ]  && PKGNAME="`basename $PKGNAME1 .deb`"
  1905.       [ "`echo $PKGNAME1 | grep ".rpm\$"`" != "" ]  && PKGNAME="`basename $PKGNAME1 .rpm`"
  1906.       [ "`echo $PKGNAME1 | grep ".txz\$"`" != "" ]  && PKGNAME="`basename $PKGNAME1 .txz`"
  1907.       [ "`echo $PKGNAME1 | grep ".tgz\$"`" != "" ]  && PKGNAME="`basename $PKGNAME1 .tgz`"
  1908.       [ "`echo $PKGNAME1 | grep ".tar.xz\$"`" != "" ] && PKGNAME="`basename $PKGNAME1 .tar.xz`"
  1909.       [ "`echo $PKGNAME1 | grep ".tar.gz\$"`" != "" ] && PKGNAME="`basename $PKGNAME1 .tar.gz`"
  1910.       # get size
  1911.       SIZE=" `echo $LINE| cut -f6 -d'|'`"
  1912.       # get category
  1913.       CAT="`echo $LINE| cut -f5 -d'|'`"
  1914.       #150813 remove extra categories .. example 'Setup;Installation' .. remove 'Installation'
  1915.       [ "`echo "$CAT" | grep ';'`" != "" ] && CAT="`echo "$CAT" | cut -f1 -d';'`"
  1916.       # get sub dir in repo
  1917.       SUBDIR="`echo $LINE| cut -f7 -d'|'`" #150213
  1918.       # get deps
  1919.       DEPS=" `echo $LINE| cut -f9 -d'|'| grep '+'`"
  1920.       # get desc
  1921.       DESC="`echo $LINE| cut -f10 -d'|'`"
  1922.       # add repo entry to tmp file
  1923.       [ "$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
  1924.       #150213 now do subdirs... slow..
  1925.       [ "`echo $SUBDIR | grep "/"`" != "" -a "`echo $SUBDIR | grep -i pet_packages`" = "" ] && echo "$PKGNAME|/$SUBDIR" >> $TMPDIR/${FILENAME}_subdirs
  1926.     done
  1927.     # done making a pre-woof repo file, print message
  1928.     [ -f $TMPDIR/$FILENAME ] && echo "Finished converting $FILENAME to pre-woof format." || exit 1
  1929.  
  1930.   else
  1931.  
  1932.     # convert pre-woof repo file to woof repo file.. takes ages..
  1933.     echo "Converting '$FILENAME' to a 'woof' compatible repo file. This could take a while..."
  1934.  
  1935.     # parse a pre-woof repo file
  1936.     cat "$FILE" | while read LINE
  1937.     do
  1938.       PKGNAME='' PKGNAMEONLY='' PKGVER='' SIZE='' CAT='' DEPS='' BUILD='' SUBDIR='' PKGEXT='.pet' DESC=''
  1939.  
  1940.       PKGNAME="`echo "$LINE" | cut -d'"' -f2`" #'micro
  1941.       [ "`echo $PKGNAME | grep ".deb\$"`" != "" ]     && PKGEXT='.deb'    && PKGNAME="`basename $PKGNAME1 .deb`"
  1942.       [ "`echo $PKGNAME | grep ".pet\$"`" != "" ]     && PKGEXT='.pet'    && PKGNAME="`basename $PKGNAME1 .pet`"
  1943.       [ "`echo $PKGNAME | grep ".rpm\$"`" != "" ]     && PKGEXT='.rpm'    && PKGNAME="`basename $PKGNAME1 .rpm`"
  1944.       [ "`echo $PKGNAME | grep ".tcz\$"`" != "" ]     && PKGEXT='.tcz'    && PKGNAME="`basename $PKGNAME1 .tcz`"
  1945.       [ "`echo $PKGNAME | grep ".tgz\$"`" != "" ]     && PKGEXT='.tgz'    && PKGNAME="`basename $PKGNAME1 .tgz`"
  1946.       [ "`echo $PKGNAME | grep ".txz\$"`" != "" ]     && PKGEXT='.txz'    && PKGNAME="`basename $PKGNAME1 .txz`"
  1947.       [ "`echo $PKGNAME | grep ".tar.gz\$"`" != "" ]    && PKGEXT='.tar.gz'   && PKGNAME="`basename $PKGNAME1 .tar.gz`"
  1948.       [ "`echo $PKGNAME | grep ".tar.xz\$"`" != "" ]    && PKGEXT='.tar.xz'   && PKGNAME="`basename $PKGNAME1 .tar.xz`"
  1949.       [ "`echo $PKGNAME | grep ".pkg.tar.gz\$"`" != "" ]  && PKGEXT='.pkg.tar.gz' && PKGNAME="`basename $PKGNAME1 .pkg.tar.gz`"
  1950.       [ "`echo $PKGNAME | grep ".pkg.tar.xz\$"`" != "" ]  && PKGEXT='.pkg.tar.xz' && PKGNAME="`basename $PKGNAME1 .pkg.tar.xz`"
  1951.  
  1952.       # get pkg name only .. without versions or suffix
  1953.       PKGNAME_ONLY="`echo "${PKGNAME}" | sed -e 's/-[0-9.]*/-/g' -e 's/-$//'`"
  1954.       # if that didnt work, use the old method
  1955.       if [ "$PKGNAME_ONLY" = '' -o "$PKGNAME_ONLY" = "$PKGNAME" ];then
  1956.         # get pkg name without version
  1957.         PKGNAMEONLY="`echo $PKGNAME | cut -d'-' -f1`"
  1958.       fi
  1959.       PKGNAME_ONLY="${PKGNAME_ONLY//-*/}"
  1960.  
  1961.       # get pkg version
  1962.       PKGVER="`LANG=C echo "$LINE" | sed -e 's/^[^0-9]*-//g' | cut -f1 -d'_' | cut -f1 -d'-'`"
  1963.       # get pkg size
  1964.       SIZE="`echo $LINE| cut -d'"' -f6 | cut -d' ' -f3`" #'micro
  1965.       SIZE=${SIZE## }
  1966.       SIZE=${SIZE%% }
  1967.       # must check again if pkg had no deps
  1968.       [ "$SIZE" = "" ] && SIZE=" `echo $LINE| cut -d'"' -f6|cut -d' ' -f2`"
  1969.       # get pkg category
  1970.       CAT="`echo $LINE | cut -d'"' -f6 | cut -d' ' -f1`"
  1971.       # remove extra categories
  1972.       [ "`echo "$CAT" | grep ';'`" != "" ] && CAT="`echo "$CAT" | cut -f1 -d';'`"
  1973.       # get pkg deps
  1974.       DEPS="`echo "$LINE" | cut -d'"' -f6 | cut -d' ' -f2 | grep ^'+'`"
  1975.       DESC="`echo "$LINE" | cut -f10 -d'|'`" #'micro
  1976.       # build the woof compatible repo file
  1977.       [ "$PKGNAME" != "" ] && echo "$PKGNAME|$PKGNAMEONLY|$VER|$BUILD|$CAT|$SIZE|$SUBDIR|${PKGNAME}${PKGEXT}|$DEPS|$DESC|$DISTRO_BINARY_COMPAT|$DISTRO_COMPAT_VERSION||" >> $TMPDIR/$FILENAME
  1978.     done
  1979.     #done making a woof repo file, print message
  1980.     [ -f $TMPDIR/$FILENAME ] && echo "Finished converting $FILENAME to woof format." || exit 1
  1981.   fi
  1982.  
  1983.   # if we are updating the repo, we dont wanna install the converted file straight over
  1984.   # the actual repo file, we wanna parse it for new pkgs and add only those
  1985.   if [ "$2" != "for_repo_update" ];then
  1986.     # we converted a repo that we actually wanna install, so install it
  1987.     mv $TMPDIR/$FILENAME ${HOME}/.packages/$FILENAME
  1988.     mv $TMPDIR/${FILENAME}_subdirs ${HOME}/.packages/${FILENAME}_subdirs 2>/dev/null
  1989.     echo "Repo file '${HOME}/.packages/$FILENAME' created."
  1990.     update_sources #210613 update the list of sources after adding the newly converted repo
  1991.     #exit 0
  1992.   else
  1993.     mv $TMPDIR/$FILENAME ${HOME}/$FILENAME
  1994.   fi
  1995. }
  1996.  
  1997.  
  1998. print_repo_info(){                # get repo settings, return current repo name FUNCLIST
  1999.  
  2000.   # get latest repo info (from sources file), or from PKGRC if that fails
  2001.   [ "$1" ] && get_repo_info "$1" || . ${PKGRC}
  2002.  
  2003.   local pkg_count=`cat ${HOME}/.packages/${REPOFILE} | wc -l`
  2004.  
  2005.   # output the repo info
  2006.   echo "- Repo:          $REPONAME"
  2007.   echo "- Repo file:     $REPOFILE"
  2008.   echo "- Package Type:  $EX"
  2009.   echo "- Packages:      $pkg_count"
  2010.   echo "- URL Mirror 1:  `echo $REPOURL1 | cut -f1-3 -d'/'`"
  2011.   [ "$REPOURL2" != "" ] && echo "- URL Mirror 2:  `echo $REPOURL2  | cut -f1-3 -d'/'`"
  2012.   [ "$REPOURL3" != "" ] && echo "- URL Mirror 3:  `echo $REPOURL3  | cut -f1-3 -d'/'`"
  2013.   [ "$REPOURL4" != "" ] && echo "- URL Mirror 4:  `echo $REPOURL4  | cut -f1-3 -d'/'`"
  2014.   echo
  2015.   echo "- Fall back to:`echo $REPOFALLBACKS | fold -w 50 -s | sed -e "s/ /, /g" -e "s/^/  /g"`"
  2016. }
  2017.  
  2018.  
  2019.  
  2020. # pkg search funcs
  2021.  
  2022. hide_blacklisted_pkgs_from_search_results(){ # hide BUILTIN and installed pkgs from searches NOLIST
  2023.  
  2024.   # dont hide package if using --force (the user may want to force a re-install of already installed pkg, for example)
  2025.   [ "$FORCE" = true ] && return 0
  2026.  
  2027.   # get pkg names (generic names, no versions) of all blacklisted packages in pipe delimited list
  2028.   blacklisted_pkgs_list="$(echo $PKG_NAME_IGNORE | sed -e 's/ /|/g')"
  2029.  
  2030.   # remove blacklisted packages from search list
  2031.   cat $TMPDIR/pkglist | grep -v -E "'$blacklisted_pkgs_list'" > $TMPDIR/pkglist_without_blacklisted
  2032.   mv $TMPDIR/pkglist_without_blacklisted $TMPDIR/pkglist
  2033.  
  2034.   # clean up tmp files
  2035.   rm $TMPDIR/pkglist_* &>/dev/null
  2036. }
  2037.  
  2038.  
  2039. hide_installed_pkgs_from_search_results(){  # hide BUILTIN and installed pkgs from searches NOLIST
  2040.  
  2041.   # dont hide package if using --force (the user may want to force a re-install of already installed pkg, for example)
  2042.   [ "$FORCE" = true ] && return 0
  2043.  
  2044.   # reset tmp file
  2045.   rm $TMPDIR/pkglist_inst &>/dev/null
  2046.  
  2047.   # get pkg names (generic names, no versions) of all installed pkgs (builtins, devx and user installed)
  2048.   inst_pkg_list="`cut -f1 -d'|' ${HOME}/.packages/user-installed-packages \
  2049.    ${HOME}/.packages/devx-only-installed-packages \
  2050.    ${HOME}/.packages/woof-installed-packages 2>/dev/null \
  2051.    | grep -v ^$ \
  2052.    | tr '\n' '|' \
  2053.    | sed -e "s/||/|/g" \
  2054.    | sed -e "s/|\$//g"`"
  2055.  
  2056.   # remove woof and user installed packages from search list
  2057.   cat $TMPDIR/pkglist | grep -v -E "'$inst_pkg_list'" > $TMPDIR/pkglist_without_inst
  2058.   mv $TMPDIR/pkglist_without_inst $TMPDIR/pkglist
  2059.  
  2060.   # clean up tmp files
  2061.   rm $TMPDIR/pkglist_* &>/dev/null
  2062. }
  2063.  
  2064.  
  2065. list_pkg_names(){                 # list pkg names in current repo only ($1 is optional filter) FUNCLIST
  2066.  
  2067.   # remove any previous searches
  2068.   rm $TMPDIR/pkglist* 2>/dev/null
  2069.  
  2070.   # get current repo ($REPOFILE)
  2071.   . ${PKGRC}
  2072.  
  2073.   # create the search results
  2074.   cut -f1 -d'|' ${HOME}/.packages/${REPOFILE} 2>/dev/null | grep "^$1" > $TMPDIR/pkglist
  2075.  
  2076.   # filter out builtin and user installed packages
  2077.   if [ $HIDE_INSTALLED = true ];then
  2078.     hide_installed_pkgs_from_search_results
  2079.   fi
  2080.  
  2081.   hide_blacklisted_pkgs_from_search_results
  2082.  
  2083.   # support pkg name aliases in finding packages
  2084.   if [ $NO_ALIASES = false ];then
  2085.     local ALIAS_LIST
  2086.     local ALIAS
  2087.     local ALIAS_RES
  2088.     # if we have some results to parse
  2089.     if [ "$1" != "" -a -f $TMPDIR/pkg_aliases ];then
  2090.       # get the list of aliases
  2091.       ALIAS_LIST="`grep -m1 "$1" $TMPDIR/pkg_aliases  2>/dev/null | tr ',' ' '`";
  2092.       # for each alias
  2093.       echo $ALIAS_LIST | while read ALIAS
  2094.       do
  2095.         [ "$ALIAS" = '' ] && continue
  2096.         # get the match from the current repo (if any)
  2097.         ALIAS_RES="`LANG=C cut -f1 -d'|' ${HOME}/.packages/${REPOFILE} 2>/dev/null | grep "^$ALIAS"`" #'
  2098.         [ ! "$ALIAS_RES" ] && ALIAS_RES="`LANG=C cut -f2 -d'|' ${HOME}/.packages/${REPOFILE} 2>/dev/null | grep "^$ALIAS"`" #'
  2099.         # if the repo match was found in the search results
  2100.         if [ "$ALIAS_RES" != "" ];then
  2101.           # add the alias results to the search results
  2102.           echo "$ALIAS_RES" >> $TMPDIR/pkglist
  2103.         fi
  2104.       done # for each alias
  2105.       # sort and clean the search results
  2106.       LANG=C cat $TMPDIR/pkglist | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq > $TMPDIR/pkglist1
  2107.       # replace the original search results
  2108.       mv $TMPDIR/pkglist1 $TMPDIR/pkglist
  2109.     fi
  2110.   fi
  2111.  
  2112.   # return the search results
  2113.   [ -s $TMPDIR/pkglist ] && cat $TMPDIR/pkglist 2>/dev/null
  2114.  
  2115.   # clean up
  2116.   [ ! -f $TMPDIR/pkglist ] && exit 1
  2117.   rm $TMPDIR/pkglist* 2>/dev/null
  2118.  
  2119. }
  2120.  
  2121.  
  2122. list_all_pkg_names(){             # list pkg names in any repo  ($1 is optional filter)FUNCLIST
  2123.  
  2124.   # remove any previous search results
  2125.   rm $TMPDIR/pkglist &>/dev/null
  2126.  
  2127.   # if bleeding edge disabled, output the list repo by repo, in the fallback order, current repo first (that order is set in update_sources)
  2128.   repo_file_list | while read repo_file
  2129.   do
  2130.     cut -f1 -d'|' ${HOME}/.packages/${repo_file} 2>/dev/null | grep "^$1" | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr >> $TMPDIR/pkglist
  2131.   done
  2132.  
  2133.   # if bleeding edge enabled, re-order the whole list, so Pkg returns the most recent pgk versions from ANY repos
  2134.   if [ "$BLEDGE" = "yes" ];then
  2135.     LANG=C cat $TMPDIR/pkglist  2>/dev/null | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr > $TMPDIR/pkglist1
  2136.     mv $TMPDIR/pkglist1 $TMPDIR/pkglist
  2137.   fi
  2138.  
  2139.   # filter out built-in and user installed packages
  2140.   if [ $HIDE_INSTALLED = true ];then
  2141.     hide_installed_pkgs_from_search_results
  2142.   fi
  2143.  
  2144.   hide_blacklisted_pkgs_from_search_results
  2145.  
  2146.   # support pkg name aliases in finding packages
  2147.   if [ $NO_ALIASES = false ];then
  2148.     local ALIAS_LIST
  2149.     local ALIAS
  2150.     local ALIAS_RES
  2151.     # if we have some results to parse
  2152.     if [ "$1" != "" -a -f $TMPDIR/pkg_aliases ];then
  2153.       # get the list of aliases
  2154.       ALIAS_LIST="`grep -m1 "$1" $TMPDIR/pkg_aliases  2>/dev/null | tr ',' ' '`";
  2155.       # for each repo
  2156.       LANG=C repo_file_list | while read RF
  2157.       do
  2158.         # and for each alias
  2159.         for ALIAS in $ALIAS_LIST; do
  2160.           # get the match from the current repo (if any)
  2161.           ALIAS_RES="`LANG=C cut -f1 -d'|' ${HOME}/.packages/${RF} 2>/dev/null | grep -m1 "^$ALIAS"`"
  2162.           [ ! "$ALIAS_RES" ] && ALIAS_RES="`LANG=C cut -f2 -d'|' ${HOME}/.packages/${RF} 2>/dev/null | grep -m1 "^$ALIAS"`"
  2163.           # if the repo match was found in the search results
  2164.           if [ "$ALIAS_RES" != "" ];then
  2165.             # add the alias results to the search results
  2166.             echo "$ALIAS_RES" >> $TMPDIR/pkglist
  2167.           fi
  2168.         done # for each alias
  2169.       done # for each repo
  2170.     fi
  2171.   fi
  2172.  
  2173.   # return the search results
  2174.   [ -s $TMPDIR/pkglist ] && LANG=C cat $TMPDIR/pkglist | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq
  2175.  
  2176.   # clean up
  2177.   [ ! -f $TMPDIR/pkglist ] && exit 1
  2178.   rm $TMPDIR/pkglist &>/dev/null
  2179. }
  2180.  
  2181.  
  2182. list_build_scripts(){             # list available build scripts  ($1 is optional filter)FUNCLIST
  2183.  
  2184.   # get settings from RC file
  2185.   . ${PKGRC}
  2186.  
  2187.   # make sure PetBuild is ready
  2188.   [ "$BUILDTOOL" = "petbuild" ] && prepare_petbuild
  2189.  
  2190.   # if no option given, just sort the results
  2191.   [ "$1" != "" ] && FILTER="grep -i $1" || FILTER="sort"
  2192.  
  2193.   # different build tools have their build scripts in different places.
  2194.   # check which BUILDTOOL we are using and list its build scripts
  2195.   case $BUILDTOOL in
  2196.     petbuild)
  2197.       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 ^$
  2198.     ;;
  2199.     buildpet)
  2200.       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 ^$
  2201.     ;;
  2202.     src2pkg)
  2203.       echo "Use: src2pkg FILE|URL or src2pkg -h"
  2204.     ;;
  2205.     sbopkg)
  2206.       sbopkg
  2207.     ;;
  2208.     *)
  2209.       echo "No build system configured. Set BUILDTOOL in $PKGRC."
  2210.       echo "Supported build tools: petbuild, buildpet, src2pkg, sbopkg"
  2211.     ;;
  2212.   esac
  2213. }
  2214.  
  2215.  
  2216. list_downloaded_pkgs(){           # list packages downloaded in WORKDIR ($1 is optional filter) FUNCLIST
  2217.  
  2218.   . ${PKGRC}
  2219.  
  2220.   cd "$WORKDIR" || { error "Cant cd into $WORKDIR"; exit 3; }
  2221.  
  2222.   local pkg
  2223.  
  2224.   # if no pkg given, list all
  2225.   if [ ! "$1" ];then
  2226.  
  2227.     # get all pkgs in WORKDIR
  2228.     find "${WORKDIR}" -maxdepth 1 -type f | sed -e "s#${WORKDIR}##g" -e "s#^/##g" | grep -v ^'.pkg' | sort \
  2229.       | while read pkg # but go through each and only print it if a valid pkg
  2230.       do
  2231.         [ "`is_local_pkg "$pkg"`" = true ] && echo $pkg || continue
  2232.       done
  2233.  
  2234.     return 0
  2235.  
  2236.   else
  2237.  
  2238.     # $1 might be a pkg, or list of pkgs, loop through them
  2239.     for x in $1; do
  2240.       if [ "$x" != "-" ];then
  2241.  
  2242.         # print the list of packages matching $x
  2243.         find "${WORKDIR}" -maxdepth 1 -type f | sed -e "s#${WORKDIR}##g" -e "s#^/##g" | grep -v ^'.pkg'|grep ^"$x" | sort \
  2244.           | while read pkg # but go through each and only print it if a valid pkg
  2245.           do
  2246.             [ "`is_local_pkg "$pkg"`" = true ] && echo $pkg || continue
  2247.           done
  2248.  
  2249.         return 0
  2250.       fi
  2251.     done
  2252.   fi
  2253.  
  2254.   cd -
  2255. }
  2256.  
  2257.  
  2258. list_installed_pkgs(){            # list user installed packages ($1 is optional filter) FUNCLIST
  2259.  
  2260.   local user_pkgs_list=''
  2261.   local builtins_list=''
  2262.   local devx_pkgs_list=''
  2263.  
  2264.   user_pkgs_list=${HOME}/.packages/user-installed-packages
  2265.  
  2266.   if [ "$HIDE_BUILTINS" != true -a -f ${HOME}/.packages/devx-only-installed-packages ];then
  2267.     devx_pkgs_list=${HOME}/.packages/devx-only-installed-packages
  2268.   fi
  2269.  
  2270.   [ "$HIDE_BUILTINS" != true ] && builtins_list=${HOME}/.packages/woof-installed-packages
  2271.  
  2272.   # search current repo file only .. $1 is the optional pkg filter.. take field1, remove empty lines, remove dashes, remove Pkg from pkg list too
  2273.   if [ ! "$1" ];then
  2274.     cut -f1 -d'|' $user_pkgs_list $devx_pkgs_list $builtins_list | grep -vE '^\$|^pkg\-'
  2275.     return 0
  2276.   else
  2277.     cut -f1 -d'|' $user_pkgs_list $devx_pkgs_list $builtins_list | grep "^$1" | grep -vE '^\$|^pkg\-'
  2278.     return 0
  2279.   fi
  2280. }
  2281.  
  2282.  
  2283. list_builtin_pkgs(){              # lists builtin packages FUNCLIST
  2284.  
  2285.   local builtins_repo
  2286.  
  2287.   builtins_repo=${HOME}/.packages/woof-installed-packages
  2288.  
  2289.   # 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
  2290.   cut -f1 -d'|' $builtins_repo | grep "^$1" | grep -v ^\$  | grep -v "\-$" | grep -v ^pkg\- | sort
  2291. }
  2292.  
  2293.  
  2294. search_pkgs(){                    # given $1, searches current repo, shows name and desc columns FUNCLIST
  2295.  
  2296.   local name
  2297.   local desc
  2298.   local descfull
  2299.   local search
  2300.  
  2301.   # convert "foo[ -_]bar" into "foo.*bar"
  2302.   search=`echo "$1" | sed -e 's| |.*|g' -e 's|-|.*|g' -e 's|_|.*|g'`
  2303.  
  2304.   # convert repo file to nice columnised output, use cat|grep so empty searches return all results
  2305.   cat ${HOME}/.packages/${REPOFILE} 2>/dev/null | grep -i "$search" | while read repo_line;
  2306.   do
  2307.     name=`echo  "$repo_line"|cut -f2  -d'|'`
  2308.     desc="`echo "$repo_line"|cut -f10 -d'|' | head -c 57`"
  2309.     descfull="`echo "$repo_line"|cut -f10 -d'|'`"
  2310.     [ "$desc" != "$descfull" ] && desc="${desc}.."
  2311.     printf "%-20s" "$name" " ${desc:-No description}"
  2312.     echo
  2313.   done
  2314. }
  2315.  
  2316.  
  2317. search_fast(){                    # given $1, searches current repo, show pkg names only, case sensitive FUNCLIST
  2318.   local RES
  2319.   local search
  2320.  
  2321.   # convert "foo[ -_]bar" into "foo.*bar"
  2322.   search=`echo "$1" | sed -e 's| |.*|g' -e 's|-|.*|g' -e 's|_|.*|g'`
  2323.  
  2324.   # get matching pkgs
  2325.   RES="`LANG=C grep "$search" ${HOME}/.packages/${REPOFILE} 2>/dev/null | cut -f1 -d'|'`"
  2326.   # error if no result
  2327.   [ ! "$RES" ] && exit 1
  2328.  
  2329.   # put results into file
  2330.   echo "$RES" > $TMPDIR/pkglist
  2331.  
  2332.   # hide built-in, devx and user installed packages
  2333.   if [ $HIDE_INSTALLED = true ];then
  2334.     hide_installed_pkgs_from_search_results
  2335.   fi
  2336.  
  2337.   hide_blacklisted_pkgs_from_search_results
  2338.  
  2339.   # now build the search results
  2340.   RES="`grep -v "^\$" $TMPDIR/pkglist 2>/dev/null | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq`"
  2341.   rm $TMPDIR/pkglist &>/dev/null
  2342.  
  2343.   #finshed making search results, print results
  2344.   [ "$RES" = "" ] && exit 1
  2345.   echo "$RES"
  2346. }
  2347.  
  2348.  
  2349. search_all_pkgs(){                # given $1, search all repos, show name and desc columns FUNCLIST
  2350.  
  2351.   local name
  2352.   local desc
  2353.   local descfull
  2354.   local search
  2355.  
  2356.   # convert "foo[ -_]bar" into "foo.*bar"
  2357.   search=`echo "$1" | sed -e 's| |.*|g' -e 's|-|.*|g' -e 's|_|.*|g'`
  2358.  
  2359.   for repo_file in `repo_file_list`
  2360.   do #090817
  2361.     # convert repo file to nice columnised output
  2362.     cat ${HOME}/.packages/${repo_file} 2>/dev/null | grep -i "$search" | while read repo_line;
  2363.     do
  2364.     # get details from repo
  2365.     name=`echo  "$repo_line"|cut -f2  -d'|'`
  2366.     desc="`echo "$repo_line"|cut -f10 -d'|' | head -c 57`"
  2367.     descfull="`echo "$repo_line"|cut -f10 -d'|'`"
  2368.     [ "$desc" != "$descfull" ] && desc="${desc}.."
  2369.     # remove spaces and comments (slackware-extra repo has some dodgy entries.. dogy names, descriptions with comments, etc)
  2370.     name="${name// /}"
  2371.     name="${name//#/}"
  2372.     [ "$name" = '' ] && continue
  2373.     # print columnised output
  2374.     printf "%-20s" "$name" " ${desc:-No description}"
  2375.     echo
  2376.     done
  2377.   done
  2378.  
  2379. }
  2380.  
  2381.  
  2382. search_all_fast(){                # given $1, search all repos, show pkg names only, case sensitive FUNCLIST
  2383.   local RES
  2384.   local search
  2385.  
  2386.   # convert "foo[ -_]bar" into "foo.*bar"
  2387.   search=`echo "$1" | sed -e 's| |.*|g' -e 's|-|.*|g' -e 's|_|.*|g'`
  2388.  
  2389.   #if bleeding edge enabled, combine into 1 list, so
  2390.   # that `pkg -g` etc return most recent from ALL repos
  2391.   rm $TMPDIR/pkglist &>/dev/null
  2392.   if [ "$BLEDGE" = "yes" ];then
  2393.     RES=''
  2394.     for RF in `repo_file_list`
  2395.     do #090817
  2396.       cat ${HOME}/.packages/${RF} 2>/dev/null | grep -i "$search" | cut -f1 -d'|' | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq | sed -e '/^$/d' >> $TMPDIR/pkglist
  2397.     done
  2398.  
  2399.   else
  2400.     # if bleeding edge disabled, output the list repo by repo, in #
  2401.     # the fallback order, current repo first (that order is set in update_sources)
  2402.     for RF in `repo_file_list`
  2403.     do #090817
  2404.       cat ${HOME}/.packages/${RF} 2>/dev/null | grep -i "$search"| cut -f1 -d'|' | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq | sed -e '/^$/d' >> $TMPDIR/pkglist
  2405.     done
  2406.  
  2407.   fi
  2408.  
  2409.   #100817 hide built-in, devx and user installed packages
  2410.   if [ $HIDE_INSTALLED = true ];then
  2411.     hide_installed_pkgs_from_search_results
  2412.   fi
  2413.  
  2414.   hide_blacklisted_pkgs_from_search_results
  2415.  
  2416.   # now build the search results
  2417.   RES="`grep -v "^\$" $TMPDIR/pkglist 2>/dev/null | sort --field-separator='-' -k1,1df -k2gr -k3gr -k4gr | uniq`"
  2418.   rm $TMPDIR/pkglist &>/dev/null
  2419.  
  2420.   #finshed making search results, print results
  2421.   [ "$RES" = "" ] && exit 1
  2422.  
  2423.   echo "$RES"
  2424. }
  2425.  
  2426.  
  2427. which_repo(){                     # list repo of given package ($1) FUNCLIST
  2428.  
  2429.   # if no valid options, quit
  2430.   [ ! "$1" -o "$1" = "-" -o "$1" = " " ] && print_usage which-repo && exit 1
  2431.  
  2432.   . ${PKGRC}
  2433.  
  2434.   local PKGNAME=''
  2435.   local PKGNAME_ONLY=''
  2436.   local pkg_ext=''
  2437.   local repo_file=''
  2438.   local repo_name=''
  2439.   local pkg_list=''
  2440.  
  2441.   PKGNAME=`get_pkg_name "$1"`
  2442.   PKGNAME_ONLY=`get_pkg_name_only "$1"`
  2443.   pkg_ext=`get_pkg_ext "$PKGNAME"`
  2444.  
  2445.   repo_file="`grep -l "|$PKGNAME.$pkg_ext|" ${HOME}/.packages/Packages-*`"
  2446.   if [ "$repo_file" != '' ];then
  2447.     # get the repo name
  2448.     repo_name="`grep -m1 "$(basename $repo_file)|" ${HOME}/.pkg/sources | cut -f1 -d'|'`"
  2449.     pkg_list="$PKGNAME $repo_name"
  2450.   else
  2451.  
  2452.     # for each repo
  2453.     for repo_file in `repo_file_list`
  2454.     do
  2455.       # get the repo name
  2456.       repo_name="`grep -m1 "$repo_file|" ${HOME}/.pkg/sources | cut -f1 -d'|'`"
  2457.       # create the entries, example: "vlc slacko14.2"
  2458.       # (each line shows a matching package, then a SPACE, then the name of repo the package lives in)
  2459.       pkg_list="$pkg_list`LANG=C grep "$PKGNAME" ${HOME}/.packages/${repo_file} 2>/dev/null | grep -m1 "^$PKGNAME|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2460.       [ "$pkg_list" = '' ] && pkg_list="$pkg_list`LANG=C grep "$PKGNAME" ${HOME}/.packages/${repo_file} 2>/dev/null | grep -m1 "|$PKGNAME.$EX|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2461.       [ "$pkg_list" = '' ] && pkg_list="$pkg_list`LANG=C grep "$PKGNAME" ${HOME}/.packages/${repo_file} 2>/dev/null | grep -m1 "|$1.$EX|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2462.       [ "$pkg_list" = '' ] && pkg_list="$pkg_list`LANG=C grep "$PKGNAME" ${HOME}/.packages/${repo_file} 2>/dev/null | grep -m1 "|$PKGNAME_ONLY|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2463.       [ "$pkg_list" = '' ] && pkg_list="$pkg_list`LANG=C grep "$PKGNAME" ${HOME}/.packages/${repo_file} 2>/dev/null | grep -m1 "^$PKGNAME_ONLY|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2464.       [ "$pkg_list" = '' ] && pkg_list="$pkg_list`LANG=C grep "$PKGNAME" ${HOME}/.packages/${repo_file} 2>/dev/null | grep -m1 "|$1|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2465.       [ "$pkg_list" = '' ] && pkg_list="$pkg_list`LANG=C grep "$PKGNAME" ${HOME}/.packages/${repo_file} 2>/dev/null | grep -m1 "^$1|" | cut -f1 -d'|' | tr '\n' '@' | sed -e "s#@# $repo_name\n#g"`"
  2466.     done
  2467.   fi
  2468.  
  2469.   # show results
  2470.   LANG=C echo -e "$pkg_list" | sed -e '/^$/d' | uniq
  2471. }
  2472.  
  2473.  
  2474. which_pkg(){                      # find out which pkg FILE ($1) comes from FUNCLIST
  2475.  
  2476.   # exit if no valid opts
  2477.   [ ! "$1" -o "$1" = '-' -o "$1" = '' ] && print_usage which && exit 1
  2478.  
  2479.   local PKGNAME=''
  2480.   local PKG_FILE_LIST=''
  2481.   local FILENAME=''
  2482.   local FILENAME_ONLY=''
  2483.   local DIRNAME=''
  2484.   local builtins_without_busybox="`find ${HOME}/.packages/builtin_files/* -maxdepth 1 -type f | grep -v "/busybox"`"
  2485.  
  2486.   # get user input
  2487.   FILENAME="`basename "$1"`"
  2488.   FILENAME_ONLY="$FILENAME"
  2489.   DIRNAME="`dirname "$1"`"
  2490.  
  2491.   # if we don't have a file, the user probably gave a command,
  2492.   # so lets check see
  2493.   if [ ! -f "$1" ];then
  2494.     # if we get a command, then that is our FILENAME
  2495.     cmd=`which "$1"`
  2496.     # if $cmd found, set FILENAME to $cmd (FILENAME now includes full path)
  2497.     [ "$cmd" != '' ] && FILENAME=$cmd && FILENAME_ONLY=`basename "$FILENAME"`
  2498.   fi
  2499.  
  2500.   # dont use relative paths
  2501.   [ "`echo "$DIRNAME" | grep "^\."`" != '' ] && DIRNAME=''
  2502.  
  2503.   # try user installed pkgs contents of $HOME/.packages/*.files
  2504.  
  2505.   # try '$dir/$file', returns filename (no path) of matching *.files
  2506.   [ "$PKG_FILE_LIST" = '' ] && PKG_FILE_LIST="`grep -l "$DIRNAME/$FILENAME\$"  ${HOME}/.packages/*.files 2>/dev/null| sed "s#$HOME/.packages/##g" 2>/dev/null`"
  2507.  
  2508.   # try '$dir/$file_*' if needed
  2509.   [ "$PKG_FILE_LIST" = '' ] && PKG_FILE_LIST="`grep -l "$DIRNAME/${FILENAME}_" ${HOME}/.packages/*.files 2>/dev/null| sed "s#$HOME/.packages/##g" 2>/dev/null`"
  2510.  
  2511.   # try '$dir/$file-*' if needed
  2512.   [ "$PKG_FILE_LIST" = '' ] && PKG_FILE_LIST="`grep -l "$DIRNAME/${FILENAME}\-" ${HOME}/.packages/*.files 2>/dev/null| sed "s#$HOME/.packages/##g" 2>/dev/null`"
  2513.  
  2514.   # if we found a package, set the package name to the name of the *.files file that we got
  2515.   [ "$PKG_FILE_LIST" != '' ] && PKGNAME="`basename "$PKG_FILE_LIST" .files`"
  2516.  
  2517.   # maybe we got nothing from *.files.. check builtins/$1* for ' $FILENAME_ONLY',
  2518.   # returns PKGNAME of the matching builtin
  2519.  
  2520.   # if needed, search inside builtin file lists (EXCEPT busybox) for ' $FILENAME_ONLY'
  2521.   [ "$PKG_FILE_LIST" = '' ] && [ "$PKGNAME" = "" ] \
  2522.     && PKGNAME="$(basename `grep -l "^ $FILENAME_ONLY\$" $builtins_without_busybox | sed "s#$HOME/.packages/builtin_files/##g"` 2>/dev/null)"
  2523.  
  2524.   # if that didn't work, search inside builtin file lists of busybox for ' $FILENAME_ONLY'
  2525.   [ "$PKG_FILE_LIST" = '' ] && [ "$PKGNAME" = "" ] \
  2526.     && PKGNAME="$(basename `grep -l "^ $FILENAME_ONLY\$" ${HOME}/.packages/builtin_files/busybox | sed "s#$HOME/.packages/builtin_files/##g"` 2>/dev/null)"
  2527.  
  2528.   # apps in ADRV sfs dont have their files listed in their $HOME/.packages/builtin_files/$pkgname.. so..
  2529.  
  2530.  
  2531.   # now check pkg names (not pkg contents) of builtins (EXCEPT busybox) for '$file'.. cos maybe we didnt find the file inside the file lists
  2532.   [ "$PKGNAME" = "" ] \
  2533.     && PKGNAME="`echo "$builtins_without_busybox" | grep -m1 "^${FILENAME_ONLY}\$" | sed "s#$HOME/.packages/builtin_files/##g" 2>/dev/null`"
  2534.  
  2535.   # now look for '$file' in user installed repo list
  2536.   [ "$PKGNAME" = "" ] \
  2537.     && PKGNAME="`cut -f1,2,8 -d'|' $HOME/.packages/user-installed-packages 2>/dev/null | sed -e "s/^/|/" -e "s/\$/|/" 2>/dev/null| grep -m1 "|$FILENAME_ONLY|" | cut -f2 -d'|'`"
  2538.  
  2539.   # also look for '$file' in user/woof/devx installed repo list
  2540.   [ "$PKGNAME" = "" ] \
  2541.     && PKGNAME="`cut -f1,2,8 -d'|' $HOME/.packages/*-installed-packages 2>/dev/null | sed -e "s/^/|/" -e "s/\$/|/" 2>/dev/null | grep -m1 "|$FILENAME_ONLY|" | cut -f2 -d'|'`"
  2542.  
  2543.  
  2544.   # we searched for an exact match in builtins, user installed, woof installed and devx pkgs, now try some fuzzier matches
  2545.  
  2546.   # try /$file_* in builtins
  2547.   [ "$PKGNAME" = "" ] \
  2548.     && PKGNAME="$(basename `grep -l " ${FILENAME_ONLY}_" ${HOME}/.packages/builtin_files/* | sed "s#$HOME/.packages/builtin_files/##g"` 2>/dev/null)"
  2549.  
  2550.   # try $file-* in builtins
  2551.   [ "$PKGNAME" = "" ] \
  2552.     && PKGNAME="$(basename `grep -l " ${FILENAME_ONLY}-" ${HOME}/.packages/builtin_files/* | sed "s#$HOME/.packages/builtin_files/##g"` 2>/dev/null)"
  2553.  
  2554.   # try $file.* in builtins
  2555.   [ "$PKGNAME" = "" ] \
  2556.     && PKGNAME="$(basename `grep -l " ${FILENAME_ONLY}\." ${HOME}/.packages/builtin_files/* | sed "s#$HOME/.packages/builtin_files/##g"` 2>/dev/null)"
  2557.  
  2558.   # try *file in builtin pkgs
  2559.   [ "$PKGNAME" = "" ] \
  2560.     && PKGNAME="$(basename `grep -l "${FILENAME_ONLY}"\$ ${HOME}/.packages/builtin_files/* | sed "s#$HOME/.packages/builtin_files/##g"` 2>/dev/null)"
  2561.  
  2562.  
  2563.   # if we still didnt find it, look in user installed package lists, for FILENAME
  2564.   [ "$PKGNAME" = "" ] \
  2565.     && PKGNAME=`cut -f1,2,8 -d '|' ${HOME}/.packages/user-installed-packages \
  2566.       | sed -e "s/^/|/" -e "s/\$/|/" 2>/dev/null \
  2567.       | grep -m1 "|${FILENAME_ONLY}|" \
  2568.       | cut -f2 -d'|'` 2>/dev/null
  2569.  
  2570.   # if we still didnt find it, look in all installed package lists, for FILENAME
  2571.   [ "$PKGNAME" = "" ] \
  2572.     && PKGNAME=`cut -f1,2,8 -d '|' ${HOME}/.packages/*-installed-packages \
  2573.       | sed -e "s/^/|/" -e "s/\$/|/" 2>/dev/null \
  2574.       | grep -m1 "|${FILENAME_ONLY}|" \
  2575.       | cut -f2 -d'|'` 2>/dev/null
  2576.  
  2577.   # if we still didnt find it, look in all installed package lists, for *FILENAME
  2578.   [ "$PKGNAME" = "" ] \
  2579.     && PKGNAME=`cut -f1,2,8 -d '|' ${HOME}/.packages/*-installed-packages \
  2580.       | sed -e "s/^/|/" -e "s/\$/|/" 2>/dev/null \
  2581.       | grep -m1 "${FILENAME_ONLY}|" \
  2582.       | cut -f2 -d'|'` 2>/dev/null
  2583.  
  2584.   # if we still didnt find it, look in all installed package lists, for FILENAME*
  2585.   [ "$PKGNAME" = "" ] \
  2586.     && PKGNAME=`cut -f1,2,8 -d '|' ${HOME}/.packages/*-installed-packages \
  2587.       | sed -e "s/^/|/" -e "s/\$/|/" \
  2588.       | grep -m1 "|${FILENAME_ONLY}" \
  2589.       | cut -f2 -d'|'` 2>/dev/null
  2590.  
  2591.  
  2592.   # clean up
  2593.   [ "$PKGNAME" = '.files' ] && PKGNAME=''
  2594.   PKGNAME="`echo "$PKGNAME" | head -1`"
  2595.  
  2596.   # now print pkgs which contain the given file ($1)
  2597.   if [ "$PKGNAME" != "" ];then
  2598.     echo "$PKGNAME"
  2599.     return 0
  2600.   else
  2601.     echo "File '$FILENAME' not found in any installed or built-in pkgs."
  2602.     exit 1
  2603.   fi
  2604. }
  2605.  
  2606.  
  2607.  
  2608. # pkg info funcs
  2609.  
  2610. pkg_contents(){                   # list package ($1) contents FUNCLIST
  2611.  
  2612.   # if no valid options, quit
  2613.   [ ! "$1" -o "$1" = "-" -o "$1" = " " ] && print_usage contents && exit 1
  2614.  
  2615.   # get settings
  2616.   . ${PKGRC}
  2617.  
  2618.   local PKGFILE
  2619.   local ext
  2620.   local PKGNAME
  2621.   local PKGNAME_ONLY
  2622.   local pkg_is_local_file
  2623.   local pkg_is_builtin
  2624.   local pkg_is_installed
  2625.   local PKGFLIST=''
  2626.  
  2627.   # get pkg extension
  2628.   ext=`get_pkg_ext "$1"`
  2629.  
  2630.   # get pkg name (includes version), but no extension or path
  2631.   PKGNAME="$(basename "$1" .$ext)"
  2632.   PKGNAME=`get_pkg_name "$PKGNAME"`
  2633.  
  2634.   # get pkg name without version
  2635.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  2636.  
  2637.   pkg_is_local_file=`is_local_pkg "$1"`
  2638.   pkg_is_builtin=`is_builtin_pkg "$PKGNAME_ONLY"`
  2639.   pkg_is_installed=`is_installed_pkg "$PKGNAME_ONLY"`
  2640.  
  2641.   # now we check various ways to find out the contents of
  2642.   # a pkg (either by *.files, user installed, woof, builtin, etc)
  2643.  
  2644.   [ "$pkg_is_local_file" = true ] && PKGFILE="$1"
  2645.  
  2646.   # try a file in the current dir if needed
  2647.   [ ! -f "$PKGFILE" ] && PKGFILE="${CURDIR}/${PKGNAME}.$ext"
  2648.   # check $WORKDIR if needed
  2649.   [ ! -f "$PKGFILE" ] && PKGFILE="${WORKDIR}/${PKGNAME}.$ext"
  2650.  
  2651.   # if the pkg is a local file
  2652.   if [ -f "$PKGFILE" ];then
  2653.     #list contents based on extension
  2654.     case "$ext" in
  2655.     sfs)
  2656.       unsquashfs -l "$PKGFILE"  | cut -f2-99 -d'/'| sed -e 's#^#/#g' 2>/dev/null | grep -v -E 'unsquashfs:|/squashfs-root|) to write' | grep -v "^/$"
  2657.     ;;
  2658.     deb)
  2659.       dpkg-deb -c "$PKGFILE" 2>/dev/null | grep -v "^/$"
  2660.      ;;
  2661.     pet|tar|tar**tcz|*txz|*tgz|*xz|*gz)
  2662.       # remove leading pkg name from each line of file list
  2663.       # (shown if listing *some* pkg file contents)
  2664.       tar -tf "$PKGFILE" 2>/dev/null \
  2665.         | sed -e "s#^./${PKGNAME}/#/#g" -e "s#^${PKGNAME}/#/#g" -e "s#^./##g" 2>/dev/null \
  2666.         | grep -v ^$ 2>/dev/null 2>/dev/null \
  2667.         | grep -v "^/$"
  2668.     ;;
  2669.     rpm)
  2670.       busybox rpm -qlp "$PKGFILE" 2>/dev/null | grep -v "^/$"
  2671.     ;;
  2672.     esac
  2673.     exit $?
  2674.   fi
  2675.  
  2676.   # if we are here, the pkg is not a downloaded pkg, so we build a
  2677.   # list of files by checking  *.files, woof, builtins ..
  2678.  
  2679.   # check if we need to (and can) get our pkg contents from builtins/PKGNAME
  2680.   local  need_to_check_builtins=`echo "$PKG_FLIST" | grep -m1 'packages/builtin_files/'`
  2681.  
  2682.   # if the pkg is a builtin, we will re-format the output of LIST to match the others (full paths on each line)
  2683.   if [ "$need_to_check_builtins" != '' -a "$pkg_is_builtin" = true ];then
  2684.  
  2685.     # reset the list of pkg files, we will re-build it
  2686.     rm $TMPDIR/pkg_file_list 2>/dev/null
  2687.  
  2688.     # first we get the package contents from $HOME/.packages/builtin_files/$PKGNAME_ONLY
  2689.     cat "$PKG_FLIST" 2>/dev/null | while read line
  2690.     do
  2691.       # parse it, so we get a full path to file on each line
  2692.       if [ "`echo "$line" | grep '^/'`" != '' ];then
  2693.         # set the dir to use in our file list
  2694.         dir="$line"
  2695.       else
  2696.         # keep previous dir
  2697.         dir="$dir"
  2698.       fi
  2699.  
  2700.       # create our new line (a full file path) to $LIST
  2701.       line_to_add="$dir/`echo "$line" | cut -f2 -d' '`"
  2702.  
  2703.       # if only a dir, skip it
  2704.       [ "$line" = "$dir" ] && continue
  2705.  
  2706.       # if its already added, skip it
  2707.       [ "`grep -m1 "$line_to_add" ${TMPDIR}/pkg_file_list 2>/dev/null `" != '' ] && continue
  2708.  
  2709.       # if its not a file on the system (it should be), skip it
  2710.       [ ! -f "$line_to_add" ] && continue
  2711.  
  2712.       # all should be ok, add the line to our list
  2713.       echo "$line_to_add" >> $TMPDIR/pkg_file_list
  2714.  
  2715.     done
  2716.  
  2717.     # now get our pkg contents list, it might have been re-formatted (if a builtin)
  2718.     PKG_FLIST="`cat ${TMPDIR}pkg_file_list 2>/dev/null`"
  2719.  
  2720.     # last resort, if we lost our file list or still dont have, try the basics again
  2721.     [ "$PKG_FLIST" = '' ] && PKG_FLIST="`find $HOME/.packages/ -maxdepth 1 -type f -name "${PKGNAME}*.files" 2>/dev/null`"
  2722.  
  2723.     # clean up the list a bit
  2724.     [ "$PKG_FLIST" != '' ] && PKG_FLIST="`echo "$PKG_FLIST" | grep -v ' ' | grep -v "^\$" | sort | uniq`"
  2725.  
  2726.     # and clean up the tmp files
  2727.     rm ${TMPDIR}pkg_file_list 2>/dev/null
  2728.   fi
  2729.  
  2730.   # try PKGNAME_ONLY.files (exact match)
  2731.   PKG_FLIST="`find $HOME/.packages/ -maxdepth 1 -type f -name "${PKGNAME_ONLY}.files" 2>/dev/null`"
  2732.  
  2733.   # try finding PKGNAME.files (exact match)
  2734.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="`find $HOME/.packages/ -maxdepth 1 -type f -name "${PKGNAME}.files" 2>/dev/null`"
  2735.  
  2736.   # try the builtins files (exact match)
  2737.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="`find $HOME/.packages/builtin_files/ -maxdepth 1 -type f -name "$PKGNAME_ONLY"`"
  2738.  
  2739.   # try PKGNAME (exact match) in user installed pkgs
  2740.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$HOME/.packages/`cut -f1 -d'|' $HOME/.packages/user-installed-packages | grep -m1 ^"$PKGNAME\$" 2>/dev/null`.files"
  2741.  
  2742.   # try PKGNAME (exact match) in all installed pkgs
  2743.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$HOME/.packages/`cut -f1 -d'|' $HOME/.packages/*-installed-packages | grep -m1 ^"$PKGNAME\$" 2>/dev/null`.files"
  2744.  
  2745.   # try PKGNAME_ONLY (exact match) in all installed pkgs
  2746.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$HOME/.packages/`cut -f2 -d'|' $HOME/.packages/*-installed-packages | grep -m1 ^"$PKGNAME_ONLY\$" 2>/dev/null`.files"
  2747.  
  2748.  
  2749.   # no exact matches found, try fuzzy searches..
  2750.  
  2751.   # try PKGNAME*.files
  2752.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="`find $HOME/.packages/ -maxdepth 1 -type f -name "/${PKGNAME}*.files"  2>/dev/null`"
  2753.  
  2754.   # try finding *PKGNAME.files
  2755.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="`find $HOME/.packages/ -maxdepth 1 -type f -name "*${PKGNAME}.files" 2>/dev/null`"
  2756.  
  2757.   # try ^PKGNAME_ONLY[_-] in user installed pkgs
  2758.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$HOME/.packages/`cut -f1 -d'|' $HOME/.packages/user-installed-packages | grep -Em1 "^$PKGNAME_ONLY[_-]" 2>/dev/null`.files"
  2759.  
  2760.   # try PKGNAME_ONLY* in user installed pkgs only
  2761.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$HOME/.packages/`cut -f1 -d'|' $HOME/.packages/user-installed-packages | grep -m1 ^"$PKGNAME_ONLY" 2>/dev/null`.files"
  2762.  
  2763.   # try PKGNAME_ONLY-* (any installed pkgs)
  2764.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$HOME/.packages/`cut -f1 -d'|' $HOME/.packages/*-installed-packages | grep -Em1 ^"${PKGNAME_ONLY}[_-]" 2>/dev/null`.files"
  2765.  
  2766.   # try PKGNAME_ONLY* (any installed pkgs)
  2767.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="$HOME/.packages/`cut -f1 -d'|' $HOME/.packages/*-installed-packages | grep -m1 ^"$PKGNAME_ONLY" 2>/dev/null`.files"
  2768.  
  2769.   # try PKGNAME_ONLY*.files
  2770.   [ ! -f "$PKG_FLIST" ] && PKG_FLIST="`find $HOME/.packages/ -maxdepth 1 -type f -name "/${PKGNAME_ONLY}*.files"  2>/dev/null`"
  2771.  
  2772.  
  2773.  
  2774.   #if we found a list of files
  2775.   if [ "$PKG_FLIST" != "" ];then
  2776.  
  2777.     print_cmd=echo
  2778.     # PKG_FLIST just might contain a path to the pkg contents
  2779.     # themselves (a *.files, or a file in builtin_files/*).. so if
  2780.     # its a file path, we will cat it, if not, its the pkg contents
  2781.     # themselves, we echo it
  2782.     if [ "`echo "$PKG_FLIST" | grep -m1 '/builtin_files/'`" != '' -o "`echo "$PKG_FLIST" | grep -m1 "packages/$PKGNAME"`" != '' ];then
  2783.       print_cmd=cat
  2784.     fi
  2785.  
  2786.     $print_cmd "$PKG_FLIST" | grep -v "^/$" 2>/dev/null
  2787.  
  2788.   else # if no files found
  2789.     if [ "$PKGNAME" != '' ];then
  2790.       INST_CHECK="`is_installed_pkg $PKGNAME 2>/dev/null`"
  2791.       if [ "$INST_CHECK" != true -o "$pkg_is_local_file" = false ];then
  2792.         error "Package must be installed, downloaded or built-in."
  2793.       else
  2794.         error "Could not get package contents, unable to get file list."
  2795.       fi
  2796.     else
  2797.       error "Could not get name of package."
  2798.     fi
  2799.   fi
  2800. }
  2801.  
  2802.  
  2803. pkg_entry(){                      # show pkg ($1) repo entry, each field on a new line FUNCLIST
  2804.  
  2805.   # exit if no valid opts
  2806.   [ ! "$1" -o "$1" = '-' ] && print_usage pkg-entry && exit 1
  2807.  
  2808.   local EX
  2809.   local PKGNAME
  2810.   local PKGNAME_ONLY
  2811.  
  2812.   # get pkg extension
  2813.   EX=`get_pkg_ext "$1"`
  2814.  
  2815.   # get pkg name with version, but no extension or path
  2816.   PKGNAME="$(basename "$1" .$EX)"
  2817.  
  2818.   # dont rely on the string the user gave us, try to get the exact match
  2819.   PKGNAME=`get_pkg_name "$PKGNAME"`
  2820.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  2821.  
  2822.   PKG_ENTRY="`cat ${HOME}/.packages/Packages-* | grep -m1 "|$PKGNAME|"`"
  2823.   [ "$PKG_ENTRY" = '' ] && PKG_ENTRY="`cat ${HOME}/.packages/Packages-* | grep -m1 "^$PKGNAME|"`"
  2824.   [ "$PKG_ENTRY" = '' ] && PKG_ENTRY="`cat ${HOME}/.packages/Packages-* | grep -m1 "|$PKGNAME_ONLY|"`"
  2825.   if [ "$PKG_ENTRY" = '' ];then
  2826.     echo "$1 not found in $REPOFILE"
  2827.     exit 1
  2828.   else
  2829.     echo "$PKG_ENTRY" | tr '|' '\n' | grep -v '^$'
  2830.   fi
  2831.   return 0
  2832. }
  2833.  
  2834.  
  2835. pkg_status(){                     # print package ($1) name, status, deps, etc FUNCLIST
  2836.  
  2837.   # exit if no valid options
  2838.   [ ! "$1" -o "$1" = "-" ] && print_usage pkg-status && exit 1
  2839.  
  2840.   # get current repo name
  2841.   . ${PKGRC}
  2842.  
  2843.   local EX
  2844.   local PKGNAME=''
  2845.   local PKGNAME_ONLY=''
  2846.   local PKGFILE=''
  2847.   local MSG=''
  2848.   local install_status=''
  2849.   local pkg_found=false
  2850.   local pkg_repo=''
  2851.   local pkg_repo_file=''
  2852.   local prev_repo=${REPONAME}
  2853.  
  2854.   # get pkg extension
  2855.   EX=`get_pkg_ext "$1"`
  2856.  
  2857.   # get pkg name with version, but no extension or path
  2858.   PKGNAME="$(basename "$1" .$EX)"
  2859.  
  2860.   # dont rely on the string the user gave us, try to get the exact match
  2861.   PKGNAME=`get_pkg_name "$PKGNAME"`
  2862.  
  2863.   # get pkg name without version
  2864.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  2865.  
  2866.   [ "$PKGNAME" = '' ] && error "Can't find $1" && exit 1
  2867.  
  2868.   # get install status of pkg (installed or not)
  2869.   [ "`is_installed_pkg $PKGNAME`" = true ] && install_status="installed" || install_status="not installed"
  2870.  
  2871.   # if pkg is installed, find out from where
  2872.   if [ "$install_status" = "installed" ];then
  2873.  
  2874.     # check user installed pkgs for $PKGNAME
  2875.     pkg_found=`LANG=C is_usr_pkg "${PKGNAME}"`
  2876.     [ "$pkg_found" = true ] && install_status="installed (user)"
  2877.  
  2878.     # if pkg not found yet, check if pkg is from the devx
  2879.     if [ -f ${HOME}/.packages/devx-only-installed-packages -a "$pkg_found" = false ];then
  2880.       pkg_found=`LANG=C is_devx_pkg "${PKGNAME}"`
  2881.       [ "$pkg_found" = true ] && install_status="installed (in devx)"
  2882.     fi
  2883.  
  2884.     # check builtins for PKGNAME_ONLY, if it exists, its a builtin pkg
  2885.     if [ -d ${HOME}/.packages/builtin_files/ -a "$pkg_found" = false ];then
  2886.       pkg_found=`LANG=C is_builtin_pkg "${PKGNAME_ONLY}"`
  2887.       [ "$pkg_found" = true ] && install_status="installed (builtin)"
  2888.     fi
  2889.  
  2890.     # last gasp, if pkg not found yet, check if pkg if listed in layers-installed packages
  2891.     if [ -f ${HOME}/.packages/layers-installed-packages -a "$pkg_found" = false ];then
  2892.       pkg_found=`LANG=C grep -m1 "^${PKGNAME}|" ${HOME}/.packages/layers-installed-packages`
  2893.  
  2894.       [ "$pkg_found" = '' ] && install_status="installed (layers)"
  2895.     fi
  2896.  
  2897.   fi
  2898.  
  2899.   # get the repo of this pkg, if needed
  2900.   [ "$pkg_repo" = '' ] && pkg_repo="`which_repo "$PKGNAME" 2>/dev/null | cut -f2 -d' '`"
  2901.  
  2902.   # if we got a repo name, get the repo file
  2903.   [ "$pkg_repo" != '' ] && pkg_repo_file="$(grep -m1 "^$pkg_repo|" ${HOME}/.pkg/sources | cut -f3 -d'|')"
  2904.  
  2905.   # if we have a repo to search for pkg info
  2906.   if [ "$pkg_repo_file" != "" ];then
  2907.  
  2908.     # get package description from its repo entry
  2909.     pkg_desc="`LANG=C grep -m1 "|${PKGNAME_ONLY}|" $HOME/.packages/$pkg_repo_file 2>/dev/null| grep -m1 "$PKGNAME" | cut -f10 -d'|' | head -1`"
  2910.  
  2911.     if [ "$pkg_desc" = '' ];then
  2912.       pkg_desc="`LANG=C grep -m1 "|${PKGNAME_ONLY}|" $HOME/.packages/Packages-* 2>/dev/null| grep -m1 "${PKGNAME}|" | cut -f10 -d'|' | head -1`"
  2913.     fi
  2914.  
  2915.     # get size of pkg
  2916.     pkg_size=`LANG=C grep -m1 "|${PKGNAME_ONLY}|" $HOME/.packages/$pkg_repo_file 2>/dev/null | cut -f6 -d'|' | head -1`
  2917.  
  2918.     # add K to end of pkg_size, if not there already
  2919.     [ "$pkg_size" != '' ] && pkg_size="${pkg_size}K" && pkg_size="${pkg_size//KK/K}"
  2920.  
  2921.     # if called with -PS, we get the full info (where we sort deps missing or installed)
  2922.     if [ "$FULL_PKG_STATUS" = true ];then
  2923.  
  2924.       # if pkg has many deps, checking the deps might take a while, print a 'please wait' msg
  2925.       please_wait=false
  2926.       # count deps of pkg
  2927.       [ "`has_deps "$PKGNAME"`" = true ] && please_wait=true
  2928.       [ "$please_wait" = true ] && echo -ne "Please wait.. Gathering dependency information.\n"
  2929.  
  2930.       set_current_repo $pkg_repo 1>/dev/null
  2931.       # sort deps into 2 lists: missing deps and installed deps
  2932.       find_deps "$PKGNAME"
  2933.       set_current_repo $prev_repo 1>/dev/null
  2934.  
  2935.       # create a nicely formatted, coloured list of deps, green for installed, yellow for missing
  2936.       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}"
  2937.       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}"
  2938.  
  2939.       DEPS_ENTRY="Installed deps: ${INSTALLED_DEPS_MSG:-None}
  2940. Missing deps:   ${MISSING_DEPS_MSG:-None}"
  2941.  
  2942.     else
  2943.  
  2944.       DEPS_ENTRY="Dependencies:   `list_deps "$PKGNAME" 2>/dev/null`"
  2945.  
  2946.     fi
  2947.  
  2948.     #250613 added desc
  2949.     MSG="
  2950. Name:           ${PKGNAME}
  2951. Description:    `echo ${pkg_desc:-No description} | fold -w 50 -s | sed '2,10 s/^/                /g'`
  2952. Size:           ${pkg_size:-Unknown}
  2953. Status:         ${install_status:-Unknown}
  2954. In Repo:        ${pkg_repo:-Not in any repos}
  2955. Repo file:      `basename ${pkg_repo_file:-Not in any repositories} 2>/dev/null`
  2956. $DEPS_ENTRY"
  2957.  
  2958.   fi
  2959.  
  2960.   # if not found in any repo, maybe a woof or alien (user-installed) package
  2961.   if [ "$MSG" = "" ];then #if nothing found in any repo
  2962.  
  2963.     PKGFILE="`list_downloaded_pkgs | grep -m1 "^$PKGNAME"`"
  2964.  
  2965.     # if the pkg is a downloaded file (in WORKDIR)
  2966.     if [ -f "$WORKDIR/$PKGFILE" ];then
  2967.  
  2968.       # get the file, and get its file size
  2969.       PKGFILE="$WORKDIR/${PKGFILE}"
  2970.       [ "$pkg_size" = '' ] && pkg_size="`du -s -k "$PKGFILE" | cut -f1`K"
  2971.       [ "$pkg_size" = '' ] && pkg_size="`grep -m1 "|${PKGNAME_ONLY}|" ${HOME}/.packages/*-installed-packages | cut -f6 -d'|' | head -1`"
  2972.  
  2973.  
  2974.       # create msg for downloaded pkgs
  2975.       MSG="Name:           ${PKGNAME}
  2976. Size:           ${pkg_size:-Unknown}
  2977. Status:         Downloaded
  2978. Note:           Downloaded package, not in any repo.
  2979. $DEPS_ENTRY"
  2980.  
  2981.     # else, search for exact match, then pkg-*, then pkg_*, then pkg* in installed pkgs
  2982.     elif [ "`grep -m1 "|${PKGNAME_ONLY}|" ${HOME}/.packages/user-installed-packages 2>/dev/null`"   \
  2983.       -o "`grep -m1 "^${PKGNAME}|" ${HOME}/.packages/user-installed-packages 2>/dev/null`"   \
  2984.       -o "`grep -m1 "^${PKGNAME}|" ${HOME}/.packages/*-installed-packages 2>/dev/null`"   \
  2985.       -o "`grep -m1 "^${PKGNAME}-" ${HOME}/.packages/*-installed-packages 2>/dev/null`" \
  2986.       -o "`grep -m1 "^${PKGNAME}_" ${HOME}/.packages/*-installed-packages 2>/dev/null`" \
  2987.       -o "`grep -m1 "^${PKGNAME}" ${HOME}/.packages/*-installed-packages 2>/dev/null`" ];then
  2988.  
  2989.       # get deps and size from its entry in user-installed-packages
  2990.       DEPS_ENTRY="Dependencies:   `grep -m1 "^$PKGNAME" ${HOME}/.packages/user-installed-packages | cut -f9 -d'|' | sed -e 's/,+/,/g' -e 's/^+//g' -e 's/,$//g' 2>/dev/null`"
  2991.       [ "$DEPS_ENTRY" = '' ] && DEPS_ENTRY="Dependencies:   `grep -m1 "^$PKGNAME" ${HOME}/.packages/*-installed-packages | cut -f9 -d'|' | sed -e 's/,+/,/' -e 's/^+//' -e 's/,$//' 2>/dev/null`"
  2992.       pkg_size=`LANG=C grep -m1 "|$PKGNAME_ONLY|" $HOME/.packages/*-installed-packages | cut -f6 -d'|' | head -1`
  2993.       # add K to end of pkg_size, if not there already
  2994.       [ "$pkg_size" != '' ] && pkg_size="${pkg_size}K" && pkg_size="${pkg_size//KK/K}"
  2995.  
  2996.       # create msg for user installed pkgs
  2997.       MSG="Name:      ${PKGNAME}
  2998. Size:           ${pkg_size:-Unknown}
  2999. Status:         ${install_status}
  3000. Repo:           Alien package, not in any repo.
  3001. $DEPS_ENTRY"
  3002.  
  3003.     else # pkg wasn't found, it's unknown to Pkg
  3004.       MSG="$PKGNAME not found.
  3005.  
  3006. Here are some packages in other repos:"
  3007.       PLIST="`which_repo ${PKGNAME}`"
  3008.       [ "$PLIST" != "" ] && MSG="$MSG
  3009. $PLIST"
  3010.       # create msg for alien pkgs
  3011.       MSG="$MSG
  3012.  
  3013. Name:           ${PKGNAME}
  3014. Status:         ${install_status:-Unknown}
  3015. Repo:           Alien package, not in $REPONAME repo.
  3016. $DEPS_ENTRY"
  3017.  
  3018.     fi
  3019.  
  3020.   fi
  3021.  
  3022.   # print message
  3023.   if [ "$MSG" != "" ];then
  3024.     # now output the final msg
  3025.     echo -e "$MSG" | grep -v ^$
  3026.     menu_entry_msg "$PKGNAME"
  3027.     echo
  3028.   else
  3029.     not_found "${PKGNAME}"
  3030.     exit 1
  3031.   fi
  3032. }
  3033.  
  3034.  
  3035. # pkg creation funcs
  3036.  
  3037. prepare_petbuild(){               # get 01mickos petbuild system from Git, if needed FUNCLIST
  3038.  
  3039.   local gitcheck=`which git`
  3040.  
  3041.   # exit if no devx
  3042.   [ "`which gcc`" = '' ] \
  3043.     && echo "You need the devx SFS installed to compile packages." \
  3044.     && rm /tmp/pkg/petbuild_prepared 2>/dev/null \
  3045.     && exit 3
  3046.  
  3047.   # exit if no git
  3048.   [ "`$gitcheck`" = '' ] \
  3049.     && echo "You need git installed to auto-install petbuild." \
  3050.     && rm /tmp/pkg/petbuild_prepared 2>/dev/null \
  3051.     && exit 3
  3052.  
  3053.   if [ ! -d /usr/share/petbuild ];then
  3054.     rm /tmp/pkg/petbuild_prepared 2>/dev/null && exit 1
  3055.   fi
  3056.  
  3057.   # if petbuild prepared flag not yet set
  3058.   if [ ! -f /tmp/pkg/petbuild_prepared ];then
  3059.  
  3060.     # backup old petbuild if installed
  3061.     [ -d /usr/share/petbuild/ ] && mv /usr/share/petbuild/ /usr/share/petbuild.backup/ 2>/dev/null
  3062.  
  3063.     # remove the original petbuild dir (we have a backup)
  3064.     if [ -d /usr/share/petbuild.backup/ ];then
  3065.       rm -rf /usr/share/petbuild 2>/dev/null
  3066.     fi
  3067.  
  3068.     # clone petbuild into /usr/share/petbuild
  3069.     git clone https://github.com/puppylinux-woof-CE/petbuilds.git /usr/share/petbuild &>/dev/null
  3070.  
  3071.     # if still no petbuild dir in /usr/share, tell user to install themselves
  3072.     if [ ! -d /usr/share/petbuild ];then
  3073.       echo >&2
  3074.       echo "Run this command to download PetBuild:" >&2
  3075.       echo >&2
  3076.       echo -e " ${green}git clone https://github.com/puppylinux-woof-CE/petbuilds /usr/share/petbuild${endcolour}" >&2
  3077.       echo >&2
  3078.       echo "Alternatively, use a different build tool backend by changing BUILDTOOL= " >&2
  3079.       echo "to one of the options below, in the $PKGRC file:" >&2
  3080.       echo "petbuild, buildpet, sbopkg or src2pkg" >&2
  3081.       echo >&2
  3082.  
  3083.       exit 1
  3084.     fi
  3085.  
  3086.     # go into buildpet dir, check which Pup we have ($DISTRO_DB_SUBNAME)
  3087.     # and checkout the right branch for the running system
  3088.     cd /usr/share/petbuild
  3089.  
  3090.     # check the puppy running and checkout the relevant branch
  3091.     if [ "`echo $DISTRO_DB_SUBNAME | grep slacko`" != '' ];then
  3092.       if [ "`echo $DISTRO_DB_SUBNAME | grep '14.2'`" != '' ];then
  3093.         git checkout slacko_142
  3094.       else
  3095.         git checkout slacko_141
  3096.       fi
  3097.     elif [ "`echo $DISTRO_DB_SUBNAME | grep tahrpup`" ];then
  3098.       git checkout tahrpup
  3099.     elif [ "`echo $DISTRO_DB_SUBNAME | grep stretch`" ];then
  3100.       git checkout stretch || git checkout tahrpup
  3101.     elif [ "`echo $DISTRO_DB_SUBNAME | grep xenialpup`" ];then
  3102.       git checkout xenialpup || git checkout tahrpup
  3103.     elif [ "$DISTRO_DB_SUBNAME" != '' ];then
  3104.       git checkout tahrpup
  3105.     else
  3106.       echo "No pet builds available for your system ($DISTRO_DB_SUBNAME),"
  3107.       echo "using buildpet instead.."
  3108.       rm -rf /usr/share/petbuild 2>/dev/null
  3109.     fi
  3110.  
  3111.     # buildpet setup finished, create flag
  3112.     echo 'true' > /tmp/pkg/petbuild_prepared
  3113.  
  3114.     cd "$CURDIR"
  3115.   fi
  3116. }
  3117.  
  3118.  
  3119. pkg_build(){                      # build package ($1) from source (see BUILDTOOL in pkgrc) FUNCLIST
  3120.  
  3121.   # exit if devx not installed
  3122.   [ "`which gcc`" = "" ]   && echo "You need the devx SFS installed to compile." && exit 1
  3123.  
  3124.   # get the settins from RC file
  3125.   . ${PKGRC}
  3126.  
  3127.   # we do a different build method for each supported build tool
  3128.   case $BUILDTOOL in
  3129.  
  3130.     petbuild) # by 01micko
  3131.  
  3132.       # use git to install the latest petbuild
  3133.       prepare_petbuild
  3134.  
  3135.       # if petbuild not installed, quit
  3136.       [ ! -d /usr/share/petbuild ] && error "PetBuild not installed at /usr/share/petbuild." && exit 3
  3137.  
  3138.       # petbuild needs a package name, or exit
  3139.       [ ! "$1" -o "$1" = "-" ] && print_usage pkg-build && exit 1 #220613 #250613
  3140.  
  3141.       # get pkg extension
  3142.       EX=`get_pkg_ext "$1"`
  3143.  
  3144.       # get pkg name only, no extension or path
  3145.       PKGNAME="$(basename "$1" .$EX)"
  3146.  
  3147.       # get petbuild PKGNAME, then build using 01mickos petbuild
  3148.       BUILDSCRIPT="`find /usr/share/petbuild -iname ${PKGNAME}"*.petbuild"| grep -m1 "$PKGNAME"`"
  3149.  
  3150.       # if we got a build script
  3151.       if [ -f "$BUILDSCRIPT" ];then
  3152.         cd `dirname "$BUILDSCRIPT"`
  3153.         # compile the pkg
  3154.         bash *.petbuild
  3155.         # after build, move any pets created to WORKDIR
  3156.         mv /usr/share/petbuild/0pets_out/*.pet "$WORKDIR" 2>/dev/null
  3157.         # back to prev dir (WORKDIR)
  3158.         cd -
  3159.       fi
  3160.  
  3161.       # if build script not found, exit
  3162.       [ ! -f "$BUILDSCRIPT" ] && echo "Build script for '$PKGNAME' not found in /usr/share/petbuild/" && exit 1
  3163.  
  3164.       # get the packages we just moved to WORKDIR
  3165.       PKGFILES="`find "$WORKDIR" -iname "$PKGNAME*.pet"`"
  3166.  
  3167.       # list any packages we just built
  3168.       [ "$PKGFILES" != '' ] && echo -e "${green}Success:${endcolour} Packages in $WORKDIR:" && echo "$PKGFILES"
  3169.     ;;
  3170.  
  3171.     buildpet) # by Tman/iguleder
  3172.  
  3173.       # exit if buildpet dir not found
  3174.       [ ! -d /usr/share/buildpet ] && error "buildpet not installed." && exit 1
  3175.  
  3176.       # buildpet expects a PKGNAME, or exit
  3177.       [ ! "$1" -o "$1" = "-" ] && print_usage pkg-build && exit 1
  3178.  
  3179.       # get pkg extension
  3180.       EX=`get_pkg_ext "$1"`
  3181.  
  3182.       # get pkg name (with version), no extension or path
  3183.       PKGNAME="$(basename "$1" .$EX)"
  3184.  
  3185.       # get the buildpet script of PKGNAME
  3186.       BUILDSCRIPT="`find /usr/share/buildpet/ -name $PKGNAME"*"`"
  3187.  
  3188.       # if build script not found, exit
  3189.       [ ! -f "$BUILDSCRIPT" ] && echo "Build script for '$PKGNAME' not found in /usr/share/buildpet/" && exit 1
  3190.  
  3191.       # now compile the package
  3192.       PKG_CONFIGURE="$PKG_CONFIGURE" PKG_CFLAGS="$PKG_CFLAGS" buildpet $BUILDSCRIPT
  3193.  
  3194.     ;;
  3195.  
  3196.     src2pkg)  # by amigo
  3197.  
  3198.       ### This code is a horribly simple wrapper to a great tool..
  3199.       ### Implement all src2pkg options here at some point..
  3200.       ##
  3201.       ### We should pass all options to src2pkg, and make it build a
  3202.       ### (petbuild|buildpet) buildscipt on successful compile
  3203.  
  3204.       # exit if src2pkg not installed
  3205.       [ "`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
  3206.  
  3207.       # exit if $1 not given or valid
  3208.       [ ! "$1" -o "$1" = "-" ]   && print_usage pkg-build && exit 1
  3209.  
  3210.       # show src2pkg help if user supplied any of the help options
  3211.       if [ "$1" = '-h'  -o "$1" = '-hh'  -o "$1" = '--help' -o "$1" = '--more-help' -o "$1" = '--list' ];then
  3212.         src2pkg $1 | sed -e "s/  src2pkg /  $SELF -pb /g"
  3213.         echo -e "\n   See 'src2pkg' by amigo"
  3214.         exit 0
  3215.       fi
  3216.  
  3217.       # get pkg extension.. not really needed here
  3218.       EX=`get_pkg_ext "$1"`
  3219.  
  3220.       # use amigos src2pkg
  3221.       src2pkg $1
  3222.  
  3223.       # if src2pkg exited without error, copy any pets it made to WORKDIR
  3224.       [ $? -eq 0 ] && mv /tmp/*.pet "$WORKDIR" 2>/dev/null
  3225.  
  3226.     ;;
  3227.  
  3228.     sbopkg)   # slackware peeps
  3229.  
  3230.       # exit if not installed
  3231.       [ "`which sbopkg`" = '' ] && error "Sbopkg not installed." && exit 3
  3232.  
  3233.       # exit if no valid options
  3234.       [ ! "$1" -o "$1" = "-" ]  && print_usage pkg-build && exit 1
  3235.  
  3236.       sbopkg -b "$1"
  3237.  
  3238.       # move any built pkgs to WORKDIR.. need a better way to do this
  3239.       if [ "`find /tmp/* -iname $1*.t*`" != '' ];then
  3240.         mv /tmp/*.tgz "$WORKDIR" 2>/dev/null
  3241.         mv /tmp/*.txz "$WORKDIR" 2>/dev/null
  3242.         mv /tmp/*.tar.xz "$WORKDIR" 2>/dev/null
  3243.         echo -e "DONE... The package should be in $WORKDIR."
  3244.       fi
  3245.  
  3246.     ;;
  3247.  
  3248.   *)
  3249.     # get pkg name only, no extension or path
  3250.     PKGNAME="$(basename "$1" .$EX)"
  3251.  
  3252.     echo "Cannot compile '$PKGNAME', no build system installed."
  3253.     echo
  3254.     echo "Please install one of the following:"
  3255.     echo " * petbuild by 01micko: http://murga-linux.com/puppy/viewtopic.php?t=96027"
  3256.     echo " * buildpet by Tman:    http://murga-linux.com/puppy/viewtopic.php?t=81056"
  3257.     echo " * src2pkg  by amigo:   http://distro.ibiblio.org/amigolinux/download/src2pkg/"
  3258.     echo " * sbopkg   by various: https://www.sbopkg.org/downloads.php"
  3259.     echo
  3260.     echo "Then set BUILDTOOL to either petbuild, buildpet, src2pkg or sbopkg "
  3261.     echo "in $PKGRC to enable building packages from source."
  3262.     echo
  3263.     echo "NOTE:"
  3264.     echo "01mickos petbuild should be installed to /usr/share/petbuild"
  3265.     echo "And Tmans buildpet should be installed to /usr/share/buildpet"
  3266.   ;;
  3267.   esac
  3268. }
  3269.  
  3270.  
  3271. pkg_repack(){                     # create package ($1) from its *.files list FUNCLIST
  3272.  
  3273.   # exit if no valid options
  3274.   [ ! "$1" -o "$1" = "-" ] && print_usage pkg-repack && exit 1
  3275.  
  3276.   local list=''
  3277.   local pkg_ext=''
  3278.   local PKGNAME=''
  3279.   local PKGNAME_ONLY=''
  3280.   local dir=''
  3281.   local build_number=''
  3282.  
  3283.   # get pkg extension
  3284.   pkg_ext=`get_pkg_ext "$1"`
  3285.  
  3286.   #get pkg name only, no extension or path
  3287.   PKGNAME="$(LANG=C basename "$1" .$pkg_ext)"
  3288.  
  3289.   # assume the file name from $1
  3290.   PKGFILE="${CURDIR}/${PKGNAME}.$pkg_ext"
  3291.  
  3292.   # don't rely on the given pkg name string, get the full name from repo if poss
  3293.   PKGNAME=`get_pkg_name "$PKGNAME"`
  3294.  
  3295.   # get pkg name without version
  3296.   PKGNAME_ONLY="`get_pkg_name_only "${PKGNAME}"`"
  3297.  
  3298.   # if the list is empty, pkg is not user installed or built in, cant show contents
  3299.   [ "`is_installed_pkg "$PKGNAME"`" = false ] && echo "$PKGNAME needs to be installed." && exit 1
  3300.  
  3301.   # if the package to built already exists, ask user to delete it
  3302.   [ -f "${PKGFILE}" ] && echo "$PKGNAME.$pkg_ext already exists in $CURDIR" && rm -f "${PKGFILE}" 2>/dev/null
  3303.  
  3304.   # if pkg exists, user didn't delete it
  3305.   [ -f "${PKGFILE}" ] && exit 0
  3306.  
  3307.   # get the build number, and increment by one
  3308.   build_number="`grep -m1 "^$PKGNAME|$PKGNAME_ONLY|" ${HOME}/.packages/user-installed-packages ${HOME}/.packages/woof-installed-packages ${HOME}/.packages/devx-only-installed-packages ${HOME}/.packages/Packages-* | cut -f4 -d'|'`"
  3309.   build_number=${build_number:-0}
  3310.   build_number="-$(($build_number + 1))"
  3311.  
  3312.   # process the file list, copy each file to our pkg folder
  3313.   pkg_contents "$PKGNAME_ONLY" | while read line
  3314.   do
  3315.     if [ -f "$line" ]; then
  3316.       linedir="$(dirname "${line}")"
  3317.       [ ! -d "${CURDIR}/$PKGNAME/${linedir}" ] && mkdir -p "${CURDIR}/$PKGNAME/${linedir}"
  3318.       cp -p -P "$line" "${CURDIR}/${PKGNAME}/${linedir}"
  3319.     fi
  3320.   done
  3321.  
  3322.   sync
  3323.  
  3324.   # make sure we populated a pkg folder, ready to package up
  3325.   [ ! -d "${CURDIR}/${PKGNAME}/" ] && error "No '$PKGNAME' directory in $CURDIR" && exit 5
  3326.  
  3327.   # pkg folder should be populated, now package it up and print final msg
  3328.   dir2pet "${CURDIR}/${PKGNAME}/" "$build_number"
  3329.   rm -rf "${CURDIR}/${PKGNAME}/" 2>/dev/null
  3330.   rm -rf "${CURDIR}/${PKGNAME}$build_number/" 2>/dev/null
  3331.   sync
  3332. }
  3333.  
  3334.  
  3335. pkg_unpack(){                     # extract/unpack $1 to current dir FUNCLIST
  3336.  
  3337.   # exit if not valid usage
  3338.   [ ! -f "$1" ] && print_usage unpack && exit 1
  3339.  
  3340.   local PKGNAME
  3341.   local PKGFILE
  3342.   local PKGEXT
  3343.   local comp
  3344.  
  3345.   # get pkg details
  3346.   PKGFILE="$1"
  3347.   PKGNAME=`get_pkg_name "$PKGFILE"`
  3348.   PKGEXT=`get_pkg_ext "$PKGFILE"`
  3349.  
  3350.   # exit if we dont have enough info
  3351.   [ "$PKGEXT" = '' ]  && error "Cant get package extension" && exit 1
  3352.   [ ! -f "$PKGFILE" ] && error "Cant find $1" && exit 6
  3353.   [ "$PKGNAME" = '' ] && error "Cant get PKGNAME" && exit 3
  3354.  
  3355.   # support overriding the output dir with $2
  3356.   if [ "$2" != "" ];then
  3357.     PKGNAME="$2"
  3358.   fi
  3359.  
  3360.   # determine compression utility
  3361.   case $PKGEXT in
  3362.  
  3363.     deb)
  3364.       mkdir "$PKGNAME" 2>/dev/null
  3365.       rm -rf "$PKGNAME"/* 2>/dev/null
  3366.       dpkg-deb -x "$PKGFILE" "$PKGNAME"    # extracts main pkg contents
  3367.       result=$?
  3368.       dpkg-deb -e "$PKGFILE" "$PKGNAME"/DEBIAN # extracts deb control files
  3369.       [ $result -eq 0 ] && echo -e "${green}Extracted${endcolour}: ${magenta}`basename $PKGFILE`${endcolour}" || error -e "${red}Error${endcolour}: Cannot extract $PKGFILE"
  3370.       return $result
  3371.       ;;
  3372.  
  3373.     rpm)
  3374.       mkdir "${PKGNAME}" 2>/dev/null
  3375.       rm -rf "$PKGNAME"/* 2>/dev/null
  3376.       # create dir called $PKGNAME, cd into it, extract the rpm in there
  3377.       cp "$PKGFILE" "$PKGNAME/"
  3378.       cd "$PKGNAME" 1>/dev/null
  3379.       # now extract the rpm contents into current dir
  3380.       exploderpm -x "$PKGFILE" &>/dev/null
  3381.       result=$?
  3382.       # clean up and leave
  3383.       rm -f *.rpm 2>/dev/null
  3384.       cd - 1>/dev/null
  3385.       # final msg
  3386.       [ $result -eq 0 ] && echo -e "${green}Success${endcolour}: File ${magenta}`basename $PKGFILE`${endcolour} extracted." || error "Cannot extract $PKGFILE"
  3387.       return $result
  3388.       ;;
  3389.  
  3390.     sfs)
  3391.  
  3392.       mkdir "/${PKGNAME}" 2>/dev/null
  3393.       rm -rf "$PKGNAME"/* 2>/dev/null
  3394.  
  3395.       # make mnt dir, mount sfs
  3396.       mkdir "/mnt/${PKGNAME}" 2>/dev/null
  3397.       mount -t squashfs "$PKGFILE" /mnt/"$PKGNAME" -o loop 1>/dev/null || { error "Could not mount $PKGFILE"; exit 3; }
  3398.       # copy sfs contents into ./$PKGNAME
  3399.       cp -a -Rf --remove-destination /mnt/"$PKGNAME" "$PKGNAME" || { error "Failed copying files from /mnt/$PKGNAME to ./$PKGNAME"; exit 4; }
  3400.       result=$?
  3401.       # clean up
  3402.       umount "/mnt/${PKGNAME}"
  3403.       rmdir "/mnt/${PKGNAME}"
  3404.       # final msg
  3405.       [ $result -eq 0 ] && echo -e "${green}Success${endcolour}: File ${magenta}`basename $PKGFILE`${endcolour} extracted." || { error "Cannot extract $PKGFILE"; exit 7; }
  3406.       return $result
  3407.       ;;
  3408.     pet)  file -b "$PKGFILE" | grep -i -q "^xz" && comp=xz || comp=gzip ;;
  3409.     tgz)  comp=gzip ;;
  3410.     gz)   comp=gzip ;;
  3411.     tbz)  comp=bzip2 ;;
  3412.     bz2)  comp=bzip2 ;;
  3413.     tlz)  comp=lzma ;;
  3414.     lzma) comp=lzma ;;
  3415.     txz)  comp=xz ;;
  3416.     xz)   comp=xz ;;
  3417.   esac
  3418.   sync
  3419.  
  3420.   # if pkg is extractable with tar, then unpack
  3421.   case $PKGEXT in
  3422.   pet|tgz|gz|tbz|bz2|tlz|lzma|txz|xz)
  3423.     ( 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" )
  3424.     ;;
  3425.   esac
  3426.  
  3427.   # return
  3428.   return ${result:-0}
  3429.  
  3430. }
  3431.  
  3432.  
  3433. pkg_combine(){                    # combine a pkg ($1) and deps into one single pkg FUNCLIST
  3434.  
  3435.   # exit if no valid options
  3436.   [ ! "$1" -o "$1" = "-" ] && print_usage pkg-combine && exit 1
  3437.  
  3438.   . ${PKGRC}
  3439.  
  3440.   local EX=''
  3441.   local PKGNAME=''
  3442.   local PKGNAME_ONLY=''
  3443.   local PKG_FILENAME=''
  3444.   local PKG_FILE=''
  3445.   local ALL_DEPS=''
  3446.   local PKG_DEPLIST=''
  3447.   local SUFFIX="${CP_SUFFIX}"
  3448.   local BUILD_DIR=$TMPDIR/build_pkg
  3449.   local SFS_FILE
  3450.   local please_wait=false
  3451.   local PREVDIR="$CURDIR"
  3452.   local ask_opt=$ASK
  3453.   local force_opt=$FORCE
  3454.  
  3455.   cd "$WORKDIR"
  3456.  
  3457.   # get pkg extension
  3458.   EX=`get_pkg_ext "$1"`
  3459.  
  3460.   # get reliable package names
  3461.   PKGNAME="$(basename "$1" .$EX)"
  3462.   PKGNAME=`get_pkg_name "$PKGNAME"`
  3463.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  3464.  
  3465.   PKG_FILE="${WORKDIR}/${PKGNAME}.$EX" #the file to install
  3466.  
  3467.   # we want to include all deps, except builtins, by default
  3468.   # we dont set HIDE_BUILTINS here, we leave that up to user
  3469.   # when user gives -F, HIDE_BUILTINS= false, and builtin
  3470.   # pkgs will be included in the package created
  3471.   FORCE=true
  3472.   HIDE_USER_PKGS=false
  3473.  
  3474.   # get full pkg filename (inc name-ver.ext).. try repos first
  3475.   PKG_FILENAME="`cut -f8 -d'|' ${HOME}/.packages/Packages-* | grep -m1 "^$PKGNAME"`"
  3476.   # then WORKDIR if needed (dont include the combined pkgs)
  3477.   [ "$PKG_FILENAME" = "" ] && PKG_FILENAME="`ls -1 "$WORKDIR" | grep -v "${SUFFIX}" | grep -v ".sfs\$" | grep -m1 "^$PKGNAME"`"
  3478.  
  3479.   # just in case its empty, revert back to the earlier PKGNAME value
  3480.   [ "$PKG_FILENAME" = "" ] && PKG_FILENAME="$PKGNAME"
  3481.  
  3482.   [ "$PKG_FILENAME" = "" ] && error "Cant find $1" && exit 1
  3483.  
  3484.   # update PKGFILE with new pkg filename (PKG)
  3485.   PKG_FILE="${WORKDIR}/${PKG_FILENAME}"
  3486.  
  3487.   # check if the desired file has already been built
  3488.   [ "$COMBINE2SFS" = true ] && final_ext=sfs || final_ext=pet
  3489.   [ -f "${WORKDIR}/${PKGNAME}-${SUFFIX}.$final_ext" ] && echo -e "File ${magenta}${WORKDIR}/${PKGNAME}-${SUFFIX}.${final_ext}${endcolour} already exists." && continue
  3490.  
  3491.   # get list of deps for PKGNAME, if no deps, exit
  3492.   PKG_DEPLIST="`FORCE=$force_opt list_deps $PKGNAME_ONLY`"
  3493.   [ "$PKG_DEPLIST" = "" ] && echo "No need to combine, $PKGNAME has no dependencies." && exit 1
  3494.  
  3495.   # if exact match of pkgname is found in a repo list
  3496.   if [ "`is_repo_pkg "$PKGNAME"`" = true ];then
  3497.  
  3498.     echo "Please wait.. Gathering dependency information."
  3499.  
  3500.     # get the deps of the pkg, but dont install
  3501.     FORCE=$force_opt find_deps "$PKGNAME"
  3502.     ALL_DEPS="`cat $TMPDIR/deps_installed $TMPDIR/deps_missing 2>/dev/null | sort | uniq`"
  3503.  
  3504.     # download the needed package
  3505.     if [ ! -f "$PKG_FILE" -o "$FORCE" = true ];then
  3506.       ASK=false pkg_download "$PKGNAME"
  3507.     fi
  3508.  
  3509.     # make work dirs
  3510.     mkdir -p "$BUILD_DIR/"
  3511.     mkdir -p "$BUILD_DIR/${PKGNAME}-${SUFFIX}"
  3512.  
  3513.     if [ -f "$PKG_FILE" ];then
  3514.       # copy the main pkg to the tmp dir
  3515.       # and remove the downloade file, we no longer need it
  3516.       cp "$PKG_FILE" "$BUILD_DIR/" 2>/dev/null && rm "$PKG_FILE"
  3517.     else
  3518.        error "Could not add the main package '$PKGNAME_ONLY'"
  3519.     fi
  3520.  
  3521.  
  3522.     # make a cleaned list to go over (sanity check)
  3523.     ALL_DEPS_LIST="`echo "$ALL_DEPS" | tr ',' '\n' | grep -v "^\$" | sort | uniq`"
  3524.  
  3525.     # go through each dep listed
  3526.     echo "$ALL_DEPS_LIST" | while read LINE
  3527.     do
  3528.  
  3529.       [ "$LINE" = "" -o "$LINE" = "-" -o "$LINE" = " " -o "$LINE" = "," -o "$LINE" = ", " ] && continue
  3530.  
  3531.       # only include builtins if HIDE_BUILTINS=true
  3532.       [ "`is_builtin_pkg "$LINE"`" = true -a "$HIDE_BUILTINS" = true ] && continue
  3533.  
  3534.       # only include devx pkgs if user gave the -f option
  3535.       [ "`is_devx_pkg "$LINE"`" = true -a "$force_opt" = false ] && continue
  3536.  
  3537.       # download the matching package(s)
  3538.       [ "`list_downloaded_pkgs "$LINE"`" = "" ] && ASK=false pkg_download "$LINE"
  3539.  
  3540.       # get the downloaded file
  3541.       PKGDEP="`find "$WORKDIR" -maxdepth 1 -type f -name "${LINE}*" | grep -v ".sfs\$" | grep -v "${SUFFIX}" | head -1`"
  3542.  
  3543.       # re-try download in other repos if needed
  3544.       if [ ! -f "$PKGDEP" ];then
  3545.         PKGREPO=''; PKGREPO="`LANG=C which_repo $LINE | cut -f2 -d' ' | head -1`"
  3546.         [ "$PKGREPO" != "" ] && ASK=false pkg_download "$LINE"
  3547.       fi
  3548.  
  3549.       #if downloaded
  3550.       if [ -f "$PKGDEP" -o "$FORCE" = true ];then
  3551.  
  3552.         # copy dep to the tmp dir, with the main pkg
  3553.         if [ ! -f "$BUILD_DIR/$PKGDEP" ];then
  3554.           cp "$PKGDEP" "$BUILD_DIR/$(basename $PKGDEP)" && rm "$PKGDEP"
  3555.         else
  3556.           error "Cannot copy $PKGDEP to $BUILD_DIR/$(basename $PKGDEP)"
  3557.         fi
  3558.  
  3559.       else # dep not found, may be missing, or in another repo
  3560.         echo -e "${yellow}Warning:${endcolour} $LINE not downloaded to $WORKDIR.. Cannot add $LINE.."
  3561.         continue
  3562.       fi
  3563.     done
  3564.  
  3565.     # we should now be ready to make our combined pkg
  3566.     cd "$BUILD_DIR"
  3567.  
  3568.     PARENTPKG=${PKGNAME}
  3569.  
  3570.     # for all pkgs in the tmp dir (nto including any 'combined' pkgs)
  3571.     TMP_PKGS="`find "$BUILD_DIR/" -maxdepth 1 -type f -name "*" | grep -v $SUFFIX | grep -v ^$ | grep -v ' ' | sort | uniq`"
  3572.  
  3573.     local count=1
  3574.  
  3575.     for i in $TMP_PKGS
  3576.     do
  3577.  
  3578.       # skip if not a valid pkg file
  3579.       [ ! "$i" -o "$i" = ' ' -o "$i" = '' -o ! -f "$i" ] && continue
  3580.  
  3581.       # dont include the combined pkgs
  3582.       [ "`echo "$i" | grep -m1 "${SUFFIX}"`" != "" ] && continue
  3583.  
  3584.       # get the extensions of each file .. they might be from different repos, so cant use $EX
  3585.       base="`basename "$i" 2>/dev/null`"
  3586.       FILE_EXT=`get_pkg_ext "$base"`
  3587.  
  3588.       [ ! "$base" -o "$base" = ' ' -o "$base" = '' ] && continue
  3589.  
  3590.       # get name without version for this pkg ($i)
  3591.       name_only=`get_pkg_name_only "$i"`
  3592.  
  3593.       [ ! "$name_only" -o "$name_only" = ' ' -o "$name_only" = '' ] && continue
  3594.  
  3595.       CONFIRM=y
  3596.       if [ "$ask_opt" = true ];then
  3597.         echo -n "Add package: $name_only  ($FILE_EXT)  (y/N):  "
  3598.         read -n 1 CONFIRM </dev/tty
  3599.         echo
  3600.       else
  3601.         echo "Adding package: $name_only  ($FILE_EXT)"
  3602.       fi
  3603.  
  3604.       # skip pkg if user wants to skip it
  3605.       if [ "$CONFIRM" != 'y' ];then
  3606.         rm "$i"
  3607.         continue
  3608.       fi
  3609.  
  3610.       # add each file
  3611.       case $FILE_EXT in
  3612.       pet)
  3613.  
  3614.         # convert and extract
  3615.         pkg_unpack "${i}" 1>/dev/null
  3616.         sync
  3617.  
  3618.         # copy extracted contents (only the stuff inside the extracted folders)
  3619.         if [ ! -d "${i/.pet/}/" ];then
  3620.           error "Dir '${i/.pet/}/' does not exist"
  3621.         fi
  3622.         cp -a --preserve=all -fr -L "${i/.pet/}/"* "${PARENTPKG}-${SUFFIX}/"
  3623.  
  3624.         if [ -f "${i/.pet/}/pinstall.sh" ];then
  3625.           mv "${i/.pet/}/pinstall.sh" "${PARENTPKG}-${SUFFIX}/pinstall_${count}.sh"
  3626.         fi
  3627.  
  3628.         # now remove the pet and extract folder
  3629.         rm "${i}"
  3630.         rm -rf "${i/.pet/}/"
  3631.         sync
  3632.         ;;
  3633.  
  3634.       deb)
  3635.  
  3636.         #cp "$i" "$BUILD_DIR/${PARENTPKG}-${SUFFIX}"
  3637.         pkg_unpack "$i" 2>$TMPDIR/$SELF-cp-errlog
  3638.         dpkg-deb -e "$i" "$BUILD_DIR/${PARENTPKG}-${SUFFIX}/DEBIAN"
  3639.  
  3640.         # .deb package names often have extra stuff at the end of the file name
  3641.         # that does not exist in the root folder name, inside the package.. so
  3642.         # we need to strip off that extra stuff to get the correct unpacked
  3643.         # package dir
  3644.         local pkg_dir="${i/.deb/}/"
  3645.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/_all/}"
  3646.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/_i386/}"
  3647.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/_amd64/}"
  3648.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/_x64/}"
  3649.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/~dfsg-[0-9]/}"
  3650.         [ ! -d "$pkg_dir" ] && pkg_dir="${pkg_dir/~dsfg-[0-9]/}"
  3651.  
  3652.         if [ ! -d "$pkg_dir" ];then
  3653.           needle="$(get_pkg_name "$i")"
  3654.           pkg_dir="$(find "${BUILD_DIR}" -type d -iname "$needle" | head -1)"
  3655.         fi
  3656.  
  3657.         if [ ! -d "$pkg_dir" ];then
  3658.           error "Dir '$pkg_dir' does not exist!"
  3659.         fi
  3660.  
  3661.         # copy extracted contents (only the stuff inside the extracted folders)
  3662.         cp -a --preserve=all -fr -L "${pkg_dir}/"* "${PARENTPKG}-${SUFFIX}/"
  3663.  
  3664.         if [ -f "${pkg_dir}DEBIAN/postinst" ];then
  3665.           mv "${pkg_dir}DEBIAN/postinst" "${PARENTPKG}-${SUFFIX}/DEBIAN/postinst_${count}"
  3666.         fi
  3667.  
  3668.         if [ -f "${pkg_dir}DEBIAN/preinst" ];then
  3669.           mv "${pkg_dir}DEBIAN/preinst" "${PARENTPKG}-${SUFFIX}/DEBIAN/preinst_${count}"
  3670.         fi
  3671.  
  3672.         # now remove the deb and extracted folder
  3673.         rm "$i"
  3674.         rm -rf "$pkg_dir"
  3675.         sync
  3676.         ;;
  3677.  
  3678.       *tgz|*txz|*tar.gz|*tar.xz|*xz|*gz)
  3679.  
  3680.         pkg_unpack "$i" 1>/dev/null
  3681.  
  3682.         # now copy $BUILD_DIR/$PKGNAME/* into our output (combined pkg) dir
  3683.         cp -a --preserve=all -fr -L "${name_only}/"* "${PARENTPKG}-${SUFFIX}/" || error "Dir '${name_only}/' does not exist!"
  3684.  
  3685.         if [ -f "${PARENTPKG}-${SUFFIX}/install/doinst.sh" ];then
  3686.           mv "${PARENTPKG}-${SUFFIX}/install/doinst.sh" "${PARENTPKG}-${SUFFIX}/install/doinst_${count}.sh"
  3687.         fi
  3688.  
  3689.         # remove the package and folder we just extracted
  3690.         rm "$i"
  3691.         rm -rf "${i/.*/}/"
  3692.         sync
  3693.         ;;
  3694.  
  3695.       rpm)
  3696.         pkg_unpack "$i" 1>/dev/null
  3697.  
  3698.         # now copy $BUILD_DIR/$PKGNAME/* into our output (combined pkg) dir
  3699.         cp -a --preserve=all -fr -L "${name_only}/"* "${PARENTPKG}-${SUFFIX}/"
  3700.  
  3701.         if [ -f "${PARENTPKG}-${SUFFIX}/install/doinst.sh" ];then
  3702.           mv "${PARENTPKG}-${SUFFIX}/install/doinst.sh" "${PARENTPKG}-${SUFFIX}/install/doinst_${count}.sh"
  3703.         fi
  3704.  
  3705.         # remove the package and folder we just extracted
  3706.         rm "$i"
  3707.         rm -rf "${i/.rpm/}/"
  3708.         ;;
  3709.  
  3710.       esac
  3711.       count=$(($count + 1))
  3712.     done
  3713.  
  3714.     # we have now unpacked the package, lets combine the pre/post install scripts
  3715.     # into one pinstall.sh script
  3716.  
  3717.     local pinstall_file="$BUILD_DIR/${PARENTPKG}-${SUFFIX}/pinstall.sh"
  3718.  
  3719.     # append this pinstall/postinst/doinst script to a new pinstall.sh
  3720.     echo '#!/bin/sh' > "$pinstall_file"
  3721.  
  3722.     # concat the post install scripts (for puppy, slackware, arch, debian/ubuntu, etc) into 'pinstall.sh'
  3723.     for i in ${PARENTPKG}-${SUFFIX}/pinstall_* ${PARENTPKG}-${SUFFIX}/install/doinst_* ${PARENTPKG}-${SUFFIX}/DEBIAN/postinst_*
  3724.     do
  3725.       [ -z ${PARENTPKG}-${SUFFIX}/${i} ] && continue
  3726.       cd ${PARENTPKG}-${SUFFIX}/ 2>/dev/null
  3727.  
  3728.       files=`find . -type f -iname "$(basename $i)"`
  3729.  
  3730.       for file in $files
  3731.       do
  3732.         if [ -f "$file" ];then
  3733.           echo "Appending ${file} to $pinstall_file"
  3734.           cat ${file} | grep -vE '^#!|^exit|exit 0|exit 1' >> "$pinstall_file"
  3735.           echo '' >> "$pinstall_file"
  3736.         fi
  3737.       done
  3738.       # we have combined the pre/post install stuff in $i into a new, combined file,
  3739.       # so remove the original
  3740.       rm -f ${PARENTPKG}-${SUFFIX}/${i}
  3741.     done
  3742.  
  3743.     chmod +x $BUILD_DIR/${PARENTPKG}-${SUFFIX}/pinstall.sh 2>/dev/null
  3744.  
  3745.     #xmessage "$(cat $BUILD_DIR/${PARENTPKG}-${SUFFIX}/pinstall.sh)"
  3746.  
  3747.     # remove any debian/ubuntu/slackware pre/post install stuff left in the package
  3748.     rm -rf $BUILD_DIR/${PARENTPKG}-${SUFFIX}/pinstall_*
  3749.     rm -rf $BUILD_DIR/${PARENTPKG}-${SUFFIX}/install
  3750.     rm -rf $BUILD_DIR/${PARENTPKG}-${SUFFIX}/DEBIAN
  3751.  
  3752.     # fix the symlinks to lib dirs - the linux-gnu-* dirs are symlinks in puppy,
  3753.     # so make sure we dont replace them with dirs (or programs won't load)
  3754.     for libdir in i386-linux-gnu i486-linux-gnu i586-linux-gnu i686-linux-gnu amd64-linux-gnu
  3755.     do
  3756.       if [ -d $BUILD_DIR/${PARENTPKG}-${SUFFIX}/usr/lib/${libdir}/ ];then
  3757.         mv -n -u $BUILD_DIR/${PARENTPKG}-${SUFFIX}/usr/lib/${libdir}/* $BUILD_DIR/${PARENTPKG}-${SUFFIX}/usr/lib/
  3758.         cd $BUILD_DIR/${PARENTPKG}-${SUFFIX}/usr/lib
  3759.         rmdir ${libdir}/
  3760.         ln -s ${libdir}/ .
  3761.         cd -
  3762.       fi
  3763.     done
  3764.  
  3765.  
  3766.     # now we are ready to build our combined pkg
  3767.     [ "`pwd`" != "$BUILD_DIR" ] && cd "$BUILD_DIR/"
  3768.  
  3769.  
  3770.     # if not building SFS, build a .pet
  3771.     if [ "$COMBINE2SFS" = false ];then #240613
  3772.       echo "Building PET package.. Please wait.."
  3773.  
  3774.       # build pet package... start with $removing PKGNAME-${SUFFIX}.pet.specs
  3775.       rm ${PKGNAME}-${SUFFIX}/*.specs 2>/dev/null
  3776.  
  3777.       # get compression type
  3778.       file -b "${PKGNAME}-${SUFFIX}" | grep -i -q "^xz" && TAREXT=xz || TAREXT=gz
  3779.  
  3780.       # tar up the folder
  3781.       tar -c -f ${PKGNAME}-${SUFFIX}.tar ${PKGNAME}-${SUFFIX} &>/dev/null
  3782.       sync
  3783.  
  3784.       case $TAREXT in
  3785.         xz)xz -z -9 -e ${PKGNAME}-${SUFFIX}.tar ;;
  3786.         gz)gzip --best ${PKGNAME}-${SUFFIX}.tar ;;
  3787.       esac
  3788.  
  3789.       TARBALL="${PKGNAME}-${SUFFIX}.tar.$TAREXT"
  3790.       FULLSIZE="`stat --format=%s ${TARBALL}`"
  3791.       MD5SUM="`md5sum $TARBALL | cut -f 1 -d ' '`"
  3792.       echo -n "$MD5SUM" >> $TARBALL
  3793.       sync
  3794.  
  3795.       mv -f $TARBALL ${PKGNAME}-${SUFFIX}.pet
  3796.       sync
  3797.  
  3798.       if [ $? -eq 1 ];then
  3799.         echo "Cannot create PET file ${PKGNAME}-${SUFFIX} from dir."
  3800.         error "Please check `pwd`"
  3801.       fi
  3802.  
  3803.       # move our new PET to $WORKDIR
  3804.       mv ${PKGNAME}-${SUFFIX}.pet $WORKDIR/${PKGNAME}-${SUFFIX}.pet
  3805.       # get the size
  3806.       s=`LANG=C du -m "$WORKDIR/${PKGNAME}-${SUFFIX}.pet" | sed "s/\s.*//"`
  3807.  
  3808.     else #240613  build a .sfs
  3809.       echo "Building SFS package.. Please wait.."
  3810.  
  3811.       dir2sfs "${PKGNAME}-${SUFFIX}" 1>/dev/null
  3812.  
  3813.       # get the full file name, it may (or not) have been appended with '-DISTRO_VERSION'
  3814.       SFS_FILE=`find . -maxdepth 1 -type f -name "*${PKGNAME}-${SUFFIX}*.sfs" | head -1`
  3815.  
  3816.       s=`LANG=C du -m "${SFS_FILE}" | sed "s/\s.*//"`
  3817.       MD5SUM=`md5sum "$SFS_FILE" | cut -f1 -d ' '`
  3818.       mv "$SFS_FILE" "$WORKDIR/"
  3819.     fi
  3820.  
  3821.     # done, go back to work dir from WORKDIR/build_pkg
  3822.     cd "$WORKDIR"
  3823.     rm -R  "$BUILD_DIR/" 2>/dev/null
  3824.  
  3825.     # create install command
  3826.     if [ "$COMBINE2SFS" = false ];then
  3827.       PKGEXT=pet
  3828.       PKGCMD="Install command: $SELF install ${WORKDIR}/${PKGNAME}-${SUFFIX}"
  3829.     else
  3830.       PKGEXT=sfs
  3831.       PKGCMD="Install command: sfs_loadr --cli -q \"${WORKDIR}/$(basename $SFS_FILE)\""
  3832.     fi
  3833.  
  3834.     #226013 updated output
  3835.     echo -e "Created package ${magenta}${PKGNAME}-${SUFFIX}.${PKGEXT}${endcolour} (${s}MB)"
  3836.     echo "The md5 checksum: $MD5SUM"
  3837.     echo "$PKGCMD"
  3838.  
  3839.   else # no exact PKGNAME match in repo
  3840.     not_found "${PKGNAME}"
  3841.     exit 1
  3842.   fi
  3843.  
  3844.   cd "$PREVDIR"
  3845. }
  3846.  
  3847.  
  3848. merge_pkg(){                      # merge the given comma-separated packages FUNCLIST
  3849.   # make sure at least 2 packages exist
  3850.  
  3851.   [ ! "$1" ] && print_usage merge && exit 1
  3852.   [ "$(echo "$1" |grep ',')" = "" ] && print_usage merge && exit 1
  3853.  
  3854.   cd "$WORKDIR" 1>/dev/null
  3855.  
  3856.   local PKG_LIST="$(echo "${1}" | tr ',' '\n' | sort -u | uniq)"
  3857.   local SUFFIX=''
  3858.  
  3859.   if [ "$2" = "--with-deps" ];then
  3860.     SUFFIX="-WITHDEPS"
  3861.     echo "Gathering dependencies to include.."
  3862.     echo
  3863.     # add deps of pkgs to list of pkgs to merge
  3864.     for package in $PKG_LIST
  3865.     do
  3866.       PKG_LIST="$PKG_LIST $(pkg le $package)"
  3867.     done
  3868.     PKG_LIST="$(echo "$PKG_LIST" | tr ' ' '\n' | sort -u | uniq)"
  3869.   fi
  3870.  
  3871.   echo "Merging the following packages:"
  3872.   echo
  3873.   echo "${PKG_LIST}" | sed 's/^/  /g'
  3874.   echo
  3875.  
  3876.   # get a new package name for the new, merged package
  3877.   local PKGNAME="`get_pkg_name "$(basename "${1//,*/}")" 2>/dev/null`"
  3878.   bash -c 'read -e -r -p "Enter a package name and hit ENTER: " -i "${PKGNAME}-MERGED${SUFFIX}" NEWPKGNAME; echo $NEWPKGNAME > /tmp/pkg/NEWPKGNAME'
  3879.   echo
  3880.   NEWPKGNAME="$(cat /tmp/pkg/NEWPKGNAME)"
  3881.  
  3882.   if [ "$NEWPKGNAME" = "" ];then
  3883.     echo "You must give a new package name!"
  3884.     return 1
  3885.   fi
  3886.  
  3887.   rm -rf "${NEWPKGNAME}/" &>/dev/null
  3888.   mkdir "${NEWPKGNAME}"
  3889.  
  3890.   # remove any old folders
  3891.   rm -rf ${WORKDIR}${NEWPKGNAME}/ &>/dev/null
  3892.  
  3893.   # for each package to merge
  3894.   for package in $PKG_LIST
  3895.   do
  3896.  
  3897.     # get the package details
  3898.     local PKGNAME="`get_pkg_name "$(basename "$package")" 2>/dev/null`"
  3899.     local PKGNAME_ONLY="`get_pkg_name_only "$PKGNAME" 2>/dev/null`"
  3900.     local PKGEXT="`get_pkg_ext "$PKGNAME"`"
  3901.     local PKGFILE=''
  3902.  
  3903.     [ "$PKGNAME" = "" ] && continue
  3904.     [ "$PKGNAME" = " " ] && continue
  3905.     [ "$PKGNAME" = "-" ] && continue
  3906.  
  3907.     # download it
  3908.     pkg_download "$PKGNAME"
  3909.  
  3910.     retval=$?
  3911.  
  3912.     if [ $retval -eq 1 ];then
  3913.       echo -e "${yellow}Warning${endcolour}: package '$PKGNAME' not downloaded."
  3914.       continue
  3915.     fi
  3916.  
  3917.     # get the downloaded file
  3918.     PKGFILE="$(find $WORKDIR -maxdepth 1 -type f -name "${PKGNAME}*${PKGEXT}")"
  3919.  
  3920.     [ ! -f "$PKGFILE" ] && \
  3921.       PKGFILE="$(find $WORKDIR -maxdepth 1 -type f -name "${PKGNAME_ONLY}_*${PKGEXT}")"
  3922.  
  3923.     [ ! -f "$PKGFILE" ] && \
  3924.       PKGFILE="$(find $WORKDIR -maxdepth 1 -type f -name "${PKGNAME_ONLY}-*${PKGEXT}")"
  3925.  
  3926.     if [ ! -f "$PKGFILE" ];then
  3927.       echo -e "${red}Error${endcolour}: could not find downloaded package '$PKGFILE'"
  3928.       return 1
  3929.     fi
  3930.  
  3931.     # unpack the downloaded file
  3932.     pkg_unpack "$PKGFILE"
  3933.     rm "${PKGFILE}"
  3934.  
  3935.     # move the unpacked files into our new package dir
  3936.     PKGDIR="$(find $WORKDIR -maxdepth 1 -type d -name "${PKGNAME}*" | grep -v ${NEWPKGNAME})"
  3937.  
  3938.     if [ ! -d "$PKGDIR" ];then
  3939.       echo -e "${red}Error${endcolour}: could not find extracted package dir '$PKGDIR'"
  3940.       return 1
  3941.     fi
  3942.  
  3943.     cp -R "$PKGDIR"/* "$NEWPKGNAME"
  3944.     rm -rf "${PKGDIR}/"
  3945.   done
  3946.  
  3947.   if [ ! -d "${WORKDIR}/$NEWPKGNAME" ];then
  3948.     echo -e "${red}Error${endcolour}: Dir ${lightblue}${NEWPKGNAME}${endcolour} NOT created!"
  3949.     return 1
  3950.   fi
  3951.  
  3952.   echo
  3953.   echo "Package contents:"
  3954.   find "${NEWPKGNAME}/"
  3955.   echo '--------------------------------'
  3956.   echo
  3957.  
  3958.   # create the merged PET package
  3959.   dir2pet "${NEWPKGNAME}/" || error "Could not create $NEWPKGNAME} PET file"
  3960.  
  3961.   rm -rf "./${NEWPKGNAME}/" &>/dev/null
  3962.   rm -rf "${WORKDIR}/${NEWPKGNAME}/" &>/dev/null
  3963.  
  3964.   # get the filename of the new PET package
  3965.   NEWPKGFILENAME="$(find . -maxdepth 1 -type f -name "${NEWPKGNAME}*.pet")"
  3966.  
  3967.   # output messages
  3968.   if [ ! -f "$NEWPKGFILENAME" ];then
  3969.     echo
  3970.     echo -e "${red}Error${endcolour}: Package ${yellow}${NEWPKGFILENAME}${endcolour} NOT created!"
  3971.     return 1
  3972.   fi
  3973.  
  3974.   return 0
  3975. }
  3976.  
  3977.  
  3978. split_pkg(){                      # split package ($1) into dev, doc, nls packages FUNCLIST
  3979.  
  3980.   # make sure the package exists
  3981.   [ ! "$1" -o ! -f "$1" ] && print_usage split && exit 1
  3982.  
  3983.   local PKGNAME="`get_pkg_name "$(basename "$1")" 2>/dev/null`"
  3984.   local PKGNAME_ONLY="`get_pkg_name_only "$PKGNAME" 2>/dev/null`"
  3985.   local PKGEXT="pet"
  3986.  
  3987.   # get package extension
  3988.   PKGEXT="`get_pkg_ext "$1"`"
  3989.  
  3990.   pkg_unpack "$1" &>/dev/null
  3991.  
  3992.   # get the package directory we just extracted
  3993.   local PKG_PATH="$(find $(dirname "$PKGNAME") -maxdepth 1 -type d -iname "$PKGNAME" | head -1)"
  3994.  
  3995.   # get the full path to the package dir (dir with contents of pkg to be split)
  3996.   PKG_PATH="$(realpath "$PKG_PATH")"
  3997.  
  3998.   # make sure we have the right package dir
  3999.   [ ! -d "$PKG_PATH" ] && PKG_PATH="$(realpath "$PKGNAME")"
  4000.  
  4001.   # get the base name of the package directory
  4002.   local PKG_DIR_NAME=$(basename "$PKG_PATH")
  4003.  
  4004.   # get the package name
  4005.   local PKG_NAME="$PKGNAME_ONLY"
  4006.  
  4007.   # get the parent directory of the package
  4008.   local PARENT_DIR="$(dirname "$PKG_PATH")"
  4009.  
  4010.   # set the correct package naming style
  4011.   local dev_suffix='_DEV'
  4012.   local doc_suffix='_DOC'
  4013.   local nls_suffix='_NLS'
  4014.   if [ "${PKGEXT:-pet}" = "deb" ];then
  4015.     dev_suffix='-dev'
  4016.     doc_suffix='-doc'
  4017.     nls_suffix='-nls'
  4018.   fi
  4019.  
  4020.   # get the sub-package names
  4021.   local EXE_PKG="$PKGNAME"
  4022.   local DEV_PKG="$(echo "$PKG_DIR_NAME" | sed -e "s/^${PKGNAME_ONLY}/${PKGNAME_ONLY}${dev_suffix}/")"
  4023.   local DOC_PKG="$(echo "$PKG_DIR_NAME" | sed -e "s/^${PKGNAME_ONLY}/${PKGNAME_ONLY}${doc_suffix}/")"
  4024.   local NLS_PKG="$(echo "$PKG_DIR_NAME" | sed -e "s/^${PKGNAME_ONLY}/${PKGNAME_ONLY}${nls_suffix}/")"
  4025.  
  4026.   # remove the target package dirs if they already exist
  4027.   [ -d "$DEV_PKG" ] && rm -rf "$DEV_PKG"
  4028.   [ -d "$DOC_PKG" ] && rm -rf "$DOC_PKG"
  4029.   [ -d "$NLS_PKG" ] && rm -rf "$NLS_PKG"
  4030.   # now create them fresh and empty
  4031.   mkdir -p "$DEV_PKG"
  4032.   mkdir -p "$DOC_PKG"
  4033.   mkdir -p "$NLS_PKG"
  4034.  
  4035.   # make sure the package directory begins with the given package name
  4036.   case $(basename "$PKG_PATH") in
  4037.     $PKG_NAME*)
  4038.         ;;
  4039.     *)
  4040.         echo "Error: $(basename "$PKG_PATH") must match $PKG_NAME"
  4041.         print_usage split
  4042.         exit 1
  4043.         ;;
  4044.   esac
  4045.  
  4046.   cd "$PKG_PATH" &>/dev/null || { echo "Not found: $PKG_PATH"; return 1; }
  4047.  
  4048.   for i in $(find -mindepth 1)
  4049.   do
  4050.     # if the file no longer exists, skip this iteration
  4051.     [ ! -e "$i" ] && continue
  4052.  
  4053.     local FILE_NAME=$(basename "$i")
  4054.  
  4055.     case "$FILE_NAME" in
  4056.         *.la|*.a|*.o|*.prl|pkgconfig|include|*.m4|*.h|*.c|*.cpp)
  4057.             PKGNAME="$DEV_PKG"
  4058.             ;;
  4059.         gdb)
  4060.             [ -d "$i" ] && PKGNAME="$DEV_PKG"
  4061.             ;;
  4062.         dir)
  4063.             [ -f "$i" ] && PKGNAME="$DOC_PKG"
  4064.             ;;
  4065.         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)
  4066.             PKGNAME="$DOC_PKG"
  4067.             ;;
  4068.         help)
  4069.             [ -d "$i" ] && PKGNAME="$DOC_PKG"
  4070.             ;;
  4071.         man|info) # if it's a directory named "man" or "info", move to doc
  4072.             [ -d "$i" ] && PKGNAME="$DOC_PKG"
  4073.              ;;
  4074.         locale|locales|lang|strings) # if it's a directory named "locale", move to nls
  4075.             [ -d "$i" ] && PKGNAME="$NLS_PKG"
  4076.             ;;
  4077.         i18n|nls)
  4078.             PKGNAME="$NLS_PKG"
  4079.             ;;
  4080.         system.profile-*|*.strings|normal.awt-*) # AbiWord stores its locale information in those files
  4081.             [ "$PKGNAME_ONLY" = "abiword" ] && PKGNAME="$NLS_PKG"
  4082.             ;;
  4083.         *)
  4084.             PKGNAME="$EXE_PKG"
  4085.             ;;
  4086.     esac
  4087.  
  4088.     # verbosity, output the redirection for each redirected file
  4089.     case "$PKGNAME" in
  4090.         $DEV_PKG|$DOC_PKG|$NLS_PKG)
  4091.             #local SUFFIX="$(echo $SUFFIX | tr [:lower:] [:upper:])"
  4092.  
  4093.             #echo "$FILE_NAME -> $PKG"
  4094.  
  4095.             # detect the sub_directory inside the package
  4096.             local SUB_DIR="${i%/$FILE_NAME}"
  4097.             SUB_DIR="${SUB_DIR:2}"
  4098.  
  4099.             # create the directory under the sub-package directory
  4100.             mkdir -p "$PARENT_DIR/$PKGNAME/$SUB_DIR"
  4101.  
  4102.             # move the file to the sub-package
  4103.             mv "$i" "$PARENT_DIR/$PKGNAME/$SUB_DIR"
  4104.  
  4105.             # get rid of empty directories in the EXE package
  4106.             rmdir "$SUB_DIR" &>/dev/null
  4107.             ;;
  4108.     esac
  4109.  
  4110.   done
  4111.  
  4112.   # if the EXE package is empty, remove it
  4113.   is_empty="$(find "$PKG_PATH" -maxdepth 2 -type f 2>/dev/null)"
  4114.   [ -z "$is_empty" ] && rm -rf "$PKG_PATH"
  4115.  
  4116.   # go back to where we started (where the main package lives)
  4117.   [ -d "$CURDIR" ] && cd "$CURDIR" &>/dev/null
  4118.  
  4119.   # build each package from the package dir
  4120.   for dir in "$EXE_PKG" "$DEV_PKG" "$DOC_PKG" "$NLS_PKG"
  4121.   do
  4122.     if [ -d "$dir" ];then
  4123.       dir2${PKGEXT//./} "$dir" && rm -rf "$dir"
  4124.     fi
  4125.   done
  4126.  
  4127.   # get a list of the new packages we created
  4128.   new_pkgs="$(find . -maxdepth 1 -type f -name "${PKGNAME}*.$PKGEXT"
  4129.  find . -maxdepth 1 -type f -name "${DEV_PKG}*.$PKGEXT"
  4130.  find . -maxdepth 1 -type f -name "${DOC_PKG}*.$PKGEXT"
  4131.  find . -maxdepth 1 -type f -name "${NLS_PKG}*.$PKGEXT")"
  4132.  
  4133.   if [ "$new_pkgs" != "" ];then
  4134.     echo
  4135.     echo -e "${green}Success${endcolour}: Package split into these new files:"
  4136.     echo "$new_pkgs" | sed 's/^.\// /'
  4137.   fi
  4138.  
  4139.   # cleanup
  4140.   [ -d "$DEV_PKG" ] && rm -rf "$DEV_PKG"
  4141.   [ -d "$DOC_PKG" ] && rm -rf "$DOC_PKG"
  4142.   [ -d "$NLS_PKG" ] && rm -rf "$NLS_PKG"
  4143.  
  4144.   return 0
  4145. }
  4146.  
  4147.  
  4148.  
  4149. # main pkg funcs
  4150.  
  4151. pkg_download(){                   # download a pkg ($1) to WORKDIR FUNCLIST
  4152.  
  4153.   # exit if no valid options
  4154.   [ ! "$1" -o "$1" = "-" ] && print_usage download && exit 1
  4155.  
  4156.   . ${PKGRC}
  4157.  
  4158.   local pkg_ext=''        # extension of pkg, empty if not a supported pkg ext
  4159.   local PKGNAME=''        # the pkgname with version
  4160.   local ORIG_PKGNAME=''   # keeps the original pkg name ($1)
  4161.   local PKG_FILENAME=''   # package file name (field 8 of repo, with extension)
  4162.   local PKGFILE=''        # full path to package file
  4163.   local NET=''            # 1 or 0 if net connection available
  4164.   local curr_repo=''      # name of current repo in the loop
  4165.   local curr_repo_ext=''  # pkg ext for the current repo in the loop
  4166.   local prev_repo_url=''  # holder for the prev checked url we know is working
  4167.   local curr_repo_url=''  # mirror that is used to download the pkg
  4168.   local curr_repo_url1='' # mirror1 for the current repo in the loop
  4169.   local curr_repo_url2='' # mirror2 for the current repo in the loop
  4170.   local curr_repo_url3='' # mirror3 for the current repo in the loop
  4171.   local curr_repo_url4='' # mirror4 for the current repo in the loop
  4172.   local pkg_in_repo=''    # true if pkg found in ANY repo, else false
  4173.  
  4174.   # get pkg extension
  4175.   pkg_ext=`get_pkg_ext "$1"`
  4176.  
  4177.   # get pkg name with version (if given!), no extension or path
  4178.   PKGNAME="$(basename "$1" .$pkg_ext)"
  4179.  
  4180.   # get full package name from given pkg name string .. vlc -> vlc-1.2.3-blah
  4181.   PKGNAME=`get_pkg_name "$PKGNAME"`
  4182.  
  4183.   # the file to save to.. not reliable till we checked the repos
  4184.   PKGFILE="${WORKDIR}/${PKGNAME}.$pkg_ext"
  4185.   PKG_FNAME_GUESS="$PKGFILE"
  4186.   # set download options
  4187.   [ "$FORCE" = true  ] && CONTINUE='' || CONTINUE='-c'
  4188.   [ "$ASK"  != true  ] && QTAG=''
  4189.  
  4190.   #if pkg not yet downloaded, or we are downloading all, or we are forcing downloads
  4191.   if [ ! -f "$PKGFILE" -o "$NO_INSTALL" = true -o "$FORCE" = true ];then
  4192.  
  4193.     # mark this pkg as not yet downloaded
  4194.     DONE=false
  4195.  
  4196.     # exit if no internet connection
  4197.     #NET="`check_net`"; [ $NET -eq 1 ] && echo "You need an internet connection" && exit 2
  4198.  
  4199.     # for each repo, starting with current repo
  4200.     repo_file_list | while read repo_file
  4201.     do
  4202.  
  4203.       # the file with our repo URL info
  4204.       sources_file="${HOME}/.pkg/sources"
  4205.  
  4206.       # check if $repo_file contains the given pkg
  4207.       pkg_in_this_repo="`LANG=C cut -f1,2,7,8 -d'|' ${HOME}/.packages/$repo_file 2>/dev/null | sed -e "s/^/|/" -e "s/$/|/" | grep -m1 "|${PKGNAME}|"`"
  4208.       [ "$pkg_in_this_repo" = '' ] && pkg_in_this_repo="`LANG=C cut -f1,2,7,8 -d'|' ${HOME}/.packages/$repo_file 2>/dev/null | sed -e "s/^/|/" -e "s/$/|/" | grep -m1 "|${PKGNAME}"`"
  4209.  
  4210.       # if package file found in current repo file
  4211.       if [ "$pkg_in_this_repo" != "" ];then #if true, its an exact match
  4212.  
  4213.         # get name of current repo in loop
  4214.         prev_repo="$curr_repo"
  4215.         curr_repo="`LANG=C grep $repo_file $sources_file 2>/dev/null | cut -f1 -d'|'`"
  4216.  
  4217.         # get ext of cur repo, it might be different
  4218.         curr_repo_ext="`LANG=C grep $repo_file $sources_file 2>/dev/null| cut -f2 -d'|'`"
  4219.  
  4220.         # keep a copy of the PKGNAME we got from `get_pkg_name $1`
  4221.         ORIG_PKGNAME=$PKGNAME
  4222.  
  4223.         # get proper PKGNAME (inc name-ver) from the repo
  4224.         PKGNAME="`echo "$pkg_in_this_repo" | cut -f3 -d'|'`"
  4225.         # just in case it didn't work, revert back to the old PKGNAME value
  4226.         [ "$PKGNAME" = "" ] && PKGNAME="$ORIG_PKGNAME"
  4227.  
  4228.         # get full pkg FILENAME (inc name-ver.ext) from the repo
  4229.         PKG_FILENAME="`echo "$pkg_in_this_repo" | cut -f5 -d'|'`"
  4230.  
  4231.         # just in case its empty, revert back to the earlier PKGNAME value
  4232.         [ "$PKG_FILENAME" = "" ] && PKG_FILENAME="$PKGNAME"
  4233.  
  4234.         # update the filename to check/download
  4235.         PKGFILE="${WORKDIR}/${PKG_FILENAME}"
  4236.  
  4237.         # skip if downloaded already, print msg
  4238.         [ -f "$PKGFILE" -a "$FORCE" = false ] && DONE=true && echo -e "Already downloaded ${magenta}${PKG_FILENAME}${endcolour}" && continue
  4239.  
  4240.         # update the package name, based on PKG_FILENAME
  4241.         PKGNAME="$(basename "$PKG_FILENAME" .$curr_repo_ext)"
  4242.  
  4243.         # update generic name
  4244.         PKGNAME_ONLY="`get_pkg_name_only "$PKGNAME"`"
  4245.  
  4246.         if [ "${PKG_FILENAME}" = '' ];then
  4247.           error "Cant find pkg file name, needed to download"
  4248.           return 1
  4249.         fi
  4250.  
  4251.         # skip pings and download if already downloaded.. unless forcing download
  4252.         if [ ! -f "$PKGFILE" -o "$FORCE" = true ];then
  4253.  
  4254.           # ask user to download
  4255.           echo -en "Download ${magenta}${PKGNAME_ONLY}${endcolour} from ${lightblue}${curr_repo}${endcolour} repo$QTAG:  "
  4256.           [ "$ASK" = true ] && read -n 1 CONFIRM </dev/tty || CONFIRM=y
  4257.           [ "$CONFIRM" != 'y' ] && echo
  4258.  
  4259.           # if user answered yes, we will now download the pkgs
  4260.           if [ "$CONFIRM" = "y" ];then #25073
  4261.  
  4262.             # only ask once
  4263.             ASK=false
  4264.  
  4265.             echo # start a new line
  4266.  
  4267.             # get the subdir (from repo line) that the package lives in
  4268.             sub_dir="`echo "$pkg_in_this_repo" | cut -f4 -d'|' | sed 's/^\.//'`"
  4269.  
  4270.             if [ "$sub_dir" = "." ];then
  4271.               sub_dir=''
  4272.             fi
  4273.  
  4274.             # pre-woof get subdir
  4275.             if [ "$sub_dir" != "" ] && [ -f "$HOME/.packages/${repo_file}_subdirs" ];then
  4276.               sub_dir="`grep -m1 "^$PKGNAME|" "$HOME/.packages/${repo_file}_subdirs"  2>/dev/null | cut -f7 -d'|'`"
  4277.             fi
  4278.  
  4279.             # get repo mirrors (only url1 is required .. we also add a final /)
  4280.             curr_repo_url1="`LANG=C grep $repo_file $sources_file 2>/dev/null| cut -f4 -d'|'`/"
  4281.             curr_repo_url2="`LANG=C grep $repo_file $sources_file 2>/dev/null| cut -f5 -d'|'`/"
  4282.             curr_repo_url3="`LANG=C grep $repo_file $sources_file 2>/dev/null| cut -f6 -d'|'`/"
  4283.             curr_repo_url4="`LANG=C grep $repo_file $sources_file 2>/dev/null| cut -f7 -d'|'`/"
  4284.  
  4285.             # make a space separated list of all our repo URLs
  4286.             curr_repo_url_list="$curr_repo_url1 $curr_repo_url2 $curr_repo_url3 $curr_repo_url4"
  4287.  
  4288.             # clean up the list, remove any double slashes at the end of each URL
  4289.             curr_repo_url_list="`echo "$curr_repo_url_list" | sed -e "s|// |/ |g" -e "s|//\$|/\$|g"`"
  4290.  
  4291.             # update the ext to that of current repo
  4292.             pkg_ext="$curr_repo_ext"
  4293.  
  4294.             # get the best repo mirror
  4295.             #if [ "$prev_repo" != "$current_repo"  -a -f $TMPDIR/curr_repo_url ] || [ ! -f $TMPDIR/curr_repo_url ];then
  4296.               #echo "Checking '$curr_repo' repo mirrors..."
  4297.               for URL in $curr_repo_url_list
  4298.               do
  4299.                 [ -z "$URL" ] && continue
  4300.                 [ "$URL" = '/' ] && continue
  4301.                 [ "$URL" = '' ] && continue
  4302.                 # add some system values into the repo URL (arch, distro version, etc.. from DISTRO_SPECS)
  4303.                 URL="`echo "$URL"| sed -e 's@${DBIN_ARCH}@'$DBIN_ARCH'@g'`"
  4304.                 URL="`echo "$URL"| sed -e 's@${DISTRO_COMPAT_VERSION}@'$DISTRO_COMPAT_VERSION'@g'`"
  4305.                 URL="`echo "$URL"| sed -e 's@${DDB_COMP}@'$DDB_COMP'@g'`"
  4306.                 URL="`echo "$URL"| sed -e 's@${SLACKWARE}@'$SLACKWARE'@g'`"
  4307.                 URL="`echo "$URL"| sed -e 's@${SLACKARCH}@'$SLACKARCH'@g'`"
  4308.                 URL="`echo "$URL"| sed -e 's@${lxDISTRO_COMPAT_VERSION}@'$lxDISTRO_COMPAT_VERSION'@g'`"
  4309.                 #ping -W2 -c1 -q $(echo  "${URL}" | awk -F/ '{print $3}') &>/dev/null
  4310.                 wget --quiet --timeout=2 --no-parent --spider "${URL}" &>/dev/null && \
  4311.                 {
  4312.                   # set the current URL
  4313.                   curr_repo_url="$URL"
  4314.                   if [ ! -z "$curr_repo_url" ];then
  4315.                     echo "$curr_repo_url" > $TMPDIR/curr_repo_url
  4316.                     break
  4317.                   fi
  4318.                 }
  4319.               done
  4320.             #else
  4321.             # curr_repo_url="`cat $TMPDIR/curr_repo_url`"
  4322.             #fi
  4323.  
  4324.             # exit if URL is not found or empty
  4325.             if [ -z "$curr_repo_url" ]; then
  4326.               error "Package URL not found"
  4327.               return 8
  4328.             fi
  4329.  
  4330.             # lets build our DOWNLOAD_URL: set it to the working repo mirror we found above
  4331.             DOWNLOAD_URL="${curr_repo_url}"
  4332.  
  4333.             # now add the subdir to DOWNLOAD_URL .. sub_dir may be empty, but we add a trailing '/' anyway
  4334.             DOWNLOAD_URL="${DOWNLOAD_URL}${sub_dir}/"
  4335.  
  4336.             # add some system values into the repo URL (arch, distro version, etc.. from DISTRO_SPECS)
  4337.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e 's@${DBIN_ARCH}@'$DBIN_ARCH'@g'`"
  4338.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e 's@${DISTRO_COMPAT_VERSION}@'$DISTRO_COMPAT_VERSION'@g'`"
  4339.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e 's@${DDB_COMP}@'$DDB_COMP'@g'`"
  4340.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e 's@${SLACKWARE}@'$SLACKWARE'@g'`"
  4341.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e 's@${SLACKARCH}@'$SLACKARCH'@g'`"
  4342.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e 's@${lxDISTRO_COMPAT_VERSION}@'$lxDISTRO_COMPAT_VERSION'@g'`"
  4343.  
  4344.             # add our package to the URL
  4345.             DOWNLOAD_URL="${DOWNLOAD_URL}${PKG_FILENAME}"
  4346.  
  4347.             # 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)
  4348.             DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e "s@//$PKG_FILENAME@/$PKG_FILENAME@g"`"
  4349.  
  4350.             # if sub_dir not empty, lets clean that bit too
  4351.             [ "$sub_dir" != '' ] && DOWNLOAD_URL="`echo "$DOWNLOAD_URL"| sed -e "s@//${sub_dir}@/${sub_dir}@g" -e "s@${sub_dir}//@${sub_dir}/@g"`"
  4352.  
  4353.             # exit if URL is not found (if we get a 404 back)
  4354.             if [ -z "$DOWNLOAD_URL" ]; then
  4355.               error "Package URL not found   $DOWNLOAD_URL"
  4356.               return 8
  4357.             else
  4358.               wget -S --spider "$DOWNLOAD_URL" &>/dev/null || \
  4359.               {
  4360.                 error "Package URL not found   $DOWNLOAD_URL"
  4361.                 return 8                 
  4362.               }
  4363.             fi
  4364.  
  4365.             # we may be using multiple URLs, so each time we change URL,
  4366.             #  remember the new one
  4367.             if [ "$OLDURL" != "$DOWNLOAD_URL" ];then
  4368.               OLDURL="$DOWNLOAD_URL";
  4369.               #echo -e "URL: ${lightblue}${DOWNLOAD_URL}${endcolour}";
  4370.             fi
  4371.  
  4372.             # if --force, remove the package if it already exists
  4373.             [ "$FORCE" = true ] && rm -f "${WORKDIR}/${PKG_FILENAME}" &>/dev/null
  4374.  
  4375.             # if file not downloaded, or forcing downloads
  4376.             if [ ! -f "${WORKDIR}/${PKG_FILENAME}" -o "$FORCE" = true ];then
  4377.  
  4378.               # BEGIN DOWNLOAD file here..
  4379.  
  4380.               # print DOWNLOADING msg
  4381.               echo -en "Downloading ${magenta}${PKG_FILENAME}${endcolour}. Please wait:     "
  4382.  
  4383.               # if called as 'gpkg', give a pop GUI (uses Xdialog) showing download progress..
  4384.               # can be used by X apps to easily start (and show!) downloads
  4385.               if [ "$SELF" = "gpkg" ];then
  4386.  
  4387.                 download_progress "$DOWNLOAD_URL" "${WORKDIR}/${PKG_FILENAME}" 2>/dev/null
  4388.  
  4389.               else # if called as 'pkg', dont use Xdialog, output to terminal
  4390.  
  4391.                 if [ "$QUIET" = true ];then
  4392.                   echo
  4393.                   echo -n "Downloading now..."
  4394.                   LANG=C wget \
  4395.                     --no-check-certificate \
  4396.                     --progress=dot -O "${WORKDIR}/${PKG_FILENAME}" \
  4397.                     -4 $CONTINUE "$DOWNLOAD_URL" &>/dev/null
  4398.                 else
  4399.                   # START DOWNLOAD, show percentage as we go
  4400.                   LANG=C wget \
  4401.                     --no-check-certificate \
  4402.                     --progress=dot -O "${WORKDIR}/${PKG_FILENAME}" \
  4403.                     -4 $CONTINUE "$DOWNLOAD_URL" 2>&1 \
  4404.                     | grep --line-buffered "%" \
  4405.                     | sed -u -e "s#\.##g" \
  4406.                     | awk '{printf("\b\b\b\b%4s", $2)}' #220613
  4407.                 fi
  4408.  
  4409.               fi
  4410.  
  4411.             fi # end if WORKDIR/PKG not a file or FORCE=true
  4412.  
  4413.             # clean up output
  4414.             [ "$QUIET" != true ] && echo -ne "\b\b\b\b"
  4415.             echo
  4416.             # if file downloaded ok
  4417.             set -x
  4418.             if [ -f "${WORKDIR}/${PKG_FILENAME}" ];then
  4419.               ( cd ~/pkg #TODO: use non explicit paths (e.g. CURDIR or WORKDIR)
  4420.                ln -s "$(basename "${PKG_FILENAME}")" "$(basename "$PKG_FNAME_GUESS")"
  4421.                )
  4422.               echo -e "${green}Downloaded:${endcolour} ${WORKDIR}/${PKG_FILENAME}"
  4423.               DONE=true
  4424.               break
  4425.  
  4426.             else # file NOT downloaded ok
  4427.  
  4428.               error "Failed to download '${WORKDIR}/${PKG_FILENAME}'."
  4429.               echo "Check '$DOWNLOAD_URL'"
  4430.  
  4431.               # remove the page we tried to get pkg from (if exists)
  4432.               rm "${WORKDIR}/index.html" &>/dev/null
  4433.               DONE=true
  4434.               exit 6
  4435.             fi
  4436.             set +x
  4437.           fi # end if CONFIRM=y
  4438.  
  4439.           # user chose not to do anything
  4440.           DONE=true
  4441.           break
  4442.  
  4443.         else # Already downloaded, skip to next iteration
  4444.  
  4445.           #echo "Package $1 already downloaded"
  4446.           DONE=true
  4447.           break
  4448.         fi
  4449.  
  4450.       else # no repo match, nothing to download
  4451.  
  4452.         # go to next repo, try to ge tthe pkg there
  4453.         DONE=false
  4454.  
  4455.       fi # end if repo match was found
  4456.  
  4457.       # if download not successful, keep going to next repo
  4458.       [ "$DONE" = true ] && continue
  4459.  
  4460.     done #done while read list of repo files
  4461.  
  4462.   fi
  4463. }
  4464.  
  4465.  
  4466. pkg_install(){                    # install downloaded package ($1) FUNCLIST
  4467.  
  4468.   . ${PKGRC}
  4469.  
  4470.   # exit if no valid option
  4471.   [ ! "$1" -o "$1" = "-" ] && print_usage install && exit 1
  4472.  
  4473.   # skip if not installing pkgs
  4474.   [ "$NO_INSTALL" = true ] && continue
  4475.  
  4476.   local pkg_ext
  4477.   local PKGNAME
  4478.   local PKGNAME_ONLY
  4479.   local PKGFILE
  4480.   local PREVDIR="$CURDIR"
  4481.   local petspecs
  4482.  
  4483.   # get pkg extension
  4484.   pkg_ext=`get_pkg_ext "$1"`
  4485.   # get pkg name only, no extension or path
  4486.   PKGNAME="$(basename "$1" .$pkg_ext)"
  4487.   # exit if no valid option
  4488.   [ "$PKGNAME" = '' ] && print_usage install && exit 1
  4489.   # get name without version
  4490.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  4491.  
  4492.   [ "`is_installed_pkg "$PKGNAME"`" = true -a "$FORCE" = false ] && echo "Already installed: $PKGNAME" && return 1
  4493.   [ "`is_blacklisted_pkg "$PKGNAME_ONLY"`" = true ] && echo "Blacklisted package: $PKGNAME" && return 1
  4494.  
  4495.   if [ -f "$1" ];then
  4496.     PKGFILE="$1"
  4497.     PKGNAME=`basename "$1" .$pkg_ext`
  4498.   else
  4499.     # get the real file name (inc path) from the given PKGFILE and pkg_ext (which may be empty)
  4500.     PKGFILE=`find "$CURDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*${pkg_ext}" | head -1`
  4501.     [ ! -f "$PKGFILE" ] && PKGFILE=`find "$CURDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*${EX}" | grep -m1 $EX`
  4502.     [ -f "$PKGFILE" ] && PKGNAME=`basename "$1" .$pkg_ext`
  4503.   fi
  4504.  
  4505.   # maybe the file is not in the current dir, but in WORKDIR, so lets look there too
  4506.   if [ ! -f "$PKGFILE" ];then
  4507.     PKGFILE=`find "$WORKDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*${pkg_ext}" | head -1`
  4508.     [ ! -f "$PKGFILE" ] && PKGFILE=`find "$WORKDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*${EX}" | grep -m1 $EX`
  4509.  
  4510.     # if we found the file in WORKDIR, make that our CURDIR
  4511.     if [ -f "$PKGFILE" ];then
  4512.       PKGNAME=`basename "$PKGFILE" .$pkg_ext`
  4513.       CURDIR="$WORKDIR"
  4514.       cd "$WORKDIR"
  4515.     else
  4516.       # if we still didn't find it, do a broader check
  4517.       PKGFILE=`find "$WORKDIR" -mount -maxdepth 1 -type f -name "${PKGNAME}*" | head -1`
  4518.       if [ -f "$PKGFILE" ];then
  4519.         PKGNAME=`basename "$PKGFILE" .$pkg_ext`
  4520.         CURDIR="$WORKDIR"
  4521.         cd "$WORKDIR"
  4522.       fi
  4523.     fi
  4524.   fi
  4525.  
  4526.   # if the file exists, or using --force
  4527.   if [ -f "$PKGFILE" -o "$FORCE" = true ];then
  4528.  
  4529.     [ ! -f "$PKGFILE" ] && echo "The file $1 was not found." && return 1
  4530.  
  4531.     # get the extension of this newly found pkg (which we should have found by now)
  4532.     pkg_ext=`get_pkg_ext "$PKGFILE"`
  4533.     # get extension again, we may have been given only a pkg name, find its extension from repo files
  4534.     [ "$pkg_ext" = "" ] && pkg_ext=`get_pkg_ext "$PKGNAME"`
  4535.  
  4536.     [ "$pkg_ext" = '' ] && echo "Not installing $PKGFILE." && error "Invalid file extension ($pkg_ext)." && return 1
  4537.  
  4538.     # remove any previous PET/pkg stuff lying around from previous installs
  4539.     rm -f /pet.specs /pinstall.sh /puninstall.sh /install/doinst.sh
  4540.  
  4541.     # if pkg is not yet installed, or we are force re-installing
  4542.  
  4543.     # get filename from download file
  4544.     PKGNAME=`basename "$PKGFILE" .$pkg_ext`
  4545.  
  4546.     # ask/inform user before install
  4547.     echo -n "Install package ${PKGNAME}$QTAG:  "
  4548.     [ "$ASK" = true ] && read -n 1 CONFIRM </dev/tty || CONFIRM=y
  4549.     [ "$ASK" = true ] && echo -ne "\b\b\n"
  4550.  
  4551.     # if user answered yes, we will now download the pkgs
  4552.     if [ "$CONFIRM" = "y" ];then
  4553.  
  4554.       # print new line if we didnt take any user input on tty
  4555.       [ "$ASK" != true ] && echo
  4556.  
  4557.       # only ask once
  4558.       ASK=false
  4559.  
  4560.       #if [ "`pkg_contents "$PKGFILE" 2>/dev/null`" = '' ];then
  4561.       # error "$PKGFILE not a valid package."
  4562.       # exit 1
  4563.       #fi
  4564.  
  4565.       # fallback to repo pkg ext if none found
  4566.       [ "$pkg_ext" = "" ] && pkg_ext="$EX"
  4567.  
  4568.       #remove the old error log
  4569.       rm $TMPDIR/$SELF-cp-errlog 2>/dev/null
  4570.  
  4571.       # extract the archive file into a $CURDIR/$PKGNAME folder
  4572.       case "$pkg_ext" in
  4573.  
  4574.         pet)
  4575.           PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.pet"`"
  4576.           #determine the compression, extend test to 'XZ'
  4577.           file -b "${PKGFILE}" | grep -i -q "^xz" && TAREXT=xz || TAREXT=gz
  4578.           [ "$TAREXT" = 'xz' ] && taropts='-xJf' || taropts='-zxf'
  4579.  
  4580.           # convert to tar extractable archive
  4581.           pet2tgz "${PKGFILE}" 1>/dev/null
  4582.  
  4583.           # now extract the file
  4584.           tar $taropts "${CURDIR}/${PKGNAME}.tar.${TAREXT}" 2> $TMPDIR/$SELF-cp-errlog
  4585.  
  4586.           # some old types need this extra step
  4587.           if [ -f "${CURDIR}/${PKGNAME}.tar.$TAREXT" ];then
  4588.             cd "${CURDIR}/${PKGNAME}/" 1>/dev/null
  4589.             tar --absolute-names $tarops "./${PKGNAME}.tar.$TAREXT" ${DIRECTSAVEPATH}/ 2> $TMPDIR/$SELF-cp-errlog #260713
  4590.             sync
  4591.             rm -f  "./${PKGNAME}.tar.$TAREXT" 1>/dev/null
  4592.             cd - 1>/dev/null
  4593.           fi
  4594.  
  4595.           # save the uninstall script for later
  4596.           if [ -f "${CURDIR}/${PKGNAME}/puninstall.sh" ];then
  4597.             mv "${CURDIR}/${PKGNAME}/puninstall.sh" ${HOME}/.packages/${PKGNAME}.remove
  4598.           fi
  4599.  
  4600.           # create a tgz
  4601.           tgz2pet "${CURDIR}/${PKGNAME}.tar.$TAREXT" 1>/dev/null
  4602.           rm "${CURDIR}/${PKGNAME}.tar.$TAREXT" 2>/dev/null
  4603.         ;;
  4604.  
  4605.         sfs)
  4606.           PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.sfs"`"
  4607.           [ ! -f "$PKGFILE" ] && PKGFILE="`find ${WORKDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.sfs"`"
  4608.           echo sfs_loadr -q --cli +"$PKGFILE" #2>/dev/null
  4609.           sfs_loadr -q --cli +"$PKGFILE" #2>/dev/null
  4610.           rm -f $HOME/.packages/${PKGNAME}.files 2>/dev/null
  4611.           # create $HOME/.packages/$PKGNAME.files
  4612.           pkg_contents "$PKGFILE" | while read line
  4613.           do
  4614.             [ -f "$line" ] && echo "$line" >> $HOME/.packages/${PKGNAME}.files
  4615.           done
  4616.           # exit, we dont need to unpack and copy
  4617.           return 0
  4618.         ;;
  4619.  
  4620.         deb)
  4621.  
  4622.           rmdir "${CURDIR}/${PKGNAME}" &>/dev/null
  4623.           mkdir -p "${CURDIR}/${PKGNAME}"
  4624.           [ ! -f "$PKGFILE" ] && PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.deb"`"
  4625.           cp "${PKGFILE}" "${CURDIR}/${PKGNAME}/${PKGNAME}.deb" || exit 4
  4626.           cd "${CURDIR}/${PKGNAME}/"
  4627.  
  4628.           if [ -f "${CURDIR}/${PKGNAME}/${PKGNAME}.deb" ];then
  4629.             dpkg-deb --contents "${CURDIR}/${PKGNAME}/${PKGNAME}.deb" \
  4630.               | grep -v '/$' \
  4631.               | tr -s ' ' \
  4632.               | cut -f6 -d' ' \
  4633.               | sed -e 's/^.//g' 2>/dev/null \
  4634.               | grep -v '^$' > $HOME/.packages/${PKGNAME}.files
  4635.           fi
  4636.  
  4637.                     # woofce: NO_MULTIARCH_SYMLINK=1 (DISTRO_SPECS)
  4638.                     if [ -z "$NO_MULTIARCH_SYMLINK" ] ; then
  4639.             if [ -f $HOME/.packages/${PKGNAME}.files ];then
  4640.               pkg_has_archdir="$(grep -m1 "$DISTRO_ARCHDIR" "$HOME/.packages/${PKGNAME}.files")"
  4641.             fi
  4642.  
  4643.                         # Workaround to avoid overwriting the $DISTRO_ARCHDIR symlink.
  4644.                         if [ "$DISTRO_ARCHDIR" != "" -a -f $HOME/.packages/${PKGNAME}.files -a "$pkg_has_archdir" != "" ]; then
  4645.                           mkdir -p /tmp/$PKGNAME
  4646.                           rm -rf /tmp/$PKGNAME/*
  4647.                           dpkg-deb -x ${CURDIR}/${PKGNAME}.deb /tmp/$PKGNAME/ 2> $TMPDIR/$SELF-cp-errlog
  4648.                           for f in $(find /tmp/$PKGNAME \( -type f -o -type l \))
  4649.                           do
  4650.                               xpath=$(echo "$f" |  cut  -f 4-30 -d "/" | sed "s/$DISTRO_ARCHDIR\///")
  4651.                               mkdir -p ${DIRECTSAVEPATH}/$(dirname "$xpath")
  4652.                               cp -a "$f" ${DIRECTSAVEPATH}/$(dirname "$xpath")/
  4653.                           done
  4654.                           rm -rf /tmp/$PKGNAME &>/dev/null
  4655.                         else
  4656.                           dpkg-deb -x ${CURDIR}/${PKGNAME}.deb ${DIRECTSAVEPATH}/ 2> $TMPDIR/$SELF-cp-errlog
  4657.                         fi
  4658.                     else
  4659.                         dpkg-deb -x ${CURDIR}/${PKGNAME}.deb ${DIRECTSAVEPATH}/ 2> $TMPDIR/$SELF-cp-errlog
  4660.                     fi
  4661.  
  4662.           if [ "`cat $TMPDIR/$SELF-cp-errlog | grep 'tar: Exiting with failure status due to previous errors'`" != "" ];then
  4663.             error "Failed to unpack $PKGNAME.deb"
  4664.             exit 1
  4665.           fi
  4666.           [ -d /DEBIAN ] && rm -rf /DEBIAN #130112 precaution.
  4667.           dpkg-deb -e "${CURDIR}/${PKGNAME}/${PKGNAME}.deb" /DEBIAN #130112 extracts deb control files to dir /DEBIAN. may have a post-install script, see below.
  4668.           rm "${PKGNAME}.deb"
  4669.  
  4670.           cd - 1>/dev/null
  4671.         ;;
  4672.  
  4673.         *tbz|*tar.bz2)
  4674.           rmdir "${CURDIR}/${PKGNAME}" &>/dev/null
  4675.           mkdir -p "${CURDIR}/${PKGNAME}"
  4676.           cp "${CURDIR}/${PKGNAME}.$pkg_ext" "${CURDIR}/${PKGNAME}/${PKGNAME}.$pkg_ext" || exit 4
  4677.           cd "${CURDIR}/${PKGNAME}/"
  4678.  
  4679.           ( umask 000 ; cat "${PKGNAME}.$pkg_ext" | bzip2 -dc | tar -xf - 2> $TMPDIR/$SELF-cp-errlog )
  4680.  
  4681.           rm "${PKGNAME}.$pkg_ext"
  4682.           cd - 1>/dev/null
  4683.         ;;
  4684.  
  4685.         *tlz|*tar.lzma)
  4686.           rmdir "${CURDIR}/${PKGNAME}" &>/dev/null
  4687.           mkdir -p "${CURDIR}/${PKGNAME}"
  4688.           PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.${pkg_ext}"`"
  4689.           cp "$PKGFILE" "${CURDIR}/${PKGNAME}/`basename $PKGFILE`" || exit 4
  4690.           cd "${CURDIR}/${PKGNAME}/"
  4691.  
  4692.           ( umask 000 ; cat "${PKGNAME}.$pkg_ext" | lzma -dc | tar -xf - 2> $TMPDIR/$SELF-cp-errlog )
  4693.  
  4694.           rm "${PKGNAME}.$pkg_ext"
  4695.           cd - 1>/dev/null
  4696.         ;;
  4697.  
  4698.         *tgz|*txz|*tar.gz|*tar.xz|*xz|*gz)
  4699.           rmdir "${CURDIR}/${PKGNAME}" &>/dev/null
  4700.           mkdir -p "${CURDIR}/${PKGNAME}"
  4701.           PKGFILE="`find ${CURDIR} -mount -maxdepth 1 -type f -name "${PKGNAME}*.${pkg_ext}"`"
  4702.           cp "$PKGFILE" "${CURDIR}/${PKGNAME}/`basename $PKGFILE`" || exit 4
  4703.           cd "${CURDIR}/${PKGNAME}/"
  4704.  
  4705.           file -b "${PKGNAME}.$pkg_ext" | grep -i -q "^xz" && TAREXT=xz || TAREXT=gz
  4706.           [ "$TAREXT" = 'xz' ] && taropts='-xJf' || taropts='-zxf'
  4707.  
  4708.           tar $taropts "${PKGNAME}.$pkg_ext" 2> $TMPDIR/$SELF-cp-errlog
  4709.           rm "${PKGNAME}.$pkg_ext"
  4710.           cd - 1>/dev/null
  4711.         ;;
  4712.  
  4713.         rpm)
  4714.           PKGNAME="`basename ${PKGFILE} .rpm`"
  4715.           busybox rpm -qp "$PKGFILE" > /dev/null 2>&1
  4716.           [ $? -ne 0 ] && exit 1
  4717.           PFILES="`busybox rpm -qpl $PKGFILE`"
  4718.           [ $? -ne 0 ] && exit 1
  4719.           echo "$PFILES" > $HOME/.packages/${PKGNAME}.files
  4720.           pkg_unpack "$PKGFILE" 1>/dev/null
  4721.         ;;
  4722.  
  4723.       esac
  4724.  
  4725.       # now extract pkg contents to /
  4726.       [ ! -d "${PKGNAME}/" ] && error "Cannot enter directory '${PKGNAME}/', it doesn't exist." && exit 4
  4727.       cd "${PKGNAME}/" 1>/dev/null
  4728.  
  4729.       cp -a --remove-destination * ${DIRECTSAVEPATH}/ 2> $TMPDIR/$SELF-cp-errlog
  4730.  
  4731.       #source is a directory, target is a symlink...
  4732.       if [ -s $TMPDIR/$SELF-cp-errlog ];then
  4733.         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 '`' |
  4734.         while read ONEDIRSYMLINK
  4735.         do
  4736.           #adding that extra trailing / does the trick...
  4737.           cp -a --remove-destination ${ONEDIRSYMLINK}/* /${ONEDIRSYMLINK}/ 2> $TMPDIR/$SELF-cp-errlog #260713
  4738.         done
  4739.       fi
  4740.       sync
  4741.       cd .. 1>/dev/null
  4742.  
  4743.       if [ ! -f $HOME/.packages/${PKGNAME}.files ] || \
  4744.          [ -z $HOME/.packages/${PKGNAME}.files ];then
  4745.         # add ${HOME}/.packages/${PKGNAME}.files, need to find regular files and links separately... then images..
  4746.         find "./${PKGNAME}/" -mount -mindepth 2 -type f | sed -e "s/\.\/${PKGNAME}//g" -e 's/\/root0\//\/root\//g' > $HOME/.packages/${PKGNAME}.files
  4747.         find "./${PKGNAME}/" -mount -mindepth 2 -type l | sed -e "s/\.\/${PKGNAME}//g" -e 's/\/root0\//\/root\//g' >> $HOME/.packages/${PKGNAME}.files
  4748.  
  4749.         for ONEIMAGEFILE in `ls -1 /*[0-9].xpm 2>/dev/null`
  4750.         do
  4751.          BASEONEIMAGE="`basename $ONEIMAGEFILE`"
  4752.          echo "/usr/local/lib/X11/pixmaps/$BASEONEIMAGE" >> $HOME/.packages/${PKGNAME}.files
  4753.         done
  4754.         for ONEIMAGEFILE in `ls -1 /*[0-9].png 2>/dev/null`
  4755.         do
  4756.          BASEONEIMAGE="`basename $ONEIMAGEFILE`"
  4757.          echo "/usr/local/lib/X11/pixmaps/$BASEONEIMAGE" >> $HOME/.packages/${PKGNAME}.files
  4758.         done
  4759.         for ONEIMAGEFILE in `ls -1 /*[^0-9].xpm 2>/dev/null`
  4760.         do
  4761.          BASEONEIMAGE="`basename $ONEIMAGEFILE`"
  4762.          echo "/usr/local/lib/X11/mini-icons/$BASEONEIMAGE" >> $HOME/.packages/${PKGNAME}.files
  4763.         done
  4764.         for ONEIMAGEFILE in `ls -1 /*[^0-9].png 2>/dev/null` #v2.16
  4765.         do
  4766.          BASEONEIMAGE="`basename $ONEIMAGEFILE`"
  4767.          echo "/usr/local/lib/X11/mini-icons/$BASEONEIMAGE" >> $HOME/.packages/${PKGNAME}.files
  4768.         done
  4769.       fi
  4770.  
  4771.       # move the *.files to $HOME/.packages
  4772.       FILES=''
  4773.       FILES="`find "$CURDIR" -mount -maxdepth 1 -iname "*.files" | head -1`"
  4774.       [ -f "`echo $FILES`" ] && mv "$FILES" "${HOME}/.packages/" 2>/dev/null #260713
  4775.  
  4776.       #pkgname.files may need to be fixed...
  4777.       cat ${HOME}/.packages/${PKGNAME}.files | grep -v '/$' | sed -e 's%^\\.%%' -e 's%^%/%' -e 's%^//%/%' 2>/dev/null > ${HOME}/.packages/${PKGNAME}.files2
  4778.       mv ${HOME}/.packages/${PKGNAME}.files2 ${HOME}/.packages/${PKGNAME}.files
  4779.  
  4780.       sort -u $HOME/.packages/${PKGNAME}.files > $HOME/.packages/${PKGNAME}.files2
  4781.       mv ${HOME}/.packages/${PKGNAME}.files2 ${HOME}/.packages/${PKGNAME}.files
  4782.  
  4783.       # some pets add images and icons at / so move them
  4784.       mv /{*.xpm,*.png} /usr/share/pixmaps/ 2>/dev/null
  4785.       mv /*.ico /usr/share/pixmaps/ 2>/dev/null
  4786.  
  4787.       # run the post install scripts (for puppy, slackware, arch, debian/ubuntu, etc)
  4788.       for i in /pinstall.sh /install/doinst.sh /DEBIAN/postinst
  4789.       do
  4790.         [ -z /${i} -o ! -e /${i} ] && continue
  4791.         chmod +x /${i}
  4792.         cd /
  4793.         nohup sh ${i} &>/dev/null
  4794.         sleep 0.2
  4795.         rm -f ${i}
  4796.         cd ${CURDIR}/
  4797.       done
  4798.       rm -rf /install
  4799.       rm -rf /DEBIAN
  4800.  
  4801.       #130314 run arch linux pkg post-install script...
  4802.       if [ -f /.INSTALL -a /usr/local/petget/ArchRunDotInstalls ];then #precaution. see 3builddistro, script created by noryb009.
  4803.         #this code is taken from below...
  4804.         dlPATTERN='|'"`echo -n "${PKGNAME}" | sed -e 's%\\-%\\\\-%'`"'|'
  4805.         archVER="`cat $TMPDIR/petget_missing_dbentries-Packages-* 2>/dev/null | grep "$dlPATTERN" | head -n 1 | cut -f 3 -d '|'`"
  4806.         if [ "$archVER" ];then #precaution.
  4807.           cd /
  4808.           mv -f .INSTALL .INSTALL1-${archVER}
  4809.           cp -a /usr/local/petget/ArchRunDotInstalls ArchRunDotInstalls
  4810.           LANG=$LANG_USER ./ArchRunDotInstalls
  4811.           rm -f ArchRunDotInstalls
  4812.           rm -f .INSTALL*
  4813.           cd ${CURDIR}/
  4814.         fi
  4815.       fi
  4816.  
  4817.       PKGCAT=''
  4818.       PKGSIZE=''
  4819.       PKGDESC=''
  4820.       DEPLIST=''
  4821.  
  4822.       # clean up pet installs
  4823.       if [ "$pkg_ext" = "pet" ];then
  4824.         cd /
  4825.  
  4826.         # get pkg info from pet.spec before we delete it, because the PET may not be a repo pkg
  4827.         petspecs="`cat /pet.specs 2>/dev/null`"
  4828.  
  4829.         if [ "$petspecs" != '' ];then
  4830.           # now get pkg info
  4831.           PKGCAT="`echo $petspecs|  cut -f5  -d'|'`"
  4832.           PKGSIZE="`echo $petspecs| cut -f6  -d'|'`"
  4833.           PKGDESC="`echo $petspecs| cut -f10 -d'|'`"
  4834.           DEPSLIST="`echo $petspecs|cut -f9  -d'|'`"
  4835.         fi
  4836.  
  4837.         cd ${CURDIR}/
  4838.       fi
  4839.  
  4840.       # cleanup a bit
  4841.       rm -R "${PKGNAME}/" 2>/dev/null || echo "${yellow}Warning:${endcolour} Cannot remove directory '${PKGNAME}/'." #150213
  4842.  
  4843.       #120102 install may have overwritten a symlink-to-dir...
  4844.       #tar defaults to not following symlinks, for both dirs and files, but i want to follow symlinks
  4845.       #for dirs but not for files. so, fix here... (note, dir entries in .files have / on end)
  4846.       cat ${HOME}/.packages/${PKGNAME}.files | grep '[a-zA-Z0-9]/$' | sed -e 's%/$%%' | grep -v '^/mnt' |
  4847.       while read ONESPEC
  4848.       do
  4849.        if [ -d "${DIRECTSAVEPATH}${ONESPEC}" ];then
  4850.         if [ ! -h "${DIRECTSAVEPATH}${ONESPEC}" ];then
  4851.          DIRLINK=""
  4852.          if [ -h "/initrd${PUP_LAYER}${ONESPEC}" ];then #120107
  4853.           DIRLINK="`readlink -m "/initrd${PUP_LAYER}${ONESPEC}" | sed -e "s%/initrd${PUP_LAYER}%%"`" #PUP_LAYER: see /etc/rc.d/PUPSTATE. 120107
  4854.           xDIRLINK="`readlink "/initrd${PUP_LAYER}${ONESPEC}"`" #120107
  4855.          fi
  4856.          if [ ! "$DIRLINK" ];then
  4857.           if [ -h "/initrd${SAVE_LAYER}${ONESPEC}" ];then #120107
  4858.            DIRLINK="`readlink -m "/initrd${SAVE_LAYER}${ONESPEC}" | sed -e "s%/initrd${SAVE_LAYER}%%"`" #SAVE_LAYER: see /etc/rc.d/PUPSTATE. 120107
  4859.            xDIRLINK="`readlink "/initrd${SAVE_LAYER}${ONESPEC}"`" #120107
  4860.           fi
  4861.          fi
  4862.          if [ "$DIRLINK" ];then
  4863.           if [ -d "$DIRLINK"  ];then
  4864.            if [ "$DIRLINK" != "${ONESPEC}" ];then #precaution.
  4865.             mkdir -p "${DIRECTSAVEPATH}${DIRLINK}" #120107
  4866.             cp -a -f --remove-destination ${DIRECTSAVEPATH}"${ONESPEC}"/* "${DIRECTSAVEPATH}${DIRLINK}/" #ha! fails if put double-quotes around entire expression.
  4867.             rm -rf "${DIRECTSAVEPATH}${ONESPEC}"
  4868.             if [ "$DIRECTSAVEPATH" = "" ];then
  4869.              ln -s "$xDIRLINK" "${ONESPEC}"
  4870.             else
  4871.              DSOPATH="`dirname "${DIRECTSAVEPATH}${ONESPEC}"`"
  4872.              DSOBASE="`basename "${DIRECTSAVEPATH}${ONESPEC}"`"
  4873.              rm -f "${DSOPATH}/.wh.${DSOBASE}" #allow underlying symlink to become visible on top.
  4874.             fi
  4875.            fi
  4876.           fi
  4877.          fi
  4878.         fi
  4879.        fi
  4880.       done
  4881.  
  4882.             # woofce: NO_MULTIARCH_SYMLINK=1 (DISTRO_SPECS)
  4883.             if [ -z "$NO_MULTIARCH_SYMLINK" ] ; then
  4884.         #121217 it seems that this problem is occurring in other modes (13 reported)...
  4885.         #121123 having a problem with multiarch symlinks in full-installation...
  4886.         #it seems that the symlink is getting replaced by a directory.
  4887.         if [ "$DISTRO_ARCHDIR" ];then #in /etc/rc.d/DISTRO_SPECS. 130112 change test from DISTRO_ARCHDIR. 130114 revert DISTRO_ARCHDIR_SYMLINKS==yes.
  4888.           if [ -d /usr/lib/${DISTRO_ARCHDIR} ];then
  4889.             if [ ! -h /usr/lib/${DISTRO_ARCHDIR} ];then
  4890.               cp -a -f --remove-destination /usr/lib/${DISTRO_ARCHDIR}/* /usr/lib/
  4891.               sync
  4892.               rm -r -f /usr/lib/${DISTRO_ARCHDIR}
  4893.               ln -s ./ /usr/lib/${DISTRO_ARCHDIR}
  4894.             fi
  4895.           fi
  4896.           if [ -d /lib/${DISTRO_ARCHDIR} ];then
  4897.             if [ ! -h /lib/${DISTRO_ARCHDIR} ];then
  4898.               cp -a -f --remove-destination /lib/${DISTRO_ARCHDIR}/* /lib/
  4899.               sync
  4900.               rm -r -f /lib/${DISTRO_ARCHDIR}
  4901.               ln -s ./ /lib/${DISTRO_ARCHDIR}
  4902.             fi
  4903.           fi
  4904.           if [ -d /usr/bin/${DISTRO_ARCHDIR} ];then
  4905.             if [ ! -h /usr/bin/${DISTRO_ARCHDIR} ];then
  4906.               cp -a -f --remove-destination /usr/bin/${DISTRO_ARCHDIR}/* /usr/bin/
  4907.               sync
  4908.               rm -r -f /usr/bin/${DISTRO_ARCHDIR}
  4909.               ln -s ./ /usr/bin/${DISTRO_ARCHDIR}
  4910.             fi
  4911.           fi
  4912.         fi
  4913.       fi
  4914.  
  4915.       #flush unionfs cache, so files in pup_save layer will appear "on top"...
  4916.       if [ "$DIRECTSAVEPATH" != "" ];then
  4917.        #but first, clean out any bad whiteout files...
  4918.        # 22sep10 shinobar: bugfix was not working clean out whiteout files
  4919.        find /initrd/pup_rw -mount -type f -name .wh.\*  -printf '/%P\n'|
  4920.        while read ONEWHITEOUT
  4921.        do
  4922.         ONEWHITEOUTFILE="`basename "$ONEWHITEOUT"`"
  4923.         ONEWHITEOUTPATH="`dirname "$ONEWHITEOUT"`"
  4924.         if [ "$ONEWHITEOUTFILE" = ".wh.__dir_opaque" ];then
  4925.          [ "`grep "$ONEWHITEOUTPATH" /root/.packages/${PKGNAME}.files`" != "" ] && rm -f "/initrd/pup_rw/$ONEWHITEOUT"
  4926.          continue
  4927.         fi
  4928.         ONEPATTERN="`echo -n "$ONEWHITEOUT" | sed -e 's%/\\.wh\\.%/%'`"'/*' ;#echo "$ONEPATTERN" >&2
  4929.         [ "`grep -x "$ONEPATTERN" /root/.packages/${PKGNAME}.files`" != "" ] && rm -f "/initrd/pup_rw/$ONEWHITEOUT"
  4930.        done
  4931.        #111229 /usr/local/petget/removepreview.sh when uninstalling a pkg, may have copied a file from sfs-layer to top, check...
  4932.        cat ${HOME}/.packages/${PKGNAME}.files |
  4933.        while read ONESPEC
  4934.        do
  4935.         [ "$ONESPEC" = "" ] && continue #precaution.
  4936.         if [ ! -d "$ONESPEC" ];then
  4937.          [ -e "/initrd/pup_rw${ONESPEC}" ] && rm -f "/initrd/pup_rw${ONESPEC}"
  4938.         fi
  4939.        done
  4940.        #now re-evaluate all the layers...
  4941.        busybox mount -t aufs -o remount,udba=reval unionfs / #remount with faster evaluation mode.
  4942.        [ $? -ne 0 ] && logger -s -t "pkg" "Failed to remount aufs / with udba=reval"
  4943.        sync
  4944.       fi
  4945.  
  4946.       # get pkg size, category and description
  4947.       [ "$PKGCAT" = ''  ] && PKGCAT=`LANG=C  grep -m1 "|${PKGNAME_ONLY}|" "${HOME}/.packages/${REPOFILE}" 2>/dev/null | cut -f5 -d'|' | head -1`
  4948.       [ "$PKGCAT" = ''  ] && PKGCAT=`LANG=C  grep -m1 "^${PKGNAME}|" "${HOME}/.packages/${REPOFILE}" 2>/dev/null | cut -f5 -d'|' | head -1`
  4949.       [ "$PKGDESC" = '' ] && PKGDESC=`LANG=C grep -m1 "|${PKGNAME_ONLY}|" "${HOME}/.packages/${REPOFILE}" 2>/dev/null| cut -f10 -d'|' | head -1`
  4950.       [ "$PKGDESC" = '' ] && PKGDESC=`LANG=C grep -m1 "^${PKGNAME}|" "${HOME}/.packages/${REPOFILE}" 2>/dev/null| cut -f10 -d'|' | head -1`
  4951.       [ "$PKGSIZE" = '' ] && PKGSIZE=`LANG=C du -s -k ${CURDIR}/${PKG} 2>/dev/null | cut -f1`
  4952.       # get pkg version
  4953.       PKGVER="`LANG=C  echo "$PKGNAME" | sed -e 's/^[^0-9]*-//g'`"
  4954.       PKGARCH="`LANG=C echo "$PKGNAME" | cut -f3 -d'-' | grep -E 'i[3-9]|x[8-9]|noarch'`"
  4955.  
  4956.       # last check for pkg info
  4957.       [ "$DEPSLIST" = '' ] && DEPSLIST="`grep -m1 "^${PKGNAME}|" ${HOME}/.packages/Packages-* 2>/dev/null | cut -f9 -d'|' | grep '+' | head -1`"
  4958.       [ "$PKGDESC" = ''  ] && PKGDESC="No description"
  4959.  
  4960.       #080917 - build woof compatible repo line .. #090817
  4961.       if [ "`cut -f1 -d'|' ${HOME}/.packages/user-installed-packages 2>/dev/null | grep -m1 ^${PKGNAME}`" = '' ];then
  4962.  
  4963.         # get the package db entry to add to user-installed-packages...
  4964.         # first, get it from petspecs if possible, or from the pkgs repo, or make one
  4965.         if [ -f /pet.specs ];then
  4966.           DB_ENTRY="`cat /pet.specs | head -n 1`"
  4967.         elif [ "`grep -m1 "^${PKGNAME}|" "${HOME}/.packages/$REPOFILE"`" != "" ];then
  4968.           DB_ENTRY="`grep -m1 "^${PKGNAME}|" "${HOME}/.packages/$REPOFILE"`"
  4969.         else
  4970.           DB_ENTRY="${PKGNAME}|${PKGNAME_ONLY}|${PKGVER}|${BUILD}|${PKGCAT}|${PKGSIZE}|${PKGDIR}|${PKGNAME}.${pkg_ext}|${DEPSLIST}|${PKGDESC}|${DISTRO_BINARY_COMPAT}|${DISTRO_COMPAT_VERSION}"
  4971.         fi
  4972.  
  4973.         # Fix for Debian executables showing as shared libs in ROX.
  4974.         if [ "$DISTRO_FILE_PREFIX" = "stretch" -o "$DISTRO_FILE_PREFIX" = "ascii" ]; then
  4975.           if [ "$(ps aux | grep ROX |grep -v grep)" ]; then # Other managers are OK
  4976.             if [ "$(which elfedit)" ];then
  4977.               grep -E '/bin/|/sbin/' ${HOME}/.packages/${PKGNAME}.files |
  4978.               while read FLINE
  4979.               do
  4980.                 [ "$(file "$FLINE" | grep -i 'shared object')" ] && elfedit --input-type=dyn --output-type=exec $FLINE
  4981.               done
  4982.             else
  4983.               echo -e "${yellow}Warning${endcolour} Recent Debian executables show as shared libraries in ROX,"
  4984.               echo "which causes ROX to fail to open or execute them. To fix that during package "
  4985.               echo "installation you should install elfutils or have devx loaded."
  4986.             fi
  4987.           fi
  4988.         fi
  4989.  
  4990.         # now add the db entry to user-installed-packages
  4991.         echo "$DB_ENTRY" >> ${HOME}/.packages/user-installed-packages #130913
  4992.  
  4993.       fi
  4994.  
  4995.       # now delete the petspecs file(s), we already added our PKG info to installed-packages
  4996.       rm /pet.specs &>/dev/null
  4997.       rm /*.pet.specs &>/dev/null
  4998.  
  4999.       # make sure /tmp has correct permission, a pkg may have overwritten it
  5000.       ls -dl /tmp | grep -q '^drwxrwxrwt' || chmod 1777 /tmp #130305 rerwin.
  5001.  
  5002.       #090817
  5003.       if [ "`is_installed_pkg $PKGNAME`" = true -a "`cat $HOME/.packages/${PKGNAME}.files`" != '' ];then
  5004.         echo -e "${green}Installed:${endcolour} $PKGNAME"
  5005.         # show if pkg has menu entry
  5006.         menu_entry_msg "$PKGNAME_ONLY"
  5007.         #080413 do fixmenus, if menu entry found #200713 moved here
  5008.         if [ "`grep -m1 '/usr/share/applications' $HOME/.packages/${PKGNAME}.files 2>/dev/null | grep desktop$`" != "" ];then
  5009.           [ ! -f /tmp/pkg/update_menus_busy ] && update_menus &
  5010.         fi
  5011.         [ "`which logger`" != '' ] && logger "$0 Package $PKGNAME installed by $APP $APPVER"
  5012.       else
  5013.         error "$PKGNAME may not have installed correctly."
  5014.       fi
  5015.  
  5016.       #100817 clean up user-installed-packages (remove duplicates and empty lines
  5017.       cat ${HOME}/.packages/user-installed-packages | grep -v "^\$" | uniq > ${HOME}/.packages/user-installed-packages_clean
  5018.       mv ${HOME}/.packages/user-installed-packages_clean ${HOME}/.packages/user-installed-packages
  5019.  
  5020.       # puppy specific fixes for installed package
  5021.       postinstall_hacks "$PKGNAME" "$PKGNAME_ONLY" &
  5022.  
  5023.       #100622 slackware 13.1: just in case any got through, remove c-shell scripts...
  5024.       rm -f /etc/profile.d/*.csh* 2>/dev/null
  5025.  
  5026.       #120523 precise puppy needs this... (refer also rc.update and 3builddistro)
  5027.       if [ "`grep '/usr/share/glib-2.0/schemas' $HOME/.packages/${PKGNAME}*.files 2>/dev/null`" != "" ];then
  5028.         [ -e /usr/bin/glib-compile-schemas ] && /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas &>/dev/null
  5029.       fi
  5030.  
  5031.       if [ "`grep '/usr/lib/gio/modules' $HOME/.packages/${PKGNAME}.files 2>/dev/null`" != "" ];then
  5032.         [ -e /usr/bin/gio-querymodules ] && /usr/bin/gio-querymodules /usr/lib/gio/modules &>/dev/null
  5033.       fi
  5034.  
  5035.       sync
  5036.  
  5037.     fi #if CONFIRM = y #100213,0.9  moved down to here, fixes install, and output msgs
  5038.  
  5039.   else # file doesn't exists, user must download it first
  5040.  
  5041.     if [ "$PKGNAME" != '' ];then
  5042.  
  5043.       echo "Package '$PKGNAME' not yet downloaded."
  5044.  
  5045.       matching_local_pkgs="`list_downloaded_pkgs $PKGNAME`"
  5046.  
  5047.       # if no matching pkgs downloaded
  5048.       if [ "$matching_local_pkgs" != "" ];then
  5049.         echo "You could install one of the following packages with \`$SELF -i PKGNAME\`:"
  5050.         echo "`list_downloaded_pkgs $(basename "$PKGNAME" .$pkg_ext 2>/dev/null) 2>/dev/null`"
  5051.  
  5052.       # else if not downloaded, and in the current repo, list the matching repo pkgs
  5053.       elif [ "$matching_local_pkgs" = "" -a "`grep -m1 "^${PKGNAME}|" "${HOME}/.packages/${REPOFILE}" 2>/dev/null`" != "" ];then
  5054.         echo "You must first download one of the following packages with \`$SELF -d PKGNAME\`:"
  5055.         echo "`$PKGSEARCH $(basename $PKGNAME .$pkg_ext)`"
  5056.  
  5057.       else
  5058.         not_found "${PKGNAME}"
  5059.       fi
  5060.  
  5061.     else # PKGNAME is empty
  5062.  
  5063.       echo "Package could not be identified."
  5064.  
  5065.     fi
  5066.  
  5067.     exit 1
  5068.  
  5069.   fi
  5070.  
  5071.   # go back to the dir we started in
  5072.   cd "$PREVDIR"
  5073.  
  5074. }
  5075.  
  5076.  
  5077. postinstall_hacks(){              # fix pkgs after installation
  5078.   local PKGNAME="$1"
  5079.   local PKGNAME_ONLY="$2"
  5080.  
  5081.   # remove %u, %U, %f (etc) from Exec lines
  5082.   DESKTOPFILE="`find /usr/share/applications -iname "${PKGNAME_ONLY}*.desktop"`"
  5083.   [ -f "$DESKTOPFILE" ] && \
  5084.     sed -i 's/ %u//' $DESKTOPFILE && \
  5085.     sed -i 's/ %U//' $DESKTOPFILE && \
  5086.     sed -i 's/ %f//' $DESKTOPFILE && \
  5087.     sed -i 's/ %F//' $DESKTOPFILE
  5088.  
  5089.   case $PKGNAME in
  5090.    0ad-*|0ad_*)
  5091.     bbe -e 's/geteuid/getppid/' /usr/games/pyrogenesis > /usr/games/pyrogenesis1 2>/dev/null
  5092.     mv /usr/games/pyrogenesis1 /usr/games/pyrogenesis
  5093.     chmod 755 /usr/games/pyrogenesis
  5094.    ;;
  5095.    openclonk-*|openclonk_*)
  5096.     bbe -e 's/geteuid/getppid/' /usr/games/openclonk > /usr/games/openclonk1 2>/dev/null
  5097.     mv /usr/games/openclonk1 /usr/games/openclonk
  5098.     chmod 755 /usr/games/openclonk
  5099.    ;;
  5100.    vlc_*|vlc-*)
  5101.     VLCDESKTOP="`find /usr/share/applications -mindepth 1 -maxdepth 1 -iname 'vlc*.desktop'`"
  5102.     [ -f $VLCDESKTOP ] && sed -i 's/ --started-from-file %U//' $VLCDESKTOP
  5103.     [ -f $VLCDESKTOP ] && sed -i 's/ --started-from-file//' $VLCDESKTOP
  5104.  
  5105.     #120907 vlc in debian/ubuntu configured to not run as root (it is a pre-compile configure option to enable running as root).
  5106.     #this hack will fix it...
  5107.     #note, this code is also in FIXUPHACK in 'vlc' template.
  5108.     if [ -f /usr/bin/bbe ];then #bbe is a sed-like utility for binary files.
  5109.      if [ -f /usr/bin/vlc  ];then
  5110.        bbe -e 's/geteuid/getppid/' /usr/bin/vlc > /tmp/vlc-temp1
  5111.        mv -f /tmp/vlc-temp1 /usr/bin/vlc
  5112.        chmod 755 /usr/bin/vlc
  5113.      fi
  5114.     fi
  5115.    ;;
  5116.    google-chrome-*) #130221 pemasu. 130224 pemasu: limit cache size...
  5117.     if [ -f /usr/bin/bbe ];then #bbe is a sed-like utility for binary files.
  5118.      if [ -f /opt/google/chrome/chrome  ];then
  5119.     bbe -e 's/geteuid/getppid/' /opt/google/chrome/chrome > /tmp/chrome-temp1
  5120.     mv -f /tmp/chrome-temp1 /opt/google/chrome/chrome
  5121.     chmod 755 /opt/google/chrome/chrome
  5122.     [ -e /usr/bin/google-chrome ] && rm -f /usr/bin/google-chrome
  5123.     echo '#!/bin/sh
  5124.  exec /opt/google/chrome/google-chrome --user-data-dir=/root/.config/chrome --disk-cache-size=10000000 --media-cache-size=10000000 "$@"' > /usr/bin/google-chrome
  5125.     chmod 755 /usr/bin/google-chrome
  5126.     ln -s google-chrome /usr/bin/chrome
  5127.     ln -s /opt/google/chrome/product_logo_48.png /usr/share/pixmaps/google-chrome.png
  5128.     ln -s /opt/google/chrome/product_logo_48.png /usr/share/pixmaps/chrome.png
  5129.     CHROMEDESKTOP="`find /usr/share/applications -mindepth 1 -maxdepth 1 -iname '*chrome*.desktop'`"
  5130.     if [ "$CHROMEDESKTOP" = "" ];then #precaution.
  5131.      echo '[Desktop Entry]
  5132. Encoding=UTF-8
  5133. Version=1.0
  5134. Name=Google Chrome web browser
  5135. GenericName=Google Chrome
  5136. Comment=Google Chrome web browser
  5137. Exec=google-chrome
  5138. Terminal=false
  5139. Type=Application
  5140. Icon=google-chrome.png
  5141. Categories=WebBrowser;' > /usr/share/applications/google-chrome.desktop
  5142.     fi
  5143.      fi
  5144.     fi
  5145.    ;;
  5146.   chromium*) #130221 pemasu. 130224 pemasu: limit cache size...
  5147.     if [ -f /usr/bin/bbe ];then #bbe is a sed-like utility for binary files.
  5148.      if [ -f /usr/lib/chromium/chromium  ];then
  5149.     bbe -e 's/geteuid/getppid/' /usr/lib/chromium/chromium > /tmp/chrome-temp1
  5150.     mv -f /tmp/chrome-temp1 /usr/lib/chromium/chromium
  5151.     chmod 755 /usr/lib/chromium/chromium
  5152.     [ -e /usr/bin/chromium ] && rm -f /usr/bin/chromium
  5153.     echo '#!/bin/sh
  5154.  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
  5155.     chmod 755 /usr/bin/chromium
  5156.     ln -s /usr/share/icons/hicolor/48x48/apps/chromium.png /usr/share/pixmaps/chromium.png
  5157.     CHROMEDESKTOP="`find /usr/share/applications -mindepth 1 -maxdepth 1 -iname '*chromium-br*.desktop'`"
  5158.     if [ "$CHROMEDESKTOP" = "" ];then #precaution.
  5159.      echo '[Desktop Entry]
  5160. Encoding=UTF-8
  5161. Version=1.0
  5162. Name=Chromium web browser
  5163. GenericName=Chromium
  5164. Comment=Chromium web browser
  5165. Exec=chromium
  5166. Terminal=false
  5167. Type=Application
  5168. Icon=chromium.png
  5169. Categories=WebBrowser;' > /usr/share/applications/chromium.desktop
  5170.     fi
  5171.      fi
  5172.     fi
  5173.    ;;
  5174.    jwm_theme_*)
  5175.     #120924 DejaVu font no good for non-Latin languages...
  5176.     #see also langpack_* pinstall.sh (template is in /usr/share/doc/langpack-template/pinstall.sh, read by momanager).
  5177.     LANGUSER="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
  5178.     case $LANGUSER in
  5179.      zh*|ja*|ko*) #chinese, japanese, korean
  5180.     sed -i -e 's%DejaVu Sans%Sans%' /etc/xdg/templates/_root_*
  5181.     sed -i -e 's%DejaVu Sans%Sans%' /root/.jwm/themes/*-jwmrc
  5182.     sed -i -e 's%DejaVu Sans%Sans%' /root/.jwm/jwmrc-theme
  5183.      ;;
  5184.     esac
  5185.     #130326 font size fix for 96 dpi...
  5186.     if [ "$PKGNAME_ONLY" ];then
  5187.      JWMTHEMEFILE="$(grep '^/root/\.jwm/themes/.*-jwmrc$' /root/.packages/${PKGNAME_ONLY}.files | head -n 1)"
  5188.      [ "$JWMTHEMEFILE" ] && hackfontsize "JWMTHEMES='${JWMTHEMEFILE}'"
  5189.     fi
  5190.    ;;
  5191.    openbox*)
  5192.     #120924 DejaVu font no good for non-Latin languages...
  5193.     #see also langpack_* pinstall.sh (template is in /usr/share/doc/langpack-template/pinstall.sh, read by momanager).
  5194.     LANGUSER="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
  5195.     case $LANGUSER in
  5196.      zh*|ja*|ko*) #chinese, japanese, korean
  5197.     sed -i -e 's%DejaVu Sans%Sans%' /etc/xdg/openbox/*.xml
  5198.     sed -i -e 's%DejaVu Sans%Sans%' /root/.config/openbox/*.xml
  5199.      ;;
  5200.     esac
  5201.    ;;
  5202.    gtk_theme_*)
  5203.     #120924 DejaVu font no good for non-Latin languages...
  5204.     #see also langpack_* pinstall.sh (template is in /usr/share/doc/langpack-template/pinstall.sh, read by momanager).
  5205.     LANGUSER="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
  5206.     case $LANGUSER in
  5207.      zh*|ja*|ko*) #chinese, japanese, korean
  5208.     GTKRCFILE="$(find /usr/share/themes -type f -name gtkrc | tr '\n' ' ')"
  5209.     for ONEGTKRC in $GTKRCFILE
  5210.     do
  5211.      sed -i -e 's%DejaVu Sans%Sans%' $ONEGTKRC
  5212.     done
  5213.      ;;
  5214.     esac
  5215.     #130326 font size fix for 96 dpi...
  5216.     if [ "$PKGNAME_ONLY" ];then
  5217.      GTKTHEMEFILE="$(grep '^/usr/share/themes/.*/gtk-2\.0/gtkrc$' /root/.packages/${PKGNAME_ONLY}.files | head -n 1)"
  5218.      [ "$GTKTHEMEFILE" ] && hackfontsize "GTKRCS='${GTKTHEMEFILE}'"
  5219.     fi
  5220.    ;;
  5221.    seamonkey*|firefox*)
  5222.     #120924 DejaVu font no good for non-Latin languages...
  5223.     #see also langpack_* pinstall.sh (template is in /usr/share/doc/langpack-template/pinstall.sh, read by momanager).
  5224.     LANGUSER="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
  5225.     case $LANGUSER in
  5226.      zh*|ja*|ko*) #chinese, japanese, korean
  5227.     MOZFILE="$(find /root/.mozilla -type f -name prefs.js -o -name '*.css' | tr '\n' ' ')"
  5228.     for ONEMOZ in $MOZFILE
  5229.     do
  5230.      sed -i -e 's%DejaVu Sans%Sans%' $ONEMOZ
  5231.     done
  5232.      ;;
  5233.     esac
  5234.    ;;
  5235.    mc_*) #121206 midnight commander
  5236.     #in ubuntu, won't run from the menu. this fixes it...
  5237.     [ -f /usr/share/applications/mc.desktop ] && sed -i -e 's%^Exec=.*%Exec=TERM=xterm mc%' /usr/share/applications/mc.desktop
  5238.    ;;
  5239.    xsane*) #130122
  5240.     #xsane puts up a warning msg at startup if running as root, remove it...
  5241.     #this code is also in file FIXUPHACK in xsane template (in Woof).
  5242.     #WARNING: this may only work for x86 binary.
  5243.     if [ -f /usr/bin/bbe ];then #bbe is a sed-like utility for binary files.
  5244.      if [ -f /usr/bin/xsane  ];then
  5245.     bbe -e 's/\x6b\x00getuid/\x6b\x00getpid/' /usr/bin/xsane > /tmp/xsane-temp1
  5246.     mv -f /tmp/xsane-temp1 /usr/bin/xsane
  5247.     chmod 755 /usr/bin/xsane
  5248.      fi
  5249.     fi
  5250.    ;;
  5251.    kompozer*) #130507
  5252.     [ -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
  5253.    ;;
  5254.   esac
  5255. }
  5256.  
  5257.  
  5258. choose_pkg(){                     # given partial name ($1), choose from a list of matching packages FUNCLIST
  5259.  
  5260.  
  5261.   # exit if no valid options
  5262.   [ ! "$1" -o "$1" = "" -o "$1" = "-" ] && print_usage get && exit 1
  5263.  
  5264.   # get $REPONAME and $EX
  5265.   . ${PKGRC}
  5266.  
  5267.   local REPOEX    # extension of pkgs in the current repo $REPONAME (from rc file)
  5268.   local PKGEX     # pkg extension we get from $1, defaults to $REPOEXT is empty
  5269.   local PKGNAME   # the name of the pkg, we get this from $1
  5270.   local PKGNAME_ONLY  # the pkg name without version, we get this from PKGNAME
  5271.   local PKGS      # the list of PKGS returned matching $1/$PKGNAME
  5272.   local INT=1     # used to provide numbered lists
  5273.  
  5274.   REPOEX=$EX
  5275.   # get pkg extension
  5276.   PKGEX=`get_pkg_ext "$1"`
  5277.  
  5278.   # if no extension, set to extension of current repo
  5279.   [ "$PKGEX" = '' ] && PKGEX=$REPOEX
  5280.  
  5281.   # get pkg name with version, no extension or path
  5282.   PKGNAME="$(basename "$1" .$PKGEX)"
  5283.  
  5284.   # get the full pkg name, to compare against repo pkgs we find
  5285.   PKGNAME_FULL="`get_pkg_name "$PKGNAME"`"
  5286.  
  5287.   # get pkg name only .. without version or suffix
  5288.   PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  5289.  
  5290.   # remove any previous user choice lists
  5291.   rm $TMPDIR/PKGLIST &>/dev/null
  5292.   rm $TMPDIR/USRPKGLIST &>/dev/null
  5293.  
  5294.   # get all pkgs that match the given pkgname
  5295.   # returns full pkg names (field1 of repo, pkgname with ver but no extension) each on a new line
  5296.   PKGS="`$PKGSEARCH "${PKGNAME}"`"
  5297.  
  5298.   if [ "$FORCE" = false -a "`is_installed_pkg "$PKGNAME_FULL"`" = true -a "$HIDE_INSTALLED" = true ];then
  5299.     # remove it from choices
  5300.     PKGS="`echo "$PKGS" | grep -v ^$PKGNAME_FULL\$`"
  5301.   fi
  5302.  
  5303.   # extra steps for ubuntu and debian repos, if multiple choices returned.. remove non-compatible archs, remove dev and dbg pkgs...
  5304.   if [ "$PKGEX" = "deb" -a "`echo "$PKGS" | wc -l`" != "1" ];then
  5305.  
  5306.     ARCH="`uname -m`"
  5307.     #remove x64 pkgs from choices if not using an x64 cpu
  5308.     [ "$ARCH" != "x86_64" ] && PKG="`echo "$PKGS" | grep -v -E 'amd64|x86_64'`"
  5309.  
  5310.     # set any pkgs matching current arch to top of list
  5311.     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
  5312.     do
  5313.       # if not searching for -dev or -dgb pkg, move it to bottom of the list
  5314.       if [ "`is_blacklisted_pkg "$LINE"`" = false -a "`echo "$PKGNAME" | grep -E "\-dbg_|\-dev_"`" = "" -a "`echo "$LINE" | grep -E "\-dbg_|\-dev_"`" != "" ];then
  5315.         PKGS="`echo "$PKGS" | grep -v  "$LINE"`"
  5316.         PKGS="$PKGS
  5317. $LINE"
  5318.       fi
  5319.       # if pkg is for current cpu arch, move it to top of the list
  5320.       if [ "`echo "$LINE" | grep -m1 "$ARCH"`" != "" ];then
  5321.         PKGS="`echo "$PKGS" | grep -v  "$LINE"`"
  5322.         PKGS="$LINE
  5323. $PKGS"
  5324.       fi
  5325.     done
  5326.     #remove debug and dev pkgs
  5327.     #PKGS="`echo "$PKGS" | grep -v "\-dbg_"`"
  5328.     #PKGS="`echo "$PKGS" | grep -v "\-dev_"`"
  5329.   fi
  5330.  
  5331.   # get the user to choose which packages they want to install
  5332.   if [ "$ASK" = true -a "$PKGS" != "" ];then
  5333.     echo "Please choose the package number. For the first package,"
  5334.     echo "enter '1', without quotes. To install multiple packages,"
  5335.     echo "enter the numbers, separated by a comma. Example:  1,3,4"
  5336.     echo
  5337.   fi
  5338.  
  5339.   # if using ubuntu/debian packages, put pkg_* before pkg-* .. else dont
  5340.   [ "$PKGEX" = 'deb' ] && sort='sort -r' || sort='sort'
  5341.  
  5342.   # go through each actual pkg that matches the pkgname search, make it a numbered list
  5343.   echo "$PKGS" | $sort -u | while read LINE
  5344.   do
  5345.     if [ "$LINE" != "" -a "$LINE" != "," -a "$LINE" != " " ];then
  5346.       [ "$ASK" = true ] && echo "${INT}. $LINE"
  5347.       echo "${INT}. $LINE" >> $TMPDIR/PKGLIST
  5348.       INT=$(($INT + 1))
  5349.     fi
  5350.   done
  5351.  
  5352.   # if pkg list was made
  5353.   if [ -f $TMPDIR/PKGLIST -a "`cat $TMPDIR/PKGLIST 2>/dev/null`" != "" ];then
  5354.  
  5355.     # set to first pkg only as default
  5356.     if [ "$ASK" = false ];then
  5357.       USRPKGLIST="$(echo "$PKGS" | $sort | head -1)"
  5358.       echo "$USRPKGLIST" > $TMPDIR/USRPKGLIST
  5359.     fi
  5360.  
  5361.     # user can now input which actual pkgs to get, chosen by number
  5362.     if [ "$ASK" = true ];then
  5363.       # only ask once
  5364.       ASK=false
  5365.  
  5366.       echo
  5367.       echo "Give the numbers of the packages you want to install,"
  5368.       echo -n "separated by a comma, or hit ENTER only to skip: "
  5369.       read USRPKGLIST1 </dev/tty
  5370.  
  5371.       # if user chose nothing (hit ENTER only), just skip
  5372.       [ "$USRPKGLIST1" = '' ] && continue
  5373.  
  5374.       # split the results into newlines, create the list of chosen pkgs (used by other funcs)
  5375.       echo "${USRPKGLIST1}" | tr ',' '\n' | while read LINE
  5376.       do
  5377.         # set chosen pkg choice(s)
  5378.         echo "`grep "^$LINE. " "$TMPDIR/PKGLIST" 2>/dev/null | cut -f2 -d' '`" >> $TMPDIR/USRPKGLIST
  5379.       done
  5380.     fi
  5381.  
  5382.     # remove temp file.. but keep $TMPDIR/USRPKGLIST, it contains our users choices, and is used by pkg_get() and get_deps()
  5383.     rm $TMPDIR/PKGLIST &>/dev/null
  5384.   fi
  5385. }
  5386.  
  5387.  
  5388. pkg_get(){                        # find, download and install $1 and its deps FUNCLIST
  5389.  
  5390.   # The function `choose_pkg` is run just before this one. It gives us $TMPDIR/USRPKGLIST,
  5391.   # which contains a list of packages the user wants to install or download.
  5392.   # In this func, we will go through the list and download/install the package, as well
  5393.   # as its dependencies (depending on what the user chose to do).
  5394.  
  5395.   . ${PKGRC}
  5396.  
  5397.   # exit if no valid options
  5398.   [ ! "$1" -o "$1" = "" -o "$1" = "-" ] && print_usage get && exit 1
  5399.  
  5400.   local PREVDIR="$CURDIR"
  5401.   local pkg_ext=`get_pkg_ext "$1"`; pkg_ext="${pkg_ext:-$EX}" # fall back to repo extension
  5402.   local PKGNAME="`get_pkg_name $(basename "$1" .$pkg_ext)`"
  5403.   local PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  5404.   local PKGLIST="${PKGNAME}"
  5405.   local pkg_builtin=''
  5406.  
  5407.   # dont ask to download or install pkg or deps, as choose_pkg() already asked
  5408.   ASK=false
  5409.  
  5410.   # exit if no valid pkg name
  5411.   [ ! "$PKGNAME" -o "$PKGNAME" = "" -o "$PKGNAME" = "-" ] && print_usage get && exit 1
  5412.  
  5413.   # we want to download all pkgs to same place
  5414.   cd "$WORKDIR"
  5415.   CURDIR="$WORKDIR"
  5416.  
  5417.   # use the list of pkgs user has chosen to install, or the given PKGNAME
  5418.   if [ "`cat "$TMPDIR/USRPKGLIST" 2>/dev/null`" != "" ];then
  5419.     PKGLIST="`cat "$TMPDIR/USRPKGLIST" | grep -v "^\$"`"
  5420.   fi
  5421.  
  5422.   if [ -z "$PKGLIST" ];then
  5423.     echo "No packages to get, exiting."
  5424.     exit 1
  5425.   fi
  5426.  
  5427.   # skip if any pkgs previously installed during our current loop (of getting 'pkg + recursive deps')
  5428.   echo "$PKGLIST" | while read pkg_in_list
  5429.   do
  5430.     name_only=`get_pkg_name_only "$pkg_in_list"`
  5431.     [ -f $TMPDIR/PKGSDONE -a "`grep "^${name_only}\$" $TMPDIR/PKGSDONE 2>/dev/null`" != '' ] && continue
  5432.   done
  5433.  
  5434.   # list the pkgs and ask to download (and maybe install)
  5435.   echo "$PKGLIST" | while read pkg
  5436.   do
  5437.  
  5438.     [ "$pkg" = '' -o ! "$pkg" ] && continue
  5439.  
  5440.     local pkg_name=`get_pkg_name "$pkg" 2>/dev/null`
  5441.     [ "$pkg_name" = '' -o ! "$pkg_name" ] && continue
  5442.  
  5443.     local pkg_name_only=`get_pkg_name_only "$pkg" 2>/dev/null`
  5444.     [ "`is_blacklisted_pkg "$pkg_name_only"`" = true ] && continue
  5445.  
  5446.     local pkg_already_done=`grep -m1 "^$pkg_name_only\$" $TMPDIR/PKGSDONE 2>/dev/null`
  5447.     [ "$pkg_already_done" != '' ] && continue
  5448.  
  5449.     local pkg_in_repo=`is_repo_pkg $pkg_name_only`
  5450.     local PKGFILE=''
  5451.  
  5452.     # dont even try to download if no matches found
  5453.     if [ "$pkg_in_repo" = true -a "$pkg_name_only" != "" ];then
  5454.  
  5455.       local pkg_is_builtin=`is_builtin_pkg "$pkg_name_only"`
  5456.       local pkg_is_in_devx=`is_devx_pkg "$pkg_name_only"`
  5457.  
  5458.       # skip getting pkg if its a builtin, unless HIDE_BUILTINS=false
  5459.       if [ "$pkg_is_builtin" = true -a "${HIDE_BUILTINS}" = true ];then
  5460.         echo "Skipping $pkg_name_only (already built-in).."
  5461.         continue
  5462.       fi
  5463.  
  5464.       # skip getting pkg if its in the devx, unless HIDE_BUILTINS=false
  5465.       if [ "$pkg_is_in_devx" = true -a "${HIDE_BUILTINS}" = true ];then
  5466.         echo "Skipping $pkg_name_only (already in devx).."
  5467.         continue
  5468.       fi
  5469.  
  5470.       # if we are intending to install the pkg
  5471.       if [ "$NO_INSTALL" = false -a "$FORCE" = false ];then
  5472.         # skip if package is already installed, unless --force given
  5473.         if [ "$FORCE" = false -a "`is_installed_pkg $pkg_name`" = true ];then
  5474.           echo -e "Package ${magenta}${pkg_name_only}${endcolour} already installed."
  5475.           echo -e "Use the -f option to force installation: $SELF add $pkg_name_only -f"
  5476.           continue
  5477.         fi
  5478.       fi
  5479.  
  5480.       # get deps early (if any)
  5481.       list_deps "$pkg_name" > ${TMPDIR}/${pkg_name}_dep_list &
  5482.  
  5483.       # DOWNLOAD PKG
  5484.       pkg_download "$pkg_name"
  5485.       set -x
  5486.       PKGFILE_symlink="${pkg_name}.${pkg_ext}"
  5487.       if [ -L "$PKGFILE_symlink" ]; then
  5488.            PKGFILE="$(readlink "$PKGFILE_symlink")"
  5489.            rm "$PKGFILE_symlink"    
  5490.       fi
  5491.       #In most cases these find statments will probably not be needed.
  5492.       [ ! -f "$PKGFILE" ] && PKGFILE="`find "$CURDIR" -maxdepth 1 -type f -name "${pkg_name}.*${pkg_ext}"`"
  5493.       # try grabbing $CURDIR/pkgname.pkg_ext
  5494.       [ ! -f "$PKGFILE" ] && PKGFILE="`find "$CURDIR" -maxdepth 1 -type f -name "${pkg_name}*.${pkg_ext}"`"
  5495.       # now try grabbing $CURDIR/pkgname.*
  5496.       [ ! -f "$PKGFILE" ] && PKGFILE="`find "$CURDIR" -maxdepth 1 -type f -name "${pkg_name}.*"`"
  5497.       # maybe try $CURDIR/pkgname-*.*
  5498.       [ ! -f "$PKGFILE" ] && PKGFILE="`find "$CURDIR" -maxdepth 1 -type f -name "${pkg_name}-*.*"`"
  5499.       # maybe try $CURDIR/pkgname_*.*
  5500.       [ ! -f "$PKGFILE" ] && PKGFILE="`find "$CURDIR" -maxdepth 1 -type f -name "${pkg_name}_*.*"`"
  5501.       # maybe try $CURDIR/pkgname*.*
  5502.       [ ! -f "$PKGFILE" ] && PKGFILE="`find "$CURDIR" -maxdepth 1 -type f -name "${pkg_name}*.*"`"
  5503.       set +x
  5504.       #This commented out code is probably no longer needed. For now keep as a reference.
  5505.       #if [ ! -f "$PKGFILE" ]; then
  5506.       #  PKGFILE_symlink="`find "$CURDIR" -maxdepth 1 -type l -name "${pkg_name}*.*"`"
  5507.       #  PKGFILE="$(readlink "$PKGFILE_symlink")"
  5508.       #  rm "$PKGFILE_symlink"
  5509.       #fi
  5510.      
  5511.       # add extension if Pkg returned an erroneous or user dir
  5512.       [ -d "$PKGFILE" ] && PKGFILE="${PKGFILE}.$pkg_ext"
  5513.  
  5514.       # if we found the package to install in CURDIR
  5515.       if [ -f "${PKGFILE}" ];then
  5516.  
  5517.         # check if we install or not
  5518.         if [ "${NO_INSTALL}" = false ];then
  5519.  
  5520.           # if a valid pkg, with files to extract
  5521.           if [ "`pkg_contents "$PKGFILE" 2>/dev/null`" != '' ];then
  5522.  
  5523.  
  5524.             #INSTALL PKG
  5525.             [ "`is_local_pkg "$PKGFILE"`" = true ] && pkg_install "${PKGFILE}"
  5526.  
  5527.  
  5528.           fi
  5529.  
  5530.         fi
  5531.  
  5532.         # if pkg was installed, or Pkg is simply downloading all deps
  5533.         if [ "`is_installed_pkg "$pkg_name_only"`" = true -o "${NO_INSTALL}" = true ];then
  5534.  
  5535.           # get the dependencies for this package
  5536.           get_deps "${pkg_name}"
  5537.  
  5538.         fi
  5539.  
  5540.       else # PKGFILE not a file
  5541.         echo "Can't find ${PKGNAME} or not a valid pkg.."
  5542.       fi
  5543.  
  5544.     else # no matches in repo found
  5545.       echo "Cannot find ${PKGNAME}.."
  5546.     fi
  5547.  
  5548.     # done with this pkg, add it to done list, will be skipped it seen again, until loop is finished
  5549.     echo "$pkg_name_only" >> $TMPDIR/PKGSDONE
  5550.   done
  5551. }
  5552.  
  5553.  
  5554. pkg_update(){                     # update installed pkgs, $1 is optional filter FUNCLIST
  5555.  
  5556.   # exit if no valid options
  5557.   #[ ! "$1" -o "$1" = "-" ] && print_usage pkg-update && exit 1
  5558.  
  5559.   # get rc file settings
  5560.   . ${PKGRC}
  5561.  
  5562.   local PKGNAME=''
  5563.   local PKGLIST=''
  5564.   local pkg_ext=$EX
  5565.   local BUILTINS=''
  5566.   local separator=''
  5567.  
  5568.   if [ "$1" != '' ];then
  5569.  
  5570.     PKGNAME=$(basename "$1")
  5571.     PKGNAME=`get_pkg_name "$PKGNAME"`
  5572.     PKGNAME_ONLY=`get_pkg_name_only "$PKGNAME"`
  5573.  
  5574.     # get installed packages
  5575.     PKGLIST="`HIDE_BUILTINS=false list_installed_pkgs "$PKGNAME_ONLY"`"
  5576.  
  5577.     #is_builtin=`is_builtin_pkg "$PKGNAME"`
  5578.  
  5579.     # dont update builtins unless -F was given
  5580.     #if [ "$is_builtin" = true -a "$HIDE_BUILTINS" = true ];then
  5581.     #  echo -e "Package ${magenta}${PKGNAME}${endcolour} is built in, not updating."
  5582.     #  #echo -e "Use `$SELF -F --pkg-update $PKGNAME` to update it anyway."
  5583.     #  exit 0
  5584.     #fi
  5585.   else
  5586.     # get installed packages
  5587.     PKGLIST="`HIDE_BUILTINS=false list_installed_pkgs`"
  5588.   fi
  5589.  
  5590.   # iterate over the list
  5591.   echo "$PKGLIST"  | grep -v ^$ | sort -u | while read installed_pkg; do
  5592.  
  5593.     [ ! "$installed_pkg" -o "$installed_pkg" = '' ] && continue
  5594.  
  5595.     # if this pkg ($installed_pkg) is not from a known repo, we cant compare its version to anything else, skip it
  5596.     #[ "`is_repo_pkg "$installed_pkg"`" = false ] && echo -e "Package ${magenta}$installed_pkg${endcolour} not found in any repos." && continue
  5597.  
  5598.     local PNAME=''
  5599.     local latest_repo_pkg=''
  5600.     local ASKREPLY='y'
  5601.     # get pkg name (without version) and version of current PKG
  5602.  
  5603.     PNAME=`get_pkg_name_only $installed_pkg`
  5604.  
  5605.     [ "$PNAME" = '' ] && continue
  5606.  
  5607.     case $DISTRO_BINARY_COMPAT in
  5608.       ubuntu|trisquel|debian|devuan)
  5609.         separator='_'
  5610.         ;;
  5611.       *)
  5612.         separator='-'
  5613.         ;;
  5614.     esac
  5615.  
  5616.     latest_repo_pkg="`grep -m1 "^${PNAME}${separator}" ${HOME}/.packages/Packages-* |cut -f1 -d'|' | head -1 | cut -f2 -d':'`"
  5617.  
  5618.     # skip if we didn't find the right package
  5619.     [ "`echo "$latest_repo_pkg" | grep "^${PNAME}${separator}"`" = '' ] && continue
  5620.  
  5621.     latest_version=`get_pkg_version "$latest_repo_pkg"`
  5622.     installed_version=`get_pkg_version "$installed_pkg"`
  5623.  
  5624.     # get latest versions
  5625.  
  5626.     # check pkg version against installed version
  5627.     if [ "$latest_version" != "" -a "$installed_version" != "" ];then
  5628.       vercmp $latest_version gt $installed_version 2>/dev/null
  5629.       RESULT=$?
  5630.  
  5631.       if [ "$RESULT" = 0 ];then #newer version available
  5632.  
  5633.         if [ "$ASK" = true ];then
  5634.           echo "Do you want to update to ${PNAME}${separator}${latest_version}? [y/N]   "
  5635.           read -n 1 ASKREPLY </dev/tty
  5636.           [ "$ASK" = true ] && echo -ne "\b\b\n"
  5637.         fi
  5638.  
  5639.         if [ "$ASK" = false -o "$ASKREPLY" = "y" ];then
  5640.           echo -e "${yellow}Update${endcolour}: ${magenta}$PNAME${endcolour} from ${bold}$installed_version${endcolour} to ${bold}$latest_version${endcolour}"
  5641.           cd "$WORKDIR"
  5642.           ASK=$ASK FORCE=$FORCE pkg_get "$latest_repo_pkg"
  5643.           cd "$CURDIR"
  5644.         fi
  5645.  
  5646.       else #280613 inform user if no update found
  5647.         echo -e "${green}Latest${endcolour}: $PNAME${separator}$installed_version"
  5648.       fi #end if vercmp XX gt YY = 0
  5649.  
  5650.     elif [ "$PNAME" = '' ];then
  5651.       error "${installed_pkg} not found."
  5652.     else #280613
  5653.       error "$installed_pkg package versions could not be compared: ${latest_version:-unknown latest} => ${installed_version:-unknown installed version}"
  5654.       #return 1
  5655.     fi #end if PKGVER != ""
  5656.   done
  5657. }
  5658.  
  5659.  
  5660. pkg_uninstall(){                  # remove an installed package ($1) FUNCLIST
  5661.  
  5662.   # quit if no valid options
  5663.   [ ! "$1" -o "$1" = "-" ] && print_usage uninstall && exit 1
  5664.  
  5665.   local PKGNAME
  5666.   local PKGNAME_ONLY
  5667.   local PKGFILE
  5668.   local pkg_ext
  5669.  
  5670.   # get pkg extension
  5671.   pkg_ext=`get_pkg_ext "$1"`
  5672.  
  5673.   #get pkg name with version, but no extension or path
  5674.   PKGNAME="$(basename "$1" .$pkg_ext)"
  5675.   PKGNAME="$(basename "$PKGNAME" .pet)"
  5676.   PKGNAME="$(basename "$PKGNAME" .deb)"
  5677.   PKGNAME="$(basename "$PKGNAME" .sfs)"
  5678.   PKGNAME="$(basename "$PKGNAME" .tar.gz)"
  5679.   PKGNAME="$(basename "$PKGNAME" .tar.xz)"
  5680.   PKGNAME="$(basename "$PKGNAME" .tgz)"
  5681.   PKGNAME="$(basename "$PKGNAME" .txz)"
  5682.   PKGNAME=`get_pkg_name "$PKGNAME"`
  5683.  
  5684.   # get pkg name only .. without versions or suffix
  5685.   PKGNAME_ONLY="`get_pkg_name_only "$PKGNAME"`"
  5686.  
  5687.   local pkg_is_builtin=`is_builtin_pkg "$PKGNAME_ONLY"`
  5688.   local is_installed=`is_installed_pkg "$PKGNAME"`
  5689.  
  5690.   # skip pkg if its a builtin
  5691.   [ "$pkg_is_builtin" = true ] && return 1
  5692.  
  5693.   local pkg_is_sfs_file="`sfs_loadr -q -i | grep -v ^sfs_loadr | grep ^$PKGNAME`"
  5694.  
  5695.   # if pkg is SFS, "uninstall" it here
  5696.   if [ "$pkg_is_sfs_file" != "" -o "$pkg_ext" = "sfs" ];then
  5697.     PKGNAME="$(sfs_loadr -q -i | grep -v ^sfs_loadr | grep ^$PKGNAME | head -1)"
  5698.     sfs_loadr -q --cli -"${CURDIR}/${PKGNAME}" 2>/dev/null
  5699.     is_installed=true # we want to continue
  5700.   fi
  5701.  
  5702.  
  5703.   if [ "$is_installed" = true -o "$FORCE" = true ];then #250713
  5704.  
  5705.     # get the list of files to be deleted, exact match
  5706.     PKGFILE="`find ${HOME}/.packages/ -maxdepth 1 -type f -name "$PKGNAME.files"`"
  5707.  
  5708.     # if no exact match, search for pkgname*
  5709.     [ ! -f "$PKGFILE" ] && PKGFILE="`find ${HOME}/.packages/ -maxdepth 1 -type f -name "${PKGNAME}*.files"`"
  5710.     [ ! -f "$PKGFILE" ] && PKGFILE="`find ${HOME}/.packages/ -maxdepth 1 -type f -name "${PKGNAME_ONLY}_*.files"`"
  5711.     [ ! -f "$PKGFILE" ] && PKGFILE="`find ${HOME}/.packages/ -maxdepth 1 -type f -name "${PKGNAME_ONLY}-*.files"`"
  5712.     [ ! -f "$PKGFILE" ] && PKGFILE="${PKGNAME}.files"
  5713.     [ ! -f "$PKGFILE" ] && PKGFILE="${PKGNAME}.sfs.files"
  5714.     [ ! -f "$PKGFILE" ] && PKGFILE="${PKGNAME}-${CP_SUFFIX}.sfs.files"
  5715.  
  5716.     #if PKG.files not found, then remove it from alien packages list
  5717.     if [ ! -f "$PKGFILE" ];then
  5718.  
  5719.       #error "'${PKGFILE}' not found.. Cleaning up.."
  5720.  
  5721.       # backup the original list of user installed pkgs
  5722.       cp ${HOME}/.packages/user-installed-packages $TMPDIR/user-installed-packages.backup
  5723.  
  5724.       # remove $PKGNAME from list of installed pkgs
  5725.       cat ${HOME}/.packages/user-installed-packages 2>/dev/null | grep -v "^${PKGNAME}" > ${HOME}/.packages/user-installed-packages.updated
  5726.  
  5727.       # if we created a new file ok
  5728.       if [ -f ${HOME}/.packages/user-installed-packages.updated ];then
  5729.         # replace the user-installed-packages file with our new one
  5730.         mv ${HOME}/.packages/user-installed-packages.updated ${HOME}/.packages/user-installed-packages 2>/dev/null
  5731.       fi
  5732.  
  5733.       # clean up user-installed-packages (remove duplicates and empty lines)
  5734.       cat ${HOME}/.packages/user-installed-packages | grep -v "^\$" | uniq > ${HOME}/.packages/user-installed-packages.clean
  5735.       mv ${HOME}/.packages/user-installed-packages.clean ${HOME}/.packages/user-installed-packages
  5736.  
  5737.       # no *.files to process, so if not forcing full uninstall, we can exit here
  5738.       [ "$FORCE" = false ] && return 1
  5739.     fi
  5740.  
  5741.     # if we are here, we have a $HOME/.packages/***.files to work with (or using --force)
  5742.  
  5743.     # get pkgs that depend on $PKGNAME
  5744.     [ "$FORCE" = false ] && dependents="`list_dependents "$PKGNAME"`" || dependents=''
  5745.  
  5746.     # if we have dependents, we should not uninstall and just exit, unless --force was given
  5747.     if [ "$dependents" != "" -a "`echo "$dependents" | grep 'not installed'`" = '' -a "$FORCE" != true ];then
  5748.  
  5749.       # inform user of dependent pkgs
  5750.       echo -e "${yellow}Warning${endcolour}: $PKGNAME_ONLY is needed by:  "
  5751.       echo -e "${magenta}$dependents${endcolour}"
  5752.       echo "Uninstall the packages above first, or use:"
  5753.       echo -e "${bold}pkg --force uninstall $PKGNAME${endcolour}."
  5754.       echo
  5755.       return 1
  5756.  
  5757.     fi
  5758.  
  5759.     # ask/inform user before uninstall
  5760.     echo -n "Uninstall the package ${PKGNAME}$QTAG:  "
  5761.     [ "$ASK" = true ] && read -n 1 CONFIRM </dev/tty || CONFIRM=y
  5762.     [ "$ASK" = true ] && echo -ne "\b\b\n"
  5763.  
  5764.     # if user answered yes, we will now uninstall the pkgs
  5765.     if [ "$CONFIRM" = "y" ];then
  5766.  
  5767.       # print new line if we didnt take any user input on tty
  5768.       [ "$ASK" != true ] && echo
  5769.  
  5770.       # execute uninstall script.
  5771.       if [ -x "${HOME}/.packages/${PKGNAME}.remove" ];then
  5772.         ${HOME}/.packages/${PKGNAME}.remove &>/dev/null
  5773.         rm -f ${HOME}/.packages/${PKGNAME}.remove &>/dev/null