#!/bin/bash

version=v4.7.2

# Install dependencies
install_packages bash util-linux procps hostapd iproute2 iw haveged dnsmasq '|' network-manager iptables make gcc g++ build-essential pkg-config libgtk-3-0 libgtk-3-dev libqrencode-dev libpng-dev || exit 1

# Clone and cd into repo
cd /tmp
git_clone -b "$version" https://github.com/lakinduakash/linux-wifi-hotspot || error "Failed to clone repository!"
cd linux-wifi-hotspot || error "Failed to cd into 'linux-wifi-hotspot'!"

# Build and install
status "Building Linux Wifi Hotspot..."
make || error "Failed to build Linux Wifi Hotspot!"
status_green "Done!"
status "Installing Linux Wifi Hotspot..."
sudo make install || error "Failed to install Linux Wifi Hotspot!"
# correct service location, on debian we are not supposed to directly write any files to /usr/lib/systemd/system
sudo mv -f /usr/lib/systemd/system/create_ap.service /lib/systemd/system/create_ap.service
# remove /usr/lib/systemd/system if empty, older debian/ubuntu versions do not use this location and having it present causes issues with other programs
[ ! "$(ls -A /usr/lib/systemd/system)" ] && sudo rm -rf /usr/lib/systemd/system

status_green "Done!"

#Raspberry Pi  WiFi module may be disabled by the panel, and if it is, this program will fail. When launching, first unblock wifi.
if [ -f /usr/sbin/rfkill ] && ! grep -q rfkill /usr/share/applications/wihotspot.desktop ;then
  sudo sed -i "s+^Exec=sh -c '+Exec=sh -c '/usr/sbin/rfkill unblock wifi; +" /usr/share/applications/wihotspot.desktop
fi

rm -rf /tmp/linux-wifi-hotspot/
