#!/bin/bash

# Bash installation script for UNIX systems only
# Use it : curl -sSL https://raw.githubusercontent.com/sundowndev/phoneinfoga/master/support/scripts/install | bash

os="$(uname -s)_$(uname -m)"

if [ $os == "Linux_x86_64" ] || [ $os == "Linux_armv6" ] || [ $os == "Darwin_x86_64" ] || [ $os == "Darwin_arm64" ] || [ $os == "Linux_arm64" ] || [ $os == "Linux_i386" ]; then
  echo "Installing PhoneInfoga"
  phoneinfoga_version=$(curl -s https://api.github.com/repos/sundowndev/phoneinfoga/releases/latest | grep tag_name | cut -d '"' -f 4)
  echo "Found version $phoneinfoga_version"

  echo "Downloading version $phoneinfoga_version..."
  wget "https://github.com/sundowndev/phoneinfoga/releases/download/$phoneinfoga_version/phoneinfoga_$os.tar.gz"

  echo "Verifying checksum..."
  curl -sSL "https://github.com/sundowndev/phoneinfoga/releases/download/$phoneinfoga_version/phoneinfoga_checksums.txt" -o phoneinfoga_SHA256SUMS
  sha256sum --ignore-missing -c phoneinfoga_SHA256SUMS
  [ $? -eq 0 ] || exit 1

  tar xfv "phoneinfoga_$os.tar.gz"
  [ $? -eq 0 ] || exit 1
  
  # Clear downloaded assets
  rm phoneinfoga_$os.tar.gz
  rm phoneinfoga_SHA256SUMS

  echo "Installation completed successfully."
  echo "Check the version : ./phoneinfoga version"
  echo "You can now install the program globally : sudo mv ./phoneinfoga /usr/bin/phoneinfoga"
else
  echo "Your OS/Arch is not supported."
  echo "Read more at https://sundowndev.github.io/phoneinfoga/install/"
  exit 1
fi

exit 0

