#!/usr/bin/env bash
version="change-username  (2018-01-22)"
# Change user name from user to something else, for a live-build system
# that was installed with refractainstaller.
# Run as root with oldname and newname as arguments.
# Copyright fsmithred@gmail.com 2011-2018
# License: GPL-3

. gettext.sh
TEXTDOMAIN=refractainstaller-base
export TEXTDOMAIN
TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAINDIR

oldname="$1"
newname="$2"
programname="$0"

show_help () {
eval_gettext "
	Version:	\$version

	Usage:
    
		\$programname    <oldname>    <newname>
" ; echo
}

# function to exit the script if there are errors
check_exit () {
	exit_code="$?"
if [[ $exit_code -ne 0 ]] ; then
	eval_gettext "	Exit due to error:  \$exit_code" ; echo
	exit 1
fi
}

# check root
if [[ $(id -u) -ne 0 ]] ; then
	gettext "	You need to be root!" ; echo
	exit 1
fi

# check that there are two arguments
if [[ $# -ne 2 ]] ; then
	show_help
	exit 1
fi

# check that oldname exists
if ! $(grep -q $oldname /etc/passwd) ; then
	eval_gettext "	\$oldname does not exist." ; echo
	exit 1
fi


:<<HOLD1
# Test if xinit or a display manager is running, and save the information
# for later use.
if ps -C xinit; then
  dm_status="no"
elif
  ps -C gdm; then
  dm_status="yes"
  dm="gdm"
elif
  ps -C gdm3; then
  dm_status="yes"
  dm="gdm3"
elif
  ps -C kdm ; then
  dm_status="yes"
	# Trinity DM (3.5.13 versions) actual PID shows as "kdm"
	if /usr/sbin/sysv-rc-conf  --list kdm-trinity|grep ":on" ; then
		dm="kdm-trinity"
    else
		dm="kdm"
    fi
elif
  # Trinity DM (R14 "testing" versions)
  ps -C tdm ; then
  dm_status="yes"
  dm="tdm-trinity"
elif
  ps -C xdm; then
  dm_status="yes"
  dm="xdm"
elif
  ps -C lightdm; then
  dm_status="yes"
  dm="lightdm"
elif
  ps -C slim; then
  dm_status="yes"
  dm="slim"
elif
  ps -C wdm; then
  dm_status="yes"
  dm="wdm"
elif
  ps -C mdm; then
  dm_status="yes"
  dm="mdm"
fi
HOLD1


# Make sure the old user is not logged in - kill that user's processes.
while true; do
eval_gettext "
	\$oldname must not be logged in if you want to proceed.
	Press 1 to kill \${oldname}'s processes now and proceed.
	Press 2 to exit the script and manually close any running programs.
" ; echo

	read ans
	case $ans in
		[Yy1]*) for i in $(pgrep -u $oldname) ; do
				kill $i
			 #killall -u "$oldname"
			 done
			 break ;;
		[Nn2]*) exit 0
	esac
done


eval_gettext "	Changing user name and group...
" ; echo
sleep 3

# Change user name and group
usermod -l $newname $oldname ; check_exit
groupmod -n $newname $oldname ; check_exit
usermod -d /home/$newname -m $newname ; check_exit

# Show that it was done
gettext "
	Checking that the name has been changed...
" ; echo
sleep 2
if ! id $oldname  >/dev/null 2>&1 ; then
	eval_gettext "
	\$oldname has been deleted.
" ; echo
else
	eval_gettext "	Something is wrong. \$oldname still exists" ; echo
	exit 1
fi
sleep 2
eval_gettext "
	Checking \${newname}'s group memberships:
" ; echo
if ! id $newname ; then
    exit 1
fi
sleep 2

# Ask to change newuser's password.
while true; do
	eval_gettext "
	Press 1 to change \${newname}'s password.
	Press 2 to continue without changing the password.
" ; echo
	read ans
	case $ans in
		[Yy1]*) passwd $newname ; break ;;
		[Nn2]*) break ;;
	esac
done


eval_gettext "
	This script will attempt to replace every instance of 
	/home/\$oldname with /home/\$newname in your user's config files.

	You may need to edit the properties of desktop icons for 
	terminal, file manager, browser and maybe text editor. 
	(Just reset the working directory.) 

" ; echo
  
eval_gettext "	Press the ENTER key when you're ready to proceed." ; read -p " "

for i in $(grep -r "/home/$oldname" /home/$newname/.config | awk -F":" ' { print $1 }'); do
    sed -i "s/\/home\/$oldname/\/home\/$newname/g" "$i"
done

#for i in $(grep -r "/home/$oldname" /home/$newname/.local | awk -F":" '{ print $1 }'); do
#	sed -i "s/\/home\/$oldname/\/home\/$newname/g" "$i"
#done

if [[ -d /home/"$oldname"/.kde/share ]] ; then
	sed -i "s/\/home\/$oldname/\/home\/$newname/g" /home/$newname/.kde/share/apps/kfileplaces/bookmarks.xml
	sed -i "s/\/home\/$oldname/\/home\/$newname/g" /home/$newname/.kde/share/config/startupconfigfiles
	sed -i "s/\/home\/$oldname/\/home\/$newname/g" /home/$newname/.kde/share/config/ksmserverrc
fi

# Change user's real name
live_user=$(awk -v pattern="$newname" -F: '$1 ~ pattern { print $5 }' /etc/passwd)
eval_gettext "
	The user's real name is currently \$live_user.
	Enter the real name you want to use (without the trailing commas.)
" ; echo
read real_name
sed -i~ "s/$live_user/$real_name,,,/" /etc/passwd



:<<HOLD
# Edit /etc/gdm3/daemon.conf
while true; do
    echo "
    Edit /etc/gdm3/daemon.conf to disable graphical auto-login?
    If you don't do this, gdm3 will hang. If that happens, you can
    reboot in recovery mode and issue the command:
    update-rc.d -f gdm3 remove
    log out as root and log in as your user. Start the desktop with:
    startx

    Edit daemon.conf?  (yes or no)
    "
    read ans
    case $ans in
      [Yy]*) nano /etc/gdm3/daemon.conf ; break ;;
      [Nn]*) break ;;
    esac
done
HOLD


# Restart the display manager
gettext "
	Done.

	You can now log in as the new user.
" ; echo

:<<HOLD2	
	If you'd like, I'll attempt to restart the xserver.
    "
sleep 2

if [[ $dm_status = yes ]]; then
	while true ; do
		echo "
	Would you like to restart the xserver?
	
	(yes or no)
	"
		read ans
		case $ans in
		  [Yy]*) /etc/init.d/"$dm" restart & break ;;
		  [Nn]*) break ;;
		esac
	done
fi
HOLD2

exit 0
