#!/bin/sh
#(c) Copyright 2009 Barry Kauler.
#100331 bad hack for t2 8.0rc/8.0rcXorg7.3 variants. 100412 removed.
#100617 add support for slackware .txz pkgs.
#100622 fixed control panel.
#100622 hack for pet search.
#100730 if subdir field not specified in pet.specs, use pet_packages-${DISTRO_COMPAT_VERSION}.
#100801 some common code extracted to support/inline_get_pet,inline_get_compat.
#100911 fix so will download more kernel pets.
#110523 Iguleder: support Scientific Linux rpm pkgs.
#110607 replace 'quirky' with 'common' repo.
#110615 support mageia
#110616 fix path for downloading kernel pets.
#110724 optional execute support/1buildversiondeps. 110817 removed.
#110725 handle exception host folder pet_packages-drake
#110819 support/findpkgs is new script to find all pkgs to be used in Puppy build.
#110823 moved kernel pet download code up, choose which kernel, fix control panel.
#111121 noryb009: improve arch linux support, patched: 0setup, 1download, 2createpackages, 3builddistro, DISTRO_COMPAT_REPOS-arch, support/arch/runDotInstalls
#111127 no longer check if host has 'printcols', 'vercmp', it is done in 'merge2out' script.
#120501 support arm build.
#120515 build from "gentoo" binary tarballs (refer support/gentoo).
#120719 support raspbian.
#120812 db category now allows optional subcategory (for which an icons exists in /usr/local/lib/X11/mini-icons).
#121102 file DISTRO_SPECS has new variable DISTRO_DB_SUBNAME. ex: for 14.0-based slacko, DISTRO_DB_SUBNAME=slacko14
#121102 pet_packages-${DISTRO_COMPAT_VERSION} (or pet_packages-${DISTRO_FILE_PREFIX}) is now pet_packages-${DISTRO_DB_SUBNAME.
#130306 arch linux: gz now xz. maybe only 'DISTRO_COMPAT_REPOS-arch'.
#131209 Packages-puppy- files renamed to Packages-pet-
#141116 handle buildroot, also general case.
#141123 extract fallback compat path from the db-file, ex: Packages-buildroot-aug2014-official
#150419 added devuan support. refer: dimkr: https://github.com/puppylinux-woof-CE/woof-CE/pull/528/files
#151103 support build with t2 pkgs in non-t2 build. (see also merge2out, 0setup, 2createpackages, support/findpkgs)
#160426 t2 alternative-compat pkgs for ubuntu, have "april" path in db, fix url.
#160823 changes for 'arm' arch. 160824
#161003 file 'build-choices' now has BUILD_CHOICE_BOARD.
#161103 BOOT_BOARD=pc when a PC. 161114 fix.
#161227 change wget spider timeout from 20 to 30.
#170825 checking ibiblio pets, wget needs  --no-check-certificate

export LANG=C #faster.

. ./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_COMPAT_REPOS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then
. ./DISTRO_COMPAT_REPOS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
else
. ./DISTRO_COMPAT_REPOS-${DISTRO_BINARY_COMPAT} #130306
fi
if [ -f ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then #w478
. ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} #has FALLBACKS_COMPAT_VERSIONS
 DPSFILE="DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}"
else
. ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT} #has FALLBACKS_COMPAT_VERSIONS
 DPSFILE="DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}"
fi
. ./DISTRO_PET_REPOS #has PACKAGELISTS_PET_ORDER

#161003
BUILD_CHOICE_BOARD='' #161003 values: raspi, odroidx, anything else for pc.
if [ -f build-choices ];then
. ./build-choices
fi
case "$BUILD_CHOICE_BOARD" in #in file build-choices.
 raspi) SDFLAG='s'; BOOT_BOARD='raspi' ;;
 odroidx) SDFLAG='s'; BOOT_BOARD='odroidx' ;;
 *) SDFLAG=''; BOOT_BOARD='pc' ;; #building for a pc.
esac
#if [ "$SDFLAG" ];then
 echo "WARNING: building for board: ${BOOT_BOARD}"
 #160928 filter pkg selection based on board type...
 xBOOT_BOARD="#$(echo -n "$BOOT_BOARD" | tr '[a-z]' '[A-Z]')" #ex: #ODROIDX
 xBOARDS='#RASPI #ODROIDX #MELE #PC'
 cp -f ${DPSFILE} /tmp/DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}COPY
 #also filter DISTRO_SPECS...
 cp -f DISTRO_SPECS /tmp/DISTRO_SPECSCOPY
 for eachBOARD in $xBOARDS
 do
  [ "$eachBOARD" == "$xBOOT_BOARD" ] && continue
  sed -i "/${eachBOARD}/d" /tmp/DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}COPY
  sed -i "/${eachBOARD}/d" /tmp/DISTRO_SPECSCOPY
 done
 echo "PKGS_SPECS_TABLE has been filtered for board '${xBOOT_BOARD}'"
 . /tmp/DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}COPY
 echo "File DISTRO_SPECS has been filtered for board '${xBOOT_BOARD}'"
 . /tmp/DISTRO_SPECSCOPY
#fi

#the files that have compatible-distro pkg docs (these were downloaded by 0setup)...
PKGLISTS_COMPAT="`echo "$PKG_DOCS_DISTRO_COMPAT" | tr ' ' '\n' | cut -f 3 -d '|' | tr '\n' ' '`" #see file DISTRO_PKGS_SPECS-ubuntu
#...format ex: 'Packages-ubuntu-intrepid-main packages-ubuntu-intrepid-universe'

#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

#w469 refer to file DISTRO_PET_REPOS...
PACKAGELISTS_PET_PRIMARY="`echo -n "$PACKAGELISTS_PET_ORDER" | cut -f 1 -d ' '`"
PACKAGELISTS_PET_SECOND="`echo -n "$PACKAGELISTS_PET_ORDER" | cut -f 2 -d ' '`" #100911
if [ "$DISTRO_TARGETARCH" = "armv7" -o "$DISTRO_TARGETARCH" = "arm64" ];then #120501 160823
 PACKAGELISTS_PET_2NDARCH="`echo -n "$PACKAGELISTS_PET_ORDER" | tr ' ' '\n' | grep -v '\-noarch' | tr '\n' ' ' | cut -f 2 -d ' '`"
 [ "$PACKAGELISTS_PET_2NDARCH" = "$PACKAGELISTS_PET_PRIMARY" ] && PACKAGELISTS_PET_2NDARCH=''
else
 PACKAGELISTS_PET_2NDARCH='Packages-pet-common-official'
fi

#remove comments from PKGS_SPECS_TABLE... make sure '|' on end... get rid of old |+udev,+whatever on end...
PKGS_SPECS_TABLE="`echo "$PKGS_SPECS_TABLE" | grep -v '^#' | grep -v '^$' | tr '\t' ' ' | tr -s ' ' | tr '+' '&' | sed -e 's%|&.*%%' | tr '&' '+' | sed -e 's% #.*%%' -e 's% $%%' -e 's%$%|%' -e 's%||$%|%'`"

#this code is also in 2createpackages
BINARIES='' #141116
[ "$DISTRO_BINARY_COMPAT" = "t2" ] && BINARIES="bz2" #download to packages-bz2-${DISTRO_COMPAT_VERSION}.
[ "$DISTRO_BINARY_COMPAT" = "slackware" ] && BINARIES="tgz_txz" #100617 download to packages-tgz_txz-${DISTRO_COMPAT_VERSION}.
[ "$DISTRO_BINARY_COMPAT" = "debian" ] && BINARIES="deb" #download to packages-deb-${DISTRO_COMPAT_VERSION}.
[ "$DISTRO_BINARY_COMPAT" = "arch" ] && BINARIES="tar_xz" #download to packages-tar_xz-${DISTRO_COMPAT_VERSION}. 130306
[ "$DISTRO_BINARY_COMPAT" = "puppy" ] && BINARIES="pet" #w018 built entirely from pet pkgs.
[ "$DISTRO_BINARY_COMPAT" = "scientific" ] && BINARIES="rpm" #110523 Iguleder: download to packages-rpm-${DISTRO_COMPAT_VERSION}.
[ "$DISTRO_BINARY_COMPAT" = "mageia" ] && BINARIES="rpm" #110615
[ "$DISTRO_BINARY_COMPAT" = "gentoo" ] && BINARIES="gentoo" #120515 download to packages-gentoo-gap6
[ "$DISTRO_BINARY_COMPAT" = "raspbian" ] && BINARIES="deb_raspbian" #download to packages-deb_raspbian-${DISTRO_COMPAT_VERSION}.
[ "$DISTRO_BINARY_COMPAT" = "ubuntu" ] && BINARIES="deb" #141116 download to packages-deb-${DISTRO_COMPAT_VERSION}.

#141116 add a general handler...
[ "$BINARIES" == "" ] && BINARIES="$DISTRO_BINARY_COMPAT" #ex: buildroot

BINARIES="${BINARIES}-${DISTRO_COMPAT_VERSION}" #w478 141116 ex: buildroot-aug2014

mkdir -p packages-${BINARIES}
mkdir -p packages-pet

#find out what pet repos are working...
echo "Finding what PET repos are working..."
PET_URLS=""
for ONE_PET_REPO in $PET_REPOS
do
 ONE_PET_SITE="`echo -n "$ONE_PET_REPO" | cut -f 1 -d '|'`"
 ONE_PET_URL="`echo -n "$ONE_PET_REPO" | cut -f 2 -d '|'`"
 #w013 changed timeout from 20 to 30 seconds...
 #if wget -t 2 -T 20 --waitretry=20 --spider -S $ONE_PET_SITE -o /dev/stdout | grep '200 OK'
 if [ "`wget --no-check-certificate -t 2 -T 30 --waitretry=20 --spider -S $ONE_PET_SITE -o /dev/stdout 2>/dev/null | grep '200 OK'`" != "" ];then
  echo "Okay: $ONE_PET_URL"
  PET_URLS="$PET_URLS$ONE_PET_URL " #success.
 fi
done
if [ "$PET_URLS" = "" ];then
 echo "Failure. Is Internet working?"
 echo -n "Press ENTER key to quit script: "
 read yesgetout
 exit
fi

echo "Finding what $BINARIES repos are working..."
BIN_URLS=""
for ONE_BIN_REPO in $REPOS_DISTRO_COMPAT #from DISTRO_PKGS_SPECS-$DISTRO_BINARY_COMPAT.
do
 ONE_BIN_SITE="`echo -n "$ONE_BIN_REPO" | cut -f 1 -d '|'`"
 ONE_BIN_URL="`echo -n "$ONE_BIN_REPO" | cut -f 2 -d '|'`"
 #w013 ditto...
 if [ "`wget -t 2 -T 30 --waitretry=20 --spider -S $ONE_BIN_SITE -o /dev/stdout 2>/dev/null | grep '200 OK'`" != "" ];then
  echo "Okay: $ONE_BIN_URL"
  BIN_URLS="$BIN_URLS$ONE_BIN_URL " #success.
 fi
done
if [ "$BIN_URLS" = "" ];then
 echo "Failure. Is Internet working?"
 echo -n "Press ENTER key to quit script: "
 read yesgetout
 exit
fi


rm -f DOWNLOAD-FAILS-PET 2>/dev/null
rm -f DOWNLOAD-FAILS-${BINARIES} 2>/dev/null
rm -f DOWNLOAD-ERRORS

#start the gui controlpanel...
echo 'Run' > /tmp/1download-status-request
./support/1download_controlpanel &

######################
if [ "$DISTRO_KERNEL_PET" != "" ];then #160824 if empty, kernel is inside sd-skeleton image. ##############
#110823 moved downloading kernels PETs up here.
#download all the linux kernel pkgs from pet repo...
echo
echo "Downloading Linux kernel packages..."
for PKGLISTFILE in $PACKAGELISTS_PET_PRIMARY $PACKAGELISTS_PET_2NDARCH #$PACKAGELISTS_PET_SECOND #$PACKAGELISTS_PET_THIRD #110616
do
 SUBDIRTRAIL="`echo -n "$PKGLISTFILE" | cut -f 3 -d '-'`" #ex: common
 SPEC_LINUXKERNELS="`cat $PKGLISTFILE | grep '^linux_kernel\-' | cut -f 2-99 -d ':' | cut -f 7,8 -d '|' | tr '\n' ' '`" 

 for SPEC_ONEKERNEL in $SPEC_LINUXKERNELS
 do
  ONEKERNEL="`echo -n "$SPEC_ONEKERNEL" | cut -f 2 -d '|'`"
  ONEKERNEL="`basename $ONEKERNEL .pet`"
  PATHKERNEL="`echo -n "$SPEC_ONEKERNEL" | cut -f 1 -d '|'`"
  
  [ "$PATHKERNEL" = "" ] && PATHKERNEL="pet_packages-${SUBDIRTRAIL}" #110616
  
  SUCCESS="no"
  [ -f packages-pet/${ONEKERNEL}.pet ] && continue
  for ONE_PET_URL in $PET_URLS
  do
   wget --spider --tries=1 --timeout=20 ${ONE_PET_URL}/${PATHKERNEL}/${ONEKERNEL}.pet
   [ $? -ne 0 ] && continue
   cd packages-pet
   wget ${ONE_PET_URL}/${PATHKERNEL}/${ONEKERNEL}.pet
   sync
   #check file downloaded correctly...
   if [ -f ${ONEKERNEL}.pet ];then
    cp -f ${ONEKERNEL}.pet tempfileonly.pet
    pet2tgz tempfileonly.pet
    [ $? -eq 0 ] && SUCCESS='yes'
    rm -f tempfileonly.pet 2>/dev/null
    rm -f tempfileonly.tar.gz 2>/dev/null
    [ "$SUCCESS" = "no" ] && rm -f ${ONEKERNEL}.pet
   fi
   cd ..
  
   #check controlpanel, whether need to pause, resume or quit...
   CP_STATUS_REQ="`cat /tmp/1download-status-request`" #Run, Pause, Resume, Quit
   pausecnt=0; PIDCP=0
   while [ "$CP_STATUS_REQ" = "Pause" ];do
    #echo "Status: paused ${pausecnt}seconds"
    if [ $PIDCP -eq 0 ];then
     yaf-splash -close never -placement top -bg orange -text "1download script is currently in paused state" &
     PIDCP=$!
    fi
    sleep 5
    pausecnt=`expr $pausecnt + 5`
    CP_STATUS_REQ="`cat /tmp/1download-status-request`"
   done
   [ $PIDCP -ne 0 ] && kill $PIDCP
   if [ "$CP_STATUS_REQ" = "Quit" ];then
    sync
    kill `cat /tmp/1download_controlpanel_pid` #1download_controlpanel
    rm -f /tmp/1download_controlpanel_pid
    echo
    echo "1download aborted."
    echo -n "Press ENTER key to quit script: "
    read yesgetout
    exit
   fi
   [ "$SUCCESS" = "yes" ] && continue 2
  done
  echo "Failed to download ${ONEKERNEL}.pet."
 done
done

####choose kernel####
KERNELPKG="$DISTRO_KERNEL_PET"
if [ -f packages-pet/$DISTRO_KERNEL_PET ];then
 KMSG="This package exists in directory packages-pet, so ok. But you can change
it now if you wish."
 KOK=''
else
 KMSG="This package does not exist in directory packages-pet, so you cannot use it.
You must choose a linux kernel PET package from those available."
 KOK='no'
fi
echo
echo "The kernel to be used is specified in variable DISTRO_KERNEL_PET in file"
echo "DISTRO_SPECS. Currently DISTRO_KERNEL_PET=${DISTRO_KERNEL_PET}"
echo "$KMSG"
if [ "$KOK" = "" ];then
 echo -n "Press ENTER only to use this kernel, press any other char to change: "
 read KOK
fi
if [ "$KOK" ];then
 CNTLOOP=1
 AVAILKERNELS="`ls -1 packages-pet/linux_kernel-*.pet`"
 [ "$AVAILKERNELS" = "" ] && echo 'No linux_kernel pkgs avail in packages-pet. Press CTRL-C.'
 for ONEKERNEL in $AVAILKERNELS
 do
  echo "$CNTLOOP  $ONEKERNEL"
  CNTLOOP=`expr $CNTLOOP + 1`
 done
 echo
 echo -n 'Type number for kernel you want to use [1]: '
 read numkernel
 [ "$numkernel" = "" ] && numkernel=1
 KERNELPKG="`echo "$AVAILKERNELS" | head -n $numkernel | tail -n 1 | rev | cut -f 1 -d '/' | rev`"
fi
if [ "$KERNELPKG" != "$DISTRO_KERNEL_PET" ];then
 dkpPATTERN="s%^DISTRO_KERNEL_PET.*%DISTRO_KERNEL_PET='${KERNELPKG}'%"
 sed -i -e "$dkpPATTERN" ./DISTRO_SPECS
 echo "...DISTRO_SPECS now set to use $KERNELPKG"
fi
fi #160824 ##############

##################################
#120501 support arm build, download sd images...
if [ "$DISTRO_TARGETARCH" = "armv7" -o "$DISTRO_TARGETARCH" = "arm64" ];then #160823
 mkdir -p sd-skeleton-images
 echo
 echo 'Please wait, probing for online SD skeleton image files...'
 wget -4 -t 2 -T 20 --waitretry=20 --spider --recursive --level=1 --accept '*.xz' -S http://distro.ibiblio.org/quirky/quirky6/${DISTRO_TARGETARCH}/sd-skeleton-images/ 2>&1 | grep '\.xz$' | rev | cut -f 1 -d '/' | rev > /tmp/1download-sd-images-probe #160823
 sync
 if [ ! -s /tmp/1download-sd-images-probe ];then
  echo "Sorry, could not access any SD images at http://distro.ibiblio.org/quirky/quirky6/${DISTRO_TARGETARCH}/sd-skeleton-images/" #160823
  echo 'Press ENTER to continue script regardless: '
  read keepgoing
  [ "$keepgoing" != "" ] && exit
 else
 
  #ask which sd-image to download...
  ALREADYDOWN="`ls -1 sd-skeleton-images | grep 'xz$'`"
  [ "$ALREADYDOWN" = "" ] && ALREADYDOWN='none'
  SDIMAGEBUTTONS=''
  for ONESDIMAGE in `cat /tmp/1download-sd-images-probe | sort -u | tr '\n' ' '`
  do
   #SDIMAGENAME="`basename $ONESDIMAGE .xz`"
   [ -f sd-skeleton-images/$ONESDIMAGE ] && continue #already downloaded.
   SDIMAGEBUTTONS="${SDIMAGEBUTTONS}<radiobutton><label>${ONESDIMAGE}</label><variable>RADIO_URL_${ONESDIMAGE}</variable></radiobutton>"
  done
  if [ "$SDIMAGEBUTTONS" != "" ];then
   export SDIMAGE_DIALOG="<window title=\"SD Image Downloader: choose file\" icon-name=\"gtk-about\" window_position=\"1\">
   <vbox>
    <text><label>These are already downloaded:</label></text>
    <text><label>\"${ALREADYDOWN}\"</label></text>
    <text><label>Please choose an SD-image file that you would like to download (Click Cancel button to not download any):</label></text>
    <frame>
     ${SDIMAGEBUTTONS}
    </frame>
    <hbox>
     <button ok></button>
     <button cancel></button>
    </hbox>
   </vbox>
   </window>"
   while [ 1 ];do
    RETPARAMS="`gtkdialog --program=SDIMAGE_DIALOG`"
    SDIMAGE_FILE=''
    [ "`echo "$RETPARAMS" | grep '^EXIT' | grep 'OK'`" != "" ] && SDIMAGE_FILE="`echo "$RETPARAMS" | grep 'RADIO_URL_' | grep '"true"' | cut -f 1 -d '=' | cut -f 3-9 -d '_'`"
    if [ "$SDIMAGE_FILE" ];then
     cd sd-skeleton-images
     wget -4 http://distro.ibiblio.org/quirky/quirky6/${DISTRO_TARGETARCH}/sd-skeleton-images/$SDIMAGE_FILE #160823
     sync
     wget -4 http://distro.ibiblio.org/quirky/quirky6/${DISTRO_TARGETARCH}/sd-skeleton-images/${SDIMAGE_FILE}.md5.txt #160823
     sync
     cd ..
     echo "...${SDIMAGE_FILE} downloaded to folder sd-skeleton-images"
     if [ -s ${SDIMAGE_FILE}.md5.txt ];then
      CORRECTMD5="`cat ${SDIMAGE_FILE}.md5.txt | cut -f 1 -d ' '`"
      DLMD5="`md5sum ${SDIMAGE_FILE} | cut -f 1 -d ' '`"
      if [ "$CORRECTMD5" != "$DLMD5" ];then
       echo "...however, the md5sum is incorrect. Deleting downloaded file..."
       rm -f $SDIMAGE_FILE
       echo -n "Press ENTER only to continue script, any other key to try download again..."
       read tryagain
       [ "$tryagain" != "" ] && continue    
      fi
     fi
    fi
    break
   done
  fi
  
 fi
fi


##################################
#110817 new script to find all pkgs for build...
./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-pet-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||
if [ $? -ne 0 ];then
 echo
 echo "ERROR: Script support/findpkgs aborted with an error, exiting."
 exit 1
fi
if [ ! -f status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then
 echo
 echo "ERROR: Something went wrong with support/findpkgs, exiting."
 exit 1
fi

echo
echo "Downloading packages..."
LINECNT=1
cat status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} |
while read ONEDBENTRY
do
 #120812 optional subcategory...
 #ONEDBENTRY=:GENERICNAME:|PETorCOMPAT|DBFILE|pkgname|nameonly|version|pkgrelease|category[;subcategory]|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|
 #...pkgname onwards is the full db entry for a package.
 LINECNT=$(($LINECNT + 1))
 PETorCOMPAT="`echo -n "$ONEDBENTRY" | cut -f 2 -d '|'`" #ex: compat
 if [ "$PETorCOMPAT" != "pet" -a "$PETorCOMPAT" != "compat" ];then
  echo
  echo "Syntax ERROR in status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION},"
  echo "line number ${LINECNT}:"
  echo "$ONEDBENTRY"
  echo -n "Ignoring this line, press ENTER to continue (CTRL-C to abort): "
  read KEEPITGOING
  continue
 fi
 DBFILE="`echo -n "$ONEDBENTRY" | cut -f 3 -d '|'`"      #ex: Packages-pet-5-official
 DB_pkgname="`echo -n "$ONEDBENTRY" | cut -f 4 -d '|'`"  #ex: abiword-2.3.4-w5
 DB_fullfilename="`echo -n "$ONEDBENTRY" | cut -f 11 -d '|'`"  #ex: abiword-2.3.4-w5.pet
 DB_path="`echo -n "$ONEDBENTRY" | cut -f 10 -d '|'`"  #ex: pet_packages-5
 if [ ! "$DB_path" ];then
  ONLINESUBDIR="`echo -n "$DBFILE" | cut -f 3 -d '-'`"   #ex: 5
  if [ "$PETorCOMPAT" = "pet" ];then #120515
   DB_path="pet_packages-${ONLINESUBDIR}"
  else
   #DB_path="compat_packages-${ONLINESUBDIR}"
   #141123 extract path from the db-file, ex: Packages-buildroot-aug2014-official
   ONLINEPRESUB="$(echo -n "$DBFILE" | cut -f 2 -d '-')"
   DB_path="${ONLINEPRESUB}/${ONLINESUBDIR}" #ex: buildroot/aug2014
  fi
 fi
 
 echo -n "$DB_fullfilename "
 
 if [ "$PETorCOMPAT" = "pet" ];then
  
  #see if pkg already downloaded...
  PETPKG="`basename $DB_fullfilename .pet`"
  [ -f packages-pet/${PETPKG}.pet ] && continue
  echo "Downloading ${DB_fullfilename}..."
  SUCCESS='no'
  for ONE_PET_URL in $PET_URLS
  do
   wget --spider --tries=1 --timeout=30 ${ONE_PET_URL}/${DB_path}/${PETPKG}.pet #w013 was 20
   [ $? -ne 0 ] && continue
   cd packages-pet
   #rxvt -name pet -bg orange -geometry 80x10 -e wget ${ONE_PET_URL}/${DB_path}/${PETPKG}.pet
   wget ${ONE_PET_URL}/${DB_path}/${PETPKG}.pet
   sync
   #check file downloaded correctly...
   if [ -f ${PETPKG}.pet ];then
    cp -f ${PETPKG}.pet tempfileonly.pet
    pet2tgz tempfileonly.pet
    [ $? -eq 0 ] && SUCCESS='yes'
    rm -f tempfileonly.pet 2>/dev/null
    rm -f tempfileonly.tar.gz 2>/dev/null
    [ "$SUCCESS" = "no" ] && rm -f ${PETPKG}.pet
   fi
   cd ..
   [ "$SUCCESS" = "yes" ] && break
  done 
  #if fail to find it, log failure, request manual download.
  [ "$SUCCESS" = "no" ] && echo "DOWNLOAD FAIL: ${PETPKG}.pet"
  [ "$SUCCESS" = "no" ] && echo "${PETPKG}.pet" >> DOWNLOAD-FAILS-PET

 else #compat
  
  #151103 is this a t2 pkg in a non-t2 build?...
  xPACKAGES="packages-${BINARIES}"
  xBIN_URLS="$BIN_URLS"
  xDISTRO_BINARY_COMPAT="$DISTRO_BINARY_COMPAT"
  if [ "$DISTRO_BINARY_COMPAT" != "t2" ];then
   if [ "$DBFILE" == "Packages-t2-april-official" ];then
    xPACKAGES='packages-bz2-april'
    xBIN_URLS="http://distro.ibiblio.org/quirky/quirky6/${DISTRO_TARGETARCH}/packages/t2" #160426 t2 april db file has "april" path.
    xDISTRO_BINARY_COMPAT='t2'
   fi
  fi
  
  #check if already downloaded...
  [ -f $xPACKAGES/$DB_fullfilename ] && continue #151103
  #arch is a bit awkward, as the pkg may or may not have '-i686' in the name...
  if [ "$xDISTRO_BINARY_COMPAT" = "arch" ];then
   altDB_fullfilename="`echo -n "$DB_fullfilename" | sed -e 's%\-i686\.pkg%.pkg%'`"
   [ -f $xPACKAGES/$altDB_fullfilename ] && continue #151103
  fi
  #...probably don't need this anymore ###NOTE###
  #use wget to see if pkg on a repository, if not try a secondary repo, then a third.
  #if found, download to packages-${BINARIES} (packages-deb).
  #if fail, log failure, request manual download.
  SUCCESS='no'
  ONEFILE="${DB_path}/${DB_fullfilename}"
  for ONE_BIN_URL in $xBIN_URLS #151103
  do
   wget --spider --tries=1 --timeout=30 ${ONE_BIN_URL}/${ONEFILE} > /dev/null 2>&1 #161227 20 t0 30
   RETSPIDER=$?
   if [ "$xDISTRO_BINARY_COMPAT" = "arch" -a $RETSPIDER -ne 0 ];then #pkg naming inconsistency problem. 151103
    altONEFILE="`echo -n "$ONEFILE" | sed -e 's%\-i686\.pkg%.pkg%'`"
    wget --spider --tries=1 --timeout=20 ${ONE_BIN_URL}/${altONEFILE} > /dev/null 2>&1
    RETSPIDER=$?
    if [ $RETSPIDER -eq 0 ];then
     ONEFILE="$altONEFILE"
     DB_fullfilename="$altDB_fullfilename"
    fi
   fi
   #...probably don't need this anymore ###NOTE###
   [ $RETSPIDER -ne 0 ] && continue
   cd $xPACKAGES #151103
   #rxvt -name pet -bg orange -geometry 80x10 -e wget ${ONE_BIN_URL}/${ONEFILE}
   wget ${ONE_BIN_URL}/${ONEFILE}
   sync
   #check file downloaded correctly...
   if [ -f $DB_fullfilename ];then
    case $xDISTRO_BINARY_COMPAT in #exs: ubuntu, debian. 151103
     ubuntu|debian|raspbian|devuan) #150419
      dpkg-deb --extract $DB_fullfilename tempdironly
      [ $? -eq 0 ] && SUCCESS='yes'
     ;;
     arch)
      xz --test $DB_fullfilename > /dev/null 2>&1
      if [ $? -eq 0 ];then
       SUCCESS='yes'
      else
       gzip --test $DB_fullfilename > /dev/null 2>&1
       [ $? -eq 0 ] && SUCCESS='yes'
      fi
     ;;
     slackware) #100617 support .txz...
      xz --test $DB_fullfilename > /dev/null 2>&1
      if [ $? -eq 0 ];then
       SUCCESS='yes'
      else
       gzip --test $DB_fullfilename > /dev/null 2>&1
       [ $? -eq 0 ] && SUCCESS='yes'
      fi
     ;;
     puppy) #w017
      PETPKG="`basename $DB_fullfilename .pet`"
      cp -f ${PETPKG}.pet tempfileonly.pet
      pet2tgz tempfileonly.pet
      [ $? -eq 0 ] && SUCCESS='yes'
      rm -f tempfileonly.pet 2>/dev/null
      rm -f tempfileonly.tar.gz 2>/dev/null
     ;;
     t2)
      bzip2 --test $DB_fullfilename > /dev/null 2>&1
      [ $? -eq 0 ] && SUCCESS='yes'
     ;;
     scientific|mageia) #110523 Iguleder. 110615
      busybox rpm -qpl $DB_fullfilename > /dev/null 2>&1
      [ $? -eq 0 ] && SUCCESS='yes'
     ;;
     gentoo) #120515
      bzip2 --test $DB_fullfilename > /dev/null 2>&1
      [ $? -eq 0 ] && SUCCESS='yes'
     ;;
     *) #141116 handle buildroot here.
      xz --test $DB_fullfilename > /dev/null 2>&1
      [ $? -eq 0 ] && SUCCESS='yes'
     ;;
    esac
   fi
   rm -rf tempdironly 2>/dev/null
   [ "$SUCCESS" = "no" ] && rm -f ${DB_fullfilename} 2>/dev/null
   cd ..
   [ "$SUCCESS" = "yes" ] && break
  done
  #if failed to find it, log failure, request manual download...
  [ "$SUCCESS" = "no" ]  && echo "$DB_fullfilename" >> DOWNLOAD-FAILS-${BINARIES}
  
 fi #end if PETorCOMPAT

 #check controlpanel, whether need to pause, resume or quit...
 CP_STATUS_REQ="`cat /tmp/1download-status-request`" #Run, Pause, Resume, Quit
 pausecnt=0; PIDCP=0
 while [ "$CP_STATUS_REQ" = "Pause" ];do
  #echo "Status: paused ${pausecnt}seconds"
  if [ $PIDCP -eq 0 ];then
   yaf-splash -close never -placement top -bg orange -text "1download script is currently in paused state" &
   PIDCP=$!
  fi
  sleep 5
  pausecnt=`expr $pausecnt + 5`
  CP_STATUS_REQ="`cat /tmp/1download-status-request`"
 done
 [ $PIDCP -ne 0 ] && kill $PIDCP
 [ "$CP_STATUS_REQ" = "Quit" ] && break 2 #pipe into while above is a separate process, need '2' to break out.

done

if [ "$CP_STATUS_REQ" = "Quit" ];then
 sync
 kill `cat /tmp/1download_controlpanel_pid` #1download_controlpanel
 rm -f /tmp/1download_controlpanel_pid
 echo
 echo "1download aborted."
 echo -n "Press ENTER key to quit script: "
 read yesgetout
 exit
fi

############################

sync
kill `cat /tmp/1download_controlpanel_pid` #1download_controlpanel
rm -f /tmp/1download_controlpanel_pid
echo
echo
echo "Finished."
echo -n "Press ENTER key to quit script: "
read yesgetout

###END###
