#!/bin/bash

auth_dir="/run/netdrive/auth"
auth_type=`cat $auth_dir/storage`

rm $auth_dir/token > /dev/null 2>&1

if [ -n "$auth_type" ];then
    echo Start authorize $auth_type
    touch $auth_dir/rclone.conf
    rclone authorize --config $auth_dir/rclone.conf --auth-no-open-browser $auth_type > $auth_dir/output
    ret=$?
    if [ $ret -eq 0 ];then
        echo Authorized
    else
        echo Authorize failed
    fi
else
    echo Nothing to authorize
    ret=1
fi

exit $ret
