#!/bin/bash
#===============================================================================
#
# DIRECTORY:
#   /home/*/.local/share/nautilus/scripts/05a-Windows/
# OR
#   /home/*/.gnome2/nautilus-sctipts/05a-Windows/ (deprecated)
#
# FILE:
#   04-WindowsDir-activate-all-services-in-Vista-and-7
#
# USAGE:
#   Right click on a Windows directory and
#   choose this nautilus script from the context menu.
#
# OPTIONS:
#   none
#
# DESCRIPTION:
#   Activate the services for IDE, SCSI, SAS and if available for S-ATA at
#     boot time.
#
# REQUIREMENTS:
#   bash, zenity, libwin-hivex-perl, libhivex-bin, coreutils and sed
#
# BUGS:
#   ---
#
# NOTES:
#   Tested on
#   - Debian 8+
#   - Arch Linux
#
# AUTHOR:
#   Patrick Neumann, patrick@neumannsland.de
#
# COMPANY:
#   (privately)
#
# VERSION:
#   0.9 (beta)
#
# LINK TO THE MOST CURRENT VERSION:
#   https://...
#
# CREATED:
#   16.03.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:
#   - Add hints:
#   -- Windows XP -> OpenGates
#   -- Windows 8/10 (all needes services are allready activated per default!)
#
# 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 "${HIVEXGET_BIN}" "libhivex-bin"
check_dep "${HXREGEDIT_BIN}" "libwin-hivex-perl"
check_dep "${PRINTF_BIN}" "coretuils"
check_dep "${RM_BIN}" "coreutils"
check_dep "${SED_BIN}" "sed"

# No XP support needed, use OpenGates instead!
check_dir "${SOURCE}" "Windows"

#-------------------------------------------------------------------------------
# Select hive.
#-------------------------------------------------------------------------------
readonly HIVE="${SOURCE}/System32/config/SYSTEM"
readonly SERVICES="atapi intelide pciide iaStorA iaStorF lsi_scsi lsi_sas"
readonly CURRENT="$( ${PRINTF_BIN} "%03d" "$( ${HIVEXGET_BIN} "${HIVE}" 'Select' 'Current' )" )"

#-------------------------------------------------------------------------------
# Switch services for IDE, S-ATA, SCSI and SAS to "start by boot loader".
#-------------------------------------------------------------------------------
for SERVICE in ${SERVICES} ; do
  if [ -f "${SOURCE}/System32/drivers/${SERVICE}.sys" ] ; then
    if [ $( ${HIVEXGET_BIN} "${HIVE}" 'ControlSet'${CURRENT}'\services\'${SERVICE} 'Start' ) -ne 0 ] ; then
      TMPREGFILE="/tmp/SYSTEM_CCS_services_${SERVICE}.reg"
      if ! ${HXREGEDIT_BIN} --export --prefix 'HKEY_LOCAL_MACHINE\SYSTEM' "${HIVE}" '\ControlSet'${CURRENT}'\services\'${SERVICE} > "${TMPREGFILE}" 2>/dev/null ; then
        hint "exporting registry node ${SERVICE} failed"
      fi      
      if ! ${SED_BIN} --in-place 's/\("Start"=dword:0000000\)\(3\)/\10/' "${TMPREGFILE}" > /dev/null 2>&1 ; then
        hint "modifying registry node ${SERVICE} failed"
      fi
      if ! ${HXREGEDIT_BIN} --merge --prefix 'HKEY_LOCAL_MACHINE\SYSTEM' "${HIVE}" "${TMPREGFILE}" > /dev/null 2>&1 ; then
        hint "merging registry node ${SERVICE} failed"
      fi
      if ! ${RM_BIN} "${TMPREGFILE}" > /dev/null 2>&1 ; then
        hint "deleting temporary file failed"
      fi
    fi
  fi
done | ${ZENITY_BIN} --progress \
                     --title="activate_all_services_in_Vista_and_7" \
                     --text="Please wait..." \
                     --pulsate

success "the reactivation of all services should have been completed successfully"

exit 0