#!/bin/bash -efu
DIALOG=Xdialog
TYRIANPACK=tyrian21.zip
TYRIANURL="https://camanis.net/tyrian/$TYRIANPACK"
DL_PATH="$HOME/.opentyrian/content_download"
CURL="curl -L -f --retry 3 --retry-connrefused --retry-delay 2 --max-time 30"
download_content() {
    $DIALOG --screen-center --wrap --title "Download and Install Tyrian content" \
    	--yesno "For start of game OpenTyrian it is necessary to load from a Internet 4.7Mb \n with the original data from Tyrian. Download ?" 0 0

    case $? in
	 0)
	    [ -d "$DL_PATH" ] || mkdir -p "$DL_PATH"
	    eval "${CURL[@]} --progress-bar $TYRIANURL -o $DL_PATH/$TYRIANPACK" 2>&1 | tr "\r" "\n"|sed "s,# *,,gm"| $DIALOG --title "Progress" --no-close \
	    --gauge "Downloading ...\n\n" 0 0
	    pushd "$DL_PATH"
	    if unzip -j -o -d "$DL_PATH" "$TYRIANPACK";then
		$DIALOG --screen-center --wrap --title "Download and Install Tyrian content" \
		--yesno "Complete. Start Game ?" 0 0 || return 1
    	    else
		$DIALOG --screen-center --wrap --title "Download and Install Tyrian content" \
    	        --msgbox "Unknown error !" 0 0
		return 1
    	    fi
	    popd
	    ;;
	*)
	    return 1
	    ;;
    esac
}

if [ -e "$DL_PATH/palette.dat" ];then
    opentyrian -t "$DL_PATH" "$@"
else
    download_content && rm -f "$DL_PATH/$TYRIANPACK" && opentyrian -t "$DL_PATH" "$@"
fi
