

/* path to temporary directory */
TempDir           = "DH1:t/"


/*

These lines describe the various crunchers and how to invoke them.
The template is as follows:


Cruncher.<number>.Name
    The name of the cruncher. Can be any string without "|" characters.

Cruncher.<number>.ID
    The cruncher's ID as used in the filetypes. You MUST have a filetype
    with this ID if this script should be able to recognize archives of
    this filetype.

Cruncher.<number>.Pack
    Path and options for this cruncher (packing). If you specify ""
    (including quotes) here, you can disable packing for this cruncher.

Cruncher.<number>.Unpack
    Path and options for this cruncher (unpacking). If you specify ""
    (including quotes) here, you can disable unpacking for this cruncher.

Cruncher.<number>.Suffix
    The usual archive suffix for archives of this cruncher (eg. ".lha",
    ".lzx" and so on).

Cruncher.<number>.Quotes
    If you specify "yes" here (without quotes), you can tell the script
    that this cruncher accepts quotes in file names.

    Set this to "no" (without quotes), if it doesn't.


Notes: - Cruncher #1 is the default cruncher. That means you can immediately
         use it to crunch archives by pressing <Return> in the requester.
       - Cruncher numbers start with 1!

*/
Cruncher.1.Name   = "LZX"
Cruncher.1.ID     = "LZX"
Cruncher.1.Pack   = "LZX -3 -r -x -e -a -Y -X0 -w" || TempDir || " a --"
Cruncher.1.Unpack = "LZX -m -X0 -w" || TempDir || " x --"
Cruncher.1.Suffix = ".lzx"
Cruncher.1.Quotes = yes

Cruncher.2.Name   = "LhA"
Cruncher.2.ID     = "LHA"
Cruncher.2.Pack   = "LhA -2 -r -x -e -a -Z -X -w" || TempDir || " a -Qo"
Cruncher.2.Unpack = "LhA -m -M -X -w" || TempDir || " x -Qo"
Cruncher.2.Suffix = ".lha"
Cruncher.2.Quotes = yes

Cruncher.3.Name   = "LhArc"
Cruncher.3.ID     = "LZH"
Cruncher.3.Pack   = "LhA -0 -r -x -e -a -Z -X -w" || TempDir || " a -Qo"
Cruncher.3.Unpack = "LhA -m -M -X -w" || TempDir || " x -Qo"
Cruncher.3.Suffix = ".lzh"
Cruncher.3.Quotes = yes

Cruncher.4.Name   = "LhArc (store)"
Cruncher.4.ID     = ""
Cruncher.4.Pack   = "LhA -z -r -x -e -a -X -w" || TempDir || " a -Qo"
Cruncher.4.Unpack = ""
Cruncher.4.Suffix = ".lzh"
Cruncher.4.Quotes = yes

Cruncher.5.Name   = "LhASFX"
Cruncher.5.ID     = "LHASFX"
Cruncher.5.Pack   = ""
Cruncher.5.Unpack = "LhA -m -M -X -w" || TempDir || " x -Qo"
Cruncher.5.Suffix = ".run"
Cruncher.5.Quotes = yes

Cruncher.6.Name   = "PKZip"
Cruncher.6.ID     = "ZIP"
Cruncher.6.Pack   = "Zip -r -k -g -9"
Cruncher.6.Unpack = "UnZip -o"
Cruncher.6.Suffix = ".zip"
Cruncher.6.Quotes = no




/************ PLEASE DO NOT CHANGE ANYTHING BELOW THIS LINE ************/

/*
$VER: Archive.dopus5 1.0 (11.8.97) © by Maik "BLiZZeR" Schreiber [Freeware]
*/


LF = "0a"x

Signal ON SYNTAX
Signal ON BREAK_C

Options RESULTS
Options FAILAT 21

Parse ARG DOpusPort function source dummy .

Address VALUE DOpusPort

If (function = "init") Then
  Call init
Else If (function = "PackArc") Then
  Call PackArc
Else If (function = "UnpackArc") Then
  Call UnpackArc
Call Quit


init: Procedure
'dopus command "PackArc" program "Archive" desc "Packs archives" source'
'dopus command "UnpackArc" program "Archive" desc "Unpacks archives" source'
Return


PackArc:
'lister query ' || source || ' numseldirs'
seldirs = RESULT
If (seldirs < 1) Then
  Return
'lister query ' || source || ' numentries'
entries = RESULT
'lister query ' || source || ' path'
sourcepath = Strip(RESULT, "B", '"')
'lister set ' || source || ' busy 1'
gads = ""
Do dummy = 1 WHILE (Cruncher.dummy.Name ~= ("CRUNCHER." || dummy || ".NAME"))
  If (Cruncher.dummy.Pack ~= "") Then
    gads = gads || Cruncher.dummy.Name || "|"
End
gads = gads || "Abbruch"
'lister request ' || source || ' "Which cruncher do you want to use?" ' || gads
curr_cruncher = RC
If (curr_cruncher ~= 0) Then
Do
  dummy = 0
  Do dummy2 = 1 WHILE (Cruncher.dummy2.Name ~= ("CRUNCHER." || dummy2 || ".NAME"))
    If (Cruncher.dummy2.Pack ~= "") Then
      dummy = dummy + 1
    If (dummy = curr_cruncher) Then
      Break
  End
  curr_cruncher = dummy2

  If (curr_cruncher ~= 0) Then
  Do
    'lister set ' || source || ' newprogress name file info bar abort'
    'lister set ' || source || ' newprogress title Creating archives...'
    'lister set ' || source || ' newprogress info Cruncher: ' || Cruncher.curr_cruncher.Name
    curr_dir = 1
    Do dummy = 0 TO (entries - 1)
      'lister query ' || source || ' abort'
      If (RESULT = 1) Then
        Break
      'lister query ' || source || ' entry #' || dummy || ' stem SOURCEFILE'
      If ((sourcefile.type > 0) & (sourcefile.selected = 1)) Then
      Do
        'lister set ' || source || ' newprogress name ' || sourcefile.name || Cruncher.curr_cruncher.Suffix
        'lister set ' || source || ' newprogress file ' || seldirs || " " || curr_dir
        'lister set ' || source || ' newprogress bar ' || seldirs || " " || curr_dir
        If (Cruncher.curr_cruncher.Quotes = yes) Then
          quote = '"'
        Else
          quote = ""
        Pragma("D", sourcepath || sourcefile.name)
        Address COMMAND Cruncher.curr_cruncher.Pack || " " || quote || sourcepath || sourcefile.name || Cruncher.curr_cruncher.Suffix || quote || " *"
        ret = RC
        Pragma("D", sourcepath)
        If (ret = 0) Then
          Address COMMAND 'Delete >NIL: "' || sourcepath || sourcefile.name || '" ALL FORCE'
        Else
        Do
          Address COMMAND 'Delete >NIL: "' || sourcepath || sourcefile.name || Cruncher.curr_cruncher.Suffix || '" FORCE'
          'lister request ' || source || ' "Errors creating archive' || LF || sourcepath || sourcefile.name || Cruncher.curr_cruncher.Suffix || '!"'
        End
        curr_dir = curr_dir + 1
      End
    End
    'lister clear ' || source || ' progress'
    'lister set ' || source || ' busy 0'
    'lister read ' || source || ' "' || sourcepath || '" force'
  End
End
'lister set ' || source || ' busy 0'
Return


UnpackArc:
'lister query ' || source || ' numselfiles'
selfiles = RESULT
If (selfiles < 1) Then
  Return
'lister query ' || source || ' numentries'
entries = RESULT
'lister query ' || source || ' path'
sourcepath = Strip(RESULT, "B", '"')
'lister set ' || source || ' busy 1'
'lister set ' || source || ' newprogress name file info bar abort'
'lister set ' || source || ' newprogress title Decrunching archives...'
curr_arc = 1
Do dummy = 0 TO (entries - 1)
  'lister query ' || source || ' abort'
  If (RESULT = 1) Then
    Break
  'lister query ' || source || ' entry #' || dummy || ' stem SOURCEFILE'
  If ((sourcefile.type < 0) & (sourcefile.selected = 1)) Then
  Do
    curr_cruncher = get_cruncher(sourcepath || sourcefile.name)
    'lister set ' || source || ' newprogress name ' || sourcefile.name
    'lister set ' || source || ' newprogress file ' || selfiles || " " || curr_arc
    If (curr_cruncher ~= 0) Then
      cruncher_name = Cruncher.curr_cruncher.Name
    Else
      cruncher_name = "«unknown»"
    'lister set ' || source || ' newprogress info Cruncher: ' || cruncher_name
    'lister set ' || source || ' newprogress bar ' || selfiles || " " || curr_arc
    If (curr_cruncher ~= 0) Then
    Do
      If (Cruncher.curr_cruncher.UnPack ~= "") Then
      Do
        dir = sourcepath || sourcefile.name
        dir = Left(dir, Length(dir) - Length(Cruncher.curr_cruncher.Suffix))
        Address COMMAND 'MakeDir >NIL: "' || dir || '"'
        If (RC = 0) Then
        Do
          If (Cruncher.curr_cruncher.Quotes = yes) Then
            quote = '"'
          Else
            quote = ""
          Pragma("D", dir)
          Address COMMAND Cruncher.curr_cruncher.Unpack || " " || quote || sourcepath || sourcefile.name || quote
          ret = RC
          Pragma("D", sourcepath)
          If (ret = 0) Then
            Address COMMAND 'Delete >NIL: "' || sourcepath || sourcefile.name || '" FORCE'
          Else
          Do
            Address COMMAND 'Delete >NIL: "' || dir || '" FORCE'
            'lister request ' || source || ' "Errors decrunching archive' || LF || sourcepath || sourcefile.name || '!"'
          End
        End
      End
    End
    curr_arc = curr_arc + 1
  End
End
'lister clear ' || source || ' progress'
'lister set ' || source || ' busy 0'
'lister read ' || source || ' "' || sourcepath || '" force'
Return


get_cruncher:
Parse ARG get_cruncher_file
'dopus getfiletype "' || get_cruncher_file || '" id'
get_cruncher_id = RESULT
get_cruncher_num = 0
Do cruncher_num = 1 WHILE (Cruncher.cruncher_num.Name ~= ("CRUNCHER." || cruncher_num || ".NAME"))
  If (Cruncher.cruncher_num.ID = get_cruncher_id) Then
  Do
    get_cruncher_num = cruncher_num
    Break
  End
End
Return get_cruncher_num


SYNTAX:
BREAK_C:
Address VALUE DOpusPort
'dopus request "Error in ARexx script' || LF || "Archive.dopus5," || LF || "Line " || SIGL || ":" || LF || LF || ErrorText(RC) || '"'
Call Quit


Quit:
Exit

