#!/bin/bash

set -e

if [ -e /etc/wpa_supplicant/wpa_supplicant.conf ]; then
    sudo mv /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf.bak
    reboot=true
fi
  
echo "Installing WiFi access point dependencies, configs, scripts and services..."
yes | pacman -Rdd linux-firmware
echo "Updating the system and installing packages..."
yes | pacman -Syu hostapd dnsmasq net-tools iw wireless_tools unzip linux-firmware-broadcom
push /opt/3dprinteros/
echo "Downloading configs, scripts and services"
curl -O https://builds.3dprinteros.com/wifi_ap.zip
echo "Unpacking configs, scripts and services"
unzip wifi_ap.zip
rm wifi_ap.zip
echo "Installation complete"
if [ -z "$reboot" ]; then
    popd
else
    sudo reboot
fi
