<commit_before>#!/bin/bash
#
# Notes: 
#   * This script must be run as root.  If ran as a standard user, it will prompt for the admin password.
#   * The files that will be removed, will be displayed on the console log.
#   * The $SCRIPT_NAME env variables was specified by the `Packages`.
#   * This is re-used as /Applications/KA-Lite/KA_Lite_Uninstall.tool during installation.
#   * This script support uninstallation without user confirmation.
#   * The script args are use in the application which auto confirms the removing of `.kalite` and uninstalling the .app. 
#
# What does this script do?
#   1. Unset the environment variables: KALITE_PYTHON and KALITE_HOME.
#   2. Remove the .plist file, kalite executable and the ka-lite resources.
#   3. When run stand-alone, it confirms removal of the KA-Lite data directory as specified by the KALITE_HOME env var.
#   4. Display a console log for this process.
#
# Some References:
#   * http://stackoverflow.com/a/2264537/845481 - Converting string to lower case in Bash shell scripting
#


#----------------------------------------------------------------------
# Global Variables
#----------------------------------------------------------------------
IS_PREINSTALL=false
if [ "$SCRIPT_NAME" == "preinstall" ]; then
    IS_PREINSTALL=true
fi
echo "IS_PREINSTALL == $IS_PREINSTALL"

KALITE_MONITOR="/Applications/KA-Lite-Monitor.app"
KALITE="kalite"
KALITE_PLIST="org.learningequality.kalite.plist"
HOME_LAUNCH_AGENTS="$HOME/Library/LaunchAgents"
ROOT_LAUNCH_AGENTS="/Library/LaunchAgents"
KALITE_EXECUTABLE_PATH="$(which $KALITE)"
KALITE_RESOURCES="/Users/Shared/ka-lite"
KALITE_USR_BIN_PATH="/usr/bin"
KALITE_USR_LOCAL_BIN_PATH="/usr/local/bin"
KALITE_UNINSTALL_SCRIPT="KA-Lite_Uninstall.tool"

REMOVE_FILES_ARRAY=()

#----------------------------------------------------------------------
# Functions
#----------------------------------------------------------------------
# @param [Integer] $1 exit code.
function key_exit() {
    echo "Press any key to exit."
    read
    exit $1
}


function append() {
    eval $1[\${#$1[*]}]=$2
}


# This function will check if the files or folders exists.
function check_files() {
    uninstall_count=0
    for file in "${REMOVE_FILES_ARRAY[@]}"; do
        if [ -e "${file}" ]; then
            ((uninstall_count++))
        fi
    done
    if [ $uninstall_count -eq 0 ]; then
        echo
        echo "Cannot find any KA-Lite files or folders, nothing to uninstall here."
        return 1
    fi
    return 0
}


# This function will remove the files or folders.
function remove_files_initiator {

    uninstall_count=0
    for file in "${REMOVE_FILES_ARRAY[@]}"; do
        if [ -e "${file}" ]; then
            echo "Will remove: ${file}"
            syslog -s -l error "Will remove: ${file}"
            ((uninstall_count++))
        fi
    done
    if [ $uninstall_count -eq 0 ]; then
        echo
        echo "Cannot find any KA-Lite files or folders, nothing to uninstall here."
        exit
    fi

    # Collect the directories and files to remove
    if [ $IS_PREINSTALL == false ]; then
        # This script is not run by the Packages module.
        # Use AppleScript so we can use a graphical `sudo` prompt.
        # This way, people can enter the username they wish to use
        # for sudo, and it is more Apple-like.
        osascript -e "do shell script \"/bin/rm -Rf ${REMOVE_FILES_ARRAY[*]}\" with administrator privileges"
    else
        # This script is being run by the Packages module.
        sudo rm -Rf ${REMOVE_FILES_ARRAY[*]}
    fi
    # If process did not succeed, let's return non-zero.
    if [ $? -ne 0 ]; then
        exit 1
    fi
}


#----------------------------------------------------------------------
# Script
#----------------------------------------------------------------------
# ENV=$(env)
# syslog -s -l error "Packages pre-installation initialize with env:'\n'$ENV" 

pushd `dirname $0` > /dev/null
SCRIPTPATH=`pwd`
popd > /dev/null

# Collect the directories and files to remove
test -d /Applications/KA-Lite                   && REMOVE_FILES_ARRAY+=("/Applications/KA-Lite")
test -f $SCRIPTPATH/$KALITE_UNINSTALL_SCRIPT    && REMOVE_FILES_ARRAY+=("$SCRIPTPATH/$KALITE_UNINSTALL_SCRIPT")
test -d $KALITE_RESOURCES                       && REMOVE_FILES_ARRAY+=("$KALITE_RESOURCES")
test -f $KALITE_USR_LOCAL_BIN_PATH/$KALITE      && REMOVE_FILES_ARRAY+=("$KALITE_USR_LOCAL_BIN_PATH/$KALITE")
test -f $KALITE_USR_BIN_PATH/$KALITE            && REMOVE_FILES_ARRAY+=("$KALITE_USR_BIN_PATH/$KALITE")
test -d $KALITE_MONITOR                         && REMOVE_FILES_ARRAY+=("$KALITE_MONITOR")

if [ $IS_PREINSTALL == false ]; then
    # Introduction 
    echo "                                                          "
    echo "   _   __  ___    _     _ _                               "
    echo "  | | / / / _ \  | |   (_) |                              "
    echo "  | |/ / / /_\ \ | |    _| |_ ___                         "
    echo "  |    \ |  _  | | |   | | __/ _ \                        "
    echo "  | |\  \| | | | | |___| | ||  __/                        "
    echo "  \_| \_/\_| |_/ \_____/_|\__\___| Uninstall              "
    echo "                                                          "
    echo "https://learningequality.org/ka-lite/                     "
    echo "                                                          "
    echo "     version 0.16.x                                       "
    echo "                                                          "
fi

for root_plist in $ROOT_LAUNCH_AGENTS/org.learningequality.*.plist; do
    if [ -e $root_plist ]; then
        append REMOVE_FILES_ARRAY $root_plist
    fi
done

for home_plist in $HOME_LAUNCH_AGENTS/org.learningequality.*.plist; do
    if [ -e $home_plist ]; then
        append REMOVE_FILES_ARRAY $home_plist
    fi
done

if [ $IS_PREINSTALL == false ]; then
    # Check that KALITE_HOME env var exists, if not, assign it a default value.
    if [ -z ${KALITE_HOME+0} ]; then 
      KALITE_HOME="$HOME/.kalite/"
    fi

    # Check if the directory exists before confirming to include it on the list.
    if [ -d "$KALITE_HOME" ]; then
        echo "The KALITE_HOME environment variable points to $KALITE_HOME."
        echo "This is the directory where the data files are located."
        echo "Answer no if you want to keep your KA-Lite data files."
        echo
        echo -n "Do you want the $KALITE_HOME directory to be deleted? (Yes/No) "
        # Check if the second input argument in the script was passed.
        if [ "$2" != "" ]; then
            remove_kalite="yes"
            syslog -s -l error "Auto confirm since $2 has value."
        else
            read remove_kalite
        fi
        # convert answer to lowercase
        remove_kalite="$(echo $remove_kalite | tr '[:upper:]' '[:lower:]')"
        if [ "$remove_kalite" == "yes" ]; then
            append REMOVE_FILES_ARRAY "$KALITE_HOME"<commit_msg>check if we don't wanna delete the $KALITE_HOME<commit_after>#!/bin/bash
#
# Notes: 
#   * This script must be run as root.  If ran as a standard user, it will prompt for the admin password.
#   * The files that will be removed, will be displayed on the console log.
#   * The $SCRIPT_NAME env variables was specified by the `Packages`.
#   * This is re-used as /Applications/KA-Lite/KA_Lite_Uninstall.tool during installation.
#   * This script support uninstallation without user confirmation.
#   * The script args are use in the application which auto confirms the removing of `.kalite` and uninstalling the .app. 
#
# What does this script do?
#   1. Unset the environment variables: KALITE_PYTHON and KALITE_HOME.
#   2. Remove the .plist file, kalite executable and the ka-lite resources.
#   3. When run stand-alone, it confirms removal of the KA-Lite data directory as specified by the KALITE_HOME env var.
#   4. Display a console log for this process.
#
# Some References:
#   * http://stackoverflow.com/a/2264537/845481 - Converting string to lower case in Bash shell scripting
#


#----------------------------------------------------------------------
# Global Variables
#----------------------------------------------------------------------
IS_PREINSTALL=false
if [ "$SCRIPT_NAME" == "preinstall" ]; then
    IS_PREINSTALL=true
fi
echo "IS_PREINSTALL == $IS_PREINSTALL"

KALITE_MONITOR="/Applications/KA-Lite-Monitor.app"
KALITE="kalite"
KALITE_PLIST="org.learningequality.kalite.plist"
HOME_LAUNCH_AGENTS="$HOME/Library/LaunchAgents"
ROOT_LAUNCH_AGENTS="/Library/LaunchAgents"
KALITE_EXECUTABLE_PATH="$(which $KALITE)"
KALITE_RESOURCES="/Users/Shared/ka-lite"
KALITE_USR_BIN_PATH="/usr/bin"
KALITE_USR_LOCAL_BIN_PATH="/usr/local/bin"
KALITE_UNINSTALL_SCRIPT="KA-Lite_Uninstall.tool"

REMOVE_FILES_ARRAY=()

#----------------------------------------------------------------------
# Functions
#----------------------------------------------------------------------
# @param [Integer] $1 exit code.
function key_exit() {
    echo "Press any key to exit."
    read
    exit $1
}


function append() {
    eval $1[\${#$1[*]}]=$2
}


# This function will check if the files or folders exists.
function check_files() {
    uninstall_count=0
    for file in "${REMOVE_FILES_ARRAY[@]}"; do
        if [ -e "${file}" ]; then
            ((uninstall_count++))
        fi
    done
    if [ $uninstall_count -eq 0 ]; then
        echo
        echo "Cannot find any KA-Lite files or folders, nothing to uninstall here."
        return 1
    fi
    return 0
}


# This function will remove the files or folders.
function remove_files_initiator {

    uninstall_count=0
    for file in "${REMOVE_FILES_ARRAY[@]}"; do
        if [ -e "${file}" ]; then
            echo "Will remove: ${file}"
            syslog -s -l error "Will remove: ${file}"
            ((uninstall_count++))
        fi
    done
    if [ $uninstall_count -eq 0 ]; then
        echo
        echo "Cannot find any KA-Lite files or folders, nothing to uninstall here."
        exit
    fi

    # Collect the directories and files to remove
    if [ $IS_PREINSTALL == false ]; then
        # This script is not run by the Packages module.
        # Use AppleScript so we can use a graphical `sudo` prompt.
        # This way, people can enter the username they wish to use
        # for sudo, and it is more Apple-like.
        osascript -e "do shell script \"/bin/rm -Rf ${REMOVE_FILES_ARRAY[*]}\" with administrator privileges"
    else
        # This script is being run by the Packages module.
        sudo rm -Rf ${REMOVE_FILES_ARRAY[*]}
    fi
    # If process did not succeed, let's return non-zero.
    if [ $? -ne 0 ]; then
        exit 1
    fi
}


#----------------------------------------------------------------------
# Script
#----------------------------------------------------------------------
# ENV=$(env)
# syslog -s -l error "Packages pre-installation initialize with env:'\n'$ENV" 

pushd `dirname $0` > /dev/null
SCRIPTPATH=`pwd`
popd > /dev/null

# Collect the directories and files to remove
test -d /Applications/KA-Lite                   && REMOVE_FILES_ARRAY+=("/Applications/KA-Lite")
test -f $SCRIPTPATH/$KALITE_UNINSTALL_SCRIPT    && REMOVE_FILES_ARRAY+=("$SCRIPTPATH/$KALITE_UNINSTALL_SCRIPT")
test -d $KALITE_RESOURCES                       && REMOVE_FILES_ARRAY+=("$KALITE_RESOURCES")
test -f $KALITE_USR_LOCAL_BIN_PATH/$KALITE      && REMOVE_FILES_ARRAY+=("$KALITE_USR_LOCAL_BIN_PATH/$KALITE")
test -f $KALITE_USR_BIN_PATH/$KALITE            && REMOVE_FILES_ARRAY+=("$KALITE_USR_BIN_PATH/$KALITE")
test -d $KALITE_MONITOR                         && REMOVE_FILES_ARRAY+=("$KALITE_MONITOR")

if [ $IS_PREINSTALL == false ]; then
    # Introduction 
    echo "                                                          "
    echo "   _   __  ___    _     _ _                               "
    echo "  | | / / / _ \  | |   (_) |                              "
    echo "  | |/ / / /_\ \ | |    _| |_ ___                         "
    echo "  |    \ |  _  | | |   | | __/ _ \                        "
    echo "  | |\  \| | | | | |___| | ||  __/                        "
    echo "  \_| \_/\_| |_/ \_____/_|\__\___| Uninstall              "
    echo "                                                          "
    echo "https://learningequality.org/ka-lite/                     "
    echo "                                                          "
    echo "     version 0.16.x                                       "
    echo "                                                          "
fi

for root_plist in $ROOT_LAUNCH_AGENTS/org.learningequality.*.plist; do
    if [ -e $root_plist ]; then
        append REMOVE_FILES_ARRAY $root_plist
    fi
done

for home_plist in $HOME_LAUNCH_AGENTS/org.learningequality.*.plist; do
    if [ -e $home_plist ]; then
        append REMOVE_FILES_ARRAY $home_plist
    fi
done

if [ $IS_PREINSTALL == false ]; then
    # Check that KALITE_HOME env var exists, if not, assign it a default value.
    if [ -z ${KALITE_HOME+0} ]; then 
      KALITE_HOME="$HOME/.kalite/"
    fi

    # Check if the directory exists before confirming to include it on the list.
    if [ -d "$KALITE_HOME" ]; then
        echo "The KALITE_HOME environment variable points to $KALITE_HOME."
        echo "This is the directory where the data files are located."
        echo "Answer no if you want to keep your KA-Lite data files."
        echo
        echo -n "Do you want the $KALITE_HOME directory to be deleted? (Yes/No) "
        # Check if the second input argument in the script was passed.
        if [ "$2" == "yes" ]; then
            remove_kalite="yes"
            syslog -s -l error "Auto confirm since $2 has value."
        elif [ "$2" == "no" ]; then
            remove_kalite="no"
            syslog -s -l error "NOT Removing $KALITE_HOME directory."
        else
            read remove_kalite
        fi
        # convert answer to lowercase
        remove_kalite="$(echo $remove_kalite | tr '[:upper:]' '[:lower:]')"
        if [ "$remove_kalite" == "yes" ]; then
            append REMOVE_FILES_ARRAY "$KALITE_HOME"
{'fraction_non_alphanumeric': 0.15930644019815995, 'fraction_numerical': 0.005307855626326964, 'mean_word_length': 1.7702411291903548, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5683414', 'n_tokens_mistral': 4364, 'n_tokens_neox': 4114, 'n_words': 1233}