#!/bin/bash
#
# 2019/12/13 Gabriel Moreau <Gabriel Moreau(A)univ-grenoble-alpes.fr> - Initial release

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
export LANG=C

VERSION='0.1.1'

function usage() {
   cat <<END_USAGE
secdapt - secure delegate apt upgrade

 secdapt upgrade
 secdapt clean
 secdapt rapido
 secdapt help
 secdapt version

VERSION ${VERSION}
END_USAGE
   }

function apt_upgrade () {
   LANG=C sudo apt-get update && { LANG=C sudo apt-get dist-upgrade && { sudo apt-get autoclean && logout; } ; }
   }

function apt_clean () {
   local remain=$( (echo -n empty; (df -m | grep '/$'| head -1)) | awk '{print $4}')
   LANG=C sudo apt-get clean && LANG=C sudo apt-get --purge autoremove
   LANG=C sudo rm -rf /var/lib/apt/lists/*
   LANG=C sudo mkdir -m 0700 /var/lib/apt/lists/partial
   remain=$(( $( (echo -n empty; (df -m | grep '/$'| head -1)) | awk '{print $4}') - $remain ))
   [ $remain -ne 0 ] && echo "# == $remain Mo disk space have been won on / volume =="
   }

function apt_rapido () {
   LANG=C sudo apt-get dist-upgrade && logout
   }


# get command
if [ $# -eq 0 ]; then usage; exit 1; fi 

command=$1
shift
case "${command}" in
   upgrade)
      apt_upgrade
      ;;

   clean)
      apt_clean 
      ;;

   rapido)
      apt_rapido
      ;;

   version)
      echo 'VERSION ' ${VERSION}
      ;;

   help|*)
      usage
      exit 3
      ;;
esac

exit


################################################################
# Documentation in POD format (like Perl)
################################################################

=head1 NAME

secdapt - secure delegate apt upgrade

=head1 SYNOPSIS

 secdapt upgrade
 secdapt clean
 secdapt rapido
 secdapt help
 secdapt version

=head1 DESCRIPTION

secdapt can be use to launch upgrade apt command on remote host with a parallel tool
(like tssh, pconsole...)


=head1 AUTHORS

Written by Gabriel Moreau, Grenoble - France


=head1 COPYRIGHT

Copyright (C) 2014-2019, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
Licence : GNU GPL version 2 or later
