Merge apt--mvo--0
[ntk/apt.git] / debian / apt.cron.daily
CommitLineData
0c132682
MZ
1#!/bin/sh
2#
0c132682
MZ
3
4#set -e
5
05f6a46a 6check_stamp()
0c132682 7{
05f6a46a
MZ
8 stamp="$1"
9 interval="$2"
10
10946ddc
MZ
11 if [ $interval -eq 0 ]; then
12 return 1
13 fi
14
05f6a46a
MZ
15 if [ ! -f $stamp ]; then
16 return 0
0c132682 17 fi
05f6a46a
MZ
18
19 # compare midnight today to midnight the day the stamp was updated
20 stamp=$(date --date=$(date -r $stamp --iso-8601) +%s)
21 now=$(date --date=$(date --iso-8601) +%s)
22 delta=$(($now-$stamp))
05f6a46a
MZ
23
24 if [ $delta -ge $interval ]; then
25 return 0
26 fi
27
28 return 1
29}
30
31update_stamp()
32{
33 stamp="$1"
34
35 touch $stamp
0c132682
MZ
36}
37
6cce801a
MV
38
39
40# we check here if autoclean was enough sizewise
41check_size_constrains()
42{
43 # min-age in days
44 MaxAge=0
45 MaxSize=0
46 CacheDir="var/cache/apt"
47 CacheArchive="archives/"
48 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
49 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
50 eval $(apt-config shell CacheDir Dir::Cache)
51 eval $(apt-config shell CacheArchive Dir::Cache::archives)
52
53 # sanity check
54 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
55 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
56 exit
57 fi
58 Cache="/"$CacheDir$CacheArchive
59
60 # check age
61 if [ ! $MaxAge -eq 0 ]; then
62 find $Cache -name "*.deb" -mtime +$MaxAge -exec rm -f {} \;
63 fi
64
65 # check size
66 if [ ! $MaxSize -eq 0 ]; then
67 # reverse-sort by mtime
68 for file in $(ls -rt $Cache/*.deb); do
69 du=$(du -s $Cache)
70 size=${du%%/*}
71 # check if the cache is small enough
72 if [ $size -lt $MaxSize ]; then
73 break
74 fi
75 # delete oldest file
76 rm -f $file
77 done
78 fi
79}
80
81check_size_constrains
82
0c132682 83UpdateInterval=0
05f6a46a 84DownloadUpgradeableInterval=0
9bd1cf87
MZ
85eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
86AutocleanInterval=$DownloadUpgradeableInterval
87eval $(apt-config shell AutocleanInterval APT::Periodic::Autoclean)
0c132682 88
0c132682
MZ
89# laptop check, on_ac_power returns:
90# 0 (true) System is on mains power
91# 1 (false) System is not on mains power
92# 255 (false) Power status could not be determined
93# Desktop systems always return 255 it seems
2288cf77 94if which on_ac_power >/dev/null; then
05f6a46a 95 on_ac_power
0c132682
MZ
96 if [ $? -eq 1 ]; then
97 exit 0
98 fi
99fi
100
05f6a46a
MZ
101UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
102if check_stamp $UPDATE_STAMP $UpdateInterval; then
103 if apt-get -qq update 2>/dev/null; then
104 if which dbus-send >/dev/null; then
105 dbus-send --system / app.apt.dbus.updated boolean:true
106 fi
107 update_stamp $UPDATE_STAMP
108 fi
0c132682
MZ
109fi
110
05f6a46a
MZ
111DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
112if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
113 apt-get -qq -d dist-upgrade 2>/dev/null
114 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
0c132682 115fi
9bd1cf87
MZ
116
117AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
118if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
119 apt-get -qq autoclean
6cce801a 120 check_size_contrains
9bd1cf87
MZ
121 update_stamp $AUTOCLEAN_STAMP
122fi