#!/bin/zsh

# dyne:II graphical mount for samba volumes
# (C) 2006 Denis "jaromil" Rojo

# requirements: gtkdialog, samba and an xterm

# This source code is free software; you can redistribute it and/or
# modify it under the terms of the GNU Public License as published 
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This source code 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.
# Please refer to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA

source /lib/dyne/utils.sh

if [ -z $DISPLAY ]; then
  error "no DISPLAY found"
fi



if [ $1 ]; then
# called with arguments: don't prompt dialog but make the mount!
# commandline argument can be in the following format:
# username@server:mount

  username=`echo $1|cut -d@ -f1`
  host=`echo $1|cut -d@ -f2`
  server=`echo $host|cut -d: -f1`
  mount=`echo $host|cut -d: -f2`

  mountpoint="/mnt/shares/$username@$server_$mount"

  echo "connecting to: $server"
  /bin/ping $server
  if [ $? = 1 ]; then
    echo "host is unreachable, operation aborted."
    sleep 5
    exit 1
  fi


  sync

  mkdir -p $mountpoint
  echo "login as user ${username}"
  sudo smbmount "//$server/$mount" $mountpoint -o username=$username

  if [ "`is_mounted $mountpoint`" = "true" ]; then
    echo "shared volume mounted succesfully!"
    rox $mountpoint
  else
    echo "error mounting the shared volume, operation aborted"
    sleep 5
    exit 1
  fi

  exit 0 

fi


MAIN_DIALOG="
<vbox>

<frame Mount Samba shared volume>

<vbox>

<text>
  <label>Your username:</label>
</text>

<entry>
  <variable>username</variable>
</entry>

<text>
  <label>server address:</label>
</text>

<entry>
  <variable>new_host</variable>
</entry>

<text>
  <label>shared volume:</label>
</text>

<entry>
  <variable>remote_volume</variable>
</entry>

</vbox>

<hbox>
  <button ok></button>
  <button cancel></button>
</hbox>

</frame>
</vbox>
"

export MAIN_DIALOG

export `gtkdialog --program=MAIN_DIALOG`

if [ "$EXIT" = "\"Cancel\"" ]; then
  echo "operation aborted."
  exit 0
fi

if [ $new_host = "\"\"" ]; then
  error_dialog "server address missing, operation aborted"
  exit 0
fi

# strip the quotes
server=`echo $new_host | sed s/\"//g`
username=`echo $username | sed s/\"//g`
volume=`echo $remote_volume | sed s/\"//g`
# mountpoint="/mnt/shares/$username@$server"

xterm -fn "-*-lucidatypewriter-*-*-*-*-18-*-*-*-*-*-*-*" -geometry 76x15 \
      -bg black -fg green -e "$0 ${username}@${server}:${volume}" &

exit 1

