<commit_before>#!/bin/bash
echo "=================================================================="
echo "$0 version 4.10 by <NAME>"

# --------------------------------------------------------------------
# Requires /usr/local/bin/gdrive executable compiled from github source for arm
# Note gdrive is included with pi-timolo on github at https://github.com/pageauc/pi-timolo
# for gdrive details see https://github.com/odeke-em/drive/releases
# To manually install gdrive binary perform the following.
# cd ~
# wget -O gdrive -q --show-progress https://raw.github.com/pageauc/pi-timolo/master/source/gdrive_armv6
# chmod +x gdrive
# sudo cp gdrive /usr/local/bin/
# rm gdrive
# cd ~
# gdrive version
# Follow instructions for initializing gdrive for pi-timolo see pi-timolo wiki
# gdrive init
#
# Edit the sync.sh variables to enable and customize features
# For more details see my github wiki pages here
# https://github.com/pageauc/pi-timolo/wiki/How-to-Setup-pi-timolo-gdrive-sync
# https://github.com/pageauc/pi-timolo/wiki/sync.sh---Automate-gdrive-Uploads-and-More
# https://github.com/pageauc/pi-timolo/wiki/How-to-Setup-config.py-Remote-Configuration
# Add appropriate line to crontab using command sudo crontab -e
# example crontab entry below without # comment char to run every 5 minutes
# */5 * * * * /home/pi/pi-timolo/sync.sh >/dev/nul
# --------------------------------------------------------------------

PROG_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # folder location of this script
cd $PROG_DIR

# ---------------  Local to Remote File Sync Settings --------------
SYNC_ON=true                 # true - Do Remote Sync  false - Non
SYNC_DIR='media/motion'            # relative to this script folder - location of files to sync
FILES_TO_SYNC='*jpg'         # Set the type of files to sync * for all
CHECK_FOR_SYNC_FILE=true     # true if sync file below is required otherwise set to false always sync
SYNC_FILE_PATH=$PROG_DIR/pi-timolo.sync  # name of pi-timolo sync lock filename

# ------------------ Remote Sync Config Settings -------------------
# Remote configuration will copy a new configuration file from remote google drive 
# to local pi-timolo config.py file and restart pi-timolo.
REMOTE_CONFIG_ON=false                 # true - Check for remote config file and install, false - Ignore Checking
REMOTE_CONFIG_DIR='sync_config_cam1'   # Remote Config Folder on google drive (Will be Created if Does Not Exist)
REMOTE_CONFIG_FILE='config.py'         # Name of new pi-timolo config file on google drive
LOCAL_CONFIG_FILE='config.py'          # pi-timolo configuration variables file (default)

# -------------------- Watch App Settings --------------------------
WATCH_APP_ON=false           # false - off  true - Check if app is running and restart or reboot
WATCH_APP='pi-timolo.py'     # App filename to monitor for Run Status
FORCE_REBOOT=false           # false - Restart pi-timolo.py if not running    true - Reboot if not running (Use with Caution)  

echo "==================== Variable Settings ==========================="
echo "SYNC_ON             =" $SYNC_ON
echo "SYNC_DIR            =" $SYNC_DIR
echo "FILES_TO_SYNC       =" $FILES_TO_SYNC
echo "CHECK_FOR_SYNC_FILE =" $CHECK_FOR_SYNC_FILE
echo "SYNC_FILE_PATH      =" $SYNC_FILE_PATH
echo ""
echo "REMOTE_CONFIG_ON    =" $REMOTE_CONFIG_ON
echo "REMOTE_CONFIG_DIR   =" $REMOTE_CONFIG_DIR
echo "REMOTE_CONFIG_FILE  =" $REMOTE_CONFIG_FILE
echo "LOCAL_CONFIG_FILE   =" $LOCAL_CONFIG_FILE
echo ""
echo "WATCH_APP_ON        =" $WATCH_APP_ON
echo "WATCH_APP           =" $WATCH_APP
echo "FORCE_REBOOT        =" $FORCE_REBOOT
echo "=================================================================="
echo "STATUS  - Current pi-timolo working dir is $PROG_DIR"
echo "START   - Processing"

# ------------------------------------------------------
function restart_pi_timolo ()
{
    echo "------------------------------------------"
    echo "START   - restart_pi_timolo"
    echo "INFO    - Starting pi-timolo"
    $PROG_DIR/pi-timolo.sh start
    sleep 5
    if [ -z "$(pgrep -f pi-timolo.py)" ] ; then
        # pi-timolo did not start
        echo "WARN    - Failed to Restart pi-timolo"
    else
        echo "INFO    - Successfully restarted pi-timolo"
    fi
}

# ------------------------------------------------------
function do_gdrive_sync ()
{
    # function to perform a gdrive sync between RPI local folder (exit if not found) and
    # a matching google drive folder. (Create remote folder if required)
    # Check if Local SYNC_DIR folder exists
    if [ ! -d "$PROG_DIR/$SYNC_DIR" ] ; then
        echo "ERROR   - Local Folder $PROG_DIR/$SYNC_DIR Does Not Exist"
        echo "          Please Check SYNC_DIR variable and/or Local Folder PATH"
        exit 1
    fi

    # Check for matching files to sync in folder
    ls -1 $PROG_DIR/$SYNC_DIR/$FILES_TO_SYNC > /dev/null 2>&1
    if [ ! "$?" = "0" ] ; then
        echo "WARNING - No Matching $FILES_TO_SYNC Files Found in $PROG_DIR/$SYNC_DIR"
        echo "STATUS  - Continue in 10 seconds. Please Wait...."
        sleep 10
    fi

    # Check if a matching remote folder exists
    # and if Not then create one
    echo "STATUS  - Check if Remote Folder /$SYNC_DIR Exists"
    echo "------------------------------------------"
    /usr/local/bin/gdrive file-id $SYNC_DIR
    if [ ! $? -eq 0 ] ; then
        echo "------------------------------------------"
        echo "STATUS  - Creating Remote Folder /$SYNC_DIR"
        echo "------------------------------------------"
        /usr/local/bin/gdrive new --folder $SYNC_DIR
        /usr/local/bin/gdrive file-id $SYNC_DIR
        if [ $? -eq 0 ] ; then
            echo "------------------------------------------"
            echo "STATUS  - Successfully Created Remote Folder /$SYNC_DIR"
        else
            echo "------------------------------------------"
            echo "ERROR   - Problem Creating Remote Folder $SYNC_DIR"
            echo "          Please Investigate Problem"
            exit 1
        fi
    fi
    echo "------------------------------------------"
    echo "STATUS  - Start gdrive Sync ...."
    echo "STATUS  - Local Source Folder - $PROG_DIR/$SYNC_DIR"
    echo "STATUS  - Remote Destn Folder - /$SYNC_DIR"
    echo "STATUS  - Files $FILES_TO_SYNC"
    echo "STATUS  - Running  This May Take Some Time ....."
    echo "STATUS  - sudo /usr/local/bin/gdrive push -no-prompt -ignore-conflict $SYNC_DIR/$FILES_TO_SYNC"
    echo "------------------------------------------"
    date
    START=$(date +%s)
    sudo /usr/local/bin/gdrive push -no-prompt -ignore-conflict $SYNC_DIR/$FILES_TO_SYNC
    if [ $? -ne 0 ] ;  then
        # Check if gdrive sync was successfully
        date
        echo "------------------------------------------"
        echo "ERROR   - gdrive Sync Failed."
        echo "          Possible Cause - See gdrive Error Message Above"
        echo "          Please Investigate Problem and Try Again"
    else
        # If successful then display processing time and remove sync file
        date
        END=$(date +%s)
        DIFF=$((END - START))
        echo "------------------------------------------"
        echo "STATUS  - $0 Completed Successfully"
        echo "STATUS  - Processing Took $DIFF seconds"<commit_msg>Release 4.20 Replace exits with return to continue other functions<commit_after>#!/bin/bash
echo "=================================================================="
echo "$0 version 4.20 by <NAME>"

# --------------------------------------------------------------------
# Requires /usr/local/bin/gdrive executable compiled from github source for arm
# Note gdrive is included with pi-timolo on github at https://github.com/pageauc/pi-timolo
# for gdrive details see https://github.com/odeke-em/drive/releases
# To manually install gdrive binary perform the following.
# cd ~
# wget -O gdrive -q --show-progress https://raw.github.com/pageauc/pi-timolo/master/source/gdrive_armv6
# chmod +x gdrive
# sudo cp gdrive /usr/local/bin/
# rm gdrive
# cd ~
# gdrive version
# Follow instructions for initializing gdrive for pi-timolo see pi-timolo wiki
# gdrive init
#
# Edit the sync.sh variables to enable and customize features
# For more details see my github wiki pages here
# https://github.com/pageauc/pi-timolo/wiki/How-to-Setup-pi-timolo-gdrive-sync
# https://github.com/pageauc/pi-timolo/wiki/sync.sh---Automate-gdrive-Uploads-and-More
# https://github.com/pageauc/pi-timolo/wiki/How-to-Setup-config.py-Remote-Configuration
# Add appropriate line to crontab using command sudo crontab -e
# example crontab entry below without # comment char to run every 5 minutes
# */5 * * * * /home/pi/pi-timolo/sync.sh >/dev/nul
# --------------------------------------------------------------------

PROG_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # folder location of this script
cd $PROG_DIR

# ---------------  Local to Remote File Sync Settings --------------
SYNC_ON=true                 # true - Do Remote Sync  false - Non
SYNC_DIR='media/motion'            # relative to this script folder - location of files to sync
FILES_TO_SYNC='*jpg'         # Set the type of files to sync * for all
CHECK_FOR_SYNC_FILE=true     # true if sync file below is required otherwise set to false always sync
SYNC_FILE_PATH=$PROG_DIR/pi-timolo.sync  # name of pi-timolo sync lock filename

# ------------------ Remote Sync Config Settings -------------------
# Remote configuration will copy a new configuration file from remote google drive 
# to local pi-timolo config.py file and restart pi-timolo.
REMOTE_CONFIG_ON=false                 # true - Check for remote config file and install, false - Ignore Checking
REMOTE_CONFIG_DIR='sync_config_cam1'   # Remote Config Folder on google drive (Will be Created if Does Not Exist)
REMOTE_CONFIG_FILE='config.py'         # Name of new pi-timolo config file on google drive
LOCAL_CONFIG_FILE='config.py'          # pi-timolo configuration variables file (default)

# -------------------- Watch App Settings --------------------------
WATCH_APP_ON=false           # false - off  true - Check if app is running and restart or reboot
WATCH_APP='pi-timolo.py'     # App filename to monitor for Run Status
FORCE_REBOOT=false           # false - Restart pi-timolo.py if not running    true - Reboot if not running (Use with Caution)  

echo "==================== Variable Settings ==========================="
echo "SYNC_ON             =" $SYNC_ON
echo "SYNC_DIR            =" $SYNC_DIR
echo "FILES_TO_SYNC       =" $FILES_TO_SYNC
echo "CHECK_FOR_SYNC_FILE =" $CHECK_FOR_SYNC_FILE
echo "SYNC_FILE_PATH      =" $SYNC_FILE_PATH
echo ""
echo "REMOTE_CONFIG_ON    =" $REMOTE_CONFIG_ON
echo "REMOTE_CONFIG_DIR   =" $REMOTE_CONFIG_DIR
echo "REMOTE_CONFIG_FILE  =" $REMOTE_CONFIG_FILE
echo "LOCAL_CONFIG_FILE   =" $LOCAL_CONFIG_FILE
echo ""
echo "WATCH_APP_ON        =" $WATCH_APP_ON
echo "WATCH_APP           =" $WATCH_APP
echo "FORCE_REBOOT        =" $FORCE_REBOOT
echo "=================================================================="
echo "STATUS  - Current pi-timolo working dir is $PROG_DIR"
echo "START   - Processing"

# ------------------------------------------------------
function restart_pi_timolo ()
{
    echo "------------------------------------------"
    echo "START   - restart_pi_timolo"
    echo "INFO    - Starting pi-timolo"
    $PROG_DIR/pi-timolo.sh start
    sleep 5
    if [ -z "$(pgrep -f pi-timolo.py)" ] ; then
        # pi-timolo did not start
        echo "WARN    - Failed to Restart pi-timolo"
    else
        echo "INFO    - Successfully restarted pi-timolo"
    fi
}

# ------------------------------------------------------
function do_gdrive_sync ()
{
    # function to perform a gdrive sync between RPI local folder (exit if not found) and
    # a matching google drive folder. (Create remote folder if required)
    # Check if Local SYNC_DIR folder exists
    if [ ! -d "$PROG_DIR/$SYNC_DIR" ] ; then
        echo "ERROR   - Local Folder $PROG_DIR/$SYNC_DIR Does Not Exist"
        echo "          Please Check SYNC_DIR variable and/or Local Folder PATH"
        return 1
    fi

    # Check for matching files to sync in folder
    ls -1 $PROG_DIR/$SYNC_DIR/$FILES_TO_SYNC > /dev/null 2>&1
    if [ ! "$?" = "0" ] ; then
        echo "WARNING - No Matching $FILES_TO_SYNC Files Found in $PROG_DIR/$SYNC_DIR"
        return 1
    fi

    # Check if a matching remote folder exists
    # and if Not then create one
    echo "STATUS  - Check if Remote Folder /$SYNC_DIR Exists"
    echo "------------------------------------------"
    /usr/local/bin/gdrive file-id $SYNC_DIR
    if [ ! $? -eq 0 ] ; then
        echo "------------------------------------------"
        echo "STATUS  - Creating Remote Folder /$SYNC_DIR"
        echo "------------------------------------------"
        /usr/local/bin/gdrive new --folder $SYNC_DIR
        /usr/local/bin/gdrive file-id $SYNC_DIR
        if [ $? -eq 0 ] ; then
            echo "------------------------------------------"
            echo "STATUS  - Successfully Created Remote Folder /$SYNC_DIR"
        else
            echo "------------------------------------------"
            echo "ERROR   - Problem Creating Remote Folder $SYNC_DIR"
            echo "          Please Investigate Problem"
            return 1
        fi
    fi
    echo "------------------------------------------"
    echo "STATUS  - Start gdrive Sync ...."
    echo "STATUS  - Local Source Folder - $PROG_DIR/$SYNC_DIR"
    echo "STATUS  - Remote Destn Folder - /$SYNC_DIR"
    echo "STATUS  - Files $FILES_TO_SYNC"
    echo "STATUS  - Running  This May Take Some Time ....."
    echo "STATUS  - sudo /usr/local/bin/gdrive push -no-prompt -ignore-conflict $SYNC_DIR/$FILES_TO_SYNC"
    echo "------------------------------------------"
    date
    START=$(date +%s)
    sudo /usr/local/bin/gdrive push -no-prompt -ignore-conflict $SYNC_DIR/$FILES_TO_SYNC
    if [ $? -ne 0 ] ;  then
        # Check if gdrive sync was successfully
        date
        echo "------------------------------------------"
        echo "ERROR   - gdrive Sync Failed."
        echo "          Possible Cause - See gdrive Error Message Above"
        echo "          Please Investigate Problem and Try Again"
    else
        # If successful then display processing time and remove sync file
        date
        END=$(date +%s)
        DIFF=$((END - START))
        echo "------------------------------------------"
        echo "STATUS  - $0 Completed Successfully"
        echo "STATUS  - Processing Took $DIFF seconds"
{'fraction_non_alphanumeric': 0.21187020810514787, 'fraction_numerical': 0.0032858707557502738, 'mean_word_length': 1.8740901042691325, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4283321', 'n_tokens_mistral': 4178, 'n_tokens_neox': 4042, 'n_words': 1306}