//////////////////////////////////////////////////////////////////////////////
// FILE    : uploader.tol
// PURPOSE : call all prepared tests about TOL features checking are binding 
// to decide if a version should be published or not.
//////////////////////////////////////////////////////////////////////////////

//Text compress = "\"c:\Archivos de programa\7-Zip\7z.exe\" a -r -tzip ";
Text compress = " ";
Text tolpkg = TmpDir+"tol_pkg/";
Text local = ".";
Text export = tolpkg+"export";
Text version = Replace(Tokenizer(Version," ")[1],".","_");
Text upload = tolpkg+"tol_pkg/"+version;
Real OSDirRemove(tolpkg);

Real OSCmdWaitWr(Text order)
{
  WriteLn(order);
  Real OSCmdWait(order)
};

Real OSCmdWaitWr("svn export --force "
  "https://www.tol-project.org/svn/tolp/trunk/tol_pkg \""+export+"\"");

Set packages = 
{
  Set aux = Select(GetDir(export)[2], Real(Text subDir)
  {
    Real fst = ASCII(Sub(subDir,1,1)); 
    LE(ASCII("A"),fst,ASCII("Z"))
  });
  Sort(aux,Real(Text a, Text b) { Compare(a,b) })
};
  
Text SetOfTextToWikiList(Set set, Text margin, Text quote)
{
  If(Card(set)==1,quote+set[1]+quote,
  SetSum(For(1,Card(set),Text(Real k)
  {
    margin+quote+set[k]+quote
  })))
};

Real OSDirMake(upload);

Real wiki = FOpen(tolpkg+"wiki.txt","wb");

Real FPutText(wiki,ReadFile("_wiki_header.txt"));

Text WriteFile(tolpkg+"compress.bat",
"cd \""+ReplaceSlash(export)+"\"\n"+
"zip -9 -r %1.zip %1\n"
"mv %1.zip ..\\tol_pkg\\"+version+"\n"
);

Set EvalSet(packages, Real(Text name)
{
  Text exp = export+"/"+name;
  Text upl = upload+"/"+name;
  Set aux = {[[Include(exp+"/"+name+".tol")]]};
  NameBlock nb = aux[1][1];
  Real FPutText(wiki,
"
=== !"+name+" ===
"+nb::_.autodoc.description+"
 * '''keywords''' : "+SetOfTextToWikiList(nb::_.autodoc.keys," ","__")+"
 * '''authors''' : "+SetOfTextToWikiList(nb::_.autodoc.authors," ","''")+"
 * '''links''' : "
  " ["+name+" Official page]"
  " [source:/tolp/trunk/tol_pkg/"+name+" view source code]"
  " [http://packages.tol-project.org/tol_pkg/"+name+".zip download full package]"
  " [http://packages.tol-project.org/tol_pkg/"+name+".oza download just OIS compressed package]
");
  Real Ois.Store(aux[1],upl+".oza");
  Real OSCmdWaitWr("\""+tolpkg+"compress.bat\" "+name)
});

Real FClose(wiki);

Text remoteUser = 
  "toldevel@tolp.localbayes.es";
Text remoteRoot = 
  "/var/www/packages";

Text WriteFile(upload+".bat",
"cd \""+ReplaceSlash(tolpkg+"/tol_pkg")+"\"\n"+
"tar cvzf "+version+".tgz "+version+"\n"+
"pscp "+version+".tgz "+remoteUser+":"+remoteRoot+"/tol_pkg/"+version+".tgz\n"
"plink "+remoteUser+" \"cd "+remoteRoot+"/tol_pkg; ./upload_tol_pkg.sh "+version+"\"\n"
);
Real OSCmdWaitWr("\""+ReplaceSlash(upload+".bat")+"\"");



/* */
