#!/bin/sh

# start this script with "osso-xterm -e" for screen output

#user='user'
#passwd='passwd'
## url without https://, e.g.
## my.server.com/owncloud
#oc_url='servername_and_path_to_your_oc'

logfile='/home/user/owncloud.log'
# uncomment next line to disable logging
#logfile='/dev/null'

cfgfile='/home/user/.simple_oc.cfg'

if [ -e $cfgfile ]
then
    . $cfgfile
else
    echo "### Initial configuration ###"
    echo "Please enter your owncloud base dir (e.g. /home/user/oc)"
    read oc_dir

    [ -d "$oc_dir" ] || {
	echo "Creating base dir $oc_dir"
	mkdir -p "$oc_dir"
    }

    echo "Please enter your username!"
    read user
    echo "Please enter your password!"
    echo "(please be aware that it will be stored in plaintext)"
    read passwd
    echo "Please enter the oc url without https:// (e.g. my.server.com/owncloud)!"
    read oc_url

    touch $cfgfile
    chmod 700 $cfgfile
    echo "oc_dir=\"$oc_dir\"" >> $cfgfile
    echo "user=\"$user\"" >> $cfgfile
    echo "passwd=\"$passwd\"" >> $cfgfile
    echo "oc_url=\"$oc_url\"" >> $cfgfile

    echo "The information is stored in the file"
    echo "$cfgfile"
    echo "Please delete this file to run this dialog again."
    echo "Return to continue and start the first sync"
    read a

fi


{
    owncloudcmd "${oc_dir}" \
	"ownclouds://${user}:${passwd}@${oc_url}/remote.php/webdav"
    retval=$?
    if [ $retval != 0 ]; then
	echo "owncloudcmd exited with return code $retval!"
	echo "Return to continue"
	read dummy
    else
	echo "owncloudcmd exited normally."
    fi
} 2>&1 | tee -a "$logfile"

