summaryrefslogtreecommitdiffstats
path: root/apt-offline
blob: 46e39d741f51e6dd1b3fd4ca68507ae9c7bb87a7 (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
#! /bin/bash
APT_SOURCES="./sources"
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
}

clean_empty () {
    for I in `ls`; do
        if [ `stat -c %b $I` -eq 0 ]; then
            echo "  remove empty file $I" && rm $I
            continue
        fi
    done
}

offline () {
    if [ -z `which apt-get 2>/dev/null` ]; then fail "apt-get missing"; fi
    if [ $UID != 0 ]; then fail "must be root"; fi
    PKGS=`ls ${APT_PKGS}/* 2>/dev/null`
    if [ ! -z "${PKGS}" ]; then
        echo "  *** install pkgs";
        for I in ${PKGS}; do
            dpkg -i --force-depends $I && rm $I || fail "unable to install $I"
        done
    fi
    apt-get clean
    SRC_LIST=`ls ${APT_SOURCES}/* 2>/dev/null`
    if [ ! -z "${SRC_LIST}" ]; then
        echo "  *** install sources lists";
        for I in ${SRC_LIST}; do
            RET=`file -bi $I | gawk '{ print $1}'`
            if [ "$RET" == "application/x-bzip2;" ]; then
                bunzip2 -q $I && mv $I.out $I
                if [ `stat -c %b $I` -eq 0 ]; then
                    echo "remove empty file $I" && rm $I
                    continue
                fi
            fi
            mv $I $LISTS_DST
        done
    fi
    apt-get check
    echo "  *** build wget lists"
    apt-get -qq --print-uris update | awk '{print "wget --quiet -O " $2 " " $1}' > ./wget-update
    apt-get -qq --print-uris dist-upgrade | awk '{print "wget --quiet -O " $2 " " $1}' > ./wget-upgrade
    for name in $MORE_PKGS; do
        apt-get -qq -d --print-uris install $name | awk '{print "wget --quiet -O " $2 " " $1}' > ./wget-upgrade;
    done
}

online() {
    if [ -z `which wget 2>/dev/null` ]; then fail "wget missing"; fi
    rm -fr $APT_SOURCES 2>/dev/null && mkdir $APT_SOURCES && cd $APT_SOURCES && sh -x ../wget-update; clean_empty && cd .. || fail "unable to download sources"
    rm -fr $APT_PKGS 2>/dev/null && mkdir $APT_PKGS && cd $APT_PKGS && sh -x ../wget-upgrade; clean_empty && cd .. || fail "unable to download pkgs"
}

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

#OPTIONS="offline online"
#select OPT in $OPTIONS; do
#    if [ "$OPT" = "offline" ]; then offline && break; fi;
#    if [ "$OPT" = "online" ]; then online && break; fi;
#    break;
#done