#!/bin/bash

# install.bash
# 2009-12-03
# Chieh Cheng

  driverArchive="2009_0918_RT2860_Linux_STA_v2.2.0.0.tar.bz2"

function checkOK ()
{
  ok=1

  if [ ! -f "${driverArchive}" ]
  then
    ok=0
    echo "${driverArchvie} not found!"
  fi

  echo "${ok}"
}

function promptToEditFile ()
{
  file="$1"
  administrator="$2"

  if [ -f "${file}" ]
  then
    read -p "Press ENTER to start editing "${file}" . . ."

    if [ "${administrator}" = "y" ]
    then
      sudo vi "${file}"
    else
      vi "${file}"
    fi
  else
    "${file} does not exist."
  fi
}

  ok=`checkOK`

  if [ ${ok} -eq 1 ]
  then
    sudo mount -o remount,rw /
    sudo mkdir /var/cache/apt
    sudo apt-get install module-assistant
    sudo m-a prepare
    tar jxpvf "${driverArchive}"

    echo
    echo "To enable WPA support, change the 'n' in the following"
    echo "two lines to 'y' in config.mk."
    echo "  HAS_WPA_SUPPLICANT=n"
    echo "  HAS_NATIVE_WPA_SUPPLICANT_SUPPORT=n"
    promptToEditFile "2009_0918_RT2860_Linux_STA_v2.2.0.0/os/linux/config.mk" "n"
    echo

    cd 2009_0918_RT2860_Linux_STA_v2.2.0.0/
    sudo make
    sudo make install

    echo
    echo "Add 'rt2860sta' to '/etc/modules' on its own separate line."
    promptToEditFile "/etc/modules" "y"
    echo

    echo "Ralink RT-2700E installation complete."
    echo "Please reboot your computer for the changes to take effect."
  else
    echo "You do not have all of the necessary component to install"
    echo "the Ralink RT-2700E driver. Installation cannot continue."
  fi
