#!/bin/bash

dir=/path/to/the/directory/containing/the/script

APP_KEY="xxx"
APP_SECRET="xxx"
ACCESS_TYPE="dropbox"
API_REQUEST_TOKEN_URL="https://api.dropbox.com/1/oauth/request_token"
API_USER_AUTH_URL="https://www.dropbox.com/1/oauth/authorize"
API_ACCESS_TOKEN_URL="https://api.dropbox.com/1/oauth/access_token"
API_FILES_PUT_URL="https://api-content.dropbox.com/1/files_put/dropbox"

CONFIG="$dir/vps-upload.config"
TEMPFILE="/tmp/vps-upload"

if [ -f $CONFIG ];then
  OAUTH_ACCESS_TOKEN_SECRET=`cat $CONFIG | sed -n '1p'`
  OAUTH_ACCESS_TOKEN=`cat $CONFIG | sed -n '2p'`
  OAUTH_ACCESS_UID=`cat $CONFIG | sed -n '3p'`
  TIME_STAMP=`date +%s`
else
  #User authorize the app
  TIME_STAMP=`date +%s`
  curl -s --data "oauth_consumer_key=$APP_KEY&oauth_signature_method=PLAINTEXT&oauth_signature=$APP_SECRET%26&oauth_timestamp=$TIME_STAMP&oauth_nonce=$RANDOM" "$API_REQUEST_TOKEN_URL" -o $TEMPFILE
  AUTHORIZE_TOKEN_SECRET=`cat $TEMPFILE | awk -F'[=&]' '{print $2}'`
  AUTHORIZE_TOKEN=`cat $TEMPFILE | awk -F'[=&]' '{print $NF}'`
  echo "$API_USER_AUTH_URL?oauth_token=$AUTHORIZE_TOKEN"
  cat $TEMPFILE
  read

  #Obtain app access token
  TIME_STAMP=`date +%s`
  curl -s --data "oauth_consumer_key=$APP_KEY&oauth_token=$AUTHORIZE_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APP_SECRET%26$AUTHORIZE_TOKEN_SECRET&oauth_timestamp=$TIME_STAMP&oauth_nonce=$RANDOM" "$API_ACCESS_TOKEN_URL" -o $TEMPFILE
  OAUTH_ACCESS_TOKEN_SECRET=`cat $TEMPFILE | awk -F'[=&]' '{print $2}'`
  OAUTH_ACCESS_TOKEN=`cat $TEMPFILE | awk -F'[=&]' '{print $4}'`
  OAUTH_ACCESS_UID=`cat $TEMPFILE | awk -F'[=&]' '{print $NF}'`

  echo $OAUTH_ACCESS_TOKEN_SECRET > $CONFIG
  echo $OAUTH_ACCESS_TOKEN >> $CONFIG
  echo $OAUTH_ACCESS_UID >> $CONFIG
  rm $TEMPFILE
  exit 0
fi

filename=`echo "$1"|awk -F'/' '{print $NF}'`
DIR=`echo "$1"|awk -F'/' '{print $(NF-1)}'`
let filesize=`du -b $1|awk -F' ' '{print $1}'`

API_CHUNKED_UPLOAD_URL='https://api-content.dropbox.com/1/chunked_upload'
API_COMMIT_CHUNKED_UPLOAD_URL='https://api-content.dropbox.com/1/commit_chunked_upload/dropbox'
let max=150000000
if [ $filesize -gt $max ];then
  let chunk=15000000
  CHUNKFILE=/dev/shm/vps-upload-chunkfile
  DD="dd if=$1 bs=1MB iflag=count_bytes of=$CHUNKFILE"
  $DD count=$chunk
  upload_id=`curl -s -H "Content-Length: $chunk" -T $CHUNKFILE "$API_CHUNKED_UPLOAD_URL?oauth_consumer_key=$APP_KEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APP_SECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$TIME_STAMP&oauth_nonce=$RANDOM" | grep -oP "\"upload_id\": \"[^\"]+\"" | awk -F'"' '{print $(NF-1)}'`
  if [ -n "$upload_id" ];then
    let offset=chunk
    while [ `echo $offset+$chunk|bc` -lt $filesize ];do
      $DD count=$chunk skip=`echo $offset/1000/1000|bc`
      curl -s -H "Content-Length: $chunk" -T $CHUNKFILE "$API_CHUNKED_UPLOAD_URL?upload_id=$upload_id&offset=$offset&oauth_consumer_key=$APP_KEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APP_SECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$TIME_STAMP&oauth_nonce=$RANDOM" -o /dev/null
      let offset=offset+chunk
    done
    $DD skip=`echo $offset/1000/1000|bc`
    curl -s -H "Content-Length: `echo $filesize-$offset|bc`" -T $CHUNKFILE "$API_CHUNKED_UPLOAD_URL?upload_id=$upload_id&offset=$offset&oauth_consumer_key=$APP_KEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APP_SECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$TIME_STAMP&oauth_nonce=$RANDOM" -o /dev/null
    curl -s -X POST "$API_COMMIT_CHUNKED_UPLOAD_URL/$DIR/$filename?upload_id=$upload_id&oauth_consumer_key=$APP_KEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APP_SECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$TIME_STAMP&oauth_nonce=$RANDOM" -o /dev/null
    rm $CHUNKFILE
  else
    echo "Something goes wrong"
  fi
else
  curl -s -H "Content-Length: $filesize" -T "$1" "$API_FILES_PUT_URL/$DIR/$filename?oauth_consumer_key=$APP_KEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APP_SECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$TIME_STAMP&oauth_nonce=$RANDOM" -o /dev/null
fi