#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Blogging functions for mesh clients
#
# License
# =======
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

PROJECT_NAME='freedombone'

export TEXTDOMAIN=${PROJECT_NAME}-mesh-blog
export TEXTDOMAINDIR="/usr/share/locale"

IPFS_PATH=/usr/bin
IPFS_COMMAND=$IPFS_PATH/ipfs
IPFS_PUBLIC=/home/$USER/.ipfs-public

BLOG_PATH=~/CreateBlog
BLOG_CONTENT_PATH=$BLOG_PATH/content
CURRENT_BLOG_INDEX=/home/$USER/.blog-index
BLOG_EDITOR='pluma'
DEFAULT_BLOG_TITLE=$"Freedombone Blog"

function remove_bad_blog_links {
    find ./ -type f -name *.css -exec sed -i -e '/googleapi/d' {} \;
    find ./ -type f -name *.scss -exec sed -i -e '/googleapi/d' {} \;
    find ./ -type f -name *.html -exec sed -i -e '/googleapi/d' {} \;
    find ./ -type f -name *.css -exec sed -i -e '/bootstrapcdn/d' {} \;
    find ./ -type f -name *.scss -exec sed -i -e '/bootstrapcdn/d' {} \;
    find ./ -type f -name *.html -exec sed -i -e '/bootstrapcdn/d' {} \;
}

function ipfs_publish {
    DIR_TO_CHECK=/home/$USER/Public
    if [ ! -d $DIR_TO_CHECK ]; then
        return
    fi

    echo ''
    echo $'Publishing to IPFS. This may take some time...'

    OLD_STAT_FILE=/home/$USER/.old_stat.txt
    NEW_STAT=$(stat -t $DIR_TO_CHECK)
    echo $($IPFS_COMMAND add -rq /home/$USER/Public | tail -n 1) > $IPFS_PUBLIC
    echo "$NEW_STAT" > $OLD_STAT_FILE

    if [ -f $IPFS_PUBLIC ]; then
        IPFS_PUBLIC_ID=$(cat $IPFS_PUBLIC)
        $IPFS_COMMAND name publish /ipfs/$IPFS_PUBLIC_ID
    fi
}

function regenerate_blog {
    clear
    echo ''
    echo $'Regenerating blog...'

    cd $BLOG_PATH
    if grep -q "SITENAME=u'${DEFAULT_BLOG_TITLE}'" $BLOG_PATH/pelicanconf.py; then
        TOX_NICK=$(toxid --showuser)
        BLOG_TITLE=$"${TOX_NICK}'s Blog"
        sed -i "s|SITENAME=.*|SITENAME=u\"${BLOG_TITLE}\"|g" $BLOG_PATH/pelicanconf.py
    fi
    make html

    cd $BLOG_PATH
    remove_bad_blog_links

    ipfs_publish
}

function view_blog {
    freedombone-mesh-visit-site '/Blog'
    exit 0
}

function new_blog {
    DATESTR=$(date "+%Y-%m-%d %H:%M:%S")

    echo $'Title: Blog Post Title' > ~/.new-blog-entry
    echo $"Date: ${DATESTR}" >> ~/.new-blog-entry
    echo $"Author: $(toxid --showuser)" >> ~/.new-blog-entry
    echo $'Category: default' >> ~/.new-blog-entry
    echo $'Tags: blog, tag' >> ~/.new-blog-entry
    echo  '' >> ~/.new-blog-entry
    echo $'Add your text here' >> ~/.new-blog-entry
    echo  '' >> ~/.new-blog-entry
    echo -n $'To include an image copy it into the ~/CreateBlog/content/images directory, ' >> ~/.new-blog-entry
    echo $'then link to it with:' >> ~/.new-blog-entry
    echo  '' >> ~/.new-blog-entry
    echo $'![My image]({filename}images/myimage.jpg)' >> ~/.new-blog-entry
    echo  '' >> ~/.new-blog-entry

    $BLOG_EDITOR ~/.new-blog-entry

    if grep -q $"Add your text here" ~/.new-blog-entry; then
        return
    fi
    if grep -q $"Blog Post Title" ~/.new-blog-entry; then
        return
    fi
    if [ ! -f $CURRENT_BLOG_INDEX ]; then
        echo '0' > $CURRENT_BLOG_INDEX
    fi

    # move to the content directory
    CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
    mv ~/.new-blog-entry $BLOG_CONTENT_PATH/${CURRENT_INDEX}_post.md

    # increment the index
    CURRENT_INDEX=$((CURRENT_INDEX + 1))
    echo "$CURRENT_INDEX" > $CURRENT_BLOG_INDEX

    regenerate_blog
}

function edit_blog {
    if [ ! -f $CURRENT_BLOG_INDEX ]; then
        return
    fi
    CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
    PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
    LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.md
    if [ ! -f $LAST_BLOG_ENTRY ]; then
        return
    fi
    $BLOG_EDITOR $LAST_BLOG_ENTRY
    regenerate_blog
}

function delete_blog {
    if [ ! -f $CURRENT_BLOG_INDEX ]; then
        return
    fi
    CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
    PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
    LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.md
    if [ ! -f $LAST_BLOG_ENTRY ]; then
        return
    fi

    if ! zenity --question --title=$'Delete the previous blog entry' --text=$"\nAre you sure that you wish to delete the previous blog entry?" --ok-label=No --cancel-label=Yes --width=300; then
        rm $LAST_BLOG_ENTRY
        if [ $CURRENT_INDEX -gt 0 ]; then
            CURRENT_INDEX=$PREVIOUS_INDEX
            echo "$CURRENT_INDEX" > $CURRENT_BLOG_INDEX
        else
            rm -f $CURRENT_BLOG_INDEX
        fi
        regenerate_blog
    fi
}

function change_theme {
    THEMES=()
    for d in $BLOG_PATH/themes/*/ ; do
        THEME_NAME=$(echo "$d" | awk -F '/' '{print $6}')
        THEMES+=("$THEME_NAME")
    done

    n=1
    curr_theme_index=
    if [ -f /home/$USER/.blog-theme-index ]; then
        curr_theme_index=$(cat /home/$USER/.blog-theme-index)
    fi
    if [ -f /tmp/.blog-themes ]; then
        rm /tmp/.blog-themes
    fi
    for a in "${THEMES[@]}"
    do
        echo "$n $a" >> /tmp/.blog-themes
        n=$[n+1]
    done

    CHOSEN_THEME_INDEX=$(
    cat /tmp/.blog-themes | \
        awk -F ' ' '{
        for(i=1;i<=NF;i++){
            print $i;
        }
}' | \
        zenity --list \
           --title=$'Select Blog Theme' \
           --column=$'Index' --column=$'Theme' \
           --print-column=1 --hide-column=1 --width=300 --height=400)
    rm /tmp/.blog-themes
    if [ ! $CHOSEN_THEME_INDEX ]; then
        exit 1
    fi

    echo "$CHOSEN_THEME_INDEX" > /home/$USER/.blog-theme-index
    CHOSEN_THEME_INDEX=$((CHOSEN_THEME_INDEX - 1))

    CHOSEN_THEME=${THEMES[$CHOSEN_THEME_INDEX]}
    cd cd $BLOG_PATH/themes/$CHOSEN_THEME
    remove_bad_blog_links
    if grep -q "THEME=" $BLOG_PATH/pelicanconf.py; then
        sed -i "s|THEME=.*|THEME='themes/${CHOSEN_THEME}'|g" $BLOG_PATH/pelicanconf.py
    else
        echo "THEME='themes/${CHOSEN_THEME}'" >> $BLOG_PATH/pelicanconf.py
    fi
    regenerate_blog
}

function menu_blog {
    data=$(zenity --list 1 $"View a blog" 2 $"New blog entry" 3 $"Edit the previous blog entry" 4 $"Delete the previous blog entry" 5 $"Change theme" --column="id" --title $"Blogging" --column=$"Choose an operation:" --hide-column=1 --print-column=1 --height=250)
    sel=$?
    case $sel in
        1) exit 1;;
        255) exit 1;;
    esac
    case $data in
        1) view_blog;;
        2) new_blog;;
        3) edit_blog;;
        4) delete_blog;;
        5) change_theme;;
    esac
}

menu_blog

exit 0
