#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Blogging functions

# License
# =======
#
# Copyright (C) 2016 Bob Mottram <bob@robotics.uk.to>
#
# 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}-blog
export TEXTDOMAINDIR="/usr/share/locale"

CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg

HOSTNAME=
AVATAR=

# get the blog hostname
if grep -q "FULLBLOG_DOMAIN_NAME" $CONFIGURATION_FILE; then
    HOSTNAME=$(grep "FULLBLOG_DOMAIN_NAME" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
fi

BASE_DIR=/var/www/$HOSTNAME/htdocs

function show_help {
    echo ''
    echo $"${PROJECT_NAME}-blog -h [hostname] -a [avatar image file]"
    echo ''
    echo $'Blogging functions'
    echo ''
    echo $'     --help                     Show help'
    echo $'  -h --hostname [name]          Hostname'
    echo $'  -a --avatar [url]             Filename or url for avatar'
    echo ''
    exit 0
}

while [[ $# > 1 ]]
do
    key="$1"

    case $key in
        --help)
            show_help
            ;;
        -h|--hostname)
            shift
            HOSTNAME="$1"
            ;;
        -a|--avatar)
            shift
            AVATAR="$1"
            ;;
        *)
            # unknown option
            ;;
    esac
    shift
done

if [ ! $HOSTNAME ]; then
    echo $'No hostname specified'
    exit 5748
fi

if [ ! -d $BASE_DIR ]; then
    echo "$BASE_DIR was not found"
    exit 1
fi

function set_avatar_from_file {
    SOURCE_IMAGE_FILE="$1"

    if [ ! -f $SOURCE_IMAGE_FILE ]; then
        echo $'Source file not found'
        exit 2
    fi
    
    # copy the source image
    cd $BASE_DIR
    AVATAR_FILES=$(find . -name avatar.png)
    read -a arr <<<$AVATAR_FILES

    for i in "${arr[@]}"
    do      
        FILENAME="$BASE_DIR$(echo \"$i\" | awk -F '.' '{print $2}')".png
        if [[ "$FILENAME" != "$SOURCE_IMAGE_FILE" ]]; then
            cp -f $SOURCE_IMAGE_FILE "$FILENAME"
        fi      
    done    
}

function set_avatar_from_url {
    if [ ! -d $BASE_DIR/customimages ]; then
        mkdir $BASE_DIR/customimages
    fi

    # download the image
    cd $BASE_DIR/customimages
    # convert to png
    wget $AVATAR -O avatar
    if [[ $AVATAR == *".gif" ]]; then
        mv avatar avatar.gif
        mogrify -format png avatar.gif
    fi
    if [[ $AVATAR == *".jpg" ]]; then
        mv avatar avatar.jpg
        mogrify -format png avatar.jpg
    fi
    if [[ $AVATAR == *".jpeg" ]]; then
        mv avatar avatar.jpeg
        mogrify -format png avatar.jpeg
    fi
    if [ -f avatar ]; then
        mv avatar avatar.png
    fi
    
    # standard size
    mogrify -resize 150x150 avatar.png
    if [ ! -f $BASE_DIR/customimages/avatar.png ]; then
        echo $'Avatar image could not be downloaded'
        exit 3
    fi
    chown -R www-data:www-data $BASE_DIR/customimages
}

if [[ "$AVATAR" == "http"* ]]; then
    set_avatar_from_url
fi

AVATAR=$BASE_DIR/customimages/avatar.png
if [ -f $AVATAR ]; then
    set_avatar_from_file $AVATAR
fi

exit 0
