#!/bin/bash
# script to download updates and to install them after a few days.
# /usr/local/sbin/updateafter, paul@vandervlis.nl
# install 'mailutils' and use it with "update-alternatives --config mailx"
# update:
# cd /usr/local/sbin/; rm updateafter; wget vandervlis.nl/files/updateafter; chmod +x updateafter

# variable, install after how many days
typeset -i DAYS=1

# some environment variables
LANG=C; export LANG
DEBIAN_FRONTEND=noninteractive; export DEBIAN_FRONTEND

# working directory
cd /var/cache/apt/archives/

# make save/ directory when not there
if ! test -e save; then mkdir save; fi

# make a list of older updates
list=`ls -1 *.deb 2>/dev/null`

# download new updates
apt-get -qq update
apt-get -qq --download-only --yes dist-upgrade

# move the older updates to save/, keep the new once
if test "$list" != ""; then mv $list save/ ;fi

# touch new updates for a timestamp, and move them to save/
list=`ls -1 *.deb 2>/dev/null` # touch behaves strange when using *.deb
if test "$list" != ""; then
  touch $list
  mv "$list" save/
fi

# move updates back when its install-time
find save/ -type f -mtime +$DAYS -exec mv {} ./ \;

# never install updates older then 1000 days, you can use this:
# touch -d "2000-01-01" bad_update.deb
find ./ -type f -mtime +1000 -exec mv {} save/ \;

# install and clean-up
list=`ls -1 *.deb 2>/dev/null`
if test "$list" != ""; then
  echo
  echo "install time"
  apt-get -q --no-download --ignore-missing --yes dist-upgrade
  apt-get -qq clean
else
  echo
  echo "Nothing to install"
fi

# move everything back
list=`ls -1 save/*.deb 2>/dev/null`
if test "$list" != ""; then
  mv save/*.deb ./
fi

