#!/bin/sh
#
# ablog_init - set up a directory for the ACME Blogmaker

if [ -d .ablog_items ] ; then
    echo "$0: .ablog_items directory already exists" >&2
    exit 1
fi
if [ -d .ablog_templates ] ; then
    echo "$0: .ablog_templates directory already exists" >&2
    exit 1
fi

echo "Setting up a new blog..."

mkdir .ablog_items
mkdir .ablog_templates
chmod 700 .ablog_items .ablog_templates

if [ -d archive ] ; then
    echo "$0: warning - archive subdir already exists" >&2
else
    mkdir archive
    chmod 755 archive
fi

cat > .ablog_templates/setup.sh << EOF
tab='	'
space=' '
dollar='$'
backslash='\'
squote="'"
dquote='"'
rfc822_format='%a, %d %b %Y %H:%M:%S GMT'
EOF
. .ablog_templates/setup.sh

ask_var()
    {
    var="$1"
    prompt="$2"
    default="$3"
    echo -n "$prompt [$default]: "
    read val
    if [ "$val" = '' ] ; then
	val="$default"
    fi
    echo "$val" |
      sed \
	-e "s/$squote/$squote$backslash$backslash$squote$squote/g" \
	-e "s/^/${var}='/" \
	-e "s/$dollar/'/" \
	>> .ablog_templates/setup.sh
    . .ablog_templates/setup.sh
    }

ask_var 'blog_title' 'Title of this blog' ''

ask_var 'blog_description' 'Description' ''

ask_var 'blog_url' 'URL for this blog' ''
html_filename=`echo "$blog_url" | sed -e 's,.*/,,'`
if [ "$html_filename" = '' ] ; then
    html_filename='index.html'
fi
basename=`echo "$html_filename" | sed -e 's/\.[^.]*//'`
rss2_filename="${basename}_rss2.xml"
cat >> .ablog_templates/setup.sh << EOF
html_filename='$html_filename'
rss2_filename='$rss2_filename'
EOF

ask_var 'language' 'Language' 'en-us'

whoami=`whoami`
hostname=`hostname`
blog_author_guess="$whoami@$hostname"
ask_var 'blog_author' "Blog author's email address" "$blog_author_guess"

hostname=`echo "$blog_author" | sed -e 's/.*@//'`
webmaster_guess="webmaster@$hostname"
ask_var 'webmaster' "Webmaster's email address" "$webmaster_guess"

ask_var 'max_xml_items' 'Maximum number of XML items to show' '40'

echo 'Setting up default template files...'
cat > .ablog_templates/header.html << 'EOF'
<html>
<head>
<title>$blog_title</title>
<link rel="alternate" type="application/rss+xml" title="RSS" href="$rss2_filename">
</head>   
<body>

<h2>$blog_title</h2>

<p>
$blog_description

<dl>

EOF
cat > .ablog_templates/header_rss2.xml << 'EOF'
<?xml version="1.0" encoding="iso-8859-1" ?>
<rss version="2.0">
<channel>
    <title>$xml_blog_title</title>
    <link>$blog_url</link>
    <description>
        $xml_blog_description
    </description>
    <managingEditor>$blog_author</managingEditor>
    <webMaster>$webmaster</webMaster>
    <language>$language</language>
    <lastBuildDate>$date</lastBuildDate>
    <generator>ACME Blogmaker</generator>

EOF
cat > .ablog_templates/item << 'EOF'
item_title=TITLE
item_author=$blog_author

DESCRIPTION
EOF
cat > .ablog_templates/item.html << 'EOF'
<br clear="all">
<p><dt> $item_title <A HREF="$permalink">(permalink)</A>:
<dd>
$item_description
<br><iframe style="width: 10em; height: 2em" scrolling="no" frameborder="0" src="http://www.acme.com/comments/link.cgi?url=$permalink"></iframe>

EOF
cat > .ablog_templates/item_rss2.xml << 'EOF'
    <item>
	<title>$xml_item_title</title>
	<link>$permalink</link>
	<pubDate>$item_date</pubDate>
	<author>$xml_item_author</author>
	<description>
	    $xml_item_description
	</description>
    </item>

EOF
cat > .ablog_templates/archive.html << 'EOF'
<html>
<head>
<title>$blog_title - $item_title</title>
</head>   
<body>

<h2>$blog_title</h2>
<h4>$item_title</h4>

<p>
$item_description

<br><iframe style="width: 10em; height: 2em" scrolling="no" frameborder="0" src="http://www.acme.com/comments/link.cgi"></iframe>

<hr>

Back to <a href="$blog_url">$blog_title</a>.<br>
<address><a href="mailto:$blog_author">$blog_author</a></address>

</body>
</html>
EOF
cat > .ablog_templates/trailer.html << 'EOF'
</dl>

<hr>

<address><a href="mailto:$blog_author">$blog_author</a></address>

</body>
</html>
EOF
cat > .ablog_templates/trailer_rss2.xml << 'EOF'
</channel>
</rss>
EOF

cat << 'EOF'
Done.  The setup and template files are in the subdirectory '.ablog_templates',
you can edit them however you like.  Your item files, when you create them,
will be in the subdirectory '.ablog_items'.  To make a new item, run the
script 'ablog_item'.  When it is through it will automatically run the
final script 'ablog" to actually create the HTML and RSS files.  You
can also run 'ablog' directly, for example if you change a template file.
EOF
