# NanoBlogger Tagcloud plugin

# Sample code for templates, based off default stylesheet
# This plugin define the following properties which must be
# added to your stylesheet!
#
# .tagMin { font-size: xx-small; }

# .tagDist1 { font-size: small; }
# .tagDist2 { font-size: small; bold }
# .tagDist3 { font-size: medium; }
# .tagDist4 { font-size: large; }
# .tagDist5 { font-size: large; bold }

# .tagMax { font-size: xx-large; }
#
# If you change TAG_FACTOR you should add more tags to the css
#
#
# <div class="sidetitle">
# Tag Cloud
# </div>
#
# <div class="side">
# $NB_Tagcloud
# </div>

PLUGIN_OUTFILE="$BLOG_DIR/$PARTS_DIR/tag_cloud.$NB_FILETYPE"
PLUGIN_DATA="$BLOG_DIR/data"
TAG_FACTOR=5

nb_msg "$plugins_action tagcloud..."

j=0; max=0; min=99
for i in $PLUGIN_DATA/cat_*.db; do
    cat[$j]=`head -1 $i`
    posts[$j]=`wc -l $i | awk '{ print $1 }'`

    if [ ${posts[$j]} -gt $max ]; then
	max=${posts[$j]}
    fi
    if [ ${posts[$j]} -lt $min ]; then
	min=${posts[$j]}
    fi
    ((j=$j + 1))
done
((diff=$max - $min))
((distribution=$diff / $TAG_FACTOR))
if [[ $distribution -eq 0 ]]; then
    distribution=1
fi

j=0
((dist5=$min + ($distribution * 5)))
((dist4=$min + ($distribution * 4)))
((dist3=$min + ($distribution * 3)))
((dist2=$min + ($distribution * 2)))
((dist=$min + $distribution))
echo '<div class="tagcloud">' > "$PLUGIN_OUTFILE"
for i in ${posts[@]}; do
#echo $i $dist5 $dist4 $dist3 $dist2 $dist
    if [[ $i -eq $min ]]; then
	class=tagMin
    elif [[ $i -eq $max ]]; then
	class=tagMax
    elif [[ $i -ge $dist5 ]]; then
	class=tagDist5
    elif [[ $i -ge $dist4 ]]; then
	class=tagDist4
    elif [[ $i -ge $dist3 ]]; then
	class=tagDist3
    elif [[ $i -ge $dist2 ]]; then
	class=tagDist2
    elif [[ $i -ge $dist ]]; then
	class=tagDist1
    fi
    # TODO needs, more NB API commmands
    catlink=`echo ${cat[$j]} | tr "[:upper:]" "[:lower:]"`
    echo "<a class=\"$class\" href=/blog/archives/$catlink/>${cat[$j]}</a>" >> "$PLUGIN_OUTFILE"
((j=$j + 1))
done
echo '</div>' >> "$PLUGIN_OUTFILE"
NB_Tagcloud=$(< "$PLUGIN_OUTFILE")
