#!/bin/sh
DIALOG=Xdialog
TTDPACK=ttd-graphics.rar
TTDURL="http://download.transporttycoon.net/files/$TTDPACK"
DL_PATH="$HOME/.openttd/content_download/data"
download_content() {
    $DIALOG --screen-center --wrap --title "Download and Install Transport Tycoon Deluxe content" \
    	--yesno "For start of game OpenTTD it is necessary to load from a Internet 3.2Mb \n with the original data from Transport Tycoon Deluxe. Download ?" 0 0

    case $? in
	 0)
	    [ -d $DL_PATH ] || mkdir -p $DL_PATH
	    eval "curl -# $TTDURL -o $DL_PATH/$TTDPACK"   2>&1 | tr "\r" "\n"|sed "s,# *,,gm"| $DIALOG --title "Progress" --no-close \
	    --gauge "Downloading ...\n\n" 0 0
	    pushd $DL_PATH
	    if unrar x "$TTDPACK";then
		$DIALOG --screen-center --wrap --title "Download and Install Transport Tycoon Deluxe content" \
    	        --yesno "Complete. Start Game ?" 0 0
    	        [ $? -eq 0 ] || exit
    	    else
		$DIALOG --screen-center --wrap --title "Download and Install Transport Tycoon Deluxe content" \
    	        --msgbox "Unknown error !" 0 0
    	    fi
	    popd
	    ;;
    esac
}

openttd
if [ $? -eq 138 ];then 
download_content && rm -f "$DL_PATH/$TTDPACK" && openttd
fi
