1	# =====================================================================
     2	# blogger.setTemplate: BloggerAPI method.
     3	#
     4	# Copyright (c) 2007,2009 Carlo Strozzi
     5	#
     6	# This program is free software; you can redistribute it and/or modify
     7	# it under the terms of the GNU General Public License as published by
     8	# the Free Software Foundation; version 2 dated June, 1991.
     9	#
    10	# This program is distributed in the hope that it will be useful,
    11	# but WITHOUT ANY WARRANTY; without even the implied warranty of
    12	# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13	# GNU General Public License for more details.
    14	#
    15	# You should have received a copy of the GNU General Public License
    16	# along with this program; if not, write to the Free Software
    17	# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    18	#
    19	# =====================================================================
    20	
    21	# =====================================================================
    22	# Notes:
    23	# 
    24	# TW uses a more elaborated template mechanism than Blogger, and the
    25	# latter API is not expressive enough to fulfill the TW capabilities.
    26	# This RPC2 method is therefore provided only for completeness but
    27	# it is of very limited use; please consider it a proof-of-concept.
    28	# Futhermore, given the file permission scheme used by a typical TW
    29	# setup, it is unlikely that CGI programs have write access to the
    30	# template directory, hence the operation is bound to failure.
    31	# =====================================================================
    32	
    33	# =====================================================================
    34	#		Local variables and functions
    35	# =====================================================================
    36	
    37	cgi.group = ()
    38	cgi.group.literal = ()
    39	cgi.tpl = tb					# Default.
    40	cgi.tpl.text = ()				# Small template
    41	cgi.tpl.file = /dev/null			# Large template
    42	tmp1 = /dev/null
    43	
    44	# =====================================================================
    45	#			Main program
    46	# =====================================================================
    47	
    48	csaGetArgs POST
    49	
    50	. $CSA_ROOT/lib/group-stuff.rc
    51	
    52	. $CSA_ROOT/lib/group-editor.rc
    53	
    54	# Prevent non editors from replacing templates.
    55	
    56	~ ,$TNS_AUTH_GRP, *,editor,* || csaExit.needauth
    57	
    58	# Save the new template to the target directory, which can only be the
    59	# server's public page area, as given the file permission scheme used by
    60	# a typical TW setup, it is unlikely that CGI programs have write access
    61	# to the template directory. Let's be very careful on the public attachment
    62	# directory, as it may not be owned by us.
    63	
    64	tw_stem = $CSA_DOCROOT/$CSA_LANG/$TNS_ATTACH_PUBDIR/$'cgi.group'
    65	test -d $tw_stem || csaExit.fault 0037 1 $CSA_PGM($#CSA_PGM)
    66	touch $tw_stem/.touch || csaExit.fault 0009 $tw_stem/.touch
    67	
    68	# No TPC really needed here, also because the two associated *.fmt
    69	# files are currently created directly by bloggerSetTemplate.awk and
    70	# to apply TPC I would need to use temporary files instead. If anything
    71	# fails then the operation can simply be repeated until the target files
    72	# are re-created correctly.
    73	
    74	csaAwkCmd bloggerSetTemplate.awk
    75	
    76	if (!~ $'cgi.tpl.text' ()) {
    77	   echo -n $'cgi.tpl.text' > $tw_stem/$'cgi.tpl'.$CSA_TPLEXT ||
    78			csaExit.fault 0009 $tw_stem/$'cgi.tpl'.$CSA_TPLEXT
    79	
    80	   # At the moment I only translate the main template, see the specs.
    81	   ~ $'cgi.tpl' tb &&
    82	     echo -n $'cgi.tpl.text' | $CSA_RESULT > $tw_stem/.tw.$CSA_TPLEXT ||
    83					csaExit.fault 0009 $tw_stem/.tw.$CSA_TPLEXT
    84	}  else {
    85	   if (!~ $'cgi.tpl.file' /dev/null) {
    86	      cp $'cgi.tpl.file' $tw_stem/$'cgi.tpl'.$CSA_TPLEXT ||
    87				csaExit.fault 0009 $tw_stem/$'cgi.tpl'.$CSA_TPLEXT
    88	
    89	      # At the moment I only translate the main template, see the specs.
    90	      ~ $'cgi.tpl' tb &&
    91	        $CSA_RESULT < $'cgi.tpl.file' > $tw_stem/.tw.$CSA_TPLEXT ||
    92				csaExit.fault 0009 $tw_stem/.tw.$CSA_TPLEXT
    93	   }  else {
    94	      # Tell the client this is her fault, not ours.
    95	      CSA_RPC2_ERR = 4
    96	      csaExit.fault 0038
    97	   }
    98	}
    99	
   100	#if (~ $tpl_name tw-*) {
   101	#
   102	#   # Page names beginning with tw-* are reserved for TW use, and usually
   103	#   # refer to views that do not correspond to actual pages on disk, so
   104	#   # we need to toggle unapplicable sections in templates.
   105	#
   106	#   tpl.if.tw.ispage = '(::DEL:)'
   107	#   tpl.fi.tw.ispage = '(:DEL::)'
   108	#   tpl.if.tw.editor = '(::DEL:)'
   109	#   tpl.fi.tw.editor = '(:DEL::)'
   110	#}
   111	
   112	csaExit.ok
   113	
   114	# End of program.