<commit_before>#!/bin/bash

# This script sets up an openaps environment to work with loop.sh,
# by defining the required devices, reports, and aliases.
#
# Released under MIT license. See the accompanying LICENSE.txt file for
# full terms and conditions
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

die() {
  echo "$@"
  exit 1
}

# defaults
max_iob=0
CGM="G4"
DIR=""
directory=""
EXTRAS=""

for i in "$@"
do
case $i in
    -d=*|--dir=*)
    DIR="${i#*=}"
    # ~/ paths have to be expanded manually
    DIR="${DIR/#\~/$HOME}"
    directory="$(readlink -m $DIR)"
    shift # past argument=value
    ;;
    -s=*|--serial=*)
    serial="${i#*=}"
    shift # past argument=value
    ;;
    -t=*|--tty=*)
    ttyport="${i#*=}"
    shift # past argument=value
    ;;
    -m=*|--max_iob=*)
    max_iob="${i#*=}"
    shift # past argument=value
    ;;
    -c=*|--cgm=*)
    CGM="${i#*=}"
    shift # past argument=value
    ;;
    -n=*|--ns-host=*)
    NIGHTSCOUT_HOST="${i#*=}"
    shift # past argument=value
    ;;
    -a=*|--api-secret=*)
    API_SECRET="${i#*=}"
    shift # past argument=value
    ;;
    -e=*|--enable=*)
    ENABLE="${i#*=}"
    shift # past argument=value
    ;;
    *)
            # unknown option
    echo "Option ${i#*=} unknown"
    ;;
esac
done

if ! [[ ${CGM,,} =~ "g4" || ${CGM,,} =~ "g5" || ${CGM,,} =~ "mdt" ]]; then
    echo "Unsupported CGM.  Please select (Dexcom) G4 (default), G5, or MDT."
    echo "If you'd like to help add Medtronic CGM support, please contact @scottleibrand on Gitter"
    echo
    DIR="" # to force a Usage prompt
fi
if ! ( git config -l | grep -q user.email ) ; then
    read -p "What email address would you like to use for git commits? " -r
    EMAIL=$REPLY
    git config --global user.email $EMAIL
fi
if ! ( git config -l | grep -q user.name ); then
    read -p "What full name would you like to use for git commits? " -r
    NAME=$REPLY
    git config --global user.name $NAME
fi
if [[ -z "$DIR" || -z "$serial" ]]; then
    echo "Usage: oref0-setup.sh <--dir=directory> <--serial=pump_serial_#> [--tty=/dev/ttySOMETHING] [--max_iob=0] [--ns-host=https://mynightscout.azurewebsites.net] [--api-secret=myplaintextsecret] [--cgm=(G4|G5|MDT)] [--enable='autosens meal']"
    read -p "Start interactive setup? [Y]/n " -r
    if [[ $REPLY =~ ^[Nn]$ ]]; then
        exit
    fi
    read -p "What would you like to call your loop directory? [myopenaps] " -r
    DIR=$REPLY
    if [[ -z $DIR ]]; then DIR="myopenaps"; fi
    echo "Ok, $DIR it is."
    directory="$(readlink -m $DIR)"
    read -p "What is your pump serial number? " -r
    serial=$REPLY
    echo "Ok, $serial it is."
    read -p "What Kind of CGM are you using? (i.e. G4, G5, MDT) " -r
    CGM=$REPLY
    echo "Ok, $CGM it is."
    read -p "Are you using mmeowlink? If not, press enter. If so, what TTY port (i.e. /dev/ttySOMETHING)? " -r
    ttyport=$REPLY
    echo -n "Ok, "
    if [[ -z "$ttyport" ]]; then
        echo -n Carelink
    else
        echo -n TTY $ttyport
    fi
    echo " it is."
    echo Are you using Nightscout? If not, press enter.
    read -p "If so, what is your Nightscout host? (i.e. https://mynightscout.azurewebsites.net)? " -r
    NIGHTSCOUT_HOST=$REPLY
    if [[ -z $NIGHTSCOUT_HOST ]]; then
        echo Ok, no Nightscout for you.
    else
        echo "Ok, $NIGHTSCOUT_HOST it is."
    fi
    if [[ ! -z $NIGHTSCOUT_HOST ]]; then
        read -p "And what is your Nightscout api secret (i.e. myplaintextsecret)? " -r
        API_SECRET=$REPLY
        echo "Ok, $API_SECRET it is."
    fi
    read -p "Do you need any advanced features? y/[N] " -r
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        read -p "Enable automatic sensitivity adjustment? y/[N] " -r
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            ENABLE+=" autosens "
        fi
        read -p "Enable advanced meal assist? y/[N] " -r
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            ENABLE+=" meal "
        fi
    fi
fi

#if [[ $# -gt 3 ]]; then
    #share_serial=$4
#fi

echo "Setting up oref0 in $directory for pump $serial with $CGM CGM,"
echo -n "NS host $NIGHTSCOUT_HOST, "
if [[ -z "$ttyport" ]]; then
    echo -n Carelink
else
    echo -n TTY $ttyport
fi
if [[ "$max_iob" -ne 0 ]]; then echo -n ", max_iob $max_iob"; fi
if [[ ! -z "$ENABLE" ]]; then echo -n ", advanced features $ENABLE"; fi
echo

read -p "Continue? y/[N] " -r
if [[ $REPLY =~ ^[Yy]$ ]]; then

echo -n "Checking $directory: "
mkdir -p $directory
if ( cd $directory && git status 2>/dev/null >/dev/null && openaps use -h >/dev/null && echo true ); then
    echo $directory already exists
elif openaps init $directory; then
    echo $directory initialized
else
    die "Can't init $directory"
fi
cd $directory || die "Can't cd $directory"
ls monitor 2>/dev/null >/dev/null || mkdir monitor || die "Can't mkdir monitor"
ls raw-cgm 2>/dev/null >/dev/null || mkdir raw-cgm || die "Can't mkdir raw-cgm"
ls cgm 2>/dev/null >/dev/null || mkdir cgm || die "Can't mkdir cgm"
ls settings 2>/dev/null >/dev/null || mkdir settings || die "Can't mkdir settings"
ls enact 2>/dev/null >/dev/null || mkdir enact || die "Can't mkdir enact"
ls upload 2>/dev/null >/dev/null || mkdir upload || die "Can't mkdir upload"

mkdir -p $HOME/src/
if [ -d "$HOME/src/oref0/" ]; then
    echo "$HOME/src/oref0/ already exists; pulling latest"
    (cd ~/src/oref0 && git fetch && git pull) || die "Couldn't pull latest oref0"
else
    echo -n "Cloning oref0 dev: "
    (cd ~/src && git clone -b dev git://github.com/openaps/oref0.git) || die "Couldn't clone oref0 dev"
fi
echo Checking oref0 installation
oref0-get-profile --exportDefaults 2>/dev/null >/dev/null || (echo Installing latest oref0 dev && cd $HOME/src/oref0/ && npm run global-install)

echo Checking mmeowlink installation
if openaps vendor add --path . mmeowlink.vendors.mmeowlink 2>&1 | grep "No module"; then
    if [ -d "$HOME/src/mmeowlink/" ]; then
        echo "$HOME/src/mmeowlink/ already exists; pulling latest dev branch"
        (cd ~/src/mmeowlink && git fetch && git checkout dev && git pull) || die "Couldn't pull latest mmeowlink dev"
    else
        echo -n "Cloning mmeowlink dev: "
        (cd ~/src && git clone -b dev git://github.com/oskarpearson/mmeowlink.git) || die "Couldn't clone mmeowlink dev"
    fi
    echo Installing latest mmeowlink dev && cd $HOME/src/mmeowlink/ && sudo pip install -e . || die "Couldn't install mmeowlink"
fi

cd $directory
if [[ "$max_iob" -eq 0 ]]; then
    oref0-get-profile --exportDefaults > preferences.json || die "Could not run oref0-get-profile"
else
    echo "{ \"max_iob\": $max_iob }" > max_iob.json && oref0-get-profile --updatePreferences max_iob.json > preferences.json && rm max_iob.json || die "Could not run oref0-get-profile"
fi

cat preferences.json
git add preferences.json

# enable log rotation
sudo cp $HOME/src/oref0/logrotate.openaps /etc/logrotate.d/openaps || die "Could not cp /etc/logrotate.d/openaps"
sudo cp $HOME/src/oref0/logrotate.rsyslog /etc/logrotate.d/rsyslog || die "Could not cp /etc/logrotate.d/rsyslog"<commit_msg>upgrade oref0 if no oref0_glucose_since<commit_after>#!/bin/bash

# This script sets up an openaps environment to work with loop.sh,
# by defining the required devices, reports, and aliases.
#
# Released under MIT license. See the accompanying LICENSE.txt file for
# full terms and conditions
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

die() {
  echo "$@"
  exit 1
}

# defaults
max_iob=0
CGM="G4"
DIR=""
directory=""
EXTRAS=""

for i in "$@"
do
case $i in
    -d=*|--dir=*)
    DIR="${i#*=}"
    # ~/ paths have to be expanded manually
    DIR="${DIR/#\~/$HOME}"
    directory="$(readlink -m $DIR)"
    shift # past argument=value
    ;;
    -s=*|--serial=*)
    serial="${i#*=}"
    shift # past argument=value
    ;;
    -t=*|--tty=*)
    ttyport="${i#*=}"
    shift # past argument=value
    ;;
    -m=*|--max_iob=*)
    max_iob="${i#*=}"
    shift # past argument=value
    ;;
    -c=*|--cgm=*)
    CGM="${i#*=}"
    shift # past argument=value
    ;;
    -n=*|--ns-host=*)
    NIGHTSCOUT_HOST="${i#*=}"
    shift # past argument=value
    ;;
    -a=*|--api-secret=*)
    API_SECRET="${i#*=}"
    shift # past argument=value
    ;;
    -e=*|--enable=*)
    ENABLE="${i#*=}"
    shift # past argument=value
    ;;
    *)
            # unknown option
    echo "Option ${i#*=} unknown"
    ;;
esac
done

if ! [[ ${CGM,,} =~ "g4" || ${CGM,,} =~ "g5" || ${CGM,,} =~ "mdt" ]]; then
    echo "Unsupported CGM.  Please select (Dexcom) G4 (default), G5, or MDT."
    echo "If you'd like to help add Medtronic CGM support, please contact @scottleibrand on Gitter"
    echo
    DIR="" # to force a Usage prompt
fi
if ! ( git config -l | grep -q user.email ) ; then
    read -p "What email address would you like to use for git commits? " -r
    EMAIL=$REPLY
    git config --global user.email $EMAIL
fi
if ! ( git config -l | grep -q user.name ); then
    read -p "What full name would you like to use for git commits? " -r
    NAME=$REPLY
    git config --global user.name $NAME
fi
if [[ -z "$DIR" || -z "$serial" ]]; then
    echo "Usage: oref0-setup.sh <--dir=directory> <--serial=pump_serial_#> [--tty=/dev/ttySOMETHING] [--max_iob=0] [--ns-host=https://mynightscout.azurewebsites.net] [--api-secret=myplaintextsecret] [--cgm=(G4|G5|MDT)] [--enable='autosens meal']"
    read -p "Start interactive setup? [Y]/n " -r
    if [[ $REPLY =~ ^[Nn]$ ]]; then
        exit
    fi
    read -p "What would you like to call your loop directory? [myopenaps] " -r
    DIR=$REPLY
    if [[ -z $DIR ]]; then DIR="myopenaps"; fi
    echo "Ok, $DIR it is."
    directory="$(readlink -m $DIR)"
    read -p "What is your pump serial number? " -r
    serial=$REPLY
    echo "Ok, $serial it is."
    read -p "What Kind of CGM are you using? (i.e. G4, G5, MDT) " -r
    CGM=$REPLY
    echo "Ok, $CGM it is."
    read -p "Are you using mmeowlink? If not, press enter. If so, what TTY port (i.e. /dev/ttySOMETHING)? " -r
    ttyport=$REPLY
    echo -n "Ok, "
    if [[ -z "$ttyport" ]]; then
        echo -n Carelink
    else
        echo -n TTY $ttyport
    fi
    echo " it is."
    echo Are you using Nightscout? If not, press enter.
    read -p "If so, what is your Nightscout host? (i.e. https://mynightscout.azurewebsites.net)? " -r
    NIGHTSCOUT_HOST=$REPLY
    if [[ -z $NIGHTSCOUT_HOST ]]; then
        echo Ok, no Nightscout for you.
    else
        echo "Ok, $NIGHTSCOUT_HOST it is."
    fi
    if [[ ! -z $NIGHTSCOUT_HOST ]]; then
        read -p "And what is your Nightscout api secret (i.e. myplaintextsecret)? " -r
        API_SECRET=$REPLY
        echo "Ok, $API_SECRET it is."
    fi
    read -p "Do you need any advanced features? y/[N] " -r
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        read -p "Enable automatic sensitivity adjustment? y/[N] " -r
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            ENABLE+=" autosens "
        fi
        read -p "Enable advanced meal assist? y/[N] " -r
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            ENABLE+=" meal "
        fi
    fi
fi

#if [[ $# -gt 3 ]]; then
    #share_serial=$4
#fi

echo "Setting up oref0 in $directory for pump $serial with $CGM CGM,"
echo -n "NS host $NIGHTSCOUT_HOST, "
if [[ -z "$ttyport" ]]; then
    echo -n Carelink
else
    echo -n TTY $ttyport
fi
if [[ "$max_iob" -ne 0 ]]; then echo -n ", max_iob $max_iob"; fi
if [[ ! -z "$ENABLE" ]]; then echo -n ", advanced features $ENABLE"; fi
echo

read -p "Continue? y/[N] " -r
if [[ $REPLY =~ ^[Yy]$ ]]; then

echo -n "Checking $directory: "
mkdir -p $directory
if ( cd $directory && git status 2>/dev/null >/dev/null && openaps use -h >/dev/null && echo true ); then
    echo $directory already exists
elif openaps init $directory; then
    echo $directory initialized
else
    die "Can't init $directory"
fi
cd $directory || die "Can't cd $directory"
ls monitor 2>/dev/null >/dev/null || mkdir monitor || die "Can't mkdir monitor"
ls raw-cgm 2>/dev/null >/dev/null || mkdir raw-cgm || die "Can't mkdir raw-cgm"
ls cgm 2>/dev/null >/dev/null || mkdir cgm || die "Can't mkdir cgm"
ls settings 2>/dev/null >/dev/null || mkdir settings || die "Can't mkdir settings"
ls enact 2>/dev/null >/dev/null || mkdir enact || die "Can't mkdir enact"
ls upload 2>/dev/null >/dev/null || mkdir upload || die "Can't mkdir upload"

mkdir -p $HOME/src/
if [ -d "$HOME/src/oref0/" ]; then
    echo "$HOME/src/oref0/ already exists; pulling latest"
    (cd ~/src/oref0 && git fetch && git pull) || die "Couldn't pull latest oref0"
else
    echo -n "Cloning oref0 dev: "
    (cd ~/src && git clone -b dev git://github.com/openaps/oref0.git) || die "Couldn't clone oref0 dev"
fi
echo Checking oref0 installation
( grep -q oref0_glucose_since `which nightscout` && oref0-get-profile --exportDefaults 2>/dev/null >/dev/null ) || (echo Installing latest oref0 dev && cd $HOME/src/oref0/ && npm run global-install)

echo Checking mmeowlink installation
if openaps vendor add --path . mmeowlink.vendors.mmeowlink 2>&1 | grep "No module"; then
    if [ -d "$HOME/src/mmeowlink/" ]; then
        echo "$HOME/src/mmeowlink/ already exists; pulling latest dev branch"
        (cd ~/src/mmeowlink && git fetch && git checkout dev && git pull) || die "Couldn't pull latest mmeowlink dev"
    else
        echo -n "Cloning mmeowlink dev: "
        (cd ~/src && git clone -b dev git://github.com/oskarpearson/mmeowlink.git) || die "Couldn't clone mmeowlink dev"
    fi
    echo Installing latest mmeowlink dev && cd $HOME/src/mmeowlink/ && sudo pip install -e . || die "Couldn't install mmeowlink"
fi

cd $directory
if [[ "$max_iob" -eq 0 ]]; then
    oref0-get-profile --exportDefaults > preferences.json || die "Could not run oref0-get-profile"
else
    echo "{ \"max_iob\": $max_iob }" > max_iob.json && oref0-get-profile --updatePreferences max_iob.json > preferences.json && rm max_iob.json || die "Could not run oref0-get-profile"
fi

cat preferences.json
git add preferences.json

# enable log rotation
sudo cp $HOME/src/oref0/logrotate.openaps /etc/logrotate.d/openaps || die "Could not cp /etc/logrotate.d/openaps"
sudo cp $HOME/src/oref0/logrotate.rsyslog /etc/logrotate.d/rsyslog || die "Could not cp /etc/logrotate.d/rsyslog"
{'fraction_non_alphanumeric': 0.15483484283431007, 'fraction_numerical': 0.006193393713372403, 'mean_word_length': 2.8445980542754734, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 49, 'https://': 4, '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': '305187', 'n_tokens_mistral': 5530, 'n_tokens_neox': 5333, 'n_words': 1792}