From 9bd6f8ef5cb773e7c3eb06f39d85bd5b94c037ec Mon Sep 17 00:00:00 2001
From: Nick White <arch@njw.me.uk>
Date: Thu, 3 May 2007 17:51:44 +0000
Subject: Added a script to ease releases, improved cli interface

Added the script prepare-release.sh
Modified and added options to allow easy downloading of single
issues and media files
Moved functions outputting to the command line to cli.c
Added a verbose flag

git-archimport-id: getht@sv.gnu.org/getht--mainline--0.1--patch-24
---
 src/Makefile.am |   2 +-
 src/cli.c       |  99 ++++++++++++++++++++++++++++++++++++
 src/getht.c     | 154 ++++++++++++++++++--------------------------------------
 src/issue.h     |   2 +-
 src/tocxml.c    |  13 ++---
 src/version.h   |   2 +-
 6 files changed, 156 insertions(+), 116 deletions(-)
 create mode 100644 src/cli.c

(limited to 'src')

diff --git a/src/Makefile.am b/src/Makefile.am
index 0bf3452..79f8673 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2,6 +2,6 @@
 
 bin_PROGRAMS = getht
 
-getht_SOURCES = config.c download.c getht.c issuemem.c	\
+getht_SOURCES = config.c cli.c download.c getht.c issuemem.c	\
 		mediarev.c mediaxml.c tocxml.c xml.c	\
 		getht.h issue.h version.h
diff --git a/src/cli.c b/src/cli.c
new file mode 100644
index 0000000..51ba380
--- /dev/null
+++ b/src/cli.c
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2006 Nick White
+ *
+ * This file is part of GetHT
+ * 
+ * GetHT 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.
+ *
+ * GetHT 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 GetHT; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "issue.h"
+#include "getht.h"
+
+void list_issues(iss ** issue, int no_of_issues, int verbose)
+{
+	int iss_no, sec_no, it_no;
+	for(iss_no=0;iss_no<=no_of_issues;iss_no++)
+	{
+		printf("[%i]\t", iss_no);
+		printf("%s\n", issue[iss_no]->title);
+		if(verbose >= 1)
+		{
+			for(sec_no=0; sec_no<=issue[iss_no]->no_of_sections; sec_no++)
+			{
+				printf("\t%i: %s\n", issue[iss_no]->section[sec_no]->number,
+					issue[iss_no]->section[sec_no]->title);
+				printf("\t%s\n", issue[iss_no]->section[sec_no]->uri);
+				if(verbose >= 2)
+				{
+					for(it_no=0;
+						it_no<=issue[iss_no]->section[sec_no]->no_of_items;
+						it_no++)
+					{
+						printf("\t\tpp %i - %i: %s\n",
+							issue[iss_no]->section[sec_no]->item[it_no]->firstpage,
+							issue[iss_no]->section[sec_no]->item[it_no]->lastpage,
+							issue[iss_no]->section[sec_no]->item[it_no]->title);
+					}
+				}
+			}
+		}
+	}
+}
+
+void list_media(iss ** issue, int no_of_issues, int verbose)
+{
+	int iss_no, med_no, med_global;
+	for(iss_no=0, med_global=0;iss_no<=no_of_issues;iss_no++)
+	{
+		if(issue[iss_no]->no_of_media >= 0)
+		{
+			printf("%s\n", issue[iss_no]->title);
+			for(med_no=0; med_no <= (issue[iss_no]->no_of_media); med_no++, med_global++)
+			{
+				printf("[%i]\t", med_global);
+				printf("%s\n", issue[iss_no]->media[med_no]->title);
+				if(verbose >= 1)
+				{
+					printf("\t%s\n", issue[iss_no]->media[med_no]->uri);
+					if(verbose >=2 && issue[iss_no]->media[med_no]->comment[0]!='\0')
+							printf("\t%s\n", issue[iss_no]->media[med_no]->comment);
+				}
+			}
+		}
+	}
+}
+
+void showusage()
+{
+	printf("Usage: getht [-ualmofhv] [-d issno] [-n medno] [-t tocfile]\n");
+	printf("-u | --update                 Update contents files\n");
+	printf("-a | --download-all           Download all issues\n");
+	printf("-d | --download-issue issno   Download issue number issno\n");
+	printf("-n | --download-media medno   Download media number n\n");
+	printf("-o | --download-all-media     Download all media\n");
+	printf("-f | --force                  Force re-download of existing files\n");
+	printf("-l | --list-issues            List available issues\n");
+	printf("-m | --list-media             List available media\n");
+	printf("-t | --tocfile file           Use alternative contents xml file\n");
+	printf("-x | --mediatocfile file      Use alternative media contents xml file\n");
+	printf("-h | --help                   Print this help message\n");
+	printf("-v | --verbose                Make output more verbose\n");
+	printf("-V | --version                Print version information\n");
+}
diff --git a/src/getht.c b/src/getht.c
index 5d2c39c..b9a18b7 100644
--- a/src/getht.c
+++ b/src/getht.c
@@ -35,10 +35,6 @@ int update_contents_files();
 
 med * findnewestmed(iss ** issue, int no_of_issues);
 
-void show_iss_struct(iss ** issue, int no_of_issues);
-
-void showusage();
-
 proxytype proxy_type; char proxy_addr[STR_MAX]; long proxy_port;
 proxyauth proxy_auth; 
 char proxy_user[STR_MAX]; char proxy_pass[STR_MAX];
@@ -67,11 +63,12 @@ int main(int argc, char *argv[])
 
 	snprintf(save_path,STR_MAX,"%s/hinduism_today",getenv("HOME"));
 
-	int downall = 0, downlatest = 0;
-	int downallmedia = 0, downlatestmedia = 0;
-	int downissue = 0, downmedia = 0;
-	int force = 0, update = 0, showstr = 0;
-	int option = 0;
+	int downall = 0, downallmedia = 0;
+	int downissue = 0, downissueno = -1;
+	int downmedia = 0, downmediano = -1;
+	int listissues = 0, listmedia = 0;
+	int force = 0, update = 0;
+	int verbose = 0, option = 0;
 
 	proxy_type = NONE;
 	proxy_port = 0;
@@ -98,18 +95,21 @@ int main(int argc, char *argv[])
 	static struct option long_opts[] =
 	{
 		{"download-all", no_argument, 0, 'a'},
-		{"download-latest", no_argument, 0, 'd'},
-		{"download-all-media", no_argument, 0, 'm'},
-		{"download-latest-media", no_argument, 0, 'n'},
+		{"download-issue", required_argument, 0, 'd'},
+		{"download-media", required_argument, 0, 'n'},
+		{"download-all-media", no_argument, 0, 'o'},
 		{"force", no_argument, 0, 'f'},
+		{"list-issues", no_argument, 0, 'l'},
+		{"list-media", no_argument, 0, 'm'},
 		{"update", no_argument, 0, 'u'},
 		{"tocfile", required_argument, 0, 't'},
 		{"mediatocfile", required_argument, 0, 'x'},
 		{"help", no_argument, 0, 'h'},
-		{"version", no_argument, 0, 'v'},
+		{"verbose", no_argument, 0, 'v'},
+		{"version", no_argument, 0, 'V'},
 		{0, 0, 0, 0}
 	};
-	while((c = getopt_long(argc, argv, "adfhmnsuvx:t:", long_opts, NULL)) != -1) {
+	while((c = getopt_long(argc, argv, "ad:fhlmn:osuvVx:t:", long_opts, NULL)) != -1) {
 		switch(c) {
 			case 'a':
 				downall = 1;
@@ -117,18 +117,26 @@ int main(int argc, char *argv[])
 				option = 1;
 				break;
 			case 'd':
-				downlatest = 1;
 				downissue = 1;
+				downissueno = atoi(optarg);
+				option = 1;
+				break;
+			case 'l':
+				listissues = 1;
 				option = 1;
 				break;
 			case 'm':
+				listmedia = 1;
+				option = 1;
+				break;
+			case 'o':
 				downallmedia = 1;
 				downmedia = 1;
 				option = 1;
 				break;
 			case 'n':
-				downlatestmedia = 1;
 				downmedia = 1;
+				downmediano = atoi(optarg);
 				option = 1;
 				break;
 			case 'f':
@@ -139,10 +147,6 @@ int main(int argc, char *argv[])
 				update = 1;
 				option = 1;
 				break;
-			case 's':
-				showstr = 1;
-				option = 1;
-				break;
 			case 't':
 				strcpy(issue_xml, strdup(optarg));
 				option = 1;
@@ -156,6 +160,10 @@ int main(int argc, char *argv[])
 				return 0;
 				break;
 			case 'v':
+				verbose++;
+				option = 1;
+				break;
+			case 'V':
 				printf("GetHT version: %s\n",VERSION);
 				option = 1;
 				return 0;
@@ -181,12 +189,11 @@ int main(int argc, char *argv[])
 
 	iss **issue;
 	int no_of_issues = -1;
-	int latest_index = -1;
 	int i;
 
-	if(downissue || showstr)
+	if(downissue || listissues)
 	{
-		issue = parsetoc(issue_xml, &no_of_issues, &latest_index);
+		issue = parsetoc(issue_xml, &no_of_issues);
 
 		if(!issue)
 		{
@@ -195,7 +202,7 @@ int main(int argc, char *argv[])
 				printf("Cannot open contents file, trying to update contents\n");
 				if(update_contents_files())
 					return 1;
-				issue = parsetoc(issue_xml, &no_of_issues, &latest_index);
+				issue = parsetoc(issue_xml, &no_of_issues);
 			}
 			else
 			{
@@ -204,24 +211,16 @@ int main(int argc, char *argv[])
 			}
 		}
 
-		if(latest_index == -1)
-		{
-			fprintf(stderr, "Error: Cannot ascertain latest issue. ");
-			fprintf(stderr, "Defaulting to first issue in contents file\n");
-			latest_index = 0;
-		}
-
 		if(downall)
 		{
 			for(i = 0; i < no_of_issues; i++)
 				downloadissue(NULL, save_path, issue[i], force);
 		}
-	
-		if(downlatest)
-			downloadissue(NULL, save_path, issue[latest_index], force);
+		else if(downissueno >= 0 && downissueno <= no_of_issues)
+			downloadissue(NULL, save_path, issue[downissueno], force);
 	}
 	
-	if(downmedia || showstr)
+	if(downmedia || listmedia)
 	{
 		int newest;
 
@@ -243,13 +242,6 @@ int main(int argc, char *argv[])
 			}
 		}
 
-		if(downlatestmedia)
-		{
-			newest = findnewestiss(issue, no_of_issues);
-			for(i = 0; i <= issue[newest]->no_of_media; i++)
-				downloadmedia(NULL, save_path, issue[newest]->media[i], force);
-		}
-
 		if(downallmedia)
 		{
 			for(i = 0; i <= no_of_issues; i++)
@@ -258,10 +250,23 @@ int main(int argc, char *argv[])
 					downloadmedia(NULL, save_path, issue[i]->media[newest], force);
 			}
 		}
+		else if(downmediano >= 0)
+		{
+			int med_global, med_no;
+
+			for(i=0,med_global=0; i<=no_of_issues; i++)
+				if(issue[i]->no_of_media >= 0)
+					for(med_no=0; med_no <= (issue[i]->no_of_media); med_no++,med_global++)
+						if(med_global == downmediano)
+							downloadmedia(NULL, save_path, issue[i]->media[med_no], force);
+		}
 	}
 
-	if(showstr)
-		show_iss_struct(issue, no_of_issues);
+	if(listissues)
+		list_issues(issue, no_of_issues, verbose);
+
+	if(listmedia)
+		list_media(issue, no_of_issues, verbose);
 
 	/* Ensure curl cleans itself up */
 	curl_easy_cleanup(main_curl_handle);
@@ -306,52 +311,6 @@ int update_contents_files()
 	return 0;
 }
 
-void show_iss_struct(iss ** issue, int no_of_issues)
-/*	Prints issue information */
-{
-	int iss_no, sec_no, med_no, it_no;
-	printf("%i Issues\n",no_of_issues);
-	for(iss_no=0;iss_no<=no_of_issues;iss_no++)
-	{
-		printf("-Issue %i-\n", (iss_no+1));
-		printf("Title:\t'%s'\n", issue[iss_no]->title);
-		printf("Preview URI:\t'%s'\n", issue[iss_no]->preview_uri);
-		printf("Months:\t'%i' - '%i'\n",issue[iss_no]->date.firstmonth,issue[iss_no]->date.lastmonth);
-		printf("Year:\t'%i'\n",issue[iss_no]->date.year);
-		printf("Number of Sections:\t'%i'\n",issue[iss_no]->no_of_sections);
-
-		for(sec_no=0; sec_no <= (issue[iss_no]->no_of_sections); sec_no++)
-		{
-			printf("\t-Section %i-\n", (sec_no));
-			printf("\tTitle:\t'%s'\n", issue[iss_no]->section[sec_no]->title);
-			printf("\tURI:\t'%s'\n", issue[iss_no]->section[sec_no]->uri);
-			printf("\tNo. of Items:\t'%i'\n", issue[iss_no]->section[sec_no]->no_of_items);
-
-			for(it_no=0; it_no <= issue[iss_no]->section[sec_no]->no_of_items; it_no++)
-			{
-				printf("\t\t-Item-\n");
-				printf("\t\tTitle:\t'%s'\n",issue[iss_no]->section[sec_no]->item[it_no]->title);
-				printf("\t\tFirst page:\t'%i'",issue[iss_no]->section[sec_no]->item[it_no]->firstpage);
-				printf("\tLast page:\t'%i'\n",issue[iss_no]->section[sec_no]->item[it_no]->lastpage);
-			}
-			it_no = 0;
-		}
-		sec_no = 0;
-
-		printf("Number of Media:\t'%i'\n",issue[iss_no]->no_of_media);
-
-		for(med_no=0; med_no <= (issue[iss_no]->no_of_media); med_no++)
-		{
-			printf("\t-Media %i-\n", (med_no));
-			printf("\tTitle:\t'%s'\n", issue[iss_no]->media[med_no]->title);
-			printf("\tURI:\t'%s'\n", issue[iss_no]->media[med_no]->uri);
-			printf("\tComment:\t'%s'\n", issue[iss_no]->media[med_no]->comment);
-			printf("\tPreview URI:\t'%s'\n", issue[iss_no]->media[med_no]->preview_uri);
-		}
-		med_no = 0;
-	}
-}
-
 int findnewestiss(iss ** issue, int no_of_issues)
 /*	returns newest issue indice */
 {
@@ -377,18 +336,3 @@ int findnewestiss(iss ** issue, int no_of_issues)
 
 	return new_iss;
 }
-
-void showusage()
-{
-	printf("Usage: getht -u -a -d -m -n -f [-t tocfile] -h -v\n");
-	printf("-u | --update                 Update contents files\n");
-	printf("-a | --download-all           Download all issues\n");
-	printf("-d | --download-latest        Download latest issue\n");
-	printf("-m | --download-all-media     Download all media\n");
-	printf("-n | --download-latest-media  Download latest issue's media\n");
-	printf("-f | --force                  Force re-download of existing files\n");
-	printf("-t | --tocfile file           Use alternative contents xml file\n");
-	printf("-x | --mediatocfile file      Use alternative media contents xml file\n");
-	printf("-h | --help                   Print this help message\n");
-	printf("-v | --version                Print version information\n");
-}
diff --git a/src/issue.h b/src/issue.h
index d224dcf..22499b5 100644
--- a/src/issue.h
+++ b/src/issue.h
@@ -66,7 +66,7 @@ typedef struct
 	int no_of_media;
 } iss;
 
-iss ** parsetoc(char *filepath, int * iss_no, int * latest);
+iss ** parsetoc(char *filepath, int * iss_no);
 iss ** parsemedia(char * filepath, iss ** issue, int * no_of_issues);
 
 med ** parsemediagz(char * media_path, int * no_of_media);
diff --git a/src/tocxml.c b/src/tocxml.c
index 3740326..5fc7166 100644
--- a/src/tocxml.c
+++ b/src/tocxml.c
@@ -28,15 +28,15 @@
 #include "issue.h"
 #include "getht.h"
 
-iss ** parsetoc(char *filepath, int * iss_no, int * latest);
-int parseissue(xmlDocPtr file, xmlNodePtr node, iss * cur_issue, int * latest);
+iss ** parsetoc(char *filepath, int * iss_no);
+int parseissue(xmlDocPtr file, xmlNodePtr node, iss * cur_issue);
 void parsesection(xmlDocPtr file, xmlNodePtr node, sec * cur_section);
 
 void tokenise_hyphons(char to_token[10], int * first, int * last);
 
 int no_of_issues;
 
-iss ** parsetoc(char *filepath, int * iss_no, int * latest)
+iss ** parsetoc(char *filepath, int * iss_no)
 /*	starts parsing of xml to issue structure	*/
 {
 	xmlDocPtr file;
@@ -78,7 +78,7 @@ iss ** parsetoc(char *filepath, int * iss_no, int * latest)
 							&(issue[no_of_issues]->date.lastmonth));
 
 					/* parse the issue */
-					parseissue(file, cnode, issue[no_of_issues], latest);
+					parseissue(file, cnode, issue[no_of_issues]);
 				}
 				cnode = cnode->next;
 			}
@@ -95,15 +95,12 @@ iss ** parsetoc(char *filepath, int * iss_no, int * latest)
 	return issue;
 }
 
-int parseissue(xmlDocPtr file, xmlNodePtr node, iss * cur_issue, int * latest)
+int parseissue(xmlDocPtr file, xmlNodePtr node, iss * cur_issue)
 /*	parses issue from xml, saving in cur_issue structure	*/
 {
 	strncpy(cur_issue->title, (char *) xmlGetProp(node, "title"), STR_MAX);
 	strncpy(cur_issue->preview_uri, (char *) xmlGetProp(node, "coverlink"), STR_MAX);
 
-	if(xmlGetProp(node, "current") && *latest==-1)
-		*latest = no_of_issues;
-
 	node = node->xmlChildrenNode;
 
 	while(node != NULL){
diff --git a/src/version.h b/src/version.h
index 0713569..b0c562f 100644
--- a/src/version.h
+++ b/src/version.h
@@ -19,4 +19,4 @@
  *
  */
 
-#define VERSION "0.0.1"
+#define VERSION "pre_release"
-- 
cgit v1.2.3