#!/bin/bash
NAME="Shortcuts@kyle.aims.ac.za"
echo "Running $0 for $NAME with arguments: $@"
extensionfile="$NAME.shell-extension.zip"

set -e

if [ "$UID" = "0" ]; then
    echo 'This should not be run as root'
    exit 101
fi

#cleanup old zip if exists
if [ -f $extensionfile ]; then
    rm $extensionfile
fi

function compile-translations {
    echo 'Compiling translations...'
    for po in locale/*/LC_MESSAGES/*.po; do
        msgfmt -cv -o ${po%.po}.mo $po;
    done
}

function compile-preferences {
    if [ -d src/schemas ]; then
        echo 'Compiling preferences...'
        glib-compile-schemas --targetdir=src/schemas src/schemas
    else
        echo 'No preferences to compile... Skipping'
    fi
}

function pack {
    if [ -d po ]; then
        rm -r po
    fi
    mkdir po
    cp locale/af/LC_MESSAGES/Shortcuts.po po/af.po
    cp locale/de/LC_MESSAGES/Shortcuts.po po/de.po
    echo 'Packing extension to zip...'
    cd src
    gnome-extensions pack -f --podir=../po/ --out-dir=../ --extra-source=shortcuts.json --extra-source=../LICENSE --extra-source=./ui/
    cd ..
    rm -r po
    echo "Extension zip created ..."
}

function updatepotfile() {
    reffile=Shortcuts.pot
    xgettext --from-code=UTF-8 --output=locale/"$reffile" src/*.js src/schemas/*.xml src/ui/*.ui
    echo "Done."
}

function install()
{
    echo "Installing extension..."
    gnome-extensions install -f $extensionfile
    echo "Done."
}


function usage() {
    echo 'Useage: ./install.sh COMMAND'
    echo 'COMMAND:'
    echo '  install       Install the extension from the created zip file'
    echo '  help           Show this help message'
    echo "  local-install  install the extension in the user's home directory"
    echo '                 under ~/.local'
    echo '  pack           Creates a zip file of the extension'
    echo '  updatepotfile  Update the pot file with new translations'
    echo '  upload         Upload to E.G.O.'
    echo '  zip            Create a zip file of the extension'
    exit 1
}

function upload() {
    echo "Uploading to extensions.gnome.org..."
    gnome-extensions upload --user ChrisLauinger77 --password-file /var/data/dev/ego_password $extensionfile

}

case "$1" in
    "pack" | "zip" )
        pack
        ;;

    "install" | "local-install")
        pack
        install
        ;;

    "updatepotfile" | "translate" )
        updatepotfile
        ;;

    "upload" )
        upload
        ;;
    * )
        usage
        ;;
esac
exit
