Text Share Online

#!/bin/zsh

# Variables
FOLDER_NAME=”CrossOverLicence”
PLIST_NAME=”com.codeweavers.CrossOver.license.plist”
BOTTLES_PATH=”$HOME/Library/Application Support/CrossOver/Bottles”
# Path where the local copy of this script will live for the background service
LOCAL_SCRIPT_PATH=”$HOME/$FOLDER_NAME/main.sh”

TOTAL_STEPS=3
STEP=0

function addStep() {
((STEP++))
echo “”
echo “33[33m$STEP/$TOTAL_STEPS $133[0m”
}

function resetSystemReg() {
local arquivo=”$1″
local data_atual=$(date +%Y-%m-%d)
local backup_arquivo=”${arquivo}.${data_atual}.bak”

# Backup the original file
cp “$arquivo” “$backup_arquivo”

# Process the file locally removing the cxoffice license section
awk ‘
BEGIN { flag = 0; }
/^[Software\\CodeWeavers\\CrossOver\\cxoffice]/ { flag = 1; }
flag && /^$/ { flag = 0; next; }
!flag
‘ “$arquivo” > “${arquivo}.tmp” && mv “${arquivo}.tmp” “$arquivo”
}

function resetBottle {
local bottlePath=”$1″
local bottleName=$(basename “$bottlePath”)

rm -f “$bottlePath”/.version
rm -f “$bottlePath”/.update-timestamp

resetSystemReg “$bottlePath/system.reg”
echo “33[32m$bottleName reseted33[0m”
}

function find_bottles() {
find “$1” -name “system.reg” -maxdepth 3 -exec dirname {} ; 2>/dev/null
}

execute_only() {
addStep “Executing renew trial”
local date=$(date +”%Y-%m-%d %H:%M:%S”)
defaults write com.codeweavers.CrossOver FirstRunDate -date “$date”
defaults write com.codeweavers.CrossOver SULastCheckTime -date “$date”
echo “33[32mTrial start date updated to $date33[0m”

addStep “Finding bottles paths…”
bottlePaths=$(find_bottles “$BOTTLES_PATH”)

if [[ -z “$bottlePaths” ]]; then
echo “No bottles found in default path. Enter custom path or press Enter to skip:”
read userBottlePath
if [[ -n “$userBottlePath” ]]; then
bottlePaths=$(find_bottles “$userBottlePath”)
fi
fi

if [[ -n “$bottlePaths” ]]; then
# Fix IFS to handle spaces in folder names
OLD_IFS=”$IFS”
IFS=$’n’
addStep “Resetting bottles install times”
for bottle in $bottlePaths; do
echo “33[32mResetting:33[0m $(basename “$bottle”)”
resetBottle “$bottle”
done
IFS=”$OLD_IFS”
else
echo “No bottles to reset.”
fi
}

install() {
TOTAL_STEPS=5
addStep “Preparing local directory…”
mkdir -p “$HOME/$FOLDER_NAME”

# Copy THIS script to the local folder so the service has a source
cp “$0” “$LOCAL_SCRIPT_PATH”
chmod +x “$LOCAL_SCRIPT_PATH”

addStep “Executing initial reset…”
execute_only

addStep “Installing macOS LaunchAgent…”
local plistPath=”$HOME/Library/LaunchAgents/$PLIST_NAME”

if [ -f “$plistPath” ]; then
launchctl unload “$plistPath” > /dev/null 2>&1
rm “$plistPath”
fi

# Create the local plist
cat > “$plistPath” << EOF
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE plist PUBLIC “-//Apple Computer//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>Label</key>
<string>$PLIST_NAME</string>
<key>ProgramArguments</key>
<array>
<string>$LOCAL_SCRIPT_PATH</string>
<string>execute</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>864000</integer>
</dict>
</plist>
EOF
echo “33[32mService Installation completed.33[0m”
launchctl load “$plistPath”
}

uninstall() {
addStep “Uninstalling the service…”
local plistPath=”$HOME/Library/LaunchAgents/$PLIST_NAME”
if [ -f “$plistPath” ]; then
launchctl unload “$plistPath” > /dev/null 2>&1
rm “$plistPath”
fi

addStep “Removing script directory…”
rm -rf “$HOME/$FOLDER_NAME”
echo “33[32mUninstallation completed.33[0m”
}

# Logic for arguments
case “$1” in
“execute”)
execute_only
;;
“install”)
install
;;
“uninstall”)
uninstall
;;
*)
echo “Usage: $0 {execute|install|uninstall}”
echo “Choosing ‘install’ by default in 3 seconds…”
sleep 3
install
;;
esac

Share This: