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

# curl https://raw.githubusercontent.com/adafruit/Adafruit-WebIDE/alpha/scripts/install.sh | sudo sh
# curl https://raw.githubusercontent.com/adafruit/Adafruit-WebIDE/test/scripts/install.sh | sudo sh
# curl https://raw.githubusercontent.com/adafruit/Adafruit-WebIDE/offline/scripts/install.sh | sudo sh -s - --offline

#tar -zcvf editor.tar.gz * --exclude .git --exclude .gitignore
#tar -zcvf editor-update.tar.gz * --exclude .git --exclude .gitignore
#scp <EMAIL>:/home/pi/Adafruit-WebIDE/editor.tar.gz editor-0.2.4.tar.gz
#scp <EMAIL>:/home/pi/Adafruit-WebIDE-Update/editor-update.tar.gz editor-0.2.4-update.tar.gz
#sudo -u webide -g webide node server

#http://stackoverflow.com/a/6946864/629189
# translate long options to short
OFFLINE=false
GITHUB=false

for arg
do
    delim=""
    case "$arg" in
       --offline) args="${args}-o ";;
       --github) args="${args}-g ";;
       --help) args="${args}-h ";;
       #--config) args="${args}-c ";;
       # pass through anything else
       *) [[ "${arg:0:1}" == "-" ]] || delim="\""
           args="${args}${delim}${arg}${delim} ";;
    esac
done
# reset the translated args
eval set -- $args
# now we can process with getopt
while getopts ":hogvc:" opt; do
    case $opt in
        h)  usage ;;
        o)  OFFLINE=true ;;
        g)  GITHUB=true ;;
        #c)  source $OPTARG ;;
        #\?) usage ;;
        :)
        echo "option -$OPTARG requires an argument"
        usage
        ;;
    esac
done

echo "$OFFLINE"

set -e
WEBIDE_ROOT="/usr/share/adafruit/webide"

#needed for SSH key and config access at this point.
WEBIDE_HOME="/home/webide"

#NODE_PATH="/usr/local/lib/node"

#if [ ! -d "$NODE_PATH" ]; then
#  mkdir -p "$NODE_PATH"
#    # Control will enter here if $DIRECTORY doesn't exist.
#fi

mkdir -p "$WEBIDE_ROOT"
mkdir -p "$WEBIDE_HOME"
cd "$WEBIDE_ROOT"

echo "**** Downloading the latest version of the WebIDE ****"
curl -L http://adafruit-download.s3.amazonaws.com/webide-0.3.8.tar.gz | tar xzf -

echo "**** Installing required libraries ****"
echo "**** (redis-server git restartd libcap2-bin avahi-daemon i2c-tools python-smbus) ****"
apt-get update
apt-get install nodejs nodejs-legacy redis-server git restartd libcap2-bin avahi-daemon i2c-tools python-smbus ntp -y

echo "**** Create webide user and group ****"
groupadd webide || true
useradd -g webide webide || true
usermod -a -G i2c,sudo webide || true

echo "**** Adding webide user to sudoers ****"
if [ -f "/etc/sudoers.tmp" ]; then
    rm /etc/sudoers.tmp
fi
cp /etc/sudoers /etc/sudoers.tmp
echo "webide ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.tmp
visudo -c -f /etc/sudoers.tmp
if [ "$?" -eq "0" ]; then
    cp /etc/sudoers.tmp /etc/sudoers
fi
rm /etc/sudoers.tmp

chown -R webide:webide "$WEBIDE_HOME"
chown -R webide:webide "$WEBIDE_ROOT"
chmod 775 "$WEBIDE_ROOT"

echo "**** Adding default .bashrc file for webide user ****"
cp "$WEBIDE_ROOT/scripts/.bashrc" "$WEBIDE_HOME"

echo "**** Installing the WebIDE as a service ****"
echo "**** (to uninstall service, execute: 'sudo update-rc.d -f adafruit-webide.sh remove') ****"
cp "$WEBIDE_ROOT/scripts/adafruit-webide.sh" "/etc/init.d"
cd /etc/init.d
chmod 755 adafruit-webide.sh

NODE_PATH=""
ARCH=$(dpkg --print-architecture)
if [ $ARCH = armhf ]; then
  NODE_PATH="\/usr\/share\/adafruit\/webide\/bin\/node_hf\/node"
  chmod +x "$WEBIDE_ROOT/bin/node_hf/node"
else
  NODE_PATH="\/usr\/share\/adafruit\/webide\/bin\/node_sf\/node"
  chmod +x "$WEBIDE_ROOT/bin/node_sf/node"
fi
sed -i "s/NODE_PATH/$NODE_PATH/g" adafruit-webide.sh

update-rc.d adafruit-webide.sh defaults

#set binaries as executable

echo "Attempting to force reload date and time from ntp server"
/etc/init.d/ntp force-reload

#Check if port 80 is in use, use 8080 if so.
PORT_USED=""
if netstat -lnt | awk '$6 == "LISTEN" && $4 ~ ".80"' | grep -q "LISTEN"
then
  redis-cli HMSET server port 8090
  PORT_USED=":8090"
  echo "**** WARNING: PORT 80 IN USE. FALLING BACK TO 8090. ****"
  echo "**** TO CHOOSE A DIFFERENT PORT USE THE FOLLOWING COMMAND: ****"
  echo "**** redis-cli HMSET server port 8090 ****"
  echo "**** AND RESTART THE SERVER ****"
fi

if $OFFLINE
then
  redis-cli HMSET server offline 1
fi

if $GITHUB
then
  redis-cli HMSET server github 1
fi

service adafruit-webide.sh start

if grep -q adafruit-webide.sh /etc/restartd.conf
then
  echo "restartd already configured"
else
  echo 'webide "node" "service adafruit-webide.sh restart" ""' >> /etc/restartd.conf
fi

#kill all restartd processes, and restart one
pkill -f restartd || true
sleep 5s
restartd

echo "**** Starting the server...(please wait) ****"
sleep 20s

echo "**** The Adafruit WebIDE is installed and running! ****"
echo "**** Commands: sudo service adafruit-webide.sh {start,stop,restart} ****"
echo "**** Navigate to http://$(hostname).local$PORT_USED to use the WebIDE"
#echo "**** To run the editor: ****"
#echo "**** cd ~/Adafruit/WebIDE ****"
#echo "**** node webide ****"
<commit_msg>Clean up the install.sh to have our stuff.<commit_after>set -e

# Defaults to install where install.sh is located
INSTALL_DIR="$( cd "$(dirname "$0")" ; pwd -P )"

BUILD_DIR=${INSTALL_DIR}/BUILD
mkdir -p $BUILD_DIR

pushd $BUILD_DIR

# Install dependencies that can be obtained from apt repos
sudo apt-get install python-dev
## ffmpeg can either be ffmpeg or libav-tools depending on distro
sudo apt-get install -y ffmpeg || true
sudo apt-get install -y libav-tools || true
sudo apt-get install -y lame flac faad vorbis-tools
sudo apt-get install -y python-alsaaudio
sudo apt-get install -y python-numpy
sudo apt-get install -y python-virtualenv

# Create a virtualenv
VENV=./venv
virtualenv $VENV --system-site-packages

# Source it
source $VENV/bin/activate

# Install mutagen
pip install mutagen

# Install decoder
wget -c http://www.brailleweb.com/downloads/decoder-1.5XB-Unix.zip
unzip -o decoder-1.5XB-Unix.zip
cp decoder-1.5XB-Unix/{codecs.pdc,*.py} $VENV/lib/python2.7/site-packages/
rm -rf decoder-1.5XB-Unix/

# Check to see if we have git
git --version > /dev/null

# Install py-spidev
pip install -e git+https://github.com/doceme/py-spidev.git#egg=spidev

# Install RPi-LED code
# FIXME: Use Kyle's fork for the different driver.
pip install -e git+git@github.com:adammhaile/RPi-LPD8806.git#egg=raspledstrip

{'fraction_non_alphanumeric': 0.15603174603174602, 'fraction_numerical': 0.013492063492063493, 'mean_word_length': 3.5892206846321923, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6453761', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2290, 'n_words': 643}