#!/bin/sh -e

##########################################################################
#   Function description:
#       Pause until user presses return
##########################################################################

pause()
{
    local junk
    
    printf "Press return to continue..."
    read junk
}

auto-root-check $0

if ! cluster-check-cron-updates; then
    exit 0
fi

if [ $# != 1 ]; then
    printf "Usage: $0 username\n"
    exit 1
fi

LOCALBASE=$(cluster-localbase)

user_name=$1
home_dir=$(awk -F : -v user_name=$user_name '$1 == user_name { print $6 }' /etc/passwd)

gid=$(awk -F : -v u=$user_name '$1 == u { print $4 }' /etc/passwd)
gid_owner=$(awk -F : -v gid=$gid '$3 == gid { print $1 }' /etc/group)
dependent_users=$(awk -F : -v gid=$gid '$4 == gid { print $0 }' /etc/passwd | wc -l)
if [ $dependent_users -gt 1 ] && [ $gid_owner = $user_name ]; then
    cat << EOM

Other users have $gid as their primary group.

You must remove their accounts first.

EOM
    pause
    exit 0  # Don't kill cluster-user-cleanup over this
fi

# FIXME: Generalize this
cat << EOM

Possible share directories are shown below.

Do not assume this is a complete list.  Users may own files in other places!

EOM

ls -d /share*/*/$user_name* /*raid*/*/$user_name* /fasttemp1/*/$user_name* 2> /dev/null || true
if [ $gid_owner != $user_name ]; then
    ls -d /share*/*/$gid_owner* /*raid*/*/$user_name* /fasttemp1/*/$gid_owner* 2> /dev/null || true
fi
printf '\n'
pause

resp="Restart"
while [ 0"$resp" = 0Restart ] && [ 0"$resp" != 0Cancel ]; do
    cat << EOM

The following files are symbolic links.  The targets they point to
will NOT be removed automatically.  You MUST deal with these targets
before proceeding to avoid orphaning files on the cluster.
EOM

    printf '\n'
    find $home_dir -type l -ls
    printf '\n'
    
    pause
    
    cat << EOM

In a separate window:

1.  Open a root shell on each file server containing files to be archived
    or removed.

2.  Run "cluster-archive-user-dir <directory>" to archive directories to
    be saved before account removal.

3.  Visually inspect the user's home directory and remove any files
    that are obviously junk.  This will reduce noise in the steps that
    follow.

EOM
    done=''
    while [ 0$done != 0done ]; do
	read -p "Type 'done' when finished: " done
    done
    
    cat << EOM

Scanning for files owned by $user_name...

There MUST be a link within $home_dir to every directory outside
$home_dir containing files owned by $user_name.

If there is not, create all necessary links before proceeding.

We will then view a list of all files owned by $user_name.

EOM
    resp=''
    while [ 0"$resp" != 0done ]; do
	read -p "Type 'done' when finished: " resp
    done
    
    find -L $home_dir -user $user_name | more
    
    cat << EOM

If there are other any files owned by $user_name that you would like to
save, archive them or chown them to another user NOW.  Be sure to
set both owner and group to something that will exist after
$user_name is removed.

EOM

    pause
    
    cat << EOM
		
Make sure *ALL* files outside $home_dir are removed, archived, or
assigned to another user before you continue.

Files in $home_dir will be removed automatically if you choose
not to archive it.

Type "Remove account" to proceed with removal of $user_name.

Type "Restart" to restart the removal process for this user.

Type "Cancel" to abort the removal.

EOM

    read -p 'Enter your choice: ' resp
done
if [ 0"$resp" != 0"Remove account" ]; then
    exit 0
fi

printf "This action cannot be undone!\n"
printf "Are you absolutely sure you want to remove $user_name? yes/[no] "

read resp
if [ 0$resp != 0'yes' ]; then
    exit 0
fi

printf "Save home directory? [yes]/no "
read save_home
if [ 0$save_home != 0no ]; then
    # Save home dir from removal by rmuser -y
    mkdir -p /home/Archived-users
    mv /home/$user_name /home/Archived-users
fi

# Save user info
mkdir -p $LOCALBASE/etc/spcm
printf "\nRemoved by $USER:\n" >> $LOCALBASE/etc/spcm/removed-users
fgrep $user_name /etc/passwd >> $LOCALBASE/etc/spcm/removed-users
fgrep $user_name /etc/group >> $LOCALBASE/etc/spcm/removed-users || true

# Group should be removed by rmuser, but it isn't!
cluster-run -c "auto-remove-user -y $user_name" all
auto-remove-user -r -y $user_name
