#!/bin/bash

# Guide a user towards a image URL with questions and exploration of
# http://download.tizen.org/.
# If you supply a file as parameter, the output will be saved in it.
# Exit with value 2 if SIGINT is sent.

# Number of images to list (if arch x86_64, ia32 images will be also used)
N=3

# Some images are excluded
BLACKLIST="-e unsafe -e emul -e efi -e testing -e installer"

# Field number used to retrieve the href value in a listing
# The fields are separated by "
HREF_FIELD_NO=6

outfile=${1:-/dev/stdout}
. /usr/sbin/dialog-helper
tmp_outfile="/tmp/url-utils.$$"

# Print the href values of a directory listing
# param #1: url of a directory listing
function url_get_href_from_listing() {
	curl -s -S "$1" |
	grep '<img src="/icons/folder.gif" alt="\[DIR\]"> <a href="' |
	cut -d '"' -f ${HREF_FIELD_NO}
}

# Print last N images url of a directory listing. The images url passed
# the BLACKLIST filter
# param #1: url like "http://download.tizen.org/snapshots/tizen/ivi/ivi/"
function url_N_last_images() {
	local base_url="$1"
	local i=0

	url_get_href_from_listing "${base_url}" |
	tac |
	while read url && [[ $i < $N ]]; do
		img_dir_list_url="${base_url}${url}images/"
		for img_dir_name in $(url_get_href_from_listing "${img_dir_list_url}"); do
			if echo "${img_dir_name}"| grep -qv ${BLACKLIST}; then
				final_img_name=$(
					curl -s -S "${img_dir_list_url}${img_dir_name}" |
					grep "raw.bz2" |
					cut -d '"' -f ${HREF_FIELD_NO}
				)
				[ -n "$final_img_name" ] && echo "${img_dir_list_url}${img_dir_name}${final_img_name}" >> $tmp_outfile
			fi
		done
		i=$(($i + 1))
	done
}

trap "exit 2" SIGINT

# Asks for profile
dialog_helper --no-items --menu "Choose your profile" 10 30 2 "IVI" "Common"
PROFIL="$DIALOGRES" 

# Asks for image type
dialog_helper --no-items --menu "Choose your image type" 10 30 2 "snapshot" "release"
TYPE="$DIALOGRES" 

# Deduces the architecture if not set
[ -z "$ARCH" ] && ARCH=$(lscpu |grep -q "^CPU op-mode.*64-bit.*" && echo "x86_64")

# "base" URL
if [ "$TYPE" = "release" ]; then
	# Only IVI profile is available in release/weekly
	if [ "$PROFIL" = "IVI" ]; then
		dialog_helper --no-items --menu "Choose your release" 10 30 2 "daily" "weekly"
		URL="http://download.tizen.org/releases/$DIALOGRES/tizen/"
	else
		URL="http://download.tizen.org/releases/daily/tizen/"
	fi
else
	URL="http://download.tizen.org/snapshots/tizen/"
fi

# This follow the (odd) naming conventions of download.tizen.org
# Also, if you have ARCH = "x86_64", images of the "ia32" architecture also will be printed
if [ "$PROFIL" = "IVI" ]; then
	URL1="${URL}ivi/ivi/"
else
	URL+="common/common-"
	if [ "$TYPE" = "snapshot" ]; then
		# Asks for display server
		dialog_helper --no-items --menu "Choose your display server" 10 30 2 "X11" "Wayland"
		DISPLAY="$DIALOGRES"

		if [ "$DISPLAY" = "X11" ]; then
			URL+="x11-"
			# For some reason a wayland folder is in a x11 image folder
			BLACKLIST+=" -e wayland"
		else
			URL+="wayland-"
		fi
	else
		URL+="wayland-"
	fi

	if [ "$ARCH" = "x86_64" ]; then
		URL2="${URL}x86_64/"
		$(url_N_last_images "$URL2")
	fi

	URL1="${URL}ia32/"
fi

# If we don't print anything, the screen will be black for few seconds
dialog --infobox "Building urls..." 10 40
$(url_N_last_images "$URL1")

# We extract various informations from the url, the result is :
# <URL> "Tizen common-wayland-mbr-x86_64 (MM/DD/YYYY #<build number>)"
entry=$(
	sed 's:\(.*/tizen_\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\.\([0-9]*\)/images/\(.*\)/.*\):\1 "Tizen \6 (\3/\4/\2 #\5)":' $tmp_outfile |
	tr '\n' ' '
)

rm -f "$tmp_outfile" 2>/dev/null

if [ -z "$entry" ]; then
	dialog --msgbox "No suitable image could be found, exiting" 10 40
	exit 1
fi

eval dialog_helper --notags --menu "\"Choose between the latests images that match your criterions\"" 16 60 6 $entry

cat << EOC >> $outfile
$(echo "$DIALOGRES")
EOC
