#!/bin/bash
# Rescapp Winpromote 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! :("

WINPROMOTE_RUNNING_STR="Promoting Windows user to Admin."
WINPROMOTE_PARTITIONS_NOT_FOUND="No Windows partitions were found or chosen."
WINPROMOTE_SAMUSERS_NOT_DETECTED="No SAM users or SAM file were detected."
WINPROMOTE_SAMUSERS_NOT_FOUND="No Windows users were found or chosen."
WINPROMOTE_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 promote to Admin")
      if [ "x${WINDOWS_USER}" != "x" ] ; then
        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}
        fi
      else
        rtux_Message_Warning ${WINPROMOTE_SAMUSERS_NOT_FOUND}
        rtux_Message_Failure ${WINPROMOTE_NOT_RESET_STR}
      fi
    else
      rtux_Message_Warning ${WINPROMOTE_BACKUP_WENT_WRONG}
      rtux_Message_Failure ${WINPROMOTE_NOT_RESET_STR}
    fi

  else
    rtux_Message_Warning ${WINPROMOTE_SAMUSERS_NOT_DETECTED}
    rtux_Message_Failure ${WINPROMOTE_NOT_RESET_STR}
  fi
else
  rtux_Message_Warning ${WINPROMOTE_PARTITIONS_NOT_FOUND}
  rtux_Message_Failure ${WINPROMOTE_NOT_RESET_STR}
fi

