Allow override of Proxy-Auto-Detect by the users configuration
[ntk/apt.git] / debian / apt.cron.daily
index 4f47ccf..ee0761b 100644 (file)
@@ -8,12 +8,15 @@
 #  Dir "/";
 #  - RootDir for all configuration files
 #
-#  Dir::Cache "var/apt/cache/";
+#  Dir::Cache "var/cache/apt/";
 #  - Set apt package cache directory
 #
-#  Dir::Cache::Archive "archives/";
+#  Dir::Cache::Archives "archives/";
 #  - Set package archive directory
 #
+#  APT::Periodic::Enable "1";
+#  - Enable the update/upgrade script (0=disable)
+#
 #  APT::Periodic::BackupArchiveInterval "0";
 #  - Backup after n-days if archive contents changed.(0=disable)
 #
 #  APT::Archives::MinAge "2"; (old, deprecated)
 #  APT::Periodic::MinAge "2"; (new)
 #  - Set minimum age of a package file. If a file is younger it
-#    will not be deleted (0=disable). Usefull to prevent races 
+#    will not be deleted (0=disable). Useful to prevent races
 #    and to keep backups of the packages for emergency.
 #
 #  APT::Archives::MaxSize "0"; (old, deprecated)
 #  APT::Periodic::MaxSize "0"; (new)
 #  - Set maximum size of the cache in MB (0=disable). If the cache
 #    is bigger, cached package files are deleted until the size
-#    requirement is met (the biggest packages will be deleted 
+#    requirement is met (the oldest packages will be deleted 
 #    first).
 #
 #  APT::Periodic::Update-Package-Lists "0";
 #    
 #  APT::Periodic::Download-Upgradeable-Packages "0";
 #  - Do "apt-get upgrade --download-only" every n-days (0=disable)
-# 
+#
+#  APT::Periodic::Download-Upgradeable-Packages-Debdelta "1";
+#  - Use debdelta-upgrade to download updates if available (0=disable)
+#
 #  APT::Periodic::Unattended-Upgrade "0";
 #  - Run the "unattended-upgrade" security upgrade script 
 #    every n-days (0=disabled)
@@ -85,7 +91,7 @@ check_stamp()
     stamp=$(date --date=$(date -r $stamp_file --iso-8601) +%s 2>/dev/null)
     if [ "$?" != "0" ]; then
         # Due to some timezones returning 'invalid date' for midnight on
-        # certain dates (eg America/Sao_Paulo), if date returns with error
+        # certain dates (e.g. America/Sao_Paulo), if date returns with error
         # remove the stamp file and return 0. See coreutils bug:
         # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
         rm -f "$stamp_file"
@@ -95,14 +101,14 @@ check_stamp()
     now=$(date --date=$(date --iso-8601) +%s 2>/dev/null)
     if [ "$?" != "0" ]; then
         # As above, due to some timezones returning 'invalid date' for midnight
-        # on certain dates (eg America/Sao_Paulo), if date returns with error
+        # on certain dates (e.g. America/Sao_Paulo), if date returns with error
         # return 0.
         return 0
     fi
 
     delta=$(($now-$stamp))
 
-    # intervall is in days, convert to sec.
+    # interval is in days, convert to sec.
     interval=$(($interval*60*60*24))
     debug_echo "check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
 
@@ -141,21 +147,14 @@ check_size_constraints()
     eval $(apt-config shell MaxSize APT::Archives::MaxSize)
     eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
 
-    CacheDir="var/cache/apt/"
-    eval $(apt-config shell CacheDir Dir::Cache)
-    CacheDir=${CacheDir%/}
-
-    CacheArchive="archives/"
-    eval $(apt-config shell CacheArchive Dir::Cache::archives)
-    CacheArchive=${CacheArchive%/}
+    Cache="/var/cache/apt/archives/"
+    eval $(apt-config shell Cache Dir::Cache::archives/d)
 
     # sanity check
-    if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
-       echo "empty Dir::Cache or Dir::Cache::archives, exiting"
+    if [ -z "$Cache" ]; then
+       echo "empty Dir::Cache::archives, exiting"
        exit
     fi
-    
-    Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
 
     # check age
     if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
@@ -198,11 +197,11 @@ check_size_constraints()
                    delta=$(($now-$ctime))
                fi
                if [ $delta -le $MinAge ]; then
-                   debug_echo "skip remove by archive size:  $file, delta=$delta < $MinAgeSec"
+                   debug_echo "skip remove by archive size:  $file, delta=$delta < $MinAge"
                    break
                else
                    # delete oldest file
-                   debug_echo "remove by archive size: $file, delta=$delta >= $MinAgeSec (sec), size=$size >= $MaxSize"
+                   debug_echo "remove by archive size: $file, delta=$delta >= $MinAge (sec), size=$size >= $MaxSize"
                    rm -f $file
                fi
            fi
@@ -219,22 +218,17 @@ do_cache_backup()
     fi
 
     # Set default values and normalize
-    Dir="/"
-    eval $(apt-config shell Dir Dir)
-    Dir=${Dir%/}
-
-    CacheDir="var/cache/apt/"
-    eval $(apt-config shell CacheDir Dir::Cache)
+    CacheDir="/var/cache/apt"
+    eval $(apt-config shell CacheDir Dir::Cache/d)
     CacheDir=${CacheDir%/}
     if [ -z "$CacheDir" ]; then
        debug_echo "practically empty Dir::Cache, exiting"
        return 0
     fi
 
-    CacheArchive="archives/"
-    eval $(apt-config shell CacheArchive Dir::Cache::Archives)
-    CacheArchive=${CacheArchive%/}
-    if [ -z "$CacheArchive" ]; then
+    Cache="${CacheDir}/archives/"
+    eval $(apt-config shell Cache Dir::Cache::Archives/d)
+    if [ -z "$Cache" ]; then
        debug_echo "practically empty Dir::Cache::archives, exiting"
        return 0
     fi
@@ -245,16 +239,15 @@ do_cache_backup()
        BackupLevel=2 ; 
     fi
     
-    CacheBackup="backup/"
-    eval $(apt-config shell CacheBackup Dir::Cache::Backup)
-    CacheBackup=${CacheBackup%/}
-    if [ -z "$CacheBackup" ]; then
+    Back="${CacheDir}/backup/"
+    eval $(apt-config shell Back Dir::Cache::Backup/d)
+    if [ -z "$Back" ]; then
        echo "practically empty Dir::Cache::Backup, exiting" 1>&2
        return
     fi
 
-    Cache="${Dir}/${CacheDir}/${CacheArchive}/"
-    Back="${Dir}/${CacheDir}/${CacheBackup}/"
+    CacheArchive="$(basename "${Cache}")"
+    test -n "${CacheArchive}" || CacheArchive="archives"
     BackX="${Back}${CacheArchive}/"
     for x in $(seq 0 1 $((${BackupLevel}-1))); do 
        eval "Back${x}=${Back}${x}/"
@@ -296,7 +289,7 @@ random_sleep()
     fi
     if [ -z "$RANDOM" ] ; then
         # A fix for shells that do not have this bash feature.
-       RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
+       RANDOM=$(( $(dd if=/dev/urandom bs=2 count=1 2> /dev/null | cksum | cut -d' ' -f1) % 32767 ))
     fi
     TIME=$(($RANDOM % $RandomSleep))
     debug_echo "sleeping for $TIME seconds"
@@ -312,19 +305,56 @@ debug_echo()
     fi
 }
 
+check_power(){
+    # laptop check, on_ac_power returns:
+    #       0 (true)    System is on main power
+    #       1 (false)   System is not on main power
+    #       255 (false) Power status could not be determined
+    # Desktop systems always return 255 it seems
+    if which on_ac_power >/dev/null; then
+        on_ac_power
+        POWER=$?
+        if [ $POWER -eq 1 ]; then
+           debug_echo "exit: system NOT on main power"
+           return 1
+        elif [ $POWER -ne 0 ]; then
+           debug_echo "power status ($POWER) undetermined, continuing"
+        fi
+        debug_echo "system is on main power."
+    fi
+    return 0
+}
+
 # ------------------------ main ----------------------------
 
-# check apt-config exstance
+if test -r /var/lib/apt/extended_states; then
+    # Backup the 7 last versions of APT's extended_states file
+    # shameless copy from dpkg cron
+    if cd /var/backups ; then
+       if ! cmp -s apt.extended_states.0 /var/lib/apt/extended_states; then
+           cp -p /var/lib/apt/extended_states apt.extended_states
+           savelog -c 7 apt.extended_states >/dev/null
+       fi
+    fi
+fi
+
+# check apt-config existence
 if ! which apt-config >/dev/null ; then
        exit 0
 fi
 
+# check if the user really wants to do something
+AutoAptEnable=1  # default is yes
+eval $(apt-config shell AutoAptEnable APT::Periodic::Enable)
+
+if [ $AutoAptEnable -eq 0 ]; then
+    exit 0
+fi
+
 # Set VERBOSE mode from  apt-config (or inherit from environment)
+VERBOSE=0
 eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
 debug_echo "verbose level $VERBOSE"
-if [ -z "$VERBOSE" ]; then
-    VERBOSE="0"
-fi
 if [ "$VERBOSE" -le 2 ]; then
     # quiet for 0,1,2
     XSTDOUT=">/dev/null"
@@ -342,25 +372,10 @@ if [ "$VERBOSE" -ge 3 ]; then
     set -x
 fi
 
-# laptop check, on_ac_power returns:
-#       0 (true)    System is on main power
-#       1 (false)   System is not on main power
-#       255 (false) Power status could not be determined
-# Desktop systems always return 255 it seems
-if which on_ac_power >/dev/null; then
-    on_ac_power
-    POWER=$?
-    if [ $POWER -eq 1 ]; then
-       debug_echo "exit: system NOT on main power"
-       exit 0
-    elif [ $POWER -ne 0 ]; then
-       debug_echo "power status ($POWER) undetermined, continuing"
-    fi
-    debug_echo "system is on main power."
-fi
+check_power || exit 0
 
 # check if we can lock the cache and if the cache is clean
-if which apt-get >/dev/null && ! eval apt-get check -f $XAPTOPT $XSTDERR ; then
+if which apt-get >/dev/null && ! eval apt-get check $XAPTOPT $XSTDERR ; then
     debug_echo "error encountered in cron job with \"apt-get check\"."
     exit 0
 fi
@@ -369,7 +384,7 @@ fi
 now=$(date +%s)
 
 # Support old Archive for compatibility.
-# Document only Periodic for all controling parameters of this script.
+# Document only Periodic for all controlling parameters of this script.
 
 UpdateInterval=0
 eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
@@ -386,12 +401,19 @@ eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
 BackupArchiveInterval=0
 eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
 
-# check if we actually have to do anything
+Debdelta=1
+eval $(apt-config shell Debdelta APT::Periodic::Download-Upgradeable-Packages-Debdelta)
+
+# check if we actually have to do anything that requires locking the cache
 if [ $UpdateInterval -eq 0 ] &&
    [ $DownloadUpgradeableInterval -eq 0 ] &&
    [ $UnattendedUpgradeInterval -eq 0 ] &&
    [ $BackupArchiveInterval -eq 0 ] &&
    [ $AutocleanInterval -eq 0 ]; then
+
+    # check cache size
+    check_size_constraints
+
     exit 0
 fi
 
@@ -401,6 +423,14 @@ do_cache_backup $BackupArchiveInterval
 # sleep random amount of time to avoid hitting the 
 # mirrors at the same time
 random_sleep
+check_power || exit 0
+
+# include default system language so that "apt-get update" will
+# fetch the right translated package descriptions
+if [ -r /etc/default/locale ]; then
+    . /etc/default/locale
+    export LANG LANGUAGE LC_MESSAGES LC_ALL
+fi
 
 # update package lists
 UPDATED=0
@@ -429,6 +459,9 @@ fi
 # download all upgradeable packages (if it is requested)
 DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
 if [ $UPDATED -eq 1 ] && check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
+    if [ $Debdelta -eq 1 ]; then
+        debdelta-upgrade >/dev/null 2>&1 || true
+    fi
     if  eval apt-get $XAPTOPT -y -d dist-upgrade $XSTDERR; then
        update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
        debug_echo "download upgradable (success)"
@@ -441,7 +474,7 @@ fi
 
 # auto upgrade all upgradeable packages
 UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
-if [ $UPDATED -eq 1 ] && which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
+if which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
     if unattended-upgrade $XUUPOPT; then
        update_stamp $UPGRADE_STAMP
        debug_echo "unattended-upgrade (success)"