#!/bin/bash
#===============================================================================
#
# DIRECTORY:
#   /home/*/.local/share/nautilus/scripts/05c-Linux/
# OR
#   /home/*/.gnome2/nautilus-sctipts/05c-Linux/ (deprecated)
#
# FILE:
#   04-ROOT-passwd
#
# USAGE:
#   Right click on a mounted partition (root file system) and
#   choose this nautilus script from the context menu.
#
# OPTIONS:
#   none
#
# DESCRIPTION:
#   Change password for a choosen user via CHROOT_DIR.
#
# REQUIREMENTS:
#   bash, zenity, gnome-terminal and passwd
#
# BUGS:
#   ---
#
# NOTES:
#   Tested on
#   - Debian 8+
#   - Arch Linux (failed!)
#
# AUTHOR:
#   Patrick Neumann, patrick@neumannsland.de
#
# COMPANY:
#   (privately)
#
# VERSION:
#   0.9 (beta)
#
# LINK TO THE MOST CURRENT VERSION:
#   https://...
#
# CREATED:
#   01.12.2020
#
# COPYRIGHT (C):
#   2015-2020 - Patrick Neumann
#
# LICENSE:
#   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 3 of the License, or
#   (at your option) any later version.
#
# WARRANTY:
#   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, see <http://www.gnu.org/licenses/>.
#
# TODO:
#   - Test other solutions:
#   -- detect config, generate password and exchange directly
#   -- copy in busybox (bb), chroot, exec bb passwd, exit and rm bb
#   -- ???
#
# HISTORY:
#   0.9 - Patrick Neumann - Initial (public) release
#
#===============================================================================

#-------------------------------------------------------------------------------
# Additional supported Distribution(s) (add before Library!).
#-------------------------------------------------------------------------------
#SUPPORTED_OSR="arch"

#-------------------------------------------------------------------------------
# Check for library (casualscripter_nautilus-scripts_functions.sh).
#-------------------------------------------------------------------------------
readonly LIBRARY="${0%/*/*}/.casualscripter_nautilus-scripts_functions.sh"
if [ ! -f "${LIBRARY}" ] ; then
  zenity --error \
         --text \
         "ERROR: casualscripter_nautilus-scripts_functions.sh MISSING!"
  exit 1
fi

source "${LIBRARY}"

#-------------------------------------------------------------------------------
# Checks (see library "casualscripter_nautilus-scripts_functions.sh").
#-------------------------------------------------------------------------------
check_dep "${AWK_BIN}" "awk"
check_dep "${CUT_BIN}" "coreutils"
check_dep "${GREP_BIN}" "grep"
check_dep "${GTERMINAL_BIN}" "gnome-terminal"
check_dep "${SLEEP_BIN}" "coreutils"
check_dep "${PASSWD_BIN}" "passwd"
check_dep "${TR_BIN}" "coreutils"

#-------------------------------------------------------------------------------
# Checks if a chroot directory is selected.
#-------------------------------------------------------------------------------
if [ -z "${SOURCE}" ] ; then
  error_exit "no file selected, I will not modify your oun system"
fi

#-------------------------------------------------------------------------------
# Avoid unintended changes to the Image.
#-------------------------------------------------------------------------------
check_if_is_looped

#-------------------------------------------------------------------------------
# Ask user for user. ^^
#-------------------------------------------------------------------------------
readonly USERS="$( ${GREP_BIN} --extended-regexp "^.*:x:(0|5[0-9]{2}|1[0-9]{3}:)" "${SOURCE}/etc/passwd" \
                   | ${CUT_BIN} -d ":" -f 1 \
                   | ${AWK_BIN} 'NR == 1 { print "TRUE", $0; }; NR > 1 { print "FALSE", $0; }' \
                   | ${TR_BIN} "\n" " " )"

readonly CHOICE="$( ${ZENITY_BIN} --list \
                                  --text "Please select a user!" \
                                  --radiolist \
                                  --column "Pick" \
                                  --column "Users" \
                                  ${USERS} \
                                  --width="400" \
                                  --height="400" )"

if [ -z "${CHOICE}" ] ; then
  error_exit "no user selected"
fi

#-------------------------------------------------------------------------------
# ... passwd ...
#-------------------------------------------------------------------------------
if [ -f "${SOURCE}/etc/shadow" ] ; then
  ${GTERMINAL_BIN} --execute ${BASH_BIN} -c \
                             "echo 'Modify user ${CHOICE}...' ; \
                              ${SUDO_BIN} ${PASSWD_BIN} --root '${SOURCE}' '${CHOICE}' ; \
                              ${SLEEP_BIN} 3"
else
  error_exit "/etc/shadow not found"
fi

exit 0