#! /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