REM (c) Copyright Barry Kauler 2012
REM in woof, script 0setup downloads a debian/ubuntu pkg db file, then pre-formats it.
REM the pre-formatted filename /tmp/woof-debdb.in is opened and read, and converted
REM to Puppy db format on stdout.
REM require pass compat-distro and compat-version on commandline. ex: ubuntu precise
REM homepages get logged to /tmp/woof-homepages.acc
REM 121111 first version.
REM 121112 significant speedup, no longer call find_cat. loads categories.dat. 121113 tidy up.
REM 121113 read Ubuntu-db 'Section' parameter to help assign category.
REM 121113 last field of ubuntu pkg db cannot be guaranteed. introduce STARTMARKER (refer 0setup).
REM 130126 no longer find category. find_cat is called afterward (see 0setup).
REM 130221 forgot &gt deps case.
REM 130225 awkward situation, "|" OR operator gets converted to a space back in prefiltering in 0setup.
REM 130305 0setup creeates /tmp/0setupcompletelistpkgs, list of all pkgs in repo, all on one line.
REM 130316 debdb2pupdb does not use /tmp/0setupcompletelistpkgs in a running puppy.
REM 130512 vercmp failed this test: if vercmp 2.2.1 ge 2.2~2011week36; then echo 'greater'; fi

REM declare an associative array, containing strings...
DECLARE assocarray$ ASSOC STRING
assocarray$("Description")=""
assocarray$("Filename")=""
assocarray$("Package")=""
assocarray$("InstalledSize")=""
assocarray$("Architecture")=""
assocarray$("Version")=""
assocarray$("Depends")=""
assocarray$("Homepage")=""
assocarray$("Section")=""
assocarray$("STARTMARKER")=""
endflag=0

REM require pass compat-distro and compat-version on commandline...
SPLIT ARGUMENT$ BY " " TO commandline$ SIZE dim0
IF dim0 < 3 THEN END 1
compatdistro$=commandline$[1]
compatversion$=commandline$[2]

test1=FILEEXISTS("/tmp/woof-debdb.in")
IF test1 EQ 0 THEN END 1

runningpuppy=0
test2=FILEEXISTS("/tmp/0setupcompletelistpkgs")
IF test2 EQ 0 THEN runningpuppy=1

IF runningpuppy EQ 0 THEN
 REM open file with list of all pkgs, all on one line...
 OPEN "/tmp/0setupcompletelistpkgs" FOR READING AS completelistfile
 READLN completelist$ FROM completelistfile
 CLOSE FILE completelistfile
 OPEN "/tmp/0setupnewinvaliddeps" FOR WRITING AS newinvaliddepsfile
ELSE
 REM created by debdb2pupdb in Woof, copied into rootfs-complete by 3builddistro...
 test2=FILEEXISTS("/usr/local/petget/invaliddepslist")
 IF test2 EQ 0 THEN END 1
 OPEN "/usr/local/petget/invaliddepslist" FOR READING AS invaliddepslistfile
 READLN invaliddepslist$ FROM invaliddepslistfile
 CLOSE FILE invaliddepslistfile
END IF

REM open a file to accumulate homepages...
test1=FILEEXISTS("/tmp/woof-homepages.acc")
IF test1 NE 0 THEN DELETE FILE "/tmp/woof-homepages.acc"
OPEN "/tmp/woof-homepages.acc" FOR WRITING AS homepagesfile

REM open intermediate input file (refer 0setup)...
OPEN "/tmp/woof-debdb.in" FOR READING AS myfile
WHILE NOT(ENDFILE(myfile)) DO
 READLN oneline$ FROM myfile
 IF NOT(ENDFILE(myfile)) THEN
  len0=LEN(oneline$)
  IF len0 EQ 0 THEN CONTINUE

  REM evaluate the line...
  SPLIT oneline$ BY "|" TO splitarray$ SIZE dimension
  onevariable$=splitarray$[0]
  onecontent$=""
  IF dimension >= 2 THEN onecontent$=splitarray$[1]
  assocarray$(onevariable$)=onecontent$
  
  REM 121113 hmmm, precise has some db's Homepage field last. introduce STARTMARKER...
  REM debian squeeze and earlier has Description field last, wheezy has Filename field last...
  IF onevariable$ = "STARTMARKER" THEN
   len2=LEN(assocarray$("Package"))
   IF len2 NE 0 THEN endflag=1
  END IF
  
  IF endflag = 1 THEN
   
   REM Filename ex: pool/main/f/firefox/abrowser-branding_11.0+build1-0ubuntu4_i386.deb
   len1=LEN(assocarray$("Filename"))
   IF len1 > 0 THEN
    pos1=INSTRREV(assocarray$("Filename"),"/")
    DB_fullfilename$=RIGHT$(assocarray$("Filename"),len1-pos1)
    DB_path$=LEFT$(assocarray$("Filename"),pos1-1)
    
    REM Version ex: 0.140
    REM Version ex: 1:2.0.10-1ubuntu3 <<<want to remove leading and trailing, to give: 2.0.10
    REM remove leading...
    revver1$=REVERSE$(assocarray$("Version"))
    pos1=REGEX(revver1$,":[0-9]$")
    IF pos1 = 0 THEN
     ver2$=assocarray$("Version")
    ELSE
     revver2$=LEFT$(revver1$,pos1-1)
     ver2$=REVERSE$(revver2$)
    END IF
    
    REM 130512 vercmp failed this test: if vercmp 2.2.1 ge 2.2~2011week36; then echo 'greater'; fi
    REM remove ~ char and all past it... see also same code for deps further down.
    pos1=REGEX(ver2$,"~")
    IF pos1 != 0 THEN
     ver2a$=LEFT$(ver2$,pos1-1)
     ver2$=ver2a$
    END IF

    REM remove trailing...
    REM note: could use compatdistro$ for improved efficiency...
    pos1=REGEX(ver2$,"\\-[0-9.]*ubuntu[0-9.]*$")
    IF pos1=0 THEN pos1=REGEX(ver2$,"\\+[0-9.]*ubuntu[0-9.]*$")
    IF pos1=0 THEN pos1=REGEX(ver2$,"\\-[0-9.]*debian[0-9.]*$")
    IF pos1=0 THEN pos1=REGEX(ver2$,"\\+[0-9.]*debian[0-9.]*$")
    IF pos1=0 THEN pos1=REGEX(ver2$,"\\-[0-9.]*raspbian[0-9.]*$")
    IF pos1=0 THEN pos1=REGEX(ver2$,"\\+[0-9.]*raspbian[0-9.]*$")
    IF pos1=0 THEN pos1=REGEX(ver2$,"\\-[0-9.]*build[0-9.]*$")
    IF pos1=0 THEN pos1=REGEX(ver2$,"\\+[0-9.]*build[0-9.]*$")
    IF pos1 = 0 THEN
     DB_version$=ver2$
     DB_pkgrelease$=""
    ELSE
     DB_version$=LEFT$(ver2$,pos1-1)
     len1=LEN(ver2$)
     DB_pkgrelease$=RIGHT$(ver2$,len1-pos1)
    END IF
    
    DB_nameonly$=assocarray$("Package")
    DB_pkgname$=CONCAT$(DB_nameonly$,"_",DB_version$)
    desc0$=assocarray$("Description")
    REM get rid of some chars that mess up find_cat (0setup already took out \t"|` ...
    desc1$=REPLACE$(desc0$,"'","")
    desc2$=REPLACE$(desc1$,"(","")
    desc3$=REPLACE$(desc2$,")","")
    desc4$=REPLACE$(desc3$,",","")
    DB_description$=desc4$
    
    REM find_cat is in Woof in support/find_cat, but this app must also execute in a running puppy when the PPM 
    REM updates the DBs, in which case path is /usr/local/petget/find_cat -- always use latter.
    REM for debian, ubuntu, use the generic name provided in the DB_path$, instead of DB_nameonly$, for find_cat...
    len3=LEN(DB_path$)
    IF len3 EQ 0 THEN
     xPackage$=DB_nameonly$
    ELSE
     REM use the basename of the path...
     pos3=INSTRREV(DB_path$,"/")
     xPackage$=RIGHT$(DB_path$,len3-pos3)
    END IF

    REM previously had "find_cat" code in here.

    REM 121113 use Section as fallback. values found in ubuntu 'main' db:
    REM admin cli-mono comm database debug devel doc editors fonts games gnome graphics httpd interpreters introspection java kde kernel libdevel libs lisp localization mail math metapackages misc net ocaml oldlibs otherosfs perl php python ruby science shells sound tex text translations utils vcs video web x11 zope
    REM values found in ubuntu 'universe' db:
    REM universe/admin universe/cli-mono universe/comm universe/database universe/debug universe/devel universe/doc universe/editors universe/electronics universe/embedded universe/fonts universe/games universe/gnome universe/gnu-r universe/gnustep universe/graphics universe/hamradio universe/haskell universe/httpd universe/interpreters universe/introspection universe/java universe/kde universe/kernel universe/libdevel universe/libs universe/lisp universe/localization universe/mail universe/math universe/metapackages universe/misc universe/net universe/news universe/ocaml universe/oldlibs universe/otherosfs universe/perl universe/php universe/python universe/ruby universe/science universe/shells universe/sound universe/tex universe/text universe/utils universe/vcs universe/video universe/web universe/x11 universe/xfce universe/zope
    REM note, most of these so-called categories are not suitable for classifying into a menu structure.
    DB_category$=assocarray$("Section")
    REM ...these will get replaced by correct category in find_cat.
    
    REM Depends ex: firefox (>= 9.0)
    REM Depends ex: libc6 (>= 2.4), libdbus-1-3 (>= 1.0.2), libdbus-glib-1-2 (>= 0.88), libglib2.0-0 (>= 2.25.11), libpolkit-gobject-1-0 (>= 0.101), libaccountsservice0 (= 0.6.15-2ubuntu9), dbus
    REM Depends ex: x11-xserver-utils, acpid (>= 1.0.4-1ubuntu4), hdparm, lsb-base (>= 1.3-9ubuntu3), powermgmt-base, laptop-detect, dmidecode (>= 2.7-1), libc6 (>= 2.4), pm-utils, consolekit
    DB_dependencies$=""
    SPLIT assocarray$("Depends") BY "," TO deparray$ SIZE dimension5
    IF dimension5 > 0 THEN
     FOR cnt5 = 0 TO dimension5-1
      choppeddep0$=CHOP$(deparray$[cnt5])
      REM remove ")" and anything past it...
      pos7=INSTR(choppeddep0$,")")
      IF pos7 > 0 THEN choppeddep$=LEFT$(choppeddep0$,pos7-1)
      ELSE choppeddep$=choppeddep0$
      pos5=INSTR(choppeddep$," ")
      depver$=""
      IF pos5 EQ 0 THEN
       depname$=choppeddep$
      ELSE
       depname$=LEFT$(choppeddep$,pos5-1)
       len7=LEN(choppeddep$)
       IF len7 > pos5 THEN
        depver1$=RIGHT$(choppeddep$,len7-pos5)
        REM depver1$ ex: "(>= 1.15.4"
        SPLIT depver1$ BY " " TO depverarray$ SIZE dim1
        IF dim1 > 1 THEN
         SELECT depverarray$[0]
          CASE "(>="
           relation$="&ge"
          CASE "(<="
           relation$="&le"
          REM 130221 forgot this...
          CASE "(>>"
           relation$="&gt"
          CASE "(="
           relation$="&eq"
          DEFAULT
           relation$=""
         END SELECT
         
         REM 130225 awkward situation, "|" OR operator gets converted to a space back in prefiltering in 0setup, so
         REM an original: Depends: libc6 (>= 2.3.4), udev | makedev (>= 2.3.1-24)
         REM gets passed in here as: Depends|libc6 (>= 2.3.4), udev makedev (>= 2.3.1-24)
         IF relation$ = "" THEN
          depver$=""
         ELSE
          REM for compatibility with earlier, have to strip leading and trailing stuff...
          REM remove leading...
          revver1$=REVERSE$(depverarray$[1])
          pos1=REGEX(revver1$,":[0-9]$")
          IF pos1 = 0 THEN
           ver2$=depverarray$[1]
          ELSE
           revver2$=LEFT$(revver1$,pos1-1)
           ver2$=REVERSE$(revver2$)
          END IF
          
          REM 130512 vercmp failed this test: if vercmp 2.2.1 ge 2.2~2011week36; then echo 'greater'; fi
          REM remove ~ char and all past it...
          pos1=REGEX(ver2$,"~")
          IF pos1 != 0 THEN
           ver2a$=LEFT$(ver2$,pos1-1)
           ver2$=ver2a$
          END IF

          REM remove trailing...
          REM note: could use compatdistro$ for improved efficiency...
          pos1=REGEX(ver2$,"\\-[0-9.]*ubuntu[0-9.]*$")
          IF pos1=0 THEN pos1=REGEX(ver2$,"\\+[0-9.]*ubuntu[0-9.]*$")
          IF pos1=0 THEN pos1=REGEX(ver2$,"\\-[0-9.]*debian[0-9.]*$")
          IF pos1=0 THEN pos1=REGEX(ver2$,"\\+[0-9.]*debian[0-9.]*$")
          IF pos1=0 THEN pos1=REGEX(ver2$,"\\-[0-9.]*raspbian[0-9.]*$")
          IF pos1=0 THEN pos1=REGEX(ver2$,"\\+[0-9.]*raspbian[0-9.]*$")
          IF pos1=0 THEN pos1=REGEX(ver2$,"\\-[0-9.]*build[0-9.]*$")
          IF pos1=0 THEN pos1=REGEX(ver2$,"\\+[0-9.]*build[0-9.]*$")
          IF pos1 = 0 THEN
           ver2b$=ver2$
          ELSE
           ver2b$=LEFT$(ver2$,pos1-1)
          END IF
          depver$=CONCAT$(relation$,ver2b$)
          REM depver$ ex: "&ge2.15"
         END IF
        END IF
       END IF
      END IF
      len6=LEN(depname$)
      IF len6 NE 0 THEN
       REM 130305 check if dep exists...
       depptn$=CONCAT$(" ",depname$," ")
       IF runningpuppy EQ 0 THEN
        pkgoffset=INSTR(completelist$,depptn$)
        IF pkgoffset EQ 0 THEN
         WRITELN depname$ TO newinvaliddepsfile
        END IF
       ELSE
        REM a much smaller list, of invalid deps only...
        pkgoffset=ISFALSE(INSTR(invaliddepslist$,depptn$))
       END IF
       IF pkgoffset NE 0 THEN
        len5=LEN(DB_dependencies$)
        IF len5 EQ 0 THEN DB_dependencies$=CONCAT$(DB_dependencies$,"+",depname$,depver$)
        ELSE DB_dependencies$=CONCAT$(DB_dependencies$,",+",depname$,depver$)
       END IF
      END IF
     NEXT
    END IF
    
    DB_size$=assocarray$("InstalledSize")
    len9=LEN(DB_size$)
    IF len9 EQ 0 THEN DB_size$="0"
    
    REM screen out all debug pkgs...
    pos4=REGEX(DB_nameonly$,"\\-dbg$")
    IF pos4 EQ 0 THEN
     REM print puppy-format db line to stdout...
     PRINT DB_pkgname$,"|",DB_nameonly$,"|",DB_version$,"|",DB_pkgrelease$,"|",DB_category$,"|",DB_size$,"K|",DB_path$,"|",DB_fullfilename$,"|",DB_dependencies$,"|",DB_description$,"|",compatdistro$,"|",compatversion$,"|"
    END IF
    
    REM also accumulate the homepages...
    len9=LEN(assocarray$("Homepage"))
    REM IF len9 NE 0 THEN WRITELN DB_nameonly$," ",assocarray$("Homepage") TO homepagesfile
    REM xPackage$ is the base path, which is usually the generic name for this pkg...
    REM IF xPackage$ NE DB_nameonly$ THEN WRITELN xPackage$," ",assocarray$("Homepage") TO homepagesfile
    IF len9 NE 0 THEN WRITELN xPackage$," ",assocarray$("Homepage") TO homepagesfile
   
   END IF
   assocarray$("Description")=""
   assocarray$("Filename")=""
   assocarray$("Package")=""
   assocarray$("InstalledSize")=""
   assocarray$("Architecture")=""
   assocarray$("Version")=""
   assocarray$("Depends")=""
   assocarray$("Homepage")=""
   assocarray$("Section")=""
   assocarray$("STARTMARKER")=""
   endflag=0
  END IF
  
 ENDIF
WEND


CLOSE FILE myfile
CLOSE FILE homepagesfile
IF runningpuppy EQ 0 THEN CLOSE FILE newinvaliddepsfile
