<commit_before>#!/bin/bash
#: Description: Enables the required modules for PiFace Clock.

#=======================================================================
# NAME: set_revision_var
# DESCRIPTION: Stores the revision number of this Raspberry Pi into
#              $RPI_REVISION
#=======================================================================
set_revision_var() {
    rev_str=$(grep "Revision" /proc/cpuinfo)
    # get the last character
    len_rev_str=${#rev_str}
    chr_index=$(($len_rev_str-1))
    chr=${rev_str:$chr_index:$len_rev_str}
    if [[ $chr == "2" || $chr == "3" ]]; then
        RPI_REVISION="1"
    else
        RPI_REVISION="2"
    fi
}

#=======================================================================
# NAME: enable_module
# DESCRIPTION: Enabled the I2C module.
#=======================================================================
enable_module() {
    echo "Enabling I2C module."
    module="i2c-bcm2708"
    modules_file="/etc/modules"
    # if $module not in $modules_file: append $module to $modules_file.
    if ! grep -q $module $modules_file; then
        echo $module >> $modules_file
    fi
}

#=======================================================================
# NAME: start_on_boot
# DESCRIPTION: Load the I2C modules and send magic number to RTC, on boot.
#=======================================================================
start_on_boot() {
    echo "Installing i2c-tools."
    apt-get update
    apt-get install i2c-tools

    echo "Changing /etc/rc.local to load time from PiFace Clock."
    # remove exit 0
    sed -i "s/exit 0//" /etc/rc.local

    if [[ $RPI_REVISION == "1" ]]; then
        i=0  # i2c-0
    else
        i=1  # i2c-1
    fi

    cat >> /etc/rc.local << EOF
modprobe i2c-dev
i2cset -y $i 0x6f 0x08 0x47
modprobe i2c:mcp7941x
echo mcp7941x 0x6f > /sys/class/i2c-dev/i2c-$i/device/new_device
hwclock -s
EOF
}

#=======================================================================
# MAIN
#=======================================================================
# check if the script is being run as root
if [[ $EUID -ne 0 ]]
then
    printf 'This script must be run as root.\nExiting..\n'
    exit 1
fi
RPI_REVISION=""
set_revision_var &&
enable_module &&
start_on_boot &&
printf 'Please *reboot* and then set your clock with:

    sudo date -s "14 JAN 2014 10:10:30"

'<commit_msg>changed i2c-tool from install to a check<commit_after>#!/bin/bash
#: Description: Enables the required modules for PiFace Clock.

#=======================================================================
# NAME: check_for_i2c_tools
# DESCRIPTION: Checks if i2c-tools is installed.
#=======================================================================
check_for_i2c_tools() {
    dpkg -s i2c-tools > /dev/null 2>&1
    if [[ $? -eq 1 ]]; then
        echo "The package `i2c-tools` is not installed. Install it with:"
        echo ""
        echo "    sudo apt-get install i2c-tools"
        echo ""
        exit 1
    fi
}

#=======================================================================
# NAME: set_revision_var
# DESCRIPTION: Stores the revision number of this Raspberry Pi into
#              $RPI_REVISION
#=======================================================================
set_revision_var() {
    rev_str=$(grep "Revision" /proc/cpuinfo)
    # get the last character
    len_rev_str=${#rev_str}
    chr_index=$(($len_rev_str-1))
    chr=${rev_str:$chr_index:$len_rev_str}
    if [[ $chr == "2" || $chr == "3" ]]; then
        RPI_REVISION="1"
    else
        RPI_REVISION="2"
    fi
}

#=======================================================================
# NAME: enable_module
# DESCRIPTION: Enabled the I2C module.
#=======================================================================
enable_module() {
    echo "Enabling I2C module."
    module="i2c-bcm2708"
    modules_file="/etc/modules"
    # if $module not in $modules_file: append $module to $modules_file.
    if ! grep -q $module $modules_file; then
        echo $module >> $modules_file
    fi
}

#=======================================================================
# NAME: start_on_boot
# DESCRIPTION: Load the I2C modules and send magic number to RTC, on boot.
#=======================================================================
start_on_boot() {
    echo "Installing i2c-tools."
    apt-get update
    apt-get install i2c-tools

    echo "Changing /etc/rc.local to load time from PiFace Clock."
    # remove exit 0
    sed -i "s/exit 0//" /etc/rc.local

    if [[ $RPI_REVISION == "1" ]]; then
        i=0  # i2c-0
    else
        i=1  # i2c-1
    fi

    cat >> /etc/rc.local << EOF
modprobe i2c-dev
i2cset -y $i 0x6f 0x08 0x47
modprobe i2c:mcp7941x
echo mcp7941x 0x6f > /sys/class/i2c-dev/i2c-$i/device/new_device
hwclock -s
EOF
}

#=======================================================================
# MAIN
#=======================================================================
# check if the script is being run as root
if [[ $EUID -ne 0 ]]
then
    printf 'This script must be run as root.\nExiting..\n'
    exit 1
fi
RPI_REVISION=""
check_for_i2c_tools &&
set_revision_var &&
enable_module &&
start_on_boot &&
printf 'Please *reboot* and then set your clock with:

    sudo date -s "14 JAN 2014 10:10:30"

'
{'fraction_non_alphanumeric': 0.3342140026420079, 'fraction_numerical': 0.02528779014908473, 'mean_word_length': 4.00945179584121, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '226126', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1572, 'n_words': 431}