#!/bin/bash
#======================================================================
# Copyright (c) 1997-1999 Daniele Giacomini daniele@pluto.linux.it
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#======================================================================
# eliminautente
#
# Permette l'eliminazione dei file di un utente indicato per nome
# come argomento dello script.
# Prima di elmininare i dati, crea un archivio compresso nella
# directory personale dell'utente che esegue questa operazione: root.
#======================================================================

#!/bin/bash
#======================================================================
# eliminautente
#======================================================================

#======================================================================
# Variabili.
#======================================================================

    #------------------------------------------------------------------
    # Il nome dell'utente viene fornito come primo e unico argomento
    # di questo script.
    #------------------------------------------------------------------
    NOME_UTENTE="$1"
    #------------------------------------------------------------------
    # Nome per un file temporaneo contenente l'elenco dei file
    # appartenenti all'utente che si vuole eliminare.
    #------------------------------------------------------------------
    ELENCO_FILE_UTENTE="/tmp/elenco_file_utente"

#======================================================================
# Funzioni.
#======================================================================

    #------------------------------------------------------------------
    # Visualizza la sintassi corretta per l'utilizzo di questo script.
    #------------------------------------------------------------------
    function sintassi () {
	echo ""
	echo "eliminautente <nome-utente>"
	echo ""
	echo "Il nome pu avere al massimo 8 caratteri."
    }

#======================================================================
# Inizio.
#======================================================================

    #------------------------------------------------------------------
    # Verifica la quantit di argomenti.
    #------------------------------------------------------------------
    if [ $# != 1 ]
    then
        #--------------------------------------------------------------
	# La quantit di argomenti  errata. Richiama la funzione
	# sintassi e termina l'esecuzione dello script restituendo
	# un valore corrispondente a falso.
        #--------------------------------------------------------------
	sintassi
        exit 1
    fi
    #------------------------------------------------------------------
    # Verifica che l'utente sia root.
    #------------------------------------------------------------------
    if [ $UID != 0 ]
    then
        #--------------------------------------------------------------
	# Dal momento che l'utente non  root, avvisa dell'errore
	# e termina l'esecuzione restituendo un valore corrispondente
	# a falso.
        #--------------------------------------------------------------
        echo \
"Questo script pu essere utilizzato solo dall'utente root."
        exit 1
    fi
    #------------------------------------------------------------------
    # Crea un elenco di tutti i file appartenenti all'utente
    # specificato.
    # Si deve evitare che find cerchi di entrare nella directory /dev/.
    #------------------------------------------------------------------
    find / -user $NOME_UTENTE -a \( -path "/dev" -prune -o -print \) \
> $ELENCO_FILE_UTENTE
    #------------------------------------------------------------------
    # Comprime i file generando un file compresso con lo stesso nome
    # dell'utente da eliminare e con estensione .tgz.
    # Si utilizza tar e in particolare:
    # z permette di comprimere automaticamente l'archivio
    #        attraverso gzip;
    #------------------------------------------------------------------
    if tar czvf ~/$NOME_UTENTE.tgz `cat $ELENCO_FILE_UTENTE`
    then
        #--------------------------------------------------------------
        # Se  andato tutto bene, elimina i file
        # (togliere il commento).
        #--------------------------------------------------------------
        #rm `cat $ELENCO_FILE_UTENTE`
        echo
    fi

#======================================================================
# Fine.
#======================================================================
