#!/bin/bash
# Rescapp Winpass run script
# Copyright (C) 2012,2013,2014,2015,2016,2017,2018,2019,2020 Adrian Gibanel Lopez
#
# Rescapp 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.
#
# Rescapp 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 Rescapp.  If not, see <http://www.gnu.org/licenses/>.

source ${RESCAPP_LIB_FILE}

set -x
set -v

# TODO: Program check runtime (Maybe to be stolen from bootinfoscript)

# MAIN PROGRAM

WINPROMOTE_OK_STR="The user was promoted to Admin OK! :)"
WINPROMOTE_NOT_OK_STR="The user was not promoted to Admin. Something went wrong! :("

WINPASS_RESET_OK_STR="Windows password was reset OK! :)"
WINPASS_NOT_RESET_STR="Windows password was not reset. Something went wrong! :("

WINUNLOCK_OK_STR="The user was unlocked OK! :)"
WINUNLOCK_NOT_OK_STR="The user was not unlocked. Something went wrong! :("

WINEASY_OK_STR="Easy Windows (promote, reset and unlock) was OK! :)"
WINEASY_NOT_OK_STR="Easy Windows (promote, reset and unlock) was not fully completed. Something went wrong! :("

WINPASS_RESET_RUNNING_STR="Resetting Windows password."
WINPROMOTE_RUNNING_STR="Promoting Windows user to Admin"
WINUNLOCK_RUNNING_STR="Unlocking Windows user"

WINEASY_PARTITIONS_NOT_FOUND="No partitions were found or chosen."
WINEASY_SAMUSERS_NOT_DETECTED="No SAM users or SAM file were detected."
WINEASY_SAMUSERS_NOT_FOUND="No Windows users were found or chosen."
WINEASY_BACKUP_WENT_WRONG="Backup of SAM directory went wrong."

SELECTED_PARTITION=$(rtux_Choose_Windows_partition);

if [ "x${SELECTED_PARTITION}" != "x" ] ; then

  if rtux_Get_Sam_Users ${SELECTED_PARTITION} ; then
    # Backup of the files in a temporary folder
    if rtux_backup_windows_config ${SELECTED_PARTITION} "${SAM_FILE}" ; then
      # Ask the user which password to reset
      WINDOWS_USER=$(rtux_Choose_Sam_User "Choose Windows user to get Easy Admin")

      if [ "x${WINDOWS_USER}" != "x" ] ; then

        EXIT_SUM=0

        if rtux_Run_Show_Progress "${WINUNLOCK_RUNNING_STR}" rtux_winunlock_payload ${SELECTED_PARTITION} ${SAM_FILE} ${WINDOWS_USER} ; then
          rtux_Message_Success ${WINUNLOCK_OK_STR}
        else
          rtux_Message_Failure ${WINUNLOCK_NOT_OK_STR}
          false
        fi

        WINUNLOCK_EXIT_VALUE=$?
        EXIT_SUM=$((EXIT_SUM+WINUNLOCK_EXIT_VALUE))

        if rtux_Run_Show_Progress "${WINPROMOTE_RUNNING_STR}" rtux_winpromote_payload ${SELECTED_PARTITION} ${SAM_FILE} ${WINDOWS_USER} ; then
          rtux_Message_Success ${WINPROMOTE_OK_STR}
        else
          rtux_Message_Failure ${WINPROMOTE_NOT_OK_STR}
          false
        fi

        WINPROMOTE_EXIT_VALUE=$?
        EXIT_SUM=$((EXIT_SUM+WINPROMOTE_EXIT_VALUE))

        if rtux_Run_Show_Progress "${WINPASS_RESET_RUNNING_STR}" rtux_winpass_reset_payload ${SELECTED_PARTITION} ${SAM_FILE} ${WINDOWS_USER} ; then
          rtux_Message_Success ${WINPASS_RESET_OK_STR}
        else
          rtux_Message_Failure ${WINPASS_NOT_RESET_STR}
          false
        fi

        WINPASS_EXIT_VALUE=$?
        EXIT_SUM=$((EXIT_SUM+WINPASS_EXIT_VALUE))

        else
          rtux_Message_Warning ${WINEASY_SAMUSERS_NOT_FOUND}
          EXIT_SUM=1
          false
        fi

    else
      rtux_Message_Warning ${WINEASY_BACKUP_WENT_WRONG}
      EXIT_SUM=1
      false
    fi

  else
    rtux_Message_Warning ${WINEASY_SAMUSERS_NOT_DETECTED}
    EXIT_SUM=1
    false
  fi

else
  rtux_Message_Warning ${WINEASY_PARTITIONS_NOT_FOUND}
  EXIT_SUM=1
  false
fi

if [ ${EXIT_SUM} -eq 0 ] ; then
  rtux_Message_Success ${WINEASY_OK_STR}
else
  rtux_Message_Failure ${WINEASY_NOT_OK_STR}
fi

