#!/bin/bash

set -e

function verify_hash() {
    local file=$1 expected_hash=$2
    actual_hash=$(sha256sum $file | awk '{print $1}')
    if [ "$actual_hash" == "$expected_hash" ]; then
        return 0
    else
        echo "$file $actual_hash (unexpected hash)" >&2
        rm "$file"
        exit 1
    fi
}

function download_if_not_exist() {
    local file_name=$1 url=$2
    if [ ! -e $file_name ] ; then
        wget -q -O $file_name "$url"
    fi
}

APPDIR="$PWD/feather-wow.AppDir"
mkdir -p "$APPDIR"
mkdir -p "$APPDIR/usr/share/applications/"
mkdir -p "$APPDIR/usr/bin"

echo "Downloading dependencies"

download_if_not_exist "feather-wow.zip" "https://build.featherwallet.org/files-wow/linux-release/$BRANCH/$FN"
unzip -q feather-wow.zip

cp "$PWD/src/assets/feather-wow.desktop" "$APPDIR/usr/share/applications/feather-wow.desktop"
cp "$PWD/src/assets/images/appicons/64x64.png" "$APPDIR/feather-wow.png"
cp "$PWD/feather-wow" "$APPDIR/usr/bin/feather-wow"

/appimagetool deploy "$APPDIR/usr/share/applications/feather-wow.desktop"
VERSION=1.0 /appimagetool ./feather-wow.AppDir

