summaryrefslogtreecommitdiff
path: root/scripts/url-util
blob: 94aabae6c10089aa0161da17393478de29dc8360 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/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.

#!/bin/bash

outfile=${1:-/dev/stdout}
mode=${2:-"basic"}

BASE_URL="http://download.tizen.org/"
IMAGE_URL=
DIALOG_RESULT=
DIALOG=/usr/bin/dialog

BACK_STRING="..(parent)"

tmpdir=$(mktemp -d /tmp/$(basename $0).XXXXXXXX)
tmpfile=$(mktemp /tmp/dialog.XXXXXXXX)
trap "rm -rf $tmpdir $tmpfile" STOP INT QUIT EXIT
pushd $tmpdir &>/dev/null

function select_topdir() {
    $DIALOG --no-items --item-help --menu "Select image type" 15 70 15  \
	"releases" "Official Tizen release : daily, weekly, milestones" \
	"snapshots/tizen" "Tizen snapshots of main project for all Tizen profiles" \
	"snapshots/devel" "Tizen snapshots of devel project for all Tizen profiles" \
	"prerelease" "Prerelease images" 2>$tmpfile
    ret=$?

   [[ $ret == 0 ]] && { DIALOG_RESULT=$(cat $tmpfile); return 0; }
    DIALOG_RESULT=
    return 1
}

function select_image() {
    dir=$1

    declare -a items

    readarray items < <(for x in $dir/*; do
        [[ ! -d $x ]] && continue
        [[ -z "$(ls -d $x/* 2>/dev/null)" ]] && continue
        basename $x
    done | sort -r)

    if [[ ${#items[@]} == 0 ]]; then
        DIALOG_RESULT=
        return 0
    fi

    items[${#items[@]}]="$BACK_STRING"

    $DIALOG --no-items --menu "Select subdirectory ($dir)" 15 70 15 $(echo "${items[@]}") 2>$tmpfile
    ret=$?

    [[ $ret == 0 ]] && { DIALOG_RESULT=$(cat $tmpfile); return 0; }
    DIALOG_RESULT=
    return 1
}

function do_sync() {
    dialog_pid=$1
    current_pid=$$

    echo -e "\nFetching images locations... Please wait...\n"

    rsync -a --relative --include=*/ --include=*.raw.* --exclude=* download.tizen.org::all/$curdir | grep -v ^d 2>/dev/null | awk '{print $5}' 2>/dev/null | while read x; do
		## exit from the url-util script if dialog process is terminated
		if [ ! -e /proc/$dialog_pid ]; then
			kill $current_pid
			exit 1
		fi
        mkdir -p $(dirname $x)
        touch $x
        echo $(basename $x)
    done

    echo "DONE - killing $dialog_pid"

    [[ -n "$dialog_pid" ]] && kill $dialog_pid
}


function enter_url() {
	$DIALOG --stderr --no-cancel --inputbox "Enter the entire url of the image :\n" 24 70 2>$tmpfile
	IMAGE_URL=$(cat $tmpfile)
	if ! curl --fail -s -I "$IMAGE_URL" > /dev/null ; then
		echo "Bad image url !"
		 $DIALOG --msgbox "The image url couldn't be reached, please verify that the url is correct." 15 70
		exit 1
	fi
}

#######################################################


if [ "$mode" = "custom" ]; then
	IMAGE_URL+=$BASE_URL

	select_topdir || exit 1

	curdir=$DIALOG_RESULT
	touch sync.out
	$DIALOG --title "Fetching images locations" --tailbox sync.out 24 70 &
	do_sync $! >>sync.out

	initial_dir=$curdir
	while [ 1 ]; do
		select_image $curdir || exit 1
		if [[ "$DIALOG_RESULT" == "${BACK_STRING}" ]]; then
			curdir=$(dirname $curdir)
		elif [[ -z "$DIALOG_RESULT" ]]; then
			break
		else
			curdir="$curdir/$DIALOG_RESULT"
		fi
	done

	IMAGE_URL+=$(echo $curdir/*.raw.*)

elif [ "$mode" = "basic" ]; then
	enter_url
fi

popd &>/dev/null

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