#!/bin/bash
####################################################################################################
#
# FILENAME:     resync-source-refs
#
# PURPOSE:      ##ADD_PURPOSE_HERE##
#
# DESCRIPTION:  ##ADD_DESCRIPTIION_HERE##
#
# INSTRUCTIONS: Execute the following command relative to your project's root directory:  
#               ./tools/resync-source-refs
#
# RELATED DOCS: TODO: ?????
#               └─ https://???.???.com
#
#               TODO: ?????
#               ├─ https://www.????.com
#               └─ https://www.????.com
#
####################################################################################################
#
##
###
#### LOAD SHARED FUNCTIONS LIBRARY #################################################################
###
##
#
if [ ! -r `dirname $0`/lib/shared-functions.sh ]; then
  echo "\nFATAL ERROR: `tput sgr0``tput setaf 1`Could not load tools/lib/shared-functions.sh.  File not found.\n"
  exit 1
fi
source `dirname $0`/lib/shared-functions.sh
#
##
###
#### CONFIRM SCRIPT EXECUTION ######################################################################
###
##
#
confirmScriptExecution "##ADD_CONFIRMATION_QUESTION##?"
#
##
###
#### CREATE LOCAL VARIABLES ########################################################################
###
##
#
# No local variables are used by this script.
#
##
###
#### CUSTOM CODE BEGINS HERE #######################################################################
###
##
#

# Add your code here.

#
##
###
#### ECHO CLOSING MESSAGE ##########################################################################
###
##
#
echoScriptCompleteMsg \
"Script completed successfully."
exit 0





# Make sure none of the sample code below is ever accidentally executed
# Delete this when you build a new script based on this template.
return 0
exit 0



####################################################################################################
## EXAMPLE ONE - EXECUTING A MULTI-STEP, COMPLEX ACTION USING THE CLI
####################################################################################################


#### REBUILD MDAPI SOURCE FILES AND FOLDERS ########################################################
#
# 0. Reset the Step Message counter and set the TOTAL STEPS to 4.
resetStepMsgCounter 4

# 1. Ensure that the SFDX Package Directory specified by $DEFAULT_PACKAGE_DIR_NAME exists.
echoStepMsg "Looking for SFDX Package Directory named \"sfdx-source/$DEFAULT_PACKAGE_DIR_NAME\""
if [ -d "$PROJECT_ROOT/sfdx-source/$DEFAULT_PACKAGE_DIR_NAME" ]; then
  echo "SFDX Package directory found at: $PROJECT_ROOT/sfdx-source/$DEFAULT_PACKAGE_DIR_NAME"
else
  echoErrorMsg "No SFDX Package Directory named \"sfdx-source/$DEFAULT_PACKAGE_DIR_NAME\" found. Aborting script."
  exit 1
fi

# 2. If there is a matching folder in the project's mdapi-source directory, delete any existing files.
echoStepMsg "Removing stale files from  \"mdapi-source/$DEFAULT_PACKAGE_DIR_NAME\""
if [ -d "$PROJECT_ROOT/mdapi-source/$DEFAULT_PACKAGE_DIR_NAME" ]; then
  (cd $PROJECT_ROOT && exec rm -rf ./mdapi-source/$DEFAULT_PACKAGE_DIR_NAME \
                    && exec mkdir  ./mdapi-source/$DEFAULT_PACKAGE_DIR_NAME)
  echo "Directory $PROJECT_ROOT/mdapi-source/$DEFAULT_PACKAGE_DIR_NAME has been cleaned and is ready for updated metadata."
else
  echo "Directory $PROJECT_ROOT/mdapi-source/$DEFAULT_PACKAGE_DIR_NAME does not exist. It will be created by the SFDX source conversion process."
fi

# 3. Convert the SFDX source into MDAPI source.
echoStepMsg "Converting SFDX source from Package Directory \"$DEFAULT_PACKAGE_DIR_NAME\". Output folder is \"mdapi-source/$DEFAULT_PACKAGE_DIR_NAME\""
echo \
"Executing force:source:convert \\
            --rootdir   ./sfdx-source/$DEFAULT_PACKAGE_DIR_NAME \\
            --outputdir ./mdapi-source/$DEFAULT_PACKAGE_DIR_NAME\n"
(cd $PROJECT_ROOT && exec sfdx force:source:convert \
                                --rootdir   ./sfdx-source/$DEFAULT_PACKAGE_DIR_NAME \
                                --outputdir ./mdapi-source/$DEFAULT_PACKAGE_DIR_NAME)


# TODO: Need to add a check to see if force:source:convert worked.
#       If it didn't, then we need to exit this script now without doing the 
#       check-only deploy to the packaging org.


# 4. Attempt a check-only deploy of the MDAPI package against the packaging org.
echoStepMsg "Attempt a check-only deploy of the MDAPI package against the packaging org"
echo \
"Executing force:mdapi:deploy \\
            --checkonly \\
            --deploydir ./mdapi-source/$DEFAULT_PACKAGE_DIR_NAME \\
            --testlevel NoTestRun \\
            --targetusername $PACKAGING_ORG_ALIAS \\
            --wait 15\n"
(cd $PROJECT_ROOT && exec sfdx force:mdapi:deploy \
                                --checkonly \
                                --deploydir ./mdapi-source/$DEFAULT_PACKAGE_DIR_NAME \
                                --testlevel NoTestRun \
                                --targetusername $PACKAGING_ORG_ALIAS \
                                --wait 15)

# Check if the previous command executed successfully. If not, abort this script.
if [ $? -ne 0 ]; then
  echoErrorMsg "The previous command did not execute as expected. Aborting script"
  exit 1
fi


####################################################################################################
## EXAMPLE TWO - PROVIDE A CLOSING MESSAGE AT THE END OF YOUR SCRIPT
####################################################################################################
#
#
#### ECHO CLOSING SUCCESS MESSAGE ##################################################################
#
echoScriptCompleteMsg "##PROCESS_COMPLETION_MESSAGE##"

##END##