Skip to content

Commit

Permalink
ADD - updates check function
Browse files Browse the repository at this point in the history
  • Loading branch information
MRColorR committed Oct 26, 2023
1 parent 2c54b0f commit bdd70b6
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#
######################################################################
## PROJECT_VERSION
PROJECT_VERSION=2.4.4
PROJECT_VERSION=2.5.4
## PROJECT NAME
COMPOSE_PROJECT_NAME=money4band
DS_PROJECT_SERVER_URL=https://discord.com/invite/Fq8eeazBAD
Expand Down
97 changes: 64 additions & 33 deletions runme.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ set-executionpolicy -scope CurrentUser -executionPolicy Bypass -Force

### Variables and constants ###
## Env file related constants and variables ##
# env file name #
$ENV_FILENAME = '.env'
# ${ENV_FILENAME} file prototype link #
$ENV_SRC = "https://github.com/MRColorR/money4band/raw/main/${ENV_FILENAME}"
# env file name and template file name #
$script:ENV_TEMPLATE_FILENAME = '.env.template'
$script:ENV_FILENAME = '.env'

# Env file default #
$DEVICE_NAME_PLACEHOLDER = 'yourDeviceName'
$script:DEVICE_NAME = 'yourDeviceName'
Expand All @@ -20,16 +20,27 @@ $script:CONFIG_JSON_FILE = "config.json"
$script:MAINMENU_JSON_FILE = "mainmenu.json"

## Docker compose related constants and variables ##
# docker compose yaml file name #
# docker compose yaml file name and template file name #
$DKCOM_TEMPLATE_FILENAME = "docker-compose.yaml.template"
$DKCOM_FILENAME = "docker-compose.yaml"
# docker compose yaml prototype file link #
$DKCOM_SRC = "https://github.com/MRColorR/money4band/raw/main/$DKCOM_FILENAME"

### Docker installer script for Windows source link ##
$DKINST_WIN_SRC = 'https://github.com/MRColorR/money4band/raw/main/.resources/.scripts/install-docker-win.ps1'
### Docker installer script for Mac source link ##
$DKINST_MAC_SRC = 'https://github.com/MRColorR/money4band/raw/main/.resources/.scripts/install-docker-mac.ps1'
## Script variables ##
## Script init and variables ##
# initialize the env file with the default values if there is no env file already present
# Check if the ${ENV_FILENAME} file is already present in the current directory, if it is not present copy from the .env.template file renaming it to ${ENV_FILENAME}, if it is present ask the user if they want to reset it or keep it as it is
if (-not (Test-Path .\${ENV_FILENAME})) {
Write-Output "No ${ENV_FILENAME} file found, copying ${ENV_FILENAME} and ${DKCOM_FILENAME} from the template files"
Copy-Item .\${ENV_TEMPLATE_FILENAME} .\${ENV_FILENAME} -Force
Copy-Item .\${DKCOM_TEMPLATE_FILENAME} .\${DKCOM_FILENAME} -Force
}
else {
Write-Output "Already found ${ENV_FILENAME} file, proceeding with setup"
}
#Read-Host -Prompt "Press Enter to continue"

# Script version getting it from ${ENV_FILENAME} file#
$SCRIPT_VERSION = (Get-Content .\${ENV_FILENAME} | Select-String -Pattern "PROJECT_VERSION=" -SimpleMatch).ToString().Split("=")[1]

Expand All @@ -40,7 +51,8 @@ $SCRIPT_NAME = $MyInvocation.MyCommand.Name # save the script name in a variable
$DS_PROJECT_SERVER_URL = (Get-Content .\${ENV_FILENAME} | Select-String -Pattern "DS_PROJECT_SERVER_URL=" -SimpleMatch).ToString().Split("=")[1]

# Script URL for update #
$UPDATE_SCRIPT_URL = "https://raw.githubusercontent.com/MRColorR/money4band/main/$SCRIPT_NAME"
$PROJECT_BRANCH = "pythonize"
$PROJECT_URL = "https://raw.githubusercontent.com/MRColorR/money4band/${PROJECT_BRANCH}"

# Script debug log file #
$DEBUG_LOG = "debug_$SCRIPT_NAME.log"
Expand Down Expand Up @@ -181,6 +193,23 @@ function fn_fail($text) {
}

## Utility functions ##
# Function to check if there are any updates available #
function check_project_updates {
# Get the current script version from the local .env file
$SCRIPT_VERSION = (Get-Content .\$ENV_FILENAME | Select-String -Pattern "PROJECT_VERSION=" -SimpleMatch).ToString().Split("=")[1]

# Get the latest script version from the .env.template file on GitHub
$webClient = New-Object System.Net.WebClient
$templateContent = $webClient.DownloadString("$PROJECT_URL/$ENV_TEMPLATE_FILENAME")
$LATEST_SCRIPT_VERSION = ($templateContent | Select-String -Pattern "PROJECT_VERSION=" -SimpleMatch).ToString().Split("=")[1]

# Compare the versions and print a message if a newer version is available
if ($SCRIPT_VERSION -lt $LATEST_SCRIPT_VERSION) {
print_and_log "GREEN" "A newer version of the script is available. Please consider updating."
}
Read-Host -Prompt "Press Enter to continue"
}

# Function to detect OS
function detect_os {
toLog_ifDebug -l "[DEBUG]" -m "Detecting OS..."
Expand Down Expand Up @@ -1080,6 +1109,7 @@ function fn_setupEnv() {
[string]$app_type # Accept the type of apps as an argument
)
print_and_log "BLUE" "Starting setupEnv function for $app_type"

# Check if ${ENV_FILENAME} file is already configured if 1 then it is already configured, if 0 then it is not configured
$ENV_CONFIGURATION_STATUS = (Get-Content .\${ENV_FILENAME} | Select-String -Pattern "ENV_CONFIGURATION_STATUS=" -SimpleMatch).ToString().Split("=")[1]
toLog_ifDebug -l "[DEBUG]" -m "Current ENV_CONFIGURATION_STATUS: $ENV_CONFIGURATION_STATUS"
Expand All @@ -1094,11 +1124,11 @@ function fn_setupEnv() {
$yn = Read-Host

if ($yn.ToLower() -eq 'y' -or $yn.ToLower() -eq 'yes') {
print_and_log "DEFAULT" "Downloading a fresh ${ENV_FILENAME} file."
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $script:ENV_SRC -OutFile "${ENV_FILENAME}"
Invoke-WebRequest -Uri $script:DKCOM_SRC -OutFile "$($script:DKCOM_FILENAME)"
$ProgressPreference = 'Continue'
print_and_log "DEFAULT" "Resetting ${ENV_FILENAME} file and ${DKCOM_FILENAME} file."
Remove-Item .\${ENV_FILENAME}
Remove-Item .\${DKCOM_FILENAME}
Copy-Item .\${ENV_TEMPLATE_FILENAME} .\${ENV_FILENAME} -Force
Copy-Item .\${DKCOM_TEMPLATE_FILENAME} .\${DKCOM_FILENAME} -Force
Clear-Host
break
}
Expand Down Expand Up @@ -1323,28 +1353,27 @@ function fn_stopStack() {
Function that will reset the ${ENV_FILENAME} file
.DESCRIPTION
This function will reset the ${ENV_FILENAME} file to the original version downloading a fresh copy from the repository.
This function will reset the ${ENV_FILENAME} file to the original version using the ${ENV_TEMPLATE_FILENAME} template file.
.EXAMPLE
Just call fn_resetEnv
.NOTES
This function has been tested until v 2.0.0. The new version has not been tested as its assume that the logic is the same as the previous one just more refined.
#>
function fn_resetEnv() {
Clear-Host
toLog_ifDebug -l "[DEBUG]" -m "Starting resetEnv function"
while ($true) {
colorprint "YELLOW" "Now a fresh env file will be downloaded and will need to be configured to be used again"
colorprint "YELLOW" "A fresh ${ENV_FILENAME} file will be created from the ${ENV_TEMPLATE_FILENAME} template file"
$yn = Read-Host "Do you wish to proceed Y/N?"
if ($yn.ToLower() -eq 'y' -or $yn.ToLower() -eq 'yes') {
try {
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $script:ENV_SRC -OutFile "${ENV_FILENAME}"
$ProgressPreference = 'Continue'
if (Test-Path .\${ENV_FILENAME}) {
Remove-Item .\${ENV_FILENAME}
}
Copy-Item .\${ENV_TEMPLATE_FILENAME} .\${ENV_FILENAME} -Force
colorprint "GREEN" "${ENV_FILENAME} file resetted, remember to reconfigure it"
}
catch {
colorprint "RED" "Error resetting ${ENV_FILENAME} file. Please check your internet connection and try again."
colorprint "RED" "Error resetting ${ENV_FILENAME} file."
}
Read-Host "Press Enter to go back to mainmenu"
break
Expand All @@ -1358,6 +1387,7 @@ function fn_resetEnv() {
colorprint "RED" "Please answer yes or no."
}
}
toLog_ifDebug -l "[DEBUG]" -m "resetEnv function ended"
}


Expand All @@ -1366,28 +1396,27 @@ function fn_resetEnv() {
Function that will reset the docker-compose.yaml file
.DESCRIPTION
This function will reset the docker-compose.yaml file to the original version downloading a fresh copy from the repository.
This function will reset the docker-compose.yaml file to the original version using the ${DKCOM_TEMPLATE_FILENAME} template file.
.EXAMPLE
Just call fn_resetDockerCompose
.NOTES
This function has been tested until v 2.0.0. The new version has not been tested as its assume that the logic is the same as the previous one just more refined.
#>
function fn_resetDockerCompose() {
Clear-Host
toLog_ifDebug -l "[DEBUG]" -m "Starting resetDockerCompose function"
while ($true) {
colorprint "YELLOW" "Now a fresh $($script:DKCOM_FILENAME) file will be downloaded"
colorprint "YELLOW" "A fresh ${DKCOM_FILENAME} file will be created from the ${DKCOM_TEMPLATE_FILENAME} template file"
$yn = Read-Host "Do you wish to proceed Y/N?"
if ($yn.ToLower() -eq 'y' -or $yn.ToLower() -eq 'yes') {
try {
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $script:DKCOM_SRC -OutFile "$($script:DKCOM_FILENAME)"
$ProgressPreference = 'Continue'
colorprint "GREEN" "$($script:DKCOM_FILENAME) file resetted, remember to reconfigure it if needed"
if (Test-Path .\${DKCOM_FILENAME}) {
Remove-Item .\${DKCOM_FILENAME}
}
Copy-Item .\${DKCOM_TEMPLATE_FILENAME} .\${DKCOM_FILENAME} -Force
colorprint "GREEN" "${DKCOM_FILENAME} file resetted, remember to reconfigure it if needed"
}
catch {
colorprint "RED" "Error resetting $($script:DKCOM_FILENAME) file. Please check your internet connection and try again."
colorprint "RED" "Error resetting ${DKCOM_FILENAME} file."
}
Read-Host "Press Enter to go back to mainmenu"
break
Expand All @@ -1401,6 +1430,7 @@ function fn_resetDockerCompose() {
colorprint "RED" "Please answer yes or no."
}
}
toLog_ifDebug -l "[DEBUG]" -m "resetDockerCompose function ended"
}


Expand Down Expand Up @@ -1448,6 +1478,7 @@ This function has been tested until v 2.0.0. The new version has not been tested
function mainmenu {
Clear-Host
colorprint "GREEN" "MONEY4BAND AUTOMATIC GUIDED SETUP v$script:SCRIPT_VERSION"
check_project_updates
colorprint "GREEN" "---------------------------------------------- "
colorprint "MAGENTA" "Join our Discord community for updates, help, and discussions: $DS_PROJECT_SERVER_URL"
colorprint "MAGENTA" "---------------------------------------------- "
Expand Down
87 changes: 61 additions & 26 deletions runme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
### Variables and constants ###

## Env file related constants and variables ##
# env file name #
# env file name and template file name #
readonly ENV_TEMPLATE_FILENAME='.env.template'
readonly ENV_FILENAME='.env'
# ${ENV_FILENAME} file prototype link #
readonly ENV_SRC="https://github.com/MRColorR/money4band/raw/main/${ENV_FILENAME}" # this should be removed and to reset copyrom the template

# Env file default #
readonly DEVICE_NAME_PLACEHOLDER='yourDeviceName'
DEVICE_NAME='yourDeviceName'
Expand All @@ -20,12 +20,22 @@ readonly CONFIG_JSON_FILE="config.json"
readonly MAINMENU_JSON_FILE="mainmenu.json"

## Docker compose related constants and variables ##
# docker compose yaml file name #
# docker compose yaml file name and template file name #
readonly DKCOM_TEMPLATE_FILENAME="docker-compose.yaml.template"
readonly DKCOM_FILENAME="docker-compose.yaml"
# docker compose yaml prototype file link #
readonly DKCOM_SRC="https://github.com/MRColorR/money4band/raw/main/$DKCOM_FILENAME"

## Script variables ##
## Script init and variables ##
# initialize the env file with the default values if there is no env file already present
# Check if the ${ENV_FILENAME} file is already present in the current directory, if it is not present copy from the .env.template file renaming it to ${ENV_FILENAME}, if it is present ask the user if they want to reset it or keep it as it is
if [ ! -f "${ENV_FILENAME}" ]; then
printf "No ${ENV_FILENAME} file found, copying ${ENV_FILENAME} and ${DKCOM_FILENAME} from the template files"
cp "${ENV_TEMPLATE_FILENAME}" "${ENV_FILENAME}"
cp "${DKCOM_TEMPLATE_FILENAME}" "${DKCOM_FILENAME}"
else
printf "Already found ${ENV_FILENAME} file, proceeding with setup"
fi
#read -r -p "Press Enter to continue"

# Script version getting it from ${ENV_FILENAME} file #
readonly SCRIPT_VERSION=$(grep -oP 'PROJECT_VERSION=\K[^#\r]+' ${ENV_FILENAME})

Expand All @@ -36,7 +46,9 @@ readonly SCRIPT_NAME=$(basename "$0") # save the script name in a variable not t
readonly DS_PROJECT_SERVER_URL=$(grep -oP 'DS_PROJECT_SERVER_URL=\K[^#\r]+' ${ENV_FILENAME})

# Script URL for update #
readonly UPDATE_SCRIPT_URL="https://raw.githubusercontent.com/MRColorR/money4band/main/${SCRIPT_NAME}"
readonly PROJECT_BRANCH="pythonize"
readonly PROJECT_URL="https://raw.githubusercontent.com/MRColorR/money4band/${PROJECT_BRANCH}"


# Script log file #
readonly DEBUG_LOG="debug_${SCRIPT_NAME}.log"
Expand Down Expand Up @@ -182,6 +194,20 @@ fn_fail() {
}

## Utility functions ##
# Function to check if there are any updates available #
check_project_updates() {
# Get the current script version from the local .env file
SCRIPT_VERSION=$(grep -oP 'PROJECT_VERSION=\K.*' "./\$ENV_FILENAME")

# Get the latest script version from the .env.template file on GitHub
LATEST_SCRIPT_VERSION=$(curl -s "$PROJECT_URL/$ENV_TEMPLATE_FILENAME" | grep -oP 'PROJECT_VERSION=\K.*')

# Compare the versions and print a message if a newer version is available
if [[ "$SCRIPT_VERSION" < "$LATEST_SCRIPT_VERSION" ]]; then
print_and_log "GREEN" "A newer version of the script is available. Please consider updating."
fi
}

# Function to detect OS
detect_os() {
toLog_ifDebug -l "[DEBUG]" -m "Detecting OS..."
Expand Down Expand Up @@ -977,6 +1003,7 @@ fn_setupProxy() {
fn_setupEnv(){
local app_type="$1" # Accept the type of apps as an argument
print_and_log "BLUE" "Starting setupEnv function for $app_type"

# Check if ${ENV_FILENAME} file is already configured if 1 then it is already configured, if 0 then it is not configured
ENV_CONFIGURATION_STATUS=$(grep -oP '# ENV_CONFIGURATION_STATUS=\K[^#\r]+' ${ENV_FILENAME})
toLog_ifDebug -l "[DEBUG]" -m "Current ENV_CONFIGURATION_STATUS: $ENV_CONFIGURATION_STATUS"
Expand All @@ -990,9 +1017,11 @@ fn_setupEnv(){
read -r yn
case $yn in
[Yy]* )
print_and_log "DEFAULT" "Downloading a fresh ${ENV_FILENAME} file."; #this partshould bechanged to use the local template file notdownloadingit from internet
curl -fsSL $ENV_SRC -o "${ENV_FILENAME}"
curl -fsSL $DKCOM_SRC -o "$DKCOM_FILENAME"
print_and_log "DEFAULT" "Resetting ${ENV_FILENAME} file and ${DKCOM_FILENAME} file."
rm "${ENV_FILENAME}"
rm "$DKCOM_FILENAME"
cp "${ENV_TEMPLATE_FILENAME}" "${ENV_FILENAME}"
cp "${DKCOM_TEMPLATE_FILENAME}" "${DKCOM_FILENAME}"
clear
break
;;
Expand Down Expand Up @@ -1187,15 +1216,15 @@ fn_resetEnv(){ # this function needs rewiting as it should use now the local .en
clear
toLog_ifDebug -l "[DEBUG]" -m "Starting resetEnv function"
while true; do
colorprint "RED" "Now a fresh env file will be downloaded and will need to be configured to be used again"
read -r -p "Do you wish to proceed Y/N? " yn
colorprint "YELLOW" "A fresh ${ENV_FILENAME} file will be created from the ${ENV_TEMPLATE_FILENAME} template file"
read -r -p "Do you wish to proceed Y/N?" yn
case $yn in
[Yy]* )
if curl -fsSL $ENV_SRC -o "${ENV_FILENAME}"; then
colorprint "GREEN" "${ENV_FILENAME} file resetted, remember to reconfigure it"
else
colorprint "RED" "Error resetting ${ENV_FILENAME} file. Please check your internet connection and try again."
if [ -f ./${ENV_FILENAME} ]; then
rm ./${ENV_FILENAME} || { colorprint "RED" "Error resetting ${ENV_FILENAME} file."; continue; }
fi
cp ./${ENV_TEMPLATE_FILENAME} ./${ENV_FILENAME} || { colorprint "RED" "Error resetting ${ENV_FILENAME} file."; continue; }
colorprint "GREEN" "${ENV_FILENAME} file resetted, remember to reconfigure it"
read -r -p "Press Enter to go back to mainmenu"
break
;;
Expand All @@ -1204,25 +1233,27 @@ fn_resetEnv(){ # this function needs rewiting as it should use now the local .en
read -r -p "Press Enter to go back to mainmenu"
break
;;
* ) colorprint "RED" "Please answer yes or no.";;
* )
colorprint "RED" "Please answer yes or no.";;
esac
done
toLog_ifDebug -l "[DEBUG]" -m "resetEnv function ended"
}



fn_resetDockerCompose(){
clear
toLog_ifDebug -l "[DEBUG]" -m "Starting resetDockerCompose function"
while true; do
colorprint "RED" "Now a fresh $DKCOM_FILENAME file will be downloaded"
colorprint "YELLOW" "A fresh ${DKCOM_FILENAME} file will be created from the ${DKCOM_TEMPLATE_FILENAME} template file"
read -r -p "Do you wish to proceed Y/N? " yn
case $yn in
[Yy]* )
if curl -fsSL $DKCOM_SRC -o "$DKCOM_FILENAME"; then
colorprint "GREEN" "$DKCOM_FILENAME file resetted, remember to reconfigure it if needed"
else
colorprint "RED" "Error resetting $DKCOM_FILENAME file. Please check your internet connection and try again."
if [ -f ./$DKCOM_FILENAME ]; then
rm ./$DKCOM_FILENAME || { colorprint "RED" "Error resetting $DKCOM_FILENAME file."; continue; }
fi
cp ./$DKCOM_TEMPLATE_FILENAME ./$DKCOM_FILENAME || { colorprint "RED" "Error resetting $DKCOM_FILENAME file."; continue; }
colorprint "GREEN" "$DKCOM_FILENAME file resetted, remember to reconfigure it"
read -r -p "Press Enter to go back to mainmenu"
break
;;
Expand All @@ -1240,7 +1271,9 @@ fn_resetDockerCompose(){
# Function that will check the necerrary dependencies for the script to run
fn_checkDependencies(){
clear
colorprint "GREEN" "MONEY4BAND AUTOMATIC GUIDED SETUP v${SCRIPT_VERSION}"$'\n'"---------------------------------------------- "
colorprint "GREEN" "MONEY4BAND AUTOMATIC GUIDED SETUP v${SCRIPT_VERSION}"
check_project_updates
colorprint "GREEN" "---------------------------------------------- "
colorprint "MAGENTA" "Join our Discord community for updates, help, and discussions: ${DS_PROJECT_SERVER_URL}"
colorprint "MAGENTA" "---------------------------------------------- "
print_and_log "YELLOW" "Checking dependencies..."
Expand All @@ -1260,7 +1293,9 @@ fn_checkDependencies(){
### Main Menu ##
mainmenu() {
clear
colorprint "GREEN" "MONEY4BAND AUTOMATIC GUIDED SETUP v${SCRIPT_VERSION}"$'\n'"---------------------------------------------- "
colorprint "GREEN" "MONEY4BAND AUTOMATIC GUIDED SETUP v${SCRIPT_VERSION}"
check_project_updates
colorprint "GREEN" "---------------------------------------------- "
colorprint "MAGENTA" "Join our Discord community for updates, help, and discussions: ${DS_PROJECT_SERVER_URL}"
colorprint "MAGENTA" "---------------------------------------------- "
colorprint "DEFAULT" "Detected OS type: ${OS_TYPE}"$'\n'"Detected architecture: $ARCH"$'\n'"Docker $DKARCH image architecture will be used if the app's image permits it"$'\n'"---------------------------------------------- "$'\n'
Expand Down

0 comments on commit bdd70b6

Please sign in to comment.