Skip to content

Commit

Permalink
generic sh - use wppus.json
Browse files Browse the repository at this point in the history
  • Loading branch information
froger-me committed Jan 9, 2024
1 parent 981f5e3 commit 80eaedf
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 69 deletions.
42 changes: 38 additions & 4 deletions integration/dummy-generic/dummy-generic.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#!/bin/bash

### CHECKING THE PACKAGE STATUS ###

if [ "$1" == "status" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_installed)" == "false" ]; then
echo "Status: Not installed"
# halt the script
exit 0
elif [ "$1" == "status" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_installed)" == "true" ]; then
echo "Status: Installed"
# halt the script
exit 0
elif [ "$1" == "status" ]; then
echo "Status: Unknown"
# halt the script
exit 1
fi

### INSTALLING THE PACKAGE ###

if [ "$1" == "install" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_installed)" == "false" ] && [ "$2" != "" ]; then
Expand Down Expand Up @@ -31,7 +47,7 @@ fi

if [ "$1" == "activate" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_installed)" == "true" ]; then
# activate the license
bash "$(dirname "$0")/wppus-api.sh" activate
bash "$(dirname "$0")/wppus-api.sh" activate_license
echo "Activated"
# halt the script
exit 0
Expand All @@ -45,7 +61,7 @@ fi

if [ "$1" == "deactivate" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_installed)" == "true" ]; then
# activate the license
bash "$(dirname "$0")/wppus-api.sh" deactivate
bash "$(dirname "$0")/wppus-api.sh" deactivate_license
echo "Deactivated"
# halt the script
exit 0
Expand All @@ -59,7 +75,26 @@ fi

if [ "$1" == "get_update_info" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_installed)" == "true" ]; then
# get the update information
bash "$(dirname "$0")/wppus-api.sh" get_update_info
info=$(bash "$(dirname "$0")/wppus-api.sh" get_update_info)
version=$(bash "$(dirname "$0")/wppus-api.sh" get_version)
new_version=$(echo -n "$info" | jq -r '.version')

echo ""
# get the current version
echo "current $version vs. remote $new_version"

if [ "$(printf '%s\n' "$new_version" "$version" | sort -V | tail -n1)" != "$version" ]; then
echo ""
echo "---------"
echo ""
echo "Update available !!! Run the \"update\" command!"
fi
echo ""
echo "---------"
echo ""
# pretty print the response
echo "$info" | jq
echo ""
# halt the script
exit 0
elif [ "$1" == "get_update_info" ]; then
Expand Down Expand Up @@ -94,6 +129,5 @@ echo " activate - activate the license"
echo " deactivate - deactivate the license"
echo " get_update_info - output information about the remote package update"
echo " update - update the package if available"
echo "Installing the package will register a cronjob that will check for updates every 12 hours, and will install the update if available."
# halt the script
exit 1
117 changes: 52 additions & 65 deletions integration/dummy-generic/wppus-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ script_path="$(cd "$(dirname "$0")"; pwd -P)/$package_name/$script_name"
zip_name="$package_name.zip"
# define the current version of the package from the wppus.json file
version=$(jq -r '.packageData.Version' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json")
# define license_key from the wppus.json file
license_key=$(jq -r '.licenseKey' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json")
# define license_signature from the wppus.json file
license_signature=$(jq -r '.licenseSignature' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json")
# define the domain
if [[ "$(uname)" == "Darwin" ]]; then
# macOS
Expand All @@ -36,17 +40,11 @@ fi
# debug mode - "true" will bypass cron setting
debug="false"

# source the bashrc file to initialize the environment variables
source ~/.bashrc

### INSTALLING THE PACKAGE ###

function install() {
# save the license in an environment variable
echo "export WPPUS_GENERIC_PACKAGE_LICENSE=\"$1\"" >> ~/.bashrc
# source the bashrc file to commit the changes
source ~/.bashrc
echo "Installed license $1" >&2
# add the license key to wppus.json
jq '.licenseKey = "'"$1"'"' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"

if [ "$debug" == "true" ]; then
# return early
Expand All @@ -66,18 +64,12 @@ function install() {
### UNINSTALLING THE PACKAGE ###

function uninstall() {
local license=$WPPUS_GENERIC_PACKAGE_LICENSE
# remove the license from the environment variable
# WPPUS_GENERIC_PACKAGE_LICENSE
sed -i '' '/WPPUS_GENERIC_PACKAGE_LICENSE/d' ~/.bashrc
unset WPPUS_GENERIC_PACKAGE_LICENSE
# remove the license signature from the environment variable
# WPPUS_GENERIC_PACKAGE_SIGNATURE
sed -i '' '/WPPUS_GENERIC_PACKAGE_SIGNATURE/d' ~/.bashrc
unset WPPUS_GENERIC_PACKAGE_SIGNATURE
# source the bashrc file to commit the changes
source ~/.bashrc
echo "Uninstalled license $license" >&2
local license=$license_key
# remove the license key from wppus.json
jq '.licenseKey = ""' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
# remove the license signature from wppus.json
jq '.licenseSignature = ""' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
license_signature=""

if [ "$debug" == "true" ]; then
# return early
Expand All @@ -96,7 +88,7 @@ function uninstall() {
function is_installed() {

# check if the WPPUS_GENERIC_PACKAGE_LICENSE environment variable is set
if [ -z "$WPPUS_GENERIC_PACKAGE_LICENSE" ]; then
if [ -z "$license_key" ]; then
# return false
echo "false"
# return early
Expand All @@ -111,7 +103,7 @@ function is_installed() {
fi

# check if the cron job exists
if crontab -l | grep -q "$script_path"; then
if crontab -l 2>/dev/null | grep -q "$script_path"; then
# return true
echo "true"
else
Expand All @@ -128,11 +120,12 @@ function send_api_request() {
local full_url=$(printf "%s/%s/?%s" "$url" "$1" "${*:2}")
# make the request
local response=$(curl -s "$full_url")

# return the response
echo "$response"
}

## ENOCODING AND DECODING FOR URLS ##
## ENCODING AND DECODING FOR URLS ##

function urlencode() {
local old_lc_collate=$LC_COLLATE
Expand All @@ -151,24 +144,21 @@ function urlencode() {
}

function urldecode() {
local url_encoded="${1//+/ }"
local url_encoded="${1}"
printf '%b' "${url_encoded//%/\\x}"
}

### CHECKING FOR UPDATES ###

function check_for_updates() {
# get a previously acquired license signature from an environment variable
# WPPUS_GENERIC_PACKAGE_SIGNATURE, and url encode it
local signature=$(urlencode "$WPPUS_GENERIC_PACKAGE_SIGNATURE")
# build the request url
local endpoint="wppus-update-api"
local args=(
"action=get_metadata"
"package_id=$package_name"
"installed_version=1.4.13"
"license_key=41ec1eba0f17d47f76827a33c7daab2c"
"license_signature=$signature"
"package_id=$(urlencode "$package_name")"
"installed_version=$(urlencode "$version")"
"license_key=$(urlencode "$license_key")"
"license_signature=$(urlencode "$license_signature")"
"update_type=Generic"
)
# make the request
Expand All @@ -180,47 +170,39 @@ function check_for_updates() {
### ACTIVATING A LICENSE ###

function activate_license() {
# get the license signature from an environment variable
# WPPUS_GENERIC_PACKAGE_LICENSE
local license=$WPPUS_GENERIC_PACKAGE_LICENSE
# build the request url
local endpoint="wppus-license-api"
local args=(
"action=activate"
"license_key=$license"
"allowed_domains=$domain"
"package_slug=$package_name"
"license_key=$(urlencode "$license_key")"
"allowed_domains=$(urlencode "$domain")"
"package_slug=$(urlencode "$package_name")"
)
# make the request
local response=$(send_api_request "$endpoint" "${args[@]}")
# get the license signature from the response
local license_signature=$(urldecode $(echo -n "$response" | jq -r '.license_signature'))
# save the license signature in an environment variable
# WPPUS_GENERIC_PACKAGE_SIGNATURE
echo "export WPPUS_GENERIC_PACKAGE_SIGNATURE=\"$license_signature\"" >> ~/.bashrc
source ~/.bashrc
# get the signature from the response
local signature=$(urldecode $(echo -n "$response" | jq -r '.license_signature'))
# add the license signature to wppus.json
jq '.licenseSignature = "'"$signature"'"' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
license_signature=$signature
}

### DEACTIVATING A LICENSE ###

function deactivate_license() {
# get the license signature from an environment variable
# WPPUS_GENERIC_PACKAGE_LICENSE
local license=$WPPUS_GENERIC_PACKAGE_LICENSE
# build the request url
local endpoint="wppus-license-api"
local args=(
"action=deactivate"
"license_key=$license"
"allowed_domains=$domain"
"package_slug=$package_name"
"license_key=$(urlencode "$license_key")"
"allowed_domains=$(urlencode "$domain")"
"package_slug=$(urlencode "$package_name")"
)
# make the request
local response=$(send_api_request "$endpoint" "${args[@]}")
# remove the license signature from the environment variable
# WPPUS_GENERIC_PACKAGE_SIGNATURE
sed -i '' '/WPPUS_GENERIC_PACKAGE_SIGNATURE/d' ~/.bashrc
source ~/.bashrc
# remove the license signature from wppus.json
jq '.licenseSignature = ""' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
license_signature=""
}

### DOWNLOADING THE PACKAGE ###
Expand All @@ -236,6 +218,16 @@ function download_update() {
echo $output_file
}

### MAIN SCRIPT ###

# check if the script was called with the argument "get_version"
if [ "$1" == "get_version" ]; then
# return the version
echo "$version"
# halt the script
exit 0
fi

# check if the script was called with the argument "install"
if [ "$1" == "install" ]; then
# install the package
Expand Down Expand Up @@ -293,6 +285,11 @@ if [ "$1" == "check_for_updates" ]; then
fi
done

# add the license key to wppus.json
jq '.licenseKey = "'"$1"'"' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
# add the license signature to wppus.json
jq '.licenseSignature = "'"$signature"'"' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"

# remove the directory
rm -rf /tmp/$package_name
# remove the zip
Expand Down Expand Up @@ -324,18 +321,8 @@ fi

# check if the script was called with the argument "get_update"
if [ "$1" == "get_update_info" ]; then
response=$(check_for_updates)

# get the current version
echo "current: $version"
echo "vs."
# get the version from the response
echo "remote: $(echo -n "$response" | jq -r '.version')"
echo ""
echo "---------"
echo ""
# pretty print the response
echo "$response" | jq
# get the update information
echo "$(check_for_updates)"
# halt the script
exit 0
fi
Expand Down

0 comments on commit 80eaedf

Please sign in to comment.