<commit_before>#!/usr/bin/env bash
# shellcheck source=/etc/openhabian.conf disable=SC1091

CONFIGFILE="/etc/openhabian.conf"

# apt/dpkg commands will not try interactive dialogs
export DEBIAN_FRONTEND="noninteractive"
export SILENT="1"

# Log everything to file
exec &> >(tee -a "/boot/first-boot.log")

# Log with timestamp
timestamp() { printf "%(%F_%T_%Z)T\\n" "-1"; }

fail_inprogress() {
  rm -f /opt/openHABian-install-inprogress
  touch /opt/openHABian-install-failed
  echo -e "$(timestamp) [openHABian] Initial setup exiting with an error!\\n\\n"
  exit 1
}

###### start ######
sleep 5
echo -e "\\n\\n$(timestamp) [openHABian] Starting the openHABian initial setup."
rm -f /opt/openHABian-install-failed
touch /opt/openHABian-install-inprogress

echo -n "$(timestamp) [openHABian] Storing configuration... "
if ! cp /boot/openhabian.conf "$CONFIGFILE"; then echo "FAILED (copy)"; fail_inprogress; fi
if ! sed -i 's|\r$||' "$CONFIGFILE"; then echo "FAILED (Unix line endings)"; fail_inprogress; fi
if ! source "$CONFIGFILE"; then echo "FAILED (source config)"; fail_inprogress; fi
if ! source "/opt/openhabian/functions/helpers.bash"; then echo "FAILED (source helpers)"; fail_inprogress; fi
if ! source "/opt/openhabian/functions/wifi.bash"; then echo "FAILED (source wifi)"; fail_inprogress; fi
if source "/opt/openhabian/functions/openhabian.bash"; then echo "OK"; else echo "FAILED (source openhabian)"; fail_inprogress; fi

if [[ "${debugmode:-on}" == "on" ]]; then
  unset SILENT
  unset DEBUGMAX
elif [[ "${debugmode:-on}" == "maximum" ]]; then
  echo "$(timestamp) [openHABian] Enable maximum debugging output"
  export DEBUGMAX=1
  set -x
fi

rfkill unblock wifi   # Wi-Fi is blocked by Raspi OS default since bullseye(?)

echo -n "$(timestamp) [openHABian] Starting webserver with installation log... "
if [[ -x $(command -v python3) ]]; then
  bash /boot/webserver.bash "start"
  sleep 5
  isWebRunning="$(ps -ef | pgrep python3)"
  if [[ -n $isWebRunning ]]; then echo "OK"; else echo "FAILED"; fi
else
  echo "SKIPPED (Python not found)"
fi

defaultUserAndGroup="openhabian"
userName="${username:-openhabian}"
groupName="${userName}"
if is_raspbian || is_raspios; then
  defaultUserAndGroup="pi"
  rm -f "/etc/sudoers.d/010_pi-nopasswd"
fi

echo -n "$(timestamp) [openHABian] Changing default username and password... "
# shellcheck disable=SC2154
if [[ -v ${userName} ]] || ! id "$defaultUserAndGroup" &> /dev/null || id "$userName" &> /dev/null; then
  echo "SKIPPED"
else
  usermod -l "$userName" "$defaultUserAndGroup"
  usermod -m -d "/home/$userName" "$userName"
  groupmod -n "$groupName" "$defaultUserAndGroup"
  echo "${userName}:${userpw:-$userName}" | chpasswd
  echo "OK"
fi

# While setup: show log to logged in user, will be overwritten by openhabian-setup.sh
echo "watch cat /boot/first-boot.log" > "$HOME/.bash_profile"

# else Raspi OS will fire up another (private) IP address see #1456
echo "noipv4ll" >> /etc/dhcpcd.conf

# shellcheck source=/etc/openhabian.conf disable=SC2154
if [[ -z $wifi_ssid ]]; then
  # Actually check if ethernet is working
  echo -n "$(timestamp) [openHABian] Setting up Ethernet connection... "
  if grep -qs "up" /sys/class/net/eth0/operstate; then echo "OK"; else echo "FAILED"; fi

  if ! running_in_docker && tryUntil "ping -c1 8.8.8.8 &> /dev/null || curl --silent --head http://www.openhab.org/docs |& grep -qs 'HTTP/1.1 200 OK'" 5 1; then
    if [[ "$hotspot" == "enable" ]] && ! [[ -x $(command -v comitup) ]]; then
      echo -n "$(timestamp) [openHABian] Installing comitup hotspot (will reboot after)... "
      setup_hotspot "install"
      cp "${BASEDIR:-/opt/openhabian}"/includes/interfaces /etc/network/
      echo "$(timestamp) [openHABian] hotspot software installed. Rebooting your system to make it take effect!"
      reboot
    fi
  fi
elif grep -qs "openHABian" /etc/wpa_supplicant/wpa_supplicant.conf && ! grep -qsE "^[[:space:]]*dtoverlay=(pi3-)?disable-wifi" /boot/config.txt; then
  echo -n "$(timestamp) [openHABian] Checking if WiFi is working... "
  if iwlist wlan0 scan |& grep -qs "Interface doesn't support scanning"; then
    # WiFi might be blocked
    rfkill unblock wifi
    ip link set wlan0 up
    if iwlist wlan0 scan |& grep -qs "Interface doesn't support scanning"; then
      echo "FAILED"
      echo -e "\\nI was not able to turn on the WiFi\\nHere is some more information:\\n"
      rfkill list all
      ip a
      echo -e "FAILED.\\n$(timestamp) [openHABian] Starting hotspot as a desperate last attempt; see if you can connect there...\\n"
    else
      echo "OK"
    fi
  else
    echo "OK"
  fi
  if tryUntil "ping -c1 8.8.8.8 &> /dev/null || curl --silent --head http://www.openhab.org/docs |& grep -qs 'HTTP/1.1 200 OK'" 5 1; then
    if [[ "$hotspot" == "enable" ]] && ! [[ -x $(command -v comitup) ]]; then
      echo -n "$(timestamp) [openHABian] Installing comitup hotspot (will reboot after)... "
      setup_hotspot "install"
      cp "${BASEDIR:-/opt/openhabian}"/includes/interfaces /etc/network/
      echo "$(timestamp) [openHABian] hotspot software installed. Rebooting your system to make it take effect!"
      reboot
    fi
  fi
else
  echo -n "$(timestamp) [openHABian] Setting up Wi-Fi connection... "

  wifiCountry="$wifi_country"
  wifiSSID="$wifi_ssid"
  wifiPassword="$<PASSWORD>"<commit_msg>Remove redundant Wi-Fi unblock<commit_after>#!/usr/bin/env bash
# shellcheck source=/etc/openhabian.conf disable=SC1091

CONFIGFILE="/etc/openhabian.conf"

# apt/dpkg commands will not try interactive dialogs
export DEBIAN_FRONTEND="noninteractive"
export SILENT="1"

# Log everything to file
exec &> >(tee -a "/boot/first-boot.log")

# Log with timestamp
timestamp() { printf "%(%F_%T_%Z)T\\n" "-1"; }

fail_inprogress() {
  rm -f /opt/openHABian-install-inprogress
  touch /opt/openHABian-install-failed
  echo -e "$(timestamp) [openHABian] Initial setup exiting with an error!\\n\\n"
  exit 1
}

###### start ######
sleep 5
echo -e "\\n\\n$(timestamp) [openHABian] Starting the openHABian initial setup."
rm -f /opt/openHABian-install-failed
touch /opt/openHABian-install-inprogress

echo -n "$(timestamp) [openHABian] Storing configuration... "
if ! cp /boot/openhabian.conf "$CONFIGFILE"; then echo "FAILED (copy)"; fail_inprogress; fi
if ! sed -i 's|\r$||' "$CONFIGFILE"; then echo "FAILED (Unix line endings)"; fail_inprogress; fi
if ! source "$CONFIGFILE"; then echo "FAILED (source config)"; fail_inprogress; fi
if ! source "/opt/openhabian/functions/helpers.bash"; then echo "FAILED (source helpers)"; fail_inprogress; fi
if ! source "/opt/openhabian/functions/wifi.bash"; then echo "FAILED (source wifi)"; fail_inprogress; fi
if source "/opt/openhabian/functions/openhabian.bash"; then echo "OK"; else echo "FAILED (source openhabian)"; fail_inprogress; fi

if [[ "${debugmode:-on}" == "on" ]]; then
  unset SILENT
  unset DEBUGMAX
elif [[ "${debugmode:-on}" == "maximum" ]]; then
  echo "$(timestamp) [openHABian] Enable maximum debugging output"
  export DEBUGMAX=1
  set -x
fi

rfkill unblock wifi   # Wi-Fi is blocked by Raspi OS default since bullseye(?)

echo -n "$(timestamp) [openHABian] Starting webserver with installation log... "
if [[ -x $(command -v python3) ]]; then
  bash /boot/webserver.bash "start"
  sleep 5
  isWebRunning="$(ps -ef | pgrep python3)"
  if [[ -n $isWebRunning ]]; then echo "OK"; else echo "FAILED"; fi
else
  echo "SKIPPED (Python not found)"
fi

defaultUserAndGroup="openhabian"
userName="${username:-openhabian}"
groupName="${userName}"
if is_raspbian || is_raspios; then
  defaultUserAndGroup="pi"
  rm -f "/etc/sudoers.d/010_pi-nopasswd"
fi

echo -n "$(timestamp) [openHABian] Changing default username and password... "
# shellcheck disable=SC2154
if [[ -v ${userName} ]] || ! id "$defaultUserAndGroup" &> /dev/null || id "$userName" &> /dev/null; then
  echo "SKIPPED"
else
  usermod -l "$userName" "$defaultUserAndGroup"
  usermod -m -d "/home/$userName" "$userName"
  groupmod -n "$groupName" "$defaultUserAndGroup"
  echo "${userName}:${userpw:-$userName}" | chpasswd
  echo "OK"
fi

# While setup: show log to logged in user, will be overwritten by openhabian-setup.sh
echo "watch cat /boot/first-boot.log" > "$HOME/.bash_profile"

# else Raspi OS will fire up another (private) IP address see #1456
echo "noipv4ll" >> /etc/dhcpcd.conf

# shellcheck source=/etc/openhabian.conf disable=SC2154
if [[ -z $wifi_ssid ]]; then
  # Actually check if ethernet is working
  echo -n "$(timestamp) [openHABian] Setting up Ethernet connection... "
  if grep -qs "up" /sys/class/net/eth0/operstate; then echo "OK"; else echo "FAILED"; fi

  if ! running_in_docker && tryUntil "ping -c1 8.8.8.8 &> /dev/null || curl --silent --head http://www.openhab.org/docs |& grep -qs 'HTTP/1.1 200 OK'" 5 1; then
    if [[ "$hotspot" == "enable" ]] && ! [[ -x $(command -v comitup) ]]; then
      echo -n "$(timestamp) [openHABian] Installing comitup hotspot (will reboot after)... "
      setup_hotspot "install"
      cp "${BASEDIR:-/opt/openhabian}"/includes/interfaces /etc/network/
      echo "$(timestamp) [openHABian] hotspot software installed. Rebooting your system to make it take effect!"
      reboot
    fi
  fi
elif grep -qs "openHABian" /etc/wpa_supplicant/wpa_supplicant.conf && ! grep -qsE "^[[:space:]]*dtoverlay=(pi3-)?disable-wifi" /boot/config.txt; then
  echo -n "$(timestamp) [openHABian] Checking if WiFi is working... "
  if iwlist wlan0 scan |& grep -qs "Interface doesn't support scanning"; then
    ip link set wlan0 up
    if iwlist wlan0 scan |& grep -qs "Interface doesn't support scanning"; then
      echo "FAILED"
      echo -e "\\nI was not able to turn on the WiFi\\nHere is some more information:\\n"
      rfkill list all
      ip a
      echo -e "FAILED.\\n$(timestamp) [openHABian] Starting hotspot as a desperate last attempt; see if you can connect there...\\n"
    else
      echo "OK"
    fi
  else
    echo "OK"
  fi
  if tryUntil "ping -c1 8.8.8.8 &> /dev/null || curl --silent --head http://www.openhab.org/docs |& grep -qs 'HTTP/1.1 200 OK'" 5 1; then
    if [[ "$hotspot" == "enable" ]] && ! [[ -x $(command -v comitup) ]]; then
      echo -n "$(timestamp) [openHABian] Installing comitup hotspot (will reboot after)... "
      setup_hotspot "install"
      cp "${BASEDIR:-/opt/openhabian}"/includes/interfaces /etc/network/
      echo "$(timestamp) [openHABian] hotspot software installed. Rebooting your system to make it take effect!"
      reboot
    fi
  fi
else
  echo -n "$(timestamp) [openHABian] Setting up Wi-Fi connection... "

  wifiCountry="$wifi_country"
  wifiSSID="$wifi_ssid"
  wifiPassword="$<PASSWORD>"
{'fraction_non_alphanumeric': 0.1509256648811202, 'fraction_numerical': 0.010713278827177897, 'mean_word_length': 3.787224471434998, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 4, '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': '5899759', 'n_tokens_mistral': 3778, 'n_tokens_neox': 3630, 'n_words': 1127}