/****************************************************************************************************/
/*                                                                                                  */
/* deploy.command.c:                                                                                */
/*                                                                                                  */
/****************************************************************************************************/

/****************************************************************************************************/
/*                                                                                                  */
/*     Copyright (C) 2006 Joerg Kunze                                                               */
/*                                                                                                  */
/*     This file is part of siliconBrain documentation.                                             */
/*                                                                                                  */
/*     siliconBrain documentation is free software; you can redistribute it and/or modify           */
/*     it under the terms of the GNU General Public License as published by                         */
/*     the Free Software Foundation; either version 2 of the License, or                            */
/*     (at your option) any later version.                                                          */
/*                                                                                                  */
/*     siliconBrain documentation is distributed in the hope that it will be useful,                */
/*     but WITHOUT ANY WARRANTY; without even the implied warranty of                               */
/*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                */
/*     GNU General Public License for more details.                                                 */
/*                                                                                                  */
/*     You should have received a copy of the GNU General Public License                            */
/*     along with this program; if not, write to the Free Software                                  */
/*     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                    */
/*                                                                                                  */
/****************************************************************************************************/
#include <ftplib.h>
#include <glob.h>

#include "deploy"

const char *deploySiliconBrainRelease       = "$siliconBrainRelease: 0.1.1 $";
const char *deploySiliconBrainRcsIdentifier = "$Id: deploy.command.c,v 1.3 2006/05/02 23:09:35 joerg Exp $";
const char *deploySiliconBrainSaveStamp     = "$siliconBrainSaveStamp: 2006/04/16 00:21:17, Joerg Kunze$";


static int ftpError( char *command, netbuf *connection ) {
   fprintf( stderr, "Ftp%s: %s\n", command, FtpLastResponse( connection ) );
   return 1;
}

#define tryFtp( command, ... )\
   if( !Ftp##command( __VA_ARGS__ ) ) {\
      ftpError( #command, connection );\
      return commandError;\
   }

extern CommandReturnCode deploy( const deployOptions *options ) {
   netbuf *connection;

   FtpInit();

   tryFtp( Connect, options->deployTargetHost, &connection );

   Obstack obstack;
   obstack_init( &obstack );
   Netrc netrc;
   netrcInit( &netrc, &obstack );
   NetrcEntry *netrcEntry = netrcLookup( &netrc, options->deployTargetHost );
   tryFtp( Login, netrcEntry->login, netrcEntry->password, connection );

   tryFtp( Chdir, options->deployTargetWeb, connection );

   glob_t globData;
   zero( globData );

   glob( "*.{htm,html}", GLOB_BRACE, 0, &globData );

   char **file;

   for( file = globData.gl_pathv; *file; ++file )
      tryFtp( Put, *file, *file, FTPLIB_ASCII, connection );

   globfree( &globData );

   obstack_free( &obstack, 0 );

   FtpClose( connection );

   return commandOk;
}

/*
$Log: deploy.command.c,v $
Revision 1.3  2006/05/02 23:09:35  joerg
published for new release 0.1.1

Revision 1.2  2006/04/26 23:19:14  joerg
ftp all htm and html files to the web site

Revision 1.1  2006/04/05 22:55:51  joerg
first do nothing version of deploy command


*/