<commit_before>#!/usr/bin/env bash

# Post installation script of KA-Lite to be used in Packages.

# Notes: 
# 1. This script must be run as root.
# 2. We use `/Users/Shared/ka-lite/` as the installation location which contains the `assessment.zip`, `pyrun`, and `scripts`.

# Steps
# 1. Symlink kalite executable to /usr/local/bin.
# 2. Set KALITE_PYTHON environment variable to the Pyrun executable.
# 3. Create plist in /Library/LaunchAgents/ folder.
# 4. Run shebangcheck script that checks the python/pyrun interpreter to use.
# 5. Remove the old asset folder to be replaced by newer assets later.
# 6. Run kalite manage syncdb --noinput.
# 7. Run kalite manage init_content_items --overwrite.
# 8. Run kalite manage unpack_assessment_zip <assessment_path>.
# 9. Run kalite manage setup --noinput..
# 10. Change the owner of the ~/.kalite/ folder and .plist file to current user.
# 11. Set the KALITE_PYTHON env var for the user doing the install so we don't need to restart after installation.
# 12. Create a copy of ka-lite-remover.sh and name it as KA-Lite_Uninstall.tool.


#----------------------------------------------------------------------
# Global Variables
#----------------------------------------------------------------------
SCRIPTPATH=$( cd $(dirname $0) ; pwd -P )

KALITE_SHARED="/Users/Shared/ka-lite"
KALITE_DIR="$HOME/.kalite"
KALITE_UNINSTALL_SCRIPT="KA-Lite_Uninstall.tool"
PYRUN_NAME="pyrun-2.7"
PYRUN_DIR="$KALITE_SHARED/$PYRUN_NAME"
PYRUN="$PYRUN_DIR/bin/pyrun"
PYRUN_PIP="$PYRUN_DIR/bin/pip"
BIN_PATH="$PYRUN_DIR/bin"
ASSESSMENT_SRC="$KALITE_SHARED/assessment/assessment.zip"
SCRIPT_PATH="$KALITE_SHARED/scripts/"
APPLICATION_PATH="/Applications/KA-Lite"
PRE_INSTALL_SCRIPT="$SCRIPT_PATH/ka-lite-remover.sh"

SYMLINK_FILE="$KALITE_SHARED/pyrun-2.7/bin/kalite"
SYMLINK_TO="/usr/local/bin"
COMMAND_SYMLINK="ln -sf $SYMLINK_FILE $SYMLINK_TO"

ORG="org.learningequality.kalite"
LAUNCH_AGENTS="/Library/LaunchAgents/"
KALITE=$(which kalite)
PLIST_SRC="$LAUNCH_AGENTS$ORG.plist"


#----------------------------------------------------------------------
# Functions
#----------------------------------------------------------------------
function update_env {
    # MUST: Make sure we have a KALITE_PYTHON env var that points to Pyrun
    msg "Setting KALITE_PYTHON environment variable to $PYRUN..."
    launchctl unsetenv KALITE_PYTHON
    launchctl setenv KALITE_PYTHON "$PYRUN"
    export KALITE_PYTHON="$PYRUN"
    if [ $? -ne 0 ]; then
        msg ".. Abort!  Error/s encountered exporting KALITE_PYTHON '$PYRUN'."
        exit 1
    fi
}


function create_plist {

    if [ -f "$PLIST_SRC" ]; then
        msg ".. Now removing '$PLIST_SRC'..."
        rm -fr $PLIST_SRC
        if [ $? -ne 0 ]; then
            msg ".. Abort!  Error/s encountered removing '$PLIST_SRC'."
            exit 1
        fi
    fi

    # Create Plist 
    msg "Now creating '$PLIST_SRC'..."
    echo "<?xml version='1.0' encoding='UTF-8'?>" >> $PLIST_SRC
    echo "<!DOCTYPE plist PUBLIC '-//Apple//DTD PLIST 1.0//EN' 'http://www.apple.com/DTDs/PropertyList-1.0.dtd'>" >> $PLIST_SRC
    echo "<plist version='1.0'>" >> $PLIST_SRC
    echo "<dict>" >> $PLIST_SRC
    echo -e "\t<key>Label</key>" >> $PLIST_SRC
    echo -e "\t<string>org.learningequality.kalite</string>" >> $PLIST_SRC
    echo -e "\t<key>ProgramArguments</key>" >> $PLIST_SRC
    echo -e "\t<array>" >> $PLIST_SRC
    echo -e "\t\t<string>sh</string>" >> $PLIST_SRC
    echo -e "\t\t<string>-c</string>" >> $PLIST_SRC
    echo -e "\t\t<string>launchctl setenv KALITE_PYTHON \"$PYRUN\"</string>" >> $PLIST_SRC
    echo -e "\t</array>" >> $PLIST_SRC
    echo -e "\t<key>RunAtLoad</key>" >> $PLIST_SRC
    echo -e "\t<true/>" >> $PLIST_SRC
    echo "</dict>" >> $PLIST_SRC
    echo "</plist>" >> $PLIST_SRC

    if [ -f "$PLIST_SRC" ]; then
        msg ".. $PLIST_SRC created successfully"
    else
        if [ $? -ne 0 ]; then
            msg ".. Abort!  Error/s encountered creating '$PLIST_SRC'."
            exit 1
        fi
    fi
}

# Print message in terminal and log for the Console application.
function msg() {
    echo "$1"
    syslog -s -l alert "KA-Lite: $1"
}


#----------------------------------------------------------------------
# Script
#----------------------------------------------------------------------

STEP=1
STEPS=11

msg "Post-installation: Preparing KA-Lite dependencies..."

ENV=$(env)
msg ".. Packages post-installation env:'\n'$ENV" 


msg "$STEP/$STEPS. Symlink kalite executable to $SYMLINK_TO..."
if [ ! -d "$SYMLINK_TO" ]; then
    msg ".. Now creating '$SYMLINK_TO'..."
    sudo mkdir -p $SYMLINK_TO
    if [ $? -ne 0 ]; then<commit_msg>Correct the number of steps and make the variables easily visible by moving it below the detailed steps comments.<commit_after>#!/usr/bin/env bash

# Post installation script of KA-Lite to be used in Packages.

# Notes: 
# 1. This script must be run as root.
# 2. We use `/Users/Shared/ka-lite/` as the installation location which contains the `assessment.zip`, `pyrun`, and `scripts`.

# Steps
# 1. Symlink kalite executable to /usr/local/bin.
# 2. Set KALITE_PYTHON environment variable to the Pyrun executable.
# 3. Create plist in /Library/LaunchAgents/ folder.
# 4. Run shebangcheck script that checks the python/pyrun interpreter to use.
# 5. Remove the old asset folder to be replaced by newer assets later.
# 6. Run kalite manage syncdb --noinput.
# 7. Run kalite manage init_content_items --overwrite.
# 8. Run kalite manage unpack_assessment_zip <assessment_path>.
# 9. Run kalite manage setup --noinput..
# 10. Change the owner of the ~/.kalite/ folder and .plist file to current user.
# 11. Set the KALITE_PYTHON env var for the user doing the install so we don't need to restart after installation.
# 12. Create a copy of ka-lite-remover.sh and name it as KA-Lite_Uninstall.tool.


#----------------------------------------------------------------------
# Global Variables
#----------------------------------------------------------------------
SCRIPTPATH=$( cd $(dirname $0) ; pwd -P )

STEP=1
STEPS=12

KALITE_SHARED="/Users/Shared/ka-lite"
KALITE_DIR="$HOME/.kalite"
KALITE_UNINSTALL_SCRIPT="KA-Lite_Uninstall.tool"
PYRUN_NAME="pyrun-2.7"
PYRUN_DIR="$KALITE_SHARED/$PYRUN_NAME"
PYRUN="$PYRUN_DIR/bin/pyrun"
PYRUN_PIP="$PYRUN_DIR/bin/pip"
BIN_PATH="$PYRUN_DIR/bin"
ASSESSMENT_SRC="$KALITE_SHARED/assessment/assessment.zip"
SCRIPT_PATH="$KALITE_SHARED/scripts/"
APPLICATION_PATH="/Applications/KA-Lite"
PRE_INSTALL_SCRIPT="$SCRIPT_PATH/ka-lite-remover.sh"

SYMLINK_FILE="$KALITE_SHARED/pyrun-2.7/bin/kalite"
SYMLINK_TO="/usr/local/bin"
COMMAND_SYMLINK="ln -sf $SYMLINK_FILE $SYMLINK_TO"

ORG="org.learningequality.kalite"
LAUNCH_AGENTS="/Library/LaunchAgents/"
KALITE=$(which kalite)
PLIST_SRC="$LAUNCH_AGENTS$ORG.plist"


#----------------------------------------------------------------------
# Functions
#----------------------------------------------------------------------
function update_env {
    # MUST: Make sure we have a KALITE_PYTHON env var that points to Pyrun
    msg "Setting KALITE_PYTHON environment variable to $PYRUN..."
    launchctl unsetenv KALITE_PYTHON
    launchctl setenv KALITE_PYTHON "$PYRUN"
    export KALITE_PYTHON="$PYRUN"
    if [ $? -ne 0 ]; then
        msg ".. Abort!  Error/s encountered exporting KALITE_PYTHON '$PYRUN'."
        exit 1
    fi
}


function create_plist {

    if [ -f "$PLIST_SRC" ]; then
        msg ".. Now removing '$PLIST_SRC'..."
        rm -fr $PLIST_SRC
        if [ $? -ne 0 ]; then
            msg ".. Abort!  Error/s encountered removing '$PLIST_SRC'."
            exit 1
        fi
    fi

    # Create Plist 
    msg "Now creating '$PLIST_SRC'..."
    echo "<?xml version='1.0' encoding='UTF-8'?>" >> $PLIST_SRC
    echo "<!DOCTYPE plist PUBLIC '-//Apple//DTD PLIST 1.0//EN' 'http://www.apple.com/DTDs/PropertyList-1.0.dtd'>" >> $PLIST_SRC
    echo "<plist version='1.0'>" >> $PLIST_SRC
    echo "<dict>" >> $PLIST_SRC
    echo -e "\t<key>Label</key>" >> $PLIST_SRC
    echo -e "\t<string>org.learningequality.kalite</string>" >> $PLIST_SRC
    echo -e "\t<key>ProgramArguments</key>" >> $PLIST_SRC
    echo -e "\t<array>" >> $PLIST_SRC
    echo -e "\t\t<string>sh</string>" >> $PLIST_SRC
    echo -e "\t\t<string>-c</string>" >> $PLIST_SRC
    echo -e "\t\t<string>launchctl setenv KALITE_PYTHON \"$PYRUN\"</string>" >> $PLIST_SRC
    echo -e "\t</array>" >> $PLIST_SRC
    echo -e "\t<key>RunAtLoad</key>" >> $PLIST_SRC
    echo -e "\t<true/>" >> $PLIST_SRC
    echo "</dict>" >> $PLIST_SRC
    echo "</plist>" >> $PLIST_SRC

    if [ -f "$PLIST_SRC" ]; then
        msg ".. $PLIST_SRC created successfully"
    else
        if [ $? -ne 0 ]; then
            msg ".. Abort!  Error/s encountered creating '$PLIST_SRC'."
            exit 1
        fi
    fi
}

# Print message in terminal and log for the Console application.
function msg() {
    echo "$1"
    syslog -s -l alert "KA-Lite: $1"
}


#----------------------------------------------------------------------
# Script
#----------------------------------------------------------------------

msg "Post-installation: Preparing KA-Lite dependencies..."

ENV=$(env)
msg ".. Packages post-installation env:'\n'$ENV" 


msg "$STEP/$STEPS. Symlink kalite executable to $SYMLINK_TO..."
if [ ! -d "$SYMLINK_TO" ]; then
    msg ".. Now creating '$SYMLINK_TO'..."
    sudo mkdir -p $SYMLINK_TO
    if [ $? -ne 0 ]; then
{'fraction_non_alphanumeric': 0.22150148872820077, 'fraction_numerical': 0.009145044661846022, 'mean_word_length': 2.2907627711686493, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 2, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3274064', 'n_tokens_mistral': 3288, 'n_tokens_neox': 3156, 'n_words': 837}