summaryrefslogtreecommitdiff
path: root/src/sync.sh
blob: 263d5556c5f802786432326efed4f808c4eea16b (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
133
134
135
136
137
138
139
140
141
142
#!/bin/sh


# save each listed package in a relevant directory, based on checksum
#
parse_line() {
    [ "$#" = "5" ] && {
        local url=$1
        local package=$2
        local checksum=$3
        local size=$4
        local files=$5

        local package_name=$(basename $package ".xipkg")

        local package_dir="$PACKAGES_DIR/$package_name.versions"
        local checksum_file=$package_dir/$checksum

        [ -d $package_dir ] || mkdir -p $package_dir
        printf "$url/$package $checksum $size $files\n" >> $checksum_file
    }
}

list_source () {
    local src=$1
    local url=$(echo $src | cut -d":" -f2-)
    local name=$(echo $src | cut -d":" -f1)
    local full_url="${url}/packages.list"
    local tmp_file="${SYNC_CACHE}/$name"

    ${VERBOSE} && printf "${LIGHT_BLACK}Indexing packages from $full_url\n"
    local status=$(download_file $tmp_file $full_url)
    
    if [ "$status" = "200" ] || [ "$status" = "000" ] && [ -f $tmp_file ]; then
        while IFS= read -r line; do
            parse_line $url $line
        done < "$tmp_file"
    else
        return 1
    fi
}

dep_graph () {
    local src=$1
    local url=$(echo $src | cut -d":" -f2-)
    local name=$(echo $src | cut -d":" -f1)
    local full_url="${url}deps.graph"
    local tmp_file="${SYNC_CACHE}/$name.deps.graph"
    [ -f $tmp_file ] && rm $tmp_file; touch $tmp_file

    local status=$(download_file $tmp_file $full_url)
    if [ "$status" = "200" ] ||  [ "$status" = "000" ]; then
        while IFS= read -r line; do
            [ "${#line}" != "0" ] && {
                local package=$(echo $line | cut -d: -f1)
                local new=$(echo $line | cut -d: -f2-)
                echo $new >> $DEP_DIR/$package
            }
        done < "$tmp_file"
    fi
}


contest () {
    local package_dir=$1

    local popular=$(wc -l $package_dir/* | sort -n | head -1 | awk '{ print $2 }' )

    local info_file=${package_dir%.versions}
    mv $popular $info_file
    rm -r $package_dir
}

popularity_contest () {
    if [ "$(find $PACKAGES_DIR -type f | wc -l)" = "0" ]; then
        printf "${RED}No packages found!\n";
        return 1
    fi

    local list=$(ls -1 -d $PACKAGES_DIR/*)
    local total=$(echo $list | wc -l)

    local completed=0
    for package_dir in $list; do
        contest $package_dir &
        completed=$((completed+1))
        ${QUIET} || hbar -T "${LARGE_CIRCLE} contesting packages..." $completed $total
    done
    ${QUIET} || hbar -t ${HBAR_COMPLETE} -T "${CHECKMARK} contested packages" $completed $completed
}

index_deps () {
    set -- ${SOURCES}
    local total=$#
    local completed=0

    for src in ${SOURCES}; do
        ${QUIET} || hbar -T "${LARGE_CIRCLE} indexing dependencies..." $completed $total
        dep_graph $src
        completed=$((completed+1))
    done
    ${QUIET} || hbar ${HBAR_COMPLETE} -T "${CHECKMARK} indexed dependencies" $completed $total
}

index_repo () {
    set -- ${SOURCES}
    local total=$#
    local completed=0

    for src in ${SOURCES}; do
        ${QUIET} || hbar -T "${LARGE_CIRCLE} syncing sources..." $completed $total
        list_source $src 
        completed=$((completed+1))
    done
    ${QUIET} || hbar ${HBAR_COMPLETE} -T "${CHECKMARK} synced sources" $completed $total
}

sync () {
    # prepare the file structure for the sync
    mkdir -p ${SYNC_CACHE}

    [ "$(ls -A $PACKAGES_DIR)" ] && rm -r $PACKAGES_DIR/*
    rm -r $DEP_DIR
    mkdir $DEP_DIR

    ${VERBOSE} && printf "${LIGHT_BLACK}Syncing\n"

    # download package lists and dep graphs at the same time
    mkdir -p ${PACKAGES_DIR}

    # index packages and dependencies
    index_repo
    ${QUIET} || hbar
    index_deps

    # wait for all jobs to complete
    wait

    # determine which version of which package should be regarded
    hbar
    popularity_contest
}