<commit_before>#!/bin/bash

{ # this ensures the entire script is downloaded #

is_installed() {
  type "$1" > /dev/null 2>&1
}

if [ -z "$DUNITER_DIR" ]; then
  DUNITER_DIR="$HOME/.duniter"
fi

latest_version() {
  echo "v1.2.6"
}

repo_url() {
  echo "https://github.com/duniter/duniter.git"
}

download() {
  if is_installed "curl"; then
    curl -qkL $*
  elif is_installed "wget"; then
    # Emulate curl with wget
    ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \
                           -e 's/-L //' \
                           -e 's/-I /--server-response /' \
                           -e 's/-s /-q /' \
                           -e 's/-o /-O /' \
                           -e 's/-C - /-c /')
    wget $ARGS
  fi
}

install_from_git() {

  local PREVIOUS_PATH
  PREVIOUS_PATH=$PATH
  if [ -d "$DUNITER_DIR/.git" ]; then
    echo "=> duniter is already installed in $DUNITER_DIR, trying to update using git"
    printf "\r=> "
    cd "$DUNITER_DIR" && (command git fetch 2> /dev/null || {
      echo >&2 "Failed to update duniter, run 'git fetch' in $DUNITER_DIR yourself." && exit 1
    })
  else
    # Cloning to $DUNITER_DIR
    echo "=> Downloading duniter from git to '$DUNITER_DIR'"
    printf "\r=> "
    mkdir -p "$DUNITER_DIR"
    command git clone "$(repo_url)" "$DUNITER_DIR"
  fi
  cd "$DUNITER_DIR" && command git checkout --quiet $(latest_version)
  if [ ! -z "$(cd "$DUNITER_DIR" && git show-ref refs/heads/master)" ]; then
    if git branch --quiet 2>/dev/null; then
      cd "$DUNITER_DIR" && command git branch --quiet -D master >/dev/null 2>&1
    else
      echo >&2 "Your version of git is out of date. Please update it!"
      cd "$DUNITER_DIR" && command git branch -D master >/dev/null 2>&1
    fi
  fi

  # Download Nodejs
  local NVER="6.9.4";
  local ARCH="x86"
  local X64=`uname -a | grep "x86_64"`
  local ARM=`uname -a | grep "arm"`
  if [ ! -z "$X64" ]; then
    ARCH="x64"
  fi
  # ARM processors
  if [ ! -z "$ARM" ]; then
    ARCH="`uname -m`"
  fi
  local NODEJS_FILENAME=node-v${NVER}-linux-${ARCH}
  local NODEJS_TARBALL=http://nodejs.org/dist/v${NVER}/${NODEJS_FILENAME}.tar.gz
  local NODEJS_ARCHIVE=$DUNITER_DIR/node.tar.gz
  local NODEJS_EXTRACTED=$DUNITER_DIR/$NODEJS_FILENAME
  if [ ! -d "$DUNITER_DIR/node" ]; then
    echo "=> Downloading '$NODEJS_TARBALL' to '$NODEJS_ARCHIVE'"
    download "$NODEJS_TARBALL" -o "$NODEJS_ARCHIVE" || {
      echo >&2 "Failed to download '$NODEJS_TARBALL'"
      return 4
    }
    tar xzf $NODEJS_ARCHIVE || {
      echo >&2 "Failed to extract '$NODEJS_ARCHIVE'"
      return 5
    }
    mv $NODEJS_FILENAME "node" || {
      echo >&2 "Failed to extract '$NODEJS_ARCHIVE'"
      return 6
    }
  fi

  # Install Duniter dependencies (NPM modules)
  NODE=$DUNITER_DIR/node/bin/node
  NPM=$DUNITER_DIR/node/bin/npm
  $NODE $NPM install
  $NODE $NPM install duniter-bma --save --production
  $NODE $NPM install duniter-crawler --save --production
  $NODE $NPM install duniter-keypair --save --production
  $NODE $NPM install duniter-prover --save --production
  $NODE $NPM install duniter-ui --production --save
  return
}

#
# Detect profile file if not specified as environment variable
# (eg: PROFILE=~/.myprofile)
# The echo'ed path is guaranteed to be an existing file
# Otherwise, an empty string is returned
#
detect_profile() {<commit_msg>[fix] Wrong installation for v1.2.6<commit_after>#!/bin/bash

{ # this ensures the entire script is downloaded #

is_installed() {
  type "$1" > /dev/null 2>&1
}

if [ -z "$DUNITER_DIR" ]; then
  DUNITER_DIR="$HOME/.duniter"
fi

latest_version() {
  echo "v1.2.6"
}

repo_url() {
  echo "https://github.com/duniter/duniter.git"
}

download() {
  if is_installed "curl"; then
    curl -qkL $*
  elif is_installed "wget"; then
    # Emulate curl with wget
    ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \
                           -e 's/-L //' \
                           -e 's/-I /--server-response /' \
                           -e 's/-s /-q /' \
                           -e 's/-o /-O /' \
                           -e 's/-C - /-c /')
    wget $ARGS
  fi
}

install_from_git() {

  local PREVIOUS_PATH
  PREVIOUS_PATH=$PATH
  if [ -d "$DUNITER_DIR/.git" ]; then
    echo "=> duniter is already installed in $DUNITER_DIR, trying to update using git"
    printf "\r=> "
    cd "$DUNITER_DIR" && (command git fetch 2> /dev/null || {
      echo >&2 "Failed to update duniter, run 'git fetch' in $DUNITER_DIR yourself." && exit 1
    })
  else
    # Cloning to $DUNITER_DIR
    echo "=> Downloading duniter from git to '$DUNITER_DIR'"
    printf "\r=> "
    mkdir -p "$DUNITER_DIR"
    command git clone "$(repo_url)" "$DUNITER_DIR"
  fi
  cd "$DUNITER_DIR" && command git checkout --quiet $(latest_version)
  if [ ! -z "$(cd "$DUNITER_DIR" && git show-ref refs/heads/master)" ]; then
    if git branch --quiet 2>/dev/null; then
      cd "$DUNITER_DIR" && command git branch --quiet -D master >/dev/null 2>&1
    else
      echo >&2 "Your version of git is out of date. Please update it!"
      cd "$DUNITER_DIR" && command git branch -D master >/dev/null 2>&1
    fi
  fi

  # Download Nodejs
  local NVER="6.9.4";
  local ARCH="x86"
  local X64=`uname -a | grep "x86_64"`
  local ARM=`uname -a | grep "arm"`
  if [ ! -z "$X64" ]; then
    ARCH="x64"
  fi
  # ARM processors
  if [ ! -z "$ARM" ]; then
    ARCH="`uname -m`"
  fi
  local NODEJS_FILENAME=node-v${NVER}-linux-${ARCH}
  local NODEJS_TARBALL=http://nodejs.org/dist/v${NVER}/${NODEJS_FILENAME}.tar.gz
  local NODEJS_ARCHIVE=$DUNITER_DIR/node.tar.gz
  local NODEJS_EXTRACTED=$DUNITER_DIR/$NODEJS_FILENAME
  if [ ! -d "$DUNITER_DIR/node" ]; then
    echo "=> Downloading '$NODEJS_TARBALL' to '$NODEJS_ARCHIVE'"
    download "$NODEJS_TARBALL" -o "$NODEJS_ARCHIVE" || {
      echo >&2 "Failed to download '$NODEJS_TARBALL'"
      return 4
    }
    tar xzf $NODEJS_ARCHIVE || {
      echo >&2 "Failed to extract '$NODEJS_ARCHIVE'"
      return 5
    }
    mv $NODEJS_FILENAME "node" || {
      echo >&2 "Failed to extract '$NODEJS_ARCHIVE'"
      return 6
    }
  fi

  # Install Duniter dependencies (NPM modules)
  NODE=$DUNITER_DIR/node/bin/node
  NPM=$DUNITER_DIR/node/bin/npm
  $NODE $NPM install
  $NODE $NPM install duniter-bma@1.1.2 --save --production
  $NODE $NPM install duniter-crawler@1.1.4 --save --production
  $NODE $NPM install duniter-keypair@1.1.3 --save --production
  $NODE $NPM install duniter-prover@1.1.2 --save --production
  $NODE $NPM install duniter-ui@1.1.5 --production --save
  return
}

#
# Detect profile file if not specified as environment variable
# (eg: PROFILE=~/.myprofile)
# The echo'ed path is guaranteed to be an existing file
# Otherwise, an empty string is returned
#
detect_profile() {
{'fraction_non_alphanumeric': 0.14907107048068416, 'fraction_numerical': 0.013270421704511943, 'mean_word_length': 2.5273010920436816, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2294722', 'n_tokens_mistral': 2583, 'n_tokens_neox': 2474, 'n_words': 676}