#!/bin/sh
#called from pkg_chooser.sh
#package to be previewed prior to installation is TREE1 -- inherited from parent.
#/tmp/petget_filterversion has the repository that installing from: 2 is Puppy2, 4 is Puppy4.

. /usr/bin/localization 



#TEMP FOR NOW....
petget +${TREE1}
#/usr/local/petget/findinstalledpkgs.sh #update list for main gui window.
exit




PUPPYVERSION=`cat /etc/puppyversion`
[ $PUPPYVERSION -eq 398 ] && PUPPYVERSION=400 #temp fixup.
[ $PUPPYVERSION -eq 399 ] && PUPPYVERSION=400 #temp fixup.
PUPVERDIGIT1="`echo -n "$PUPPYVERSION" | cut -b 1`"

REPOVERSION="`cat /tmp/petget_filterversion`"
fltrVERSION="$REPOVERSION"
[ "$fltrVERSION" = "$PUPVERDIGIT1" ] && fltrVERSION="" #current version.

REPOINFO="`cat /root/.packages/livepackages${fltrVERSION}.txt`"

#get name-only and version of this pkg...
prePATTERN='^"'"$TREE1"'" '
PKGNAMEONLY="`echo "$REPOINFO" | grep "$prePATTERN" | cut -f 4 -d '"' | cut -f 1 -d ':' | cut -f 1 -d ' '`" #'geany
PKGVERSIONONLY="`echo "$REPOINFO" | grep "$prePATTERN" | cut -f 4 -d '"' | cut -f 1 -d ':' | cut -f 2 -d ' '`" #'geany
if [ "$PKGNAMEONLY" = "$PKGVERSIONONLY" ];then
 PKGNAMEONLY="`echo -n "$TREE1"' | sed -e 's/-[0-9]/ /' | cut -f 1 -d ' '`"
 nPATTERN='s/'"$PKGNAMEONLY"'-//'
 PKGVERSIONONLY="`echo -n "$TREE1" | sed -e "$nPATTERN"`"
fi

#want to find out what deps need to be installed, so get a list of all installed packages...
#petget already called findinstalledpkgs.sh (from pkg_chooser.sh) to get a list of user-installed pkgs...
USERINSTALLEDPKGS="/tmp/petget_installedpkgs" #entries same format as [live]packages.txt
#now get installed pkgs when the live-CD was built:
UNLEASHEDINSTALLEDPKGS="`cat /root/.packages/packages.txt | grep '" on "'`"


#check if another version of pkg already installed...
echo -n "" /tmp/petget_prewarnings
namePATTERN=' "'"$PKGNAMEONLY"' '
USERALREADY="`echo "$USERINSTALLEDPKGS" | grep "$namePATTERN" | cut -f 2 -d '"' | tr '\n' ' '`"
UNLEASHEDALREADY="`echo "$UNLEASHEDINSTALLEDPKGS" | grep "$namePATTERN" | cut -f 2 -d '"' | tr '\n' ' '`"
ALREADYMSG=""
if [ "$USERALREADY" != "" ];then
 ALREADYMSG1="$(eval_gettext "Careful, you have already installed another version of the same package:
 \$USERALREADY")"
 echo "<text><label>\"${ALREADYMSG1}\"</label></text>" >> /tmp/petget_prewarnings
fi
if [ "$UNLEASHEDALREADY" != "" ];then
 ALREADYMSG2="$(eval_gettext "Careful, Puppy already has another version of the same package builtin:
 \$UNLEASHEALREADY")"
 echo "<text><label>\"${ALREADYMSG2}\"</label></text>" >> /tmp/petget_prewarnings
fi

#now find any missing deps...
echo -n "" > /tmp/petget_missingdeps
oPKGDEPS="`echo "$REPOINFO" | grep "$prePATTERN" | cut -f 6 -d '"' | grep -o '+.*' | cut -f 1 -d ' ' | sed -e 's/ +/ /g' | sed -e 's/,+/ /g'`" #'geany
PKGDEPS="$oPKGDEPS"
while [ "$PKGDEPS" != "" ];do #recursive.
 nxtPKGDEPS=""
 for ONEDEP in $PKGDEPS
 do
  #only look in current repository...
  #these deps can be name-only or with version...
  depPATTERN1='"'"$ONEDEP"' '
  depPATTERN2='"'"$ONEDEP"'"'
  MISSINGDEP="`echo "$REPINFO" | grep "$depPATTERN2" | grep '" off "' | cut -f 2 -d '"'`" #'geany
  [ "$MISSINGDEP" = "" ] && MISSINGDEP="`echo "$REPINFO" | grep "$depPATTERN1" | grep '" off "' | head -n 1 | cut -f 2 -d '"'`" #'geany
  if [ "$MISSINGDEP" != "" ];then
   echo -n "${MISSINGDEP} " >> /tmp/petget_missingdeps
   depPATTERN3='"'"$MISSINGDEP"'"'
   xPKGDEPS="`echo "$REPOINFO" | grep "$depPATTERN3" | cut -f 6 -d '"' | grep -o '+.*' | cut -f 1 -d ' ' | sed -e 's/ +/ /g' | sed -e 's/,+/ /g'`" #'geany
   [ "$xPKGDEPS" != "" ] && nxtPKGDEPS="$nxtPKGDEPS $xPKGDEPS"
  fi
  #maybe it is installed, or not listed in the repo...
  
  
 done
 PKGDEPS="$nxtPKGDEPS"
done

if [ "$oPKGDEPS" != "" ];then
 DEPMSG0="$(eval_gettext "The package lists these immediate dependencies:
 \$oPKGDEPS")"
 echo "<text><label>\"$DEPMSG0\"</label></text>" >> /tmp/petget_prewarnings
fi
if [ -s /tmp/petget_missingdeps ];then #file has a space-delimited list of pkgs.
 allPKGDEPS="`cat /tmp/petget_missingdeps`"
 DEPMSG1="$(eval_gettext "These missing packages are required and will be installed:
 \$allPKGDEPS")"
 echo "<text><label>\"$DEPMSG1\"</label></text>" >> /tmp/petget_prewarnings
fi


WARNINGMSGS="`cat /tmp/petget_prewarnings`"

export PREVIEW_DIALOG="<window title=\"$(eval_gettext "PETget package manager")\" icon-name=\"gtk-about\">
<vbox>
 <text><label>$(eval_gettext "You have chosen to install \$TREE1")</label></text>
 
 ${WARNINGMSGS}
 
 <hbox>
  <button ok></button>
  <button cancel></button>
 </hbox>
</vbox>
</window>
"


RETPARAMS="`gtkdialog3 --program=PREVIEW_DIALOG`"

eval "$RETPARAMS"

#NOTE, add extra param when call petget from commandline ****************
[ "$EXIT" = "Ok" ] && exec petget +$TREE1 $REPOVERSION

###END###
