summaryrefslogtreecommitdiffstats
path: root/apt-offline
blob: 553b98963169efd65a3beab058fba79401a01f64 (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
#! /bin/bash
APT_SRCS=./srcs
APT_PKGS=./pkgs

LISTS_DST=/var/lib/apt/lists/

MORE_PKGS=$@
RED="\033[0;31m"
RESET="\033[0m"

fail () {
    echo -e "\n   ${RED}@2-#*% ==>>  $1${RESET}" && [ ! -z "$2" ] && exit $2
}

rm_empty () {
    if [ $(stat -c %b $1) -eq 0 ]; then
        echo "  remove empty file $1" && rm $1
    fi
}

offline () {
    if [ -z $(which apt-get 2>/dev/null) ]; then fail "apt-get missing" 1; fi
    if [ $UID != 0 ]; then fail "must be root" 1; fi
    echo "  *** install pkgs";
    for pkg in $(ls ${APT_PKGS}/* 2>/dev/null); do
        dpkg -i --force-depends $pkg && rm $pkg || fail "unable to install $I"
    done
    apt-get clean
    echo "  *** install sources lists";
    for list in $(ls ${APT_SRCS}/* 2>/dev/null); do
        RET=$(file -bi $list | gawk '{ print $1}')
        if [ "$RET" == "application/x-bzip2;" ]; then
            bunzip2 -q $list && mv $list.out $list
            rm_empty $list
        fi
        [ -f $list ] && mv $list $LISTS_DST
    done
    apt-get check
    echo "  *** build download lists"
    apt-get -qq --print-uris update | awk '{print $2 " " $1}' > ./update-urls
    apt-get -qq --print-uris dist-upgrade | awk '{print $2 " " $1}' > ./upgrade-urls
    for name in $MORE_PKGS; do
        apt-get -qq -d --print-uris install $name | awk '{print $2 " " $1}' > ./upgrade-urls;
    done
}

download() {
    cat $1 | while read line; do
        out=$(echo $line | cut -d' ' -f1);
        url=$(echo $line | cut -d' ' -f2 | sed "s/'//g");
        echo " get $url" && curl -s -o$out $url || fail "unable to download $url"
        rm_empty $out
    done
}

online() {
    if [ -z $(which curl 2>/dev/null) ]; then fail "curl missing" 1; fi
    for dir in $APT_SRCS $APT_PKGS; do
        rm -fr $dir 2>/dev/null && mkdir $dir || fail "unable to create empty dir $dir" 1
    done
    cd $APT_SRCS && download ../update-urls && cd ..
    cd $APT_PKGS && download ../upgrade-urls && cd ..
}

ping -q -W 1 -c 1 debian.org >/dev/null
if [ $? -eq 0 ]; then
    online
else
    offline
fi