#!/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.
#100303 small bugfix.
#100320 prevent crash if $5 only has name, no version number.
#100508 bugfix empty field.
#120517 update the compileddistro items.
#120719 support raspbian.
#120814 support sub-category. 120819 sub-category fixes.
#130224 add Document;utility

#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[;subcategory]|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|
#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|slackware|12.2|official|

. /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_subcategory="" #120814
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%^\\-%%'`"
 [ "$DEF_version" = "" ] && DEF_version="UNKNOWN" #100320
 [ $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
[ "$DEF_compiledrelease" = "" ] && DEF_compiledrelease="$DISTRO_COMPAT_VERSION" #100508

#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}"

#120814 add sub-categories (note, icons for all categories and sub-cat are in /usr/local/lib/X11/mini-icons) ...
ITEMSCAT="<item>${DEF_category}</item>"
for ONECAT in 'NO CATEGORY' Desktop 'Desktop;appearance' 'Desktop;applet' 'Desktop;settings' 'Desktop;sleep' 'Desktop;windows' System 'System;memory' 'System;print' 'System;process' 'System;security' 'System;storage' Setup 'Setup;installation' 'Setup;puppy' 'Setup;wizard' Utility 'Utility;development' 'Utility;help' 'Utility;package' 'Utility;shell' Filesystem 'Filesystem;filemanager' 'Filesystem;find' 'Filesystem;storage' Graphic 'Graphic;camera' 'Graphic;draw' 'Graphic;flow' 'Graphic;paint' 'Graphic;print' 'Graphic;scanner' 'Graphic;utility' 'Graphic;viewer' Document 'Document;catalog' 'Document;layout' 'Document;edit' 'Document;viewer' 'Document;utility' Business 'Business;calc' 'Business;finance' 'Business;planner' 'Business;spreadsheet' Personal 'Personal;database' 'Personal;date' 'Personal;education' 'Personal;organizer' 'Personal;security' 'Personal;timing' Network 'Network;connect' 'Network;firewall' 'Network;phone' 'Network;transfer' 'Network;utility' Internet 'Internet;block' 'Internet;browser' 'Internet;chat' 'Internet;login' 'Internet;mailnews' 'Internet;transfer' 'Internet;utility' Multimedia 'Multimedia;mediaplayer' 'Multimedia;optical' 'Multimedia;sound' 'Multimedia;utility' 'Multimedia;video' Fun 'Fun;adventure' 'Fun;arcade' 'Fun;boardgame' 'Fun;cardgame' 'Fun;puzzle' 'Fun;shooter' Help BuildingBlock
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 gentoo mageia puppy raspbian scientific 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 DLG_REPONAME='
 <window title="Name of repository" icon-name="gtk-index" window_position="1">
  <vbox>
    <text use-markup="true">
     <label>"This field is intended to have a unique single-word that identifies the repository where the PET package is being hosted. For example, we have used the word <b>official</b> for PETs hosted at ibiblio.org and the official mirrors. 

However, this field is not being used for anything, so currently does not serve any purpose, so recommend leave it empty. Besides, if you have created a PET that could be hosted at various sites, then you do not really have a single name that is appropriate."</label>
      <variable>VAR_RP</variable>
    </text>
    <button>
      <label>Close</label>
      <action type="closewindow">VAR_RP</action>
    </button>
  </vbox>
 </window>'

export DLG_FOLDER='
 <window title="Folder path" icon-name="gtk-index" window_position="1">
  <vbox>
    <text use-markup="true">
     <label>"The Puppy Package Manager knows the URLs of the repositories, however, individual PET packages may be in different folders within the repo.

If the PET is to be hosted on the offical repo ibiblio.org or mirrors, then this field can be left empty, as the package mamager knows to look in a folder named pet_packages-*, where * is <b>lucid</b> for Lucid Puppy, <b>wary5</b> for Wary Puppy, <b>drake</b> for Drake Puppy.

It is recommended to leave this field empty."</label>
      <variable>VAR_F</variable>
    </text>
    <button>
      <label>Close</label>
      <action type="closewindow">VAR_F</action>
    </button>
  </vbox>
 </window>'

export DLG_DEPS='
 <window title="Dependencies" icon-name="gtk-index" window_position="1">
  <vbox>
    <text use-markup="true">
     <label>"Enter a list of package names that this package is dependent on. If you know that the packages are already builtin to Puppy, then no need to list them. Here is an example:

<b>+enchant,+fribidi,+goffice&amp;ge4.5.6,+libgsf,+wv</b>

Please put a plus character in front of each name, comma delimiter. You may also optionally append a version-qualification, as shown for goffice. The accepted operators are <b>&amp;le &amp;ge &amp;lt &amp;gt &amp;eq</b>.

If your package requires a certain kernel version, this may also be specified. Note, the dhcpcd package is one example. You can specify kernel-version dependency like this:

<b>+linux_kernel&amp;ge2.6.38</b>

Note1: It is planned in the future to implement a minus character prefix (-) to dependencies (instead of the +), which will mean a conflicting package (this package cannot work if that package is installed).

Note2: More than one version-condition is allowed, for example, to specify a range: <b>+alsa-lib&amp;ge2.3&amp;lt2.4</b>

Note3: You can find out the names of inbuilt packages in file /root/.packages/woof-installed-packages. The names of other not-installed packages can be found in /root/.packages/Packages-* files.

Note4: As Puppy can use packages from many different distros, sometimes the same application will have a different name, even something as simple as alsa-lib or alsa_lib. When analyzing dependencies, Puppy has a list of package-name aliases to handle this. See variable PKG_NAME_ALIASES in file PKGS_MANAGEMENT (also in /root/.packages/ in a running Puppy."</label>
      <variable>VAR_DEPS</variable>
    </text>
    <button>
      <label>Close</label>
      <action type="closewindow">VAR_DEPS</action>
    </button>
  </vbox>
 </window>'

export DLG_CAT='
 <window title="Categories" icon-name="gtk-index" window_position="1">
  <vbox>
    <text use-markup="true">
     <label>"These are the top-level categories:

<i>Desktop System Setup Utility Filesystem Graphic Document Business Personal Network Internet Multimedia Fun Help BuildingBlock</i>

You will also find these in the top-level menu (see bottom-left of screen). You may also optionally select a sub-category, but if none seem to apply, just choose the top-level only -- for example, <b>Fun</b> has sub-categories <b>adventure</b>, <b>arcade</b>, <b>boardgame</b>, <b>cardgame</b>, <b>puzzle</b> and <b>shooter</b>, but if none of those seem to apply, just choose <b>Fun</b>.

<b>BuildingBlock</b> packages are those that are system-level utilities, data and libraries, that you would not normally choose to install via the Puppy Package Manager. Thus, they are by default filtered out by the PPM. For example, <i>libsdl</i> is a library, however some applications will have it as a dependency so if you choose to install one of those, then libsdl will also get installed -- <i>libsdl</i> is not something that you would want to install on its own, thus it qualifies as a <b>BuildingBlock</b>.

<b>Technical notes:</b>
Note that the Puppy Package Manager will recognize category and subcategory, and filters packages based on category, to display a smaller selection to choose from. Packages are listed in the PPM with icons alongside, that are obtained from /usr/local/lib/X11/mini-icons -- there is a generic icon for every category and sub-category.

A category, with optional subcategory, does not mean that the application has a menu entry. A menu entry will only exist if an appropriate .desktop file exists in /usr/share/applications -- this is provided in the package. The .desktop file specifies whereabouts in the menu the entry is located, and ideally we would expect it to correspond to the category;subcategory field described above, however in theory the .desktop file can place it anywhere else."</label>
      <variable>VAR_C</variable>
    </text>
    <button>
      <label>Close</label>
      <action type="closewindow">VAR_C</action>
    </button>
  </vbox>
 </window>'



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>
    <button>
      <input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input>
      <action type=\"launch\">DLG_CAT</action>
    </button>
  </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>*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>
   <button>
      <input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input>
      <action type=\"launch\">DLG_DEPS</action>
   </button>
  </hbox>

  <hbox>
   <text><label>Description:</label></text>
   <entry>
    <default>${DEF_description}</default><variable>DB_description</variable>
   </entry>
  </hbox>

  <frame Extra optional>
  <hbox>
   <text><label>Folder path within repository:</label></text>
   <entry>
    <default>\"${DEF_path}\"</default><variable>DB_path</variable>
   </entry>
   <button>
      <input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input>
      <action type=\"launch\">DLG_FOLDER</action>
   </button>
  </hbox>
  
  <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>
   <button>
      <input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input>
      <action type=\"launch\">DLG_REPONAME</action>
   </button>

  </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="`gtkdialog4 --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=""
[ "$DB_compiledrelease" = "DISTRO INDEPENDENT" ] && DB_compiledrelease="" #100303
DB_version="`echo -n "$DB_version" | sed -e 's% %%g'`" #100320 precaution, get rid of any spaces.
if [ "$DB_version" = "" -o "$DB_version" = "UNKNOWN" ];then #100320
 DB_pkgname="$DB_nameonly"
 DB_version=""
else
 DB_pkgname="${DB_nameonly}-${DB_version}"
fi

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###
