#!/bin/bash
# Copyright 2013 Abraham Massry
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
#

curl_installed=`curl --version 2>&1`
protocol='https'
site='wsend.net'
#port='8000'
#if [ "$port" ]; then
#  host=$protocol'://'$site':'$port
#else
host=$protocol'://'$site
#fi
freeSpaceK=`df -k $HOME | tail -n 1 | head -n 1 | awk '{print $4}'`
clientOS=`uname`
echoerr() { 
  echo "$@" 1>&2; 
}
bashInstall () {
 if [ -e "$HOME/.bashrc" ]; then
   als_set=`grep "alias wsend=" $HOME/.bashrc`
   if [ "$als_set" ]; then
     #do nothing
     true
   else
     echo "alias wsend='$HOME/.wsend/wsend'" >> $HOME/.bashrc
     echo "alias wsend='$HOME/.wsend/wsend'" >> $HOME/.bash_profile
   fi
 else
   echo "alias wsend='$HOME/.wsend/wsend'" >> $HOME/.bashrc
   echo "alias wsend='$HOME/.wsend/wsend'" >> $HOME/.bash_profile
 fi
}
cshInstall () {
 if [ -e "$HOME/.cshrc" ]; then
   als_set=`grep "alias wsend " $HOME/.cshrc`
   if [ "$als_set" ]; then
     #do nothing
     true
   else
     echo "alias wsend '$HOME/.wsend/wsend'" >> $HOME/.cshrc
   fi
 else
   echo "alias wsend '$HOME/.wsend/wsend'" >> $HOME/.cshrc
 fi
}

kshInstall () {
 if [ -e "$HOME/.kshrc" ]; then
   als_set=`grep "alias wsend=" $HOME/.kshrc`
   if [ "$als_set" ]; then
     #do nothing
     true
   else
     echo "alias wsend='$HOME/.wsend/wsend'" >> $HOME/.kshrc
   fi
 else
   echo "alias wsend='$HOME/.wsend/wsend'" >> $HOME/.kshrc
 fi
}

zshInstall () {
 if [ -e "$HOME/.zshrc" ]; then
   als_set=`grep "alias wsend=" $HOME/.zshrc`
   if [ "$als_set" ]; then
     #do nothing
     true
   else
     echo "alias -g wsend='$HOME/.wsend/wsend'" >> $HOME/.zshrc
   fi
 else
   echo "alias -g wsend='$HOME/.wsend/wsend'" >> $HOME/.zshrc
 fi
}


registerInfoMessage () {
  echoerr -e "\033[01;36m"
  echoerr "info:    "
  echoerr "info:    It appears you aren't registered"
  echoerr "info:    Registration is free and comes with 2GB of storage space"
  echoerr "info:    Plus get 1G of space for every friend you refer"
  echoerr "info:    "
  echoerr "info:    Sign up now with: wsend --register"
  echoerr "info:    "
  echoerr "info:    unregistered accounts are limited to 200MB"
  echoerr "info:    your unregistered transfer will now continue"
  echoerr "info:    "
  echoerr -e "\033[00m"
}

freeInfoMessage () {
  echoerr -e "\033[01;36m"
  echoerr "info:    "
  echoerr "info:    free accounts are limited to 2GB files"
  echoerr "info:    for more space refer friends with: "
  echoerr "info:    "
  echoerr "info:    wsend --refer friend@example.com"
  echoerr "info:    "
  echoerr "info:    or purchase space at: "
  echoerr "info:    https://wsend.net"
  echoerr "info:    and get up to 10GB files"
  echoerr "info:    "
  echoerr "info:    your free transfer will now continue"
  echoerr "info:    "
  echoerr -e "\033[00m"
}

unregisteredSignUp () {
  echoerr -e "\033[01;36m"
  echoerr "info:    "
  echoerr "info:    creating unregistered account"
  echoerr "info:    Registration is free and comes with 2GB of storage space"
  echoerr "info:    Plus get 1G of space for every friend you refer"
  echoerr "info:    "
  echoerr "info:    Sign up now with: wsend --register"
  echoerr "info:    "
  echoerr "info:    unregistered accounts are limited to 200MB"
  echoerr "info:    your unregistered transfer will now continue"
  echoerr "info:    "
  echoerr -e "\033[00m"
  unregCurlReturnVal=`curl -F "start=1" $host/createunreg 2>/dev/null`
  echo "$unregCurlReturnVal" > $HOME/.wsend/.id
  id=$unregCurlReturnVal
}

notEnoughSpaceErr () {
  echoerr -e "\033[01;31m"
  echoerr "error:   "
  echoerr "error:   not enough space in your account for this transfer"
  echoerr "info:    "
  echoerr "error:   you can register with: wsend --register,"
  echoerr "info:    "
  echoerr "error:   send referrals with wsend --refer friend@example.com, or"
  echoerr "error:   upgrade to a paid account at https://wsend.net"
  echoerr "error:   "
  echoerr -e "\033[00m"
}

filesizeTooLarge () {
  echoerr -e "\033[01;31m"
  echoerr "error:   "
  echoerr "error:   this file is too large for your account"
  echoerr "error:   you can register with: wsend --register"
  echoerr "error:   for 2GB files, or"
  echoerr "error:   upgrade to a paid account at https://wsend.net"
  echoerr "error:   for 10GB files"
  echoerr "error:   "
  echoerr -e "\033[00m"
}

register() {
  echoerr -e "\033[01;36m"
  echoerr "info:    enter email and password to register"
  echoerr -e "\033[00m"
  read -p "email: " entered_email
  stty -echo
  read -p "Password: " passw; echo
  stty echo
  uidFromFile=`cat $HOME/.wsend/.id`
  registrationMessage=`curl -F "uid=$uidFromFile" -F "email=$entered_email" -F "password=$passw" $host/register_cli 2>/dev/null`
  echoerr -e "\033[01;36m"
  echoerr "info:    message from server:"
  echoerr "info:    $registrationMessage"
  echoerr -e "\033[00m"
}

login() {
  echoerr -e "\033[01;36m"
  echoerr "info:    enter email and password to login"
  echoerr -e "\033[00m"
  read -p "email: " entered_email
  stty -echo
  read -p "Password: " passw; echo
  stty echo
  loggedInID=`curl -F "email=$entered_email" -F "password=$passw" $host/login_cli 2>/dev/null`
  if [ $loggedInID ]; then
    echo "$loggedInID" > $HOME/.wsend/.id
    echoerr -e "\033[01;36m"
    echoerr "info:    logged in sucessfully"
    echoerr -e "\033[00m"
  else
    echoerr -e "\033[01;31m"
    echoerr "error:   incorrect email password combo"
    echoerr -e "\033[00m"
  fi
}


getAccountType() { 
  user_type=`curl -F "uid=$id" $host/usertype 2>/dev/null`
}

getAccountSpace() {
  accountSizeAvailable=`curl -F "uid=$id" -F "size=$fileToSendSize" $host/userspaceavailable 2>/dev/null`
  accountSize=`curl -F "uid=$id" $host/userspace 2>/dev/null`
}

refer() {
  if [ $user_type != 'unregistered' ] && [ $user_type != 'unknown' ]; then
    echoerr -e "\033[01;36m"
    echoerr "info:    referring a friend"
    echoerr -ne "\e[00m"
    referEmailStatus=`curl -F "email=$friendEmail" -F "id=$id" $host/refer_cli 2>/dev/null`
    if [ $referEmailStatus == "success" ]; then 
      echoerr -e "\033[01;36m"
      echoerr "info:    friend referred successfully"
      echoerr -e "\033[00m"
    else
      echoerr -e "\033[01;31m"
      echoerr "error:   something went wrong with the referral process";
      echoerr -e "\033[00m"
    fi
  else
    echoerr -e "\033[01;31m"
    echoerr "error:   referrals available to registered accounts";
    echoerr -e "\033[00m"
  fi
}

referLink() {
  if [ $user_type != 'unregistered' ] && [ $user_type != 'unknown' ]; then
    echoerr -e "\033[01;36m"
    echoerr "info:    getting referral link"
    echoerr " "
    echoerr -ne "\e[00m"
    referLinkUrl=`curl -F "id=$id" $host/referlink_cli 2>/dev/null`
    echo $referLinkUrl
  else
    echoerr -e "\033[01;31m"
    echoerr "error:   referrals available to registered accounts";
    echoerr -e "\033[00m"
  fi
  
}

if [ "$curl_installed" ]; then
  #continue
  true
else
  echoerr -e "\033[01;31m"
  echoerr "error:   curl is required but it is not installed. Aborting";
  echoerr "error:   For ubuntu please run: sudo apt-get install curl";
  echoerr -e "\033[00m"
  exit 1;
fi
# check to see if directory exists
if [ -d "$HOME/.wsend" ]; then
  wsend_dir="$HOME/.wsend"
  #check version
  installedVersion=`cat $wsend_dir/version`
  latestVersion=`curl https://raw.github.com/abemassry/wsend/master/version 2>/dev/null`
  if [ "$installedVersion" != "$latestVersion" ]; then
    echoerr -e "\033[01;36m"
    echoerr "info:    "
    echoerr "info:    new version detected, auto updating"
    echoerr "info:    "
    echoerr -e "\033[00m"
    wsDL=`curl -o $HOME/.wsend/wsend https://raw.github.com/abemassry/wsend/master/wsend 2>/dev/null`
    chmod +x $HOME/.wsend/wsend
    #supporting files as well
    rmDL=`curl -o $HOME/.wsend/README.md https://raw.github.com/abemassry/wsend/master/README.md 2>/dev/null`
    cpDL=`curl -o $HOME/.wsend/COPYING https://raw.github.com/abemassry/wsend/master/COPYING 2>/dev/null`
    newLatestVersion=`curl -o $HOME/.wsend/version https://raw.github.com/abemassry/wsend/master/version 2>/dev/null`
  fi
else
  # if not, install
  if [ "$freeSpaceK" -gt 100 ]; then
    mkdir $HOME/.wsend
    #download wsend and put it in directory
    wsDL=`curl -o $HOME/.wsend/wsend https://raw.github.com/abemassry/wsend/master/wsend 2>/dev/null`
    chmod +x $HOME/.wsend/wsend
    #supporting files as well
    rmDL=`curl -o $HOME/.wsend/README.md https://raw.github.com/abemassry/wsend/master/README.md 2>/dev/null`
    cpDL=`curl -o $HOME/.wsend/COPYING https://raw.github.com/abemassry/wsend/master/COPYING 2>/dev/null`
    newLatestVersionDL=`curl -o $HOME/.wsend/version https://raw.github.com/abemassry/wsend/master/version 2>/dev/null`
  else
    echoerr "not enough free space to continue. Aborting";
    exit 1;
  fi
  #add alias to shell
  #execute alias command
  if [ $SHELL == "/bin/bash" ]; then
    bashInstall
  elif [ $SHELL == "/bin/csh" ]; then
    cshInstall
  elif [ $SHELL == "/bin/tcsh" ]; then
    cshInstall
  elif [ $SHELL == "/bin/ksh" ]; then
    kshInstall
  elif [ $SHELL == "/bin/zsh" ]; then
    zshInstall
  fi #install done
fi 
#check for installation fin
#check to see if user is registered
if [ -e "$HOME/.wsend/.id" ]; then
  id=`cat $HOME/.wsend/.id`
  #ask server for account type
  #user_type=`curl -F uid=$id $host/usertype > >(tee /dev/null) 2> >(tee /dev/null >&2)`
  user_type=`curl -F "uid=$id" $host/usertype 2>/dev/null`
  if [ $user_type == 'free' ]; then
    freeInfoMessage
  elif [ $user_type == 'unregistered' ]; then
    registerInfoMessage
  elif [ $user_type == 'unknown' ]; then
    unregisteredSignUp
  fi
else
  echoerr -e "\033[01;36m"
  echoerr "info:    "
  echoerr "info:    Installing and signing up for the first time"
  echoerr "info:    with an unregistered account"
  echoerr "info:    if you already have an account you can log in with:"
  echoerr "info:    "
  echoerr "info:    wsend --login"
  echoerr "info:    "
  echoerr "info:    your transfer will continue"
  echoerr "info:    "
  echoerr -e "\033[00m"
  unregisteredSignUp
  getAccountType
fi

RETVAL=0
loginBool="false"
registerBool="false"
referBool="false"
referLinkBool="false"
fileSendBool="false"
echoUsage="false"
fileOrDirToSend="$1"

if [ "$1" ]; then
  firstArg="$1"
else
  echoUsage="true"
fi

while test -n "$1"; do
  case "$1" in
   --login|-l)
      loginBool="true"
      referLinkBool="false"
      registerBool="false"
      referBool="false"
      fileSendBool="false"
      shift
      ;;
   --register|-r)
      registerBool="true"
      referLinkBool="false"
      loginBool="false"
      fileSendBool="false"
      referBool="false"
      shift
      ;;
   --refer)
      referBool="true"
      referLinkBool="false"
      loginBool="false"
      registerBool="false"
      fileSendBool="false"
      friendEmail=$2
      shift
      shift
      ;;
   --refer-link)
      referLinkBool="true"
      loginBool="false"
      referBool="false"
      registerBool="false"
      fileSendBool="false"
      friendEmail=$2
      shift
      ;;
    *)
      fileSendBool="true"
      loginBool="false"
      registerBool="false"
      referBool="false"
      shift
      ;;
  esac
done

if [ "$loginBool" == "true" ]; then
  login
fi

if [ "$registerBool" == "true" ]; then
  register
fi

if [ "$referBool" == "true" ]; then
  refer
fi

if [ "$referLinkBool" == "true" ]; then
  referLink
fi

#exit $RETVAL


if [ "$fileSendBool" == "true" ]; then
  if [ -d "$fileOrDirToSend" ]; then
    pass=1
    #fileToSendSize=`du -s Downloads/ | awk {'print $1'}`
    #fileToSendSize=$fileToSendSize * 1024
    #getAccountSpace
    #if [ "$accountSizeAvailable" == "not enough space in your account for this transfer" ]; then
    #  notEnoughSpaceErr
    #else
    #  
    #  if [ "$accountSizeAvailable" == "file is too big for your account size" ]; then
    #    filesizeTooLarge
    #  else
    #    #curlReturn=`curl -F "filehandle=@$fileToSend;uid=$id" $host/upload_cli > >(tee $urlOutput) 2> >(tee /dev/null >&2)`
    #    curlReturn=`curl -F "uid=$id" -F "filehandle=@$fileToSend" $host/upload_cli`
    #    echo $curlReturn
    #  fi
    #fi
  else
    fileToSend=$fileOrDirToSend
    if [ "$clientOS" == "Darwin" ]; then
      fileToSendSize=$(stat -f %z "$fileToSend")
    else
      fileToSendSize=$(stat -c%s "$fileToSend")
    fi
    getAccountSpace
    if [ "$accountSizeAvailable" == "not enough space in your account for this transfer" ]; then
      notEnoughSpaceErr
    elif [ "$accountSizeAvailable" == "file is too big for your account size" ]; then
      filesizeTooLarge
    else
      #curlReturn=`curl -F "filehandle=@$fileToSend;uid=$id" $host/upload_cli > >(tee $urlOutput) 2> >(tee /dev/null >&2)`
      curlReturn=`curl -F "uid=$id" -F "filehandle=@$fileToSend" $host/upload_cli`
      echo $curlReturn
    fi
  fi
fi

if [ "$echoUsage" == "true" ]; then
  echoerr -e "\033[01;31m"
  echoerr "error:   "
  echoerr "error:   Usage: $0 [-l|--login] [-r|--register]"
  echoerr "error:   Usage: [--refer friend@example.com]"
  echoerr "error:   Usage: [--refer-link]"
  echoerr "error:   or send a file, example:"
  echoerr "error:   $0 file.txt"
  echoerr "error:   "
  echoerr -e "\033[00m"
fi


syntax highlighted by Code2HTML, v. 0.9.1