#!/bin/bash
#we have DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}, for example
#DISTRO_PKGS_SPECS-puppy-wary5, which has variable PKGS_SPECS_TABLE which is a list of
#chosen packages.
#this script is for rebuilding PKGS_SPECS_TABLE, with a choice suiting you, and
#taking into account dependencies.
#110610 first version.
#120111 updated for woof2.
#121102 file DISTRO_SPECS has new variable DISTRO_DB_SUBNAME. ex: for 14.0-based slacko, DISTRO_DB_SUBNAME=slacko14

. ./DISTRO_SPECS
[ ! "$DISTRO_DB_SUBNAME" ] && DISTRO_DB_SUBNAME="$DISTRO_COMPAT_VERSION" #121102 fallback if DISTRO_DB_SUBNAME not defined in file DISTRO_SPECS.

if [ -f ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then
 CURRENT_CHOICE=DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
else
 CURRENT_CHOICE=DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}
fi
. ./DISTRO_PET_REPOS #has PACKAGELISTS_PET_ORDER

. ./PKGS_MANAGEMENT
xPKG_NAME_ALIASES="`echo "$PKG_NAME_ALIASES" | tr ' ' '\n' | grep -v '^$' | sed -e 's%^%|%' -e 's%$%|%' -e 's%,%|,|%g' -e 's%\\*%.*%g'`"
echo "$xPKG_NAME_ALIASES" > /tmp/1choosepackages_pkg_name_aliases_patterns
echo "$xPKG_NAME_ALIASES" | tr ',' '\n' > /tmp/1choosepackages_pkg_name_aliases_patterns2

XD7="Xdialog  --wrap --stdout --left --title \"1choosepackages: welcome\" --msgbox \"Welcome to the '1choosepackages' script. Ultimately, this script is planned to be an easy GUI means of selecting all packages in the PKGS_SPECS_TABLE variable (in file ${CURRENT_CHOICE}), however for now it only offers selection of the base PET packages.

The base PET packages are those used in all Puppy builds. These are in the 'noarch' and 'common' repos at ibiblio.org. Click the 'Help' button in the next dialog window if you want more explanation.

Running this script will not wreak your current PKGS_SPECS_TABLE (hopefully), it will only update your choices of what base packages you want in the build, and will modify PKGS_SPECS_TABLE accordingly -- you will be able to view all changes before committing.\" 0 90"
eval "$XD7"
[ $? -ne 0 ] && exit

#default yes choices in wary5 5.1.2...
YES_puppy_wary5="915resolution a52dec abiword afternoonstalker align alsa-lib21a alsa-utils21 apr apr-util asunder atk audiofile aufs2-util-old aufs2-util autoconf automake axel ayttm bacon bash batrachians bbc_provided bc bcrypt bin86 binutils bison boehm-gc bones burgerspace busybox bzip2 cairo cddetect cdparanoia-III cdrkit cgtkcalc chmlib cmake coreutils cosmosmash cpio cups cups_pdf curl cvs ddcprobe desk_icon_theme_jq8flat desk_icon_theme_silver_marble desk_icon_theme_smooth_color desk_icon_theme_stardust dhcpcd dialog dictd_client didiwiki dietlibc diffstat diffutils disktype dmidecode docbookx dosfstools dpkg-deb dvdauthor dvd+rwtools e2fsprogs e3_utf8 edid eject elspci embutils_cut enchant epdfview ethtool exiv2 expat faac faad2 ffconvert ffmpeg file file_sharing-curlftpfs-mpscan findutils flac flashplayer flatzebra flex floppy_format flsynclient foomatic-filters fpm2 freeglut freememapplet_tray freetype fribidi fuse gawk gcc_dev gcc_lib gccmakedep gcolor2 gdb gdbm gdmap geany getcurpos getdir gettext gettext-tools gexec gfnrename gfontsel gftp ghasher ghostscript gifsicle git glade3 glib glibc_dev glibc glibc_locales glipper gmeasures gmp gnumeric gnome-menus goffice gparted gperf gphoto2 gpptp grep groff grub grub4dos gtk+ gtkam gtk-chtheme gtkdialog2 gtkdialog3 gtklp gtk_theme_citrus_cut gtk_theme_flat_grey_rounded gtk_theme_gradient_grey gtk_theme_polished_blue gtksourceview gtrayicon guess_fs gutenprint gview gwhere gzip hardinfo helpsurfer hiawatha hicolor-icon-theme homebank hotplug2stdout hunspell hunspell-en-us id3lib ifplugd ifplugd_old imake inkscapelite inotail inotify-tools installwatch intltool iptables isomaster jasper jimtcl jwm2 jwmconfig2 ktsuss lame lcms libao libart libcap libcddb libcdio libdaemon libdc1394 libdv libdvdcss libdvdnav libdvdplay libdvdread libexif libexif-gtk libgee libgif libgl libglade2 libgnomecanvas libgnomecups libgnomeprint libgnomeprintui libgphoto2 libgsf libgtkhtml libical libid3tag libidl libidn libieee1284 libjpeg libjpeg_old libltdl libmad libmng libofx libogg libpciaccess libpng libpthread-stubs libraw1394 librsvg libsigc++ libsndfile libstdc++ libtheora libtiff libtool libusb libvorbis libvpx libxcb_base libxml2 libxscrnsaver libxslt libyahoo2 linux_firewall linux_firmware_dvb linux_headers linuxwacom m4 madplay make makebootfat man man_cut maze-rays mesa_base mesa-demos metamail mhwaveedit mingetty minixcal mirdir mktemp modem_stats module-init-tools mp mpfr mplayer_gui mplayerplug-in mplayer_codecs_basic ms-sys mscw mtpaint mtr mut2 nasm nbtscan ncurses ndiswrapper nenscript neon netpbm net_setup net-tools network_tray nicoedit normalize notecase ntfs-3g ntfsprogs ogle ogle_gui opencore-amr OpenSP openssh_client openssl orc osmo pango panoramixproto_xorg73 parted partview patch patchutils pbackup pburn pcd pciutils pcmciautils pcre pdvdrsab peasyscan perl perl_tiny perl-digest-sha1 perl-digest-sha1 perl-extutils-depends perl-extutils-pkgconfig perl-html-parser perl-html-parser perl-uri perl-xml-simple perl-xml-parser pfilesearch pfind picocom pixman pkgconfig pmetatagger pmirror pmirrorget pmusic pnethood poppler popt powerapplet_tray pplog ppp pprocess pptp precord prename procps programchooser pschedule psip psmisc psync ptiming ptooltips puppyinputdetect puppy-podcast-grabber puppyserialdetect pupradio pupx pure_ftpd pwallpaper pwsget python pzchmview readline replaceit retrovol rman rox_filer rp_pppoe_cli rsync rubix rxvt-unicode samba_client samba_mount_cifs sane-backends scale2x schroedinger scrollkeeper SDL SDL_image SDL_mixer SDL_ttf seamonkey1 seamonkey1_addon_adblock seamonkey1_addon_copyplaintext seamonkey1_addon_flashblock seamonkey1_addon_fullerscreen seamonkey1_addon_user_agent_switcher seamonkey1_addon_zombiekeys sed setserial setvol sfs-converter sgtt shared-mime-info snap2 speex sqlite squashfs-tools squashfs-tools4 ssh_gui startup-notification strace subversion sudo superscan sys-freedos sysfsutils syslinux sysprof sysvinit tar texinfo transmission truncate udev_167p unclutter unionfs_utils unzip usb-modeswitch usb-modeswitch-data util-linux util-macros vala vamps vcdimager viewnior vobcopy vorbis-tools waitmax wavplay wcpufreq wget wireless-tools wpa_supplicant wv wv_patterns_wingding wvdial x11proto x264 xarchive xclip xcompmgr xcur2png xdelta xdg_puppy xdialog xf86-input-synaptics xf86-video-ati-radeon_mach64_r128-june2008 xf86-video-geode xf86-video-i810 xf86-video-i810_old xf86-video-nv xf86-video-openchrome xf86-video-rdc xfdiff-cut xfprot xkbconfigmanager xload xlock_gui xlockmore xorg-input-wizardpen xorg-server xorg73_base_t2 xsane xserver_xorg_partialcut_t2 xserver_xvesa_t2 xsoldier xtrans xvidcore xz yaf_splash yasm ycalc you2pup zip zlib zzt2_fixup_hack_xorg73 "

YES_PETS_wary_512="afternoonstalker align axel bacon bash batrachians bbc_provided bcrypt bones burgerspace cddetect cgtkcalc coreutils cosmosmash cups_pdf ddcprobe desk_icon_theme_jq8flat desk_icon_theme_silver_marble desk_icon_theme_smooth_color desk_icon_theme_stardust dictd_client didiwiki dietlibc disktype e3_utf8 edid elspci embutils_cut ffconvert file_sharing-curlftpfs-mpscan flatzebra floppy_format flsynclient fpm2 freememapplet_tray gcolor2 gdmap getcurpos getdir gexec gfnrename gfontsel ghasher gifsicle glipper gmeasures gnome-menus gpptp grub grub4dos gtk-chtheme gtkdialog2 gtk_theme_citrus_cut gtk_theme_flat_grey_rounded gtk_theme_gradient_grey gtk_theme_polished_blue gtrayicon guess_fs gview gwhere hardinfo helpsurfer hiawatha hotplug2stdout hunspell-en-us id3lib ifplugd_old inotail installwatch jimtcl jwm2 jwmconfig2 ktsuss libjpeg_old libstdc++ linux_firewall linux_firmware_dvb makebootfat man_cut maze-rays metamail mingetty minixcal mirdir modem_stats mp mplayerplug-in mplayer_codecs_basic ms-sys mscw mut2 nbtscan nenscript net_setup panoramixproto_xorg73 partview pbackup pburn pcd pdvdrsab peasyscan pfilesearch pfind picocom pmetatagger pmirror pmirrorget pmusic pnethood pplog pprocess pptp precord prename programchooser pschedule psip psync ptiming ptooltips puppyinputdetect puppy-podcast-grabber puppyserialdetect pupradio pupx pure_ftpd pwallpaper pwsget pzchmview replaceit rox_filer rp_pppoe_cli rubix samba_client samba_mount_cifs scale2x seamonkey1 setvol sfs-converter sgtt shared-mime-info snap2 squashfs-tools squashfs-tools4 ssh_gui sudo superscan sys-freedos syslinux sysvinit transmission truncate udev_167p unclutter unionfs_utils usb-modeswitch usb-modeswitch-data viewnior vobcopy waitmax wavplay wcpufreq wvdial xarchive xclip xcur2png xdg_puppy xdialog xfdiff-cut xfprot xkbconfigmanager xlock_gui xlockmore xserver_xvesa_t2 xsoldier xz yaf_splash yasm ycalc you2pup "

YES_HOST_PKGS_wary_512="915resolution a52dec abiword alsa-lib21a alsa-utils21 apr apr-util asunder atk audiofile aufs2-util-old aufs2-util autoconf automake ayttm bc bin86 binutils bison boehm-gc busybox bzip2 cairo cdparanoia-III cdrkit chmlib cmake cpio cups curl cvs dhcpcd dialog diffstat diffutils dmidecode docbookx dosfstools dpkg-deb dvdauthor dvd+rwtools e2fsprogs eject enchant epdfview ethtool exiv2 expat faac faad2 ffmpeg file findutils flac flashplayer flex foomatic-filters freeglut freetype fribidi fuse gawk gcc_dev gcc_lib gccmakedep gdb gdbm geany gettext gettext-tools gftp ghostscript git glade3 glib glibc_dev glibc glibc_locales gmp gnumeric goffice gparted gperf gphoto2 grep groff gtk+ gtkam gtkdialog3 gtklp gtksourceview gutenprint gzip hicolor-icon-theme homebank hunspell ifplugd imake inkscapelite inotify-tools intltool iptables isomaster jasper lame lcms libao libart libcap libcddb libcdio libdaemon libdc1394 libdv libdvdcss libdvdnav libdvdplay libdvdread libexif libexif-gtk libgee libgif libgl libglade2 libgnomecanvas libgnomecups libgnomeprint libgnomeprintui libgphoto2 libgsf libgtkhtml libical libid3tag libidl libidn libieee1284 libjpeg libltdl libmad libmng libofx libogg libpciaccess libpng libpthread-stubs libraw1394 librsvg libsigc++ libsndfile libtheora libtiff libtool libusb libvorbis libvpx libxcb_base libxml2 libxscrnsaver libxslt libyahoo2 linux_headers linuxwacom m4 madplay make man mesa_base mesa-demos mhwaveedit mktemp module-init-tools mpfr mplayer_gui mtpaint mtr nasm ncurses ndiswrapper neon netpbm net-tools network_tray nicoedit normalize notecase ntfs-3g ntfsprogs ogle ogle_gui opencore-amr OpenSP openssh_client openssl orc osmo pango parted patch patchutils pciutils pcmciautils pcre perl perl_tiny perl-digest-sha1 perl-digest-sha1 perl-extutils-depends perl-extutils-pkgconfig perl-html-parser perl-html-parser perl-uri perl-xml-simple perl-xml-parser pixman pkgconfig poppler popt powerapplet_tray ppp procps psmisc python readline retrovol rman rsync rxvt-unicode sane-backends schroedinger scrollkeeper SDL SDL_image SDL_mixer SDL_ttf seamonkey1_addon_adblock seamonkey1_addon_copyplaintext seamonkey1_addon_flashblock seamonkey1_addon_fullerscreen seamonkey1_addon_user_agent_switcher seamonkey1_addon_zombiekeys sed setserial speex sqlite startup-notification strace subversion sysfsutils sysprof tar texinfo unzip util-linux util-macros vala vamps vcdimager vorbis-tools wget wireless-tools wpa_supplicant wv wv_patterns_wingding x11proto x264 xcompmgr xdelta xf86-input-synaptics xf86-video-ati-radeon_mach64_r128-june2008 xf86-video-geode xf86-video-i810 xf86-video-i810_old xf86-video-nv xf86-video-openchrome xf86-video-rdc xload xorg-input-wizardpen xorg-server xorg73_base_t2 xsane xserver_xorg_partialcut_t2 xtrans xvidcore zip zlib zzt2_fixup_hack_xorg73 "

#ask which pkg-choice-list to use...
if [ -d support/HISTORY-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then
 ARCHIVED_CHOICES="`find support/HISTORY-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} -maxdepth 1 -name 'DISTRO_PKGS_SPECS-*' | sed -e 's%^%ARCHIVE %' -e 's%$% off%'`"
 CNT=1
 echo -n "" > /tmp/1choosepackages-archchoices
 echo "$ARCHIVED_CHOICES" |
 while read ONECH
 do
  echo "$ONECH" | sed -e 's%ARCHIVE %Archive${CNT} %' >> /tmp/1choosepackages-archchoices
  CNT=`expr $CNT + 1`
 done
 ARCHIVED_CHOICES="`cat /tmp/1choosepackages-archchoices | tr '\n' ' '`"
fi
XD0="Xdialog --left --wrap --stdout --title \"1choosepackages: select input\" --radiolist \"Please choose the file that has the choice of packages for the Puppy build. Recommend choose '${CURRENT_CHOICE}'. Whatever is chosen below, file '${CURRENT_CHOICE}' will be updated and used by later scripts to build Puppy.\n\nNote, selecting an 'Archive*' entry here does not mean that you will be choosing older PET packages. These files are only lists of package names, and the actual PETs are chosen from the database files, so will be the most recent versions. Look at variable PACKAGELISTS_PET_ORDER in file DISTRO_PET_REPOS to see the PET search order, also mouse-over a package in the next dialog window to see what PET package is actually being chosen.\" 27 99 0 Current ${CURRENT_CHOICE} on Reference \"A fallback of recommended choices\" off ${ARCHIVED_CHOICES}"
RESULTS="`eval "$XD0"`"
[ $? -ne 0 ] && exit

yaf-splash -placement center -bg yellow -close never -fontsize large -text "Processing, please wait..." &
YPID=$!

#using this one...
ALLCHOICES="Current ${CURRENT_CHOICE}
Reference internal
`cat /tmp/1choosepackages-archchoices`"
resPTN="^${RESULTS} "
CHOICES_INPUT="`echo "$ALLCHOICES" | grep "$resPTN" | cut -f 2 -d ' '`"
case $CHOICES_INPUT in
 internal)
  YES_PUPS="$YES_PETS_wary_512"
  YES_HOST_PKGS="$YES_HOST_PKGS_wary_512"
 ;;
 *)
  . ./${CHOICES_INPUT} #ex: file DISTRO_PKGS_SPECS-puppy-wary5, which has variable $PKGS_SPECS_TABLE
  #only want entries like yes|915resolution||exe,dev,doc,nls (3rd field empty)...
  #YES_PUPS="`echo "$PKGS_SPECS_TABLE" | grep '^yes|[0-9a-zA-Z].*||' | cut -f 2 -d '|' | tr '\n' ' '`"
  YES_PUPS="`echo "$PKGS_SPECS_TABLE" | grep '^yes|[0-9a-zA-Z][^|]*||' | cut -f 2 -d '|' | tr '\n' ' '`"
  YES_HOST_PKGS="`echo "$PKGS_SPECS_TABLE" | grep '^yes|[0-9a-zA-Z][^|]*|[0-9a-zA-Z]' | cut -f 2 -d '|' | tr '\n' ' '`"
 ;;
esac

#w469 modify compat-distro fallback list...
if [ "$FALLBACKS_COMPAT_VERSIONS" != "" ];then
 FALLBACKS_COMPAT_VERSIONS="`echo -n "$FALLBACKS_COMPAT_VERSIONS" | grep -o "${DISTRO_COMPAT_VERSION}.*"`"
 #ex: 'koala jaunty intrepid' gets reduced to 'jaunty intrepid' if DISTRO_COMPAT_VERSION=jaunty
fi

echo "$YES_PUPS" | tr -s ' ' | sed -e 's% $%%' | tr ' ' '\n' | sed -e 's%^%|%' -e 's%$%|%' > /tmp/1choosepackages-yes-pups-ptns
sync

#Packages-puppy-common-official is repo of pet pkgs likely to be needed in all puppies...
#YESITEMS="`grep --file=/tmp/1choosepackages-yes-pups-ptns Packages-puppy-common-official | cut -f 2,10 -d '|' | sed -e 's%"%%g' -e 's%|% "%' -e 's%$%" on%'`" #'geany
#echo "$YESITEMS" > /tmp/1choosepackages-yesitems
#NOITEMS="`grep -v --file=/tmp/1choosepackages-yes-pups-ptns Packages-puppy-common-official | cut -f 2,10 -d '|' | sed -e 's%"%%g' -e 's%|% "%' -e 's%$%" off%'`" #'geany
#echo -n "" > /tmp/1choosepackages-noitems
#echo "$NOITEMS" |
#while read ONEITEM
#do
# NAME1="`echo -n "$ONEITEM" | cut -f 1 -d '|' | sed -e 's%_DEV%%' -e 's%_DOC%%' -e 's%_NLS%%'`"
# fPTN1="^${FIELD1} "
# [ "`grep "$fPTN1" /tmp/1choosepackages-yesitems`" != "" ] && continue
# echo "$ONEITEM" >> /tmp/1choosepackages-noitems
#done
#NOITEMS="`cat /tmp/1choosepackages-noitems`"
YESITEMS="`grep --file=/tmp/1choosepackages-yes-pups-ptns Packages-puppy-noarch-official Packages-puppy-common-official | cut -f 2-99 -d ':' | cut -f 2,10 -d '|' | sort --field-separator='|' --key=1,1 --unique | sed -e 's%"%%g' -e 's%|% "%' -e 's%$%" on%'`" #'geany
NOITEMS="`grep -v --file=/tmp/1choosepackages-yes-pups-ptns Packages-puppy-noarch-official Packages-puppy-common-official | cut -f 2-99 -d ':' | grep -v -E '_DEV|_DOC|_NLS' | cut -f 2,10 -d '|' | sort --field-separator='|' --key=1,1 --unique | sed -e 's%"%%g' -e 's%|% "%' -e 's%$%" off%'`" #'geany

ALLITEMS="`echo "$YESITEMS
$NOITEMS" | sort --key=1,1 --field-separator=' ' --unique`"
echo "$ALLITEMS" > /tmp/1choosepackages-allitems

#if pkg is a host pkg, insert "HOSTPACKAGE"...
echo -n "" > /tmp/1choosepackages-allitems-mod
cat /tmp/1choosepackages-allitems |
while read ONELINE
do
 oPTN=" `echo -n "$ONELINE" | cut -f 1 -d ' '` "
 if [ "`echo " ${YES_HOST_PKGS} " | grep "$oPTN"`" != "" ];then
  ONEMODIF="`echo "$ONELINE" | sed -e 's% "% "HOSTPACKAGE %'`"
  echo "$ONEMODIF" >> /tmp/1choosepackages-allitems-mod
 else
  echo "$ONELINE" >> /tmp/1choosepackages-allitems-mod
 fi
done
mv -f /tmp/1choosepackages-allitems-mod /tmp/1choosepackages-allitems
ALLITEMS="`cat /tmp/1choosepackages-allitems`"

#add deps shown on mouse-over...
echo -n "" > /tmp/1choosepackages-allitems-withhelp
echo "$ALLITEMS" |
while read ONEITEM
do
 NAME1="`echo -n "$ONEITEM" | cut -f 1 -d ' '`"
 
 #in case of multiple pets with same name...
 n0PTN="^${NAME1} "
 [ "`grep "$n0PTN" /tmp/1choosepackages-allitems-withhelp`" != "" ] && continue
 
 #INLINE_PASSED1='1choosepackages'
 GENERICNAME="$NAME1"
 
 if [ "`echo -n "$ONEITEM" | grep 'HOSTPACKAGE'`" != "" ];then
  #pkg is chosen from the host repos...
  echo "${ONEITEM} \"This package will be provided from the ${DISTRO_BINARY_COMPAT}|${DISTRO_COMPAT_VERSION} host repos.\"" >> /tmp/1choosepackages-allitems-withhelp
 else
  ##note, PACKAGELISTS_PET_ORDER is specified in DISTRO_PET_REPOS included above...
  #. ./support/inline_get_pet
  ##...returns SPEC_PETPKG, ex: SPEC_PETPKG=pet_packages-quirky|acpid-1.0.8-20100603.pet
  ##...returns PKGLIST, name of repo, ex: Packages-puppy-common-official
  
  #120111 woof2 has findpkgs...
  support/findpkgs
  #...returns file status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
  # each line is full db entry for pkg, prefixed with :generic-name:, pet/compat and repo-filename.
  # ex: :a52dec:|compat|Packages-puppy-wary5-official|a52dec-0.7.4-w5|a52dec|0.7.4-w5||BuildingBlock|68K||a52dec-0.7.4-w5.pet||A free ATSC A52 stream decoder|puppy|wary5||
  #PETPKG="`echo -n "$SPEC_PETPKG" | cut -f 2 -d '|'`"
  gnPTN=":${GENERICNAME}:"
  #***NOTE*** this can find more than one pkg... *FIX*
  fndPKG="`grep "$gnPTN" status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} | head -n 1`"
  PETPKG="`echo -n "$fndPKG" | cut -f 11 -d '|'`"
  PKGLIST="`echo -n "$fndPKG" | cut -f 3 -d '|'`"
 
  #n1PTN="|${PETPKG}|"
  #HOST_DISTRO="`grep "$n1PTN" Packages-puppy-common-official | tail -n 1 | cut -f 11 -d '|'`"
  #HOST_RELEASE="`grep "$n1PTN" Packages-puppy-common-official | tail -n 1 | cut -f 12 -d '|'`"
  #DEPS="`grep "$n1PTN" Packages-puppy-common-official | tail -n 1 | cut -f 9 -d '|'`"
  HOST_DISTRO="`echo -n "$fndPKG" | cut -f 14 -d '|'`"
  HOST_RELEASE="`echo -n "$fndPKG" | cut -f 15 -d '|'`"
  DEPS="`echo -n "$fndPKG" | cut -f 12 -d '|'`"
  
  HOSTCOMP="${HOST_DISTRO}|${HOST_RELEASE}"
  [ "$HOST_DISTRO" = "" ] && HOSTCOMP="NOARCH (no compiled excutables, or distro-independent such as static)"
  [ "$DEPS" = "" ] && DEPS="none-specified"
  echo "${ONEITEM} \"DEPENDENCIES:${DEPS} PET:${PETPKG} REPO:${PKGLIST} PACKAGE-COMPILED:${HOSTCOMP}\"" >> /tmp/1choosepackages-allitems-withhelp
 fi
 
done

ALLITEMSWITHHELP="`cat /tmp/1choosepackages-allitems-withhelp | tr '\n' ' '`"

kill $YPID

XD1="Xdialog --wrap --stdout --left --separator \"|\" --title \"1choosepackages: choose common PET packages\" --item-help --help \"The common PET packages are those that will normally be in all Puppy\n
builds. You may want to disable some of these, for example\n
'ghostscript' when the package is available from the host distro\n
(note, our ghostscript PET is very small). PETs that do not have\n
any compiled binary executables (scripts or data files only) are\n
labelled 'NOARCH'. In the case of compiled executables, the common\n
repo probably has a PET that will work, but in some cases you may\n
have to recompile or use a host-distro package.\n\n
Note1: 'PACKAGE-COMPILED' identifies which distro and release\n
version the binary executables were compiled in.\n\n
Note2: Our practice has been often not to give any dependencies\n
when we know that Puppy will definitely have all the required\n
packages. Also, some package-creators have been lax with regard\n
to specifying deps, so take the deps field as a guide only.\n\n
Note3: Important: if a common PET package is unticked, but if\n
PKGS_SPECS_TABLE has an entry for same package in the host repo\n
(format: no|ghostscript|ghostscript|exe,dev,doc,nls)\n
then the host package will be set to 'yes'. And vice-versa.\n\n
Warning: some packages may be marked as NOARCH, but actually do\n
have compiled binaries. The opposite may also be true. Note to\n
package creators: specify fields 11&12 as |puppy|| for compiled\n
binaries that will suit all builds, for true NOARCH specify |||.\" --checklist \"These are the base packages that are used in all Puppy builds.\n\n If ticked, then a PET will be used chosen from the database files specified by this variable in file DISTRO_PET_REPOS:\n PACKAGELISTS_PET_ORDER=${PACKAGELISTS_PET_ORDER}\n..mouseover packages below to see what PET will be chosen, plus other details.\n\nIf unticked, you may optionally create an entry in PKGS_SPECS_TABLE to use an equivalent package from the host repository. 'HOSTPACKAGE' marks packages that already have an alternate entry (so you would only tick those boxes if you wanted to use a PET from the 'noarch' or 'common' repos instead).\" 49 90 0 ${ALLITEMSWITHHELP}"
RETVALS2="`eval "$XD1"`"
[ $? -ne 0 ] && exit

yaf-splash -placement center -bg yellow -close never -fontsize large -text "Processing, please wait..." &
YPID=$!

#echo "$RETVALS2"
YESCOMMONNAMES="`echo "$RETVALS2" | tr '|' '\n'`" #generic name on each line.
#echo "YESCOMMONNAMES=$YESCOMMONNAMES" #TEST
xYESCOMMONNAMES="`echo "$YESCOMMONNAMES" | sed -e 's%^%|%' -e 's%$%|%'`"
echo "$xYESCOMMONNAMES" > /tmp/1choosepackages-yescommonnames-enc

# CURRENT_CHOICE=DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
#CURRENT_CHOICE file got included above, so we have PKGS_SPECS_TABLE...
#reorganise PKGS_SPECS_TABLE move comments to end of next line...
COMMENTLINE=""
echo -n "" > /tmp/1choosepackages-pkgs-specs-table
if [ "$PKGS_SPECS_TABLE" ];then
 echo "$PKGS_SPECS_TABLE" |
 while read ONELINE
 do
  [ "`echo -n "$ONELINE" | grep -E '^#|^yes|^no'`" = "" ] && continue #screen out any invalid lines.
  if [ "`echo -n "$ONELINE" | grep '^#'`" != "" ];then
   COMMENTLINE="${COMMENTLINE} ${ONELINE}"
  else
   ONELINE="`echo -n "$ONELINE" | sed -e 's%|$%%'`" #get rid of any trailing |
   echo "${ONELINE}|${COMMENTLINE}" >> /tmp/1choosepackages-pkgs-specs-table
   COMMENTLINE=""
  fi
 done
 PKGS_SPECS_TABLE="`cat /tmp/1choosepackages-pkgs-specs-table`"
fi

#set yes/no in PKGS_SPECS_TABLE...
HOSTENTRIESMISSING="" ; HOSTENTRIESTURNEDON=""
ALLCOMMONNAMES="`cat /tmp/1choosepackages-allitems-withhelp | cut -f 1 -d ' ' | tr '\n' ' '`"
for ONECOMMON in $ALLCOMMONNAMES
do
 ocPTN="^${ONECOMMON}$"
 ocPTN1="|${ONECOMMON}|[0-9a-zA-Z]"
 FNDENTRY1="`grep "$ocPTN1" /tmp/1choosepackages-pkgs-specs-table`"
 ocPTN2="|${ONECOMMON}||"
 FNDENTRY2="`grep "$ocPTN2" /tmp/1choosepackages-pkgs-specs-table`"
 if [ "`echo "$YESCOMMONNAMES" | grep "$ocPTN"`" = "" ];then
  #not chosen
  noPTN="s%^yes|${ONECOMMON}||%no|${ONECOMMON}||%"
  sed -i -e "$noPTN" /tmp/1choosepackages-pkgs-specs-table
  if [ "$FNDENTRY1" ];then
   CNT=1
   echo "$FNDENTRY1" |
   while read ONEENTRY
   do
    sed -i -e "/${ONEENTRY}/d" /tmp/1choosepackages-pkgs-specs-table
    if [ $CNT -eq 1 ];then
     #warn if this is being turned on...
     if [ "`echo "$ONEENTRY" | grep "^no|"`" != "" ];then
      HOSTENTRIESTURNEDON="${HOSTENTRIESTURNEDON}
${ONEENTRY}"
     fi
     echo "$ONEENTRY" | sed -e 's%^no|%yes|%' >> /tmp/1choosepackages-pkgs-specs-table #yes 1st found only.
    else
     echo "$ONEENTRY" | sed -e 's%^yes|%no|%' >> /tmp/1choosepackages-pkgs-specs-table
    fi
    sync
    CNT=`expr $CNT + 1`
   done
  else
   FNDPTNS=""
   #find out if an equivalent pkg name already chosen...
   for ONEPTN in `cat /tmp/1choosepackages_pkg_name_aliases_patterns2`
   do
    if [ "`echo "|${ONECOMMON}|" | grep "$ONEPTN"`" ];then
     FNDPTNS="`grep "$ONEPTN" /tmp/1choosepackages_pkg_name_aliases_patterns | tr ',' '\n'`"
     break
    fi
   done
   if [ "$FNDPTNS" ];then
    echo "$FNDPTNS" > /tmp/1choosepackages-fndptns
    [ "`grep --file=/tmp/1choosepackages-fndptns /tmp/1choosepackages-yescommonnames-enc`" = "" ] && HOSTENTRIESMISSING="$HOSTENTRIESMISSING $ONECOMMON" #i/p file has format |915resolution| each line.
   else
    HOSTENTRIESMISSING="$HOSTENTRIESMISSING $ONECOMMON"
   fi
  fi
 else
  #chosen
  if [ "$FNDENTRY2" ];then
   yesPTN="s%^no|${ONECOMMON}||%yes|${ONECOMMON}||%"
   sed -i -e "$yesPTN" /tmp/1choosepackages-pkgs-specs-table
  else
   echo "yes|${ONECOMMON}||exe,dev,doc,nls| #common package" >> /tmp/1choosepackages-pkgs-specs-table
  fi
  if [ "$FNDENTRY1" ];then
   echo "$FNDENTRY1" |
   while read ONEENTRY
   do
    sed -i -e "/${ONEENTRY}/d" /tmp/1choosepackages-pkgs-specs-table
    echo "$ONEENTRY" | sed -e 's%^yes|%no|%' >> /tmp/1choosepackages-pkgs-specs-table
    sync
   done
  fi
 fi
done

PKGS_SPECS_TABLE="`cat /tmp/1choosepackages-pkgs-specs-table | sort --key=2 --field-separator='|'`"
echo "$PKGS_SPECS_TABLE" > /tmp/1choosepackages-pkgs-specs-table
defaulttexteditor /tmp/1choosepackages-pkgs-specs-table &
ZPID=$!
sleep 1

kill $YPID

##110609 find missing deps...
##/tmp/1choosepackages-allitems-withhelp lists all common pkgs, with deps.
#for ONECOMM in $YESCOMMONNAMES #ticked common pkgs, each line ex: 915resolution
#do
# [ "$YESCOMMONNAMES" = "" ] && continue
# yc1PTN="^${ONECOMM} "
# ONEDEPS="`grep "$yc1PTN" /tmp/1choosepackages-allitems-withhelp | grep -o 'DEPENDENCIES:[^ ]*' | sed -e 's%DEPENDENCIES:%%'`"
# for ONEDEP in $ONEDEPS
# do
# done
# 
# ycPTN="^${ONECOMM}|"
# grep "^yes|" /tmp/1choosepackages-pkgs-specs-table | grep
# 
#done


#checkbox not ticked, so common PET not used, these alternate host entries were previously off, now on...
if [ "$HOSTENTRIESTURNEDON" ];then
 XD5="Xdialog  --wrap --stdout --left --title \"1choosepackages: host pkgs turned on\" --msgbox \"These packages were not chosen in the PET common repository (checkboxes not ticked), however variable PKGS_SPECS_TABLE has alternative entries for equivalent packages from the host repos. These were previously turned 'off' and they are now 'on':
 
${HOSTENTRIESTURNEDON}
 
This is for your information. If you don't want any of them to be turned on, remove those entries from PKGS_SPECS_TABLE (yes, delete them, so they won't get turned on again next time that you run this script) -- see separate window, and save changes.\" 0 90"
 eval "$XD5"
fi

echo "${HOSTENTRIESMISSING}" > /tmp/1choosepackages-common-entries-missing
if [ "$HOSTENTRIESMISSING" ];then
 XD3="Xdialog  --wrap --stdout --left --title \"1choosepackages: maybe missing common packages\" --msgbox \"These packages were not chosen in the PET common repository (checkboxes not ticked), nor are there entries in PKGS_SPECS_TABLE to use an alternate host package:
 
${HOSTENTRIESMISSING}
 
Just bringing this to your attention. If you think that any of these should be in your Puppy build, either run the 1choosepackages script again and tick the checkbox, or make an entry in PKGS_SPECS_TABLE variable in file ${CURRENT_CHOICE} to use an equivalent package from the host repos.
PKGS_SPECS_TABLE is currently being displayed in a separate window. You can edit that directly now, inserting entries, or it can be done later -- write down any names from above list that you will want to insert later.
Note, the above list has been written to /tmp/1choosepackages-common-entries-missing\" 0 90"
 eval "$XD3"
fi

#to do, save...
DATENOW="`date +%Y%m%d%H%M`"
XD4="Xdialog  --wrap --stdout --left --title \"1choosepackages: save\" --yesno \"The updated PKGS_SPECS_TABLE variable is displayed in a separate window. If it looks alright, click the 'Yes' button to save it to file '${CURRENT_CHOICE}'.

Note, if you click 'Yes', the original file '${CURRENT_CHOICE}' will be archived as support/HISTORY-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}/${CURRENT_CHOICE}-VERSION${DISTRO_VERSION}-DATE${DATENOW}

IMPORTANT: If you have made any changes to the separate window displaying PKGS_SPECS_TABLE, please save and close it now, before clicking 'Yes' here...\" 0 100"
eval "$XD4"
RETSTATUS=$?
kill $ZPID

if [ $RETSTATUS -eq 0 ];then

 PKGS_SPECS_TABLE="`cat /tmp/1choosepackages-pkgs-specs-table`"
 
 #archive...
 mkdir -p support/HISTORY-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}/
 cp -f ${CURRENT_CHOICE} support/HISTORY-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}/${CURRENT_CHOICE}-VERSION${DISTRO_VERSION}-DATE${DATENOW}
 
 echo "#fallbacks when looking for pet pkgs (space-separated list)...
FALLBACKS_COMPAT_VERSIONS='${FALLBACKS_COMPAT_VERSIONS}'

#PKGS_SPECS_TABLE table format:
#will pkg be in puppy-build.
#    Generic name for pkg. Note: PET packages, if exist, use this name.
#            Comma-separated list of compatible-distro pkg(s). '-' prefix, exclude.
#            Must be exact name-only of pkg, else '*' on end is wildcard to search full name.
#            Empty field, then use PET pkg.
#                                    How the package will get split up in woof (optional redirection '>' operator).
#                                    Missing field, it goes into exe. Can also redirect >null, means dump it.
#yes|abiword|iceword,iceword-plugins|exe,dev,doc,nls

#example showing wildcard. finds all full pkg names with 'gcc-4.3*',
#but, exclude any 'gcc-4.3-doc*' matches...
#yes|gcc|gcc,gcc-4.3*,-gcc-4.3-doc*|exe,dev,doc,nls

#101013 have introduced a basic mechanism to choose different pkg for old or new kernel.
#both pkgs need to have 'yes' below, see example 'aufs2-util' and 'aufs2-util-old'
#variable PKG_PREFER_OLDER in file PKGS_MANAGEMENT and 3builddistro will choose one of them.

PKGS_SPECS_TABLE='${PKGS_SPECS_TABLE}'" > ${CURRENT_CHOICE}
fi

echo "...finished"
###END###
