#!/bin/sh
#optional params passed in:
#"${NAMEONLY}" "${PUPCATEGORY}" "${PUPOFFICIALDEPS}" "${PUPMENUDESCR}" "$BASEPKG" "$SIZEK" "$PUPPATH" "$ARCHDEPENDENT" "$DEFREPO"
#w476 bug fixes, to work properly with dir2pet.
#w482 set default name of repo to 'official'.
#v425 category does not necessarily have anything to do with menu.
#100201 added PUPPATH, ARCHDEPENDENT, DEFREPO, improve setting of distro name & version.

#old ex: "abiword-2.6.3" "abiword 2.6.3: wordprocessor" ONOFF "Document +fribidi,+gtk+,+goffice,+wv,+enchant 7556K" 
#new: pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|
#ex: abiword-1.2.4|abiword|1.2.4|5|Document|999K|slackware/ab|abiword-1.2.4-5-i486.tgz|+aiksausus,+gtk2|a nice wordprocessor|
#optionally on the end: compileddistro|compiledrelease|repo| (fields 11,12,13)
#ex: slackware|12.2|official|
#this is not normally needed, as the name of the file holding the database info, for ex
#'Packages-slackware-12.2-official' identifies this extra information.

. /etc/DISTRO_SPECS
#. /root/.packages/DISTRO_PKGS_SPECS
#. /root/.packages/DISTRO_PET_REPOS

mkdir -p /var/local/petspec #100201

DEF_nameonly="abiword"
DEF_version="1.2.3-1"
DEF_category="BuildingBlock"
DEF_size_val="UNKNOWN"
#DEF_path="pet_packages-4"
DEF_fullfilename="abiword-1.2.3-1.pet"
DEF_dependencies="+aiksaurus,+gtk2"
DEF_description="A powerful wordprocessor"
DEF_compileddistro="$DISTRO_BINARY_COMPAT"
DEF_compiledrelease="$DISTRO_COMPAT_VERSION"
#DEF_repo="official"
DEF_kernel="`uname -r`"
DEF_pkgname="${DEF_nameonly}-${DEF_version}"


if [ $5 ];then
 DEF_nameonly="$1"
 [ "$2" != "EMPTY" ] && DEF_category="$2"
 [ "$3" != "EMPTY" ] && DEF_dependencies="$3"
 [ "$3" = "EMPTY" ] && DEF_dependencies=""
 [ "$4" != "EMPTY" ] && DEF_description="$4"
 [ "$4" = "EMPTY" ] && DEF_description="no description provided"
 DEF_pkgname="$5"
 xPATTERN="s%${DEF_nameonly}%%"
 DEF_version="`echo -n "$DEF_pkgname" | sed -e "$xPATTERN" -e 's%^\\-%%'`"
 [ $5 ] && DEF_size_val="$6"
 DEF_fullfilename="${5}.pet"
fi

#100201 subdirectory of package on repo...
DEF_path=""
[ ! $7 ] && [ -f /var/local/petspec/DEF_path ] && DEF_path="`cat /var/local/petspec/DEF_path`"
[ $7 ] && [ "$7" != "EMPTY" ] && DEF_path="${7}"

#100201 determine if pkg is only scripts...
ARCHDEPENDENT='yes'
[ $8 ] && ARCHDEPENDENT="${8}"
if [ "$ARCHDEPENDENT" != "yes" -a "$ARCHDEPENDENT" != "no" ];then
 DEF_compileddistro="`echo -n "$ARCHDEPENDENT" | cut -f 1 -d '|'`"
 DEF_compiledrelease="`echo -n "$ARCHDEPENDENT" | cut -f 2 -d '|'`"
fi
if [ "$ARCHDEPENDENT" = "no" ];then
 DEF_compileddistro="DISTRO INDEPENDENT"
 DEF_compiledrelease="DISTRO INDEPENDENT"
fi

#100201 id of repository...
DEF_repo=""
[ ! $9 ] && [ -f /var/local/petspec/DEF_repo ] && DEF_repo="`cat /var/local/petspec/DEF_repo`"
[ $9 ] && [ "$9" != "EMPTY" ] && DEF_repo="${9}"

#sort category items...
ITEMSCAT="<item>${DEF_category}</item>"
for ONECAT in 'NO CATEGORY' Desktop System Setup Utility Filesystem Graphic Document Calculate Personal Network Internet Multimedia Fun Help BuildingBlock Develop
do
 [ "$ONECAT" = "$DEF_category" ] && continue
 ITEMSCAT="${ITEMSCAT}<item>${ONECAT}</item>"
done

#sort compileddistro items...
ITEMSCOMP="<item>${DEF_compileddistro}</item>"
for ONECHOICE in 'DISTRO INDEPENDENT' arch debian puppy slackware t2 ubuntu
do
 [ "$ONECHOICE" = "${DEF_compileddistro}" ] && continue
 ITEMSCOMP="${ITEMSCOMP}<item>${ONECHOICE}</item>"
done

DEPENDENCIES=""
if [ "$DEF_dependencies" != "" ];then
 DEPENDENCIES="<default>${DEF_dependencies}</default>"
fi

export MAIN_DIALOG="
<window title=\"Package database-entry creator\" icon-name=\"gtk-index\">
 <vbox>
 
  <text use-markup=\"true\"><label>\"<b>The fields marked with '*' are required, the rest fill in as much as possible or if uncertain leave blank.</b>\"</label></text>
  
  <hbox>
   <text><label>\"    \"</label></text><text><label>*Name-only:</label></text>
   <entry>
    <default>${DEF_nameonly}</default><variable>DB_nameonly</variable>
   </entry>
  </hbox>
  <hbox>
   <text><label>\"                  \"</label></text><text><label>*Version:</label></text>
   <entry>
    <default>${DEF_version}</default><variable>DB_version</variable>
   </entry>
  </hbox>

  <hbox>
    <text>
      <label>*Category:</label>
    </text>
	<combobox>
      <variable>DB_category</variable>
	  ${ITEMSCAT}
    </combobox>
  </hbox>
  <hbox>
   <text><label>\"                  \"</label></text><text><label>Size of installed package:</label></text>
   <entry>
    <default>${DEF_size_val}</default><variable>DB_size_val</variable>
   </entry>
   <text><label>KB</label></text>
  </hbox>

  <hbox>
   <text><label>Folder path within repository:</label></text>
   <entry>
    <default>\"${DEF_path}\"</default><variable>DB_path</variable>
   </entry>
  </hbox>
  <hbox>
   <text><label>*Full package name:</label></text>
   <entry>
    <default>${DEF_fullfilename}</default><variable>DB_fullfilename</variable>
   </entry>
  </hbox>
  <hbox>
   <text><label>Dependencies:</label></text>
   <entry>
    ${DEPENDENCIES}<variable>DB_dependencies</variable>
   </entry>
  </hbox>
  <hbox>
   <text><label>Description:</label></text>
   <entry>
    <default>${DEF_description}</default><variable>DB_description</variable>
   </entry>
  </hbox>

  <frame Extra optional>
  <hbox>
    <text>
      <label>Compiled in compatible-distro:</label>
    </text>
	<combobox>
      <variable>DB_compileddistro</variable>
	  ${ITEMSCOMP}
    </combobox>
  </hbox>
  <hbox>
   <text><label>\"    \"</label></text><text><label>Compatible-distro version:</label></text>
   <entry>
    <default>\"${DEF_compiledrelease}\"</default><variable>DB_compiledrelease</variable>
   </entry>
  </hbox>
  <hbox>
   <text><label>\"    \"</label></text><text><label>Single unique name of repository:</label></text>
   <entry>
    <default>\"${DEF_repo}\"</default><variable>DB_repo</variable>
   </entry>
  </hbox>
  </frame>

   <hbox>
    <button>
     <label>Help</label>
     <action>defaulthtmlviewer http://puppylinux.com/woof/pkg-db-format.htm & </action>
    </button>
    <button>
     <label>Generate package database entry</label>
     <action type=\"exit\">GENERATE_DB</action>
    </button>
    <button cancel></button>
   </hbox>
 </vbox>
</window>
"

RETVALS="`gtkdialog3 --program=MAIN_DIALOG`"
#ex:
#DB_category="BuildingBlock"
#DB_compileddistro="ubuntu"
#DB_compiledrelease="jaunty"
#DB_dependencies="+aiksaurus,+gtk2"
#DB_description="A powerful wordprocessor"
#DB_fullfilename="abiword-1.2.3-1.pet"
#DB_path="pet_packages-5"
#DB_pkgname="abiword"
#DB_repo="5"
#DB_size_val="UNKNOWN"
#DB_version="1.2.3-1"
#EXIT="GENERATE_DB"

eval "$RETVALS"

[ "$EXIT" != "GENERATE_DB" ] && exit 1

echo -n "$DB_path" > /var/local/petspec/DEF_path #100201
echo -n "$DB_repo" > /var/local/petspec/DEF_repo #100201

DB_size="$DB_size_val"'K'
[ "$DB_size_val" = "UNKNOWN" ] && DB_size=""
[ "$DB_category" = "NO CATEGORY" ] && DB_category=""
[ "$DB_compileddistro" = "DISTRO INDEPENDENT" ] && DB_compileddistro=""
[ "$DEF_compiledrelease" = "DISTRO INDEPENDENT" ] && DB_compiledrelease=""
DB_pkgname="${DB_nameonly}-${DB_version}"

DB_ENTRY="${DB_pkgname}|${DB_nameonly}|${DB_version}|${DB_pkgrelease}|${DB_category}|${DB_size}|${DB_path}|${DB_fullfilename}|${DB_dependencies}|${DB_description}|${DB_compileddistro}|${DB_compiledrelease}|${DB_repo}|"
echo
echo "$DB_ENTRY"
echo "This is the package database entry:

$DB_ENTRY" > /tmp/petspec_db_entry

nohup defaulttextviewer /tmp/petspec_db_entry 2>/dev/null &

exit 0

###END###
