#!/bin/bash
#
# Main task dialog.
#
# 0) Start / stop gnunetd
# 1) Start graphical UI (with starting gnunetd if required)
# 2) Start a shell with gnunet commands in path
# 3) Run configuration utility
# 4) Run help
# 5) Download Apple's X11 package
# 6) Check for updates
# 7) View recent log entries

# Locate install directory

ROOTDIR=`dirname $0`

if [ `echo $ROOTDIR | cut -c1` = . ]; then
  ROOTDIR=`pwd`
fi

if [ ! -d $ROOTDIR ]; then
  echo "Can't locate GNUnet rootdir."
  exit 0
fi

export ROOTDIR

. $ROOTDIR/GNUnet-funcs

gnetconf

checkversion

pango

while true; do
  cgnunetd
  if [ $pid != 0 ]; then
    gnet="Stop gnunetd"
  else
    gnet="Start gnunetd"
  fi

  sel=`$CD dropdown --text "Select function" --items "$gnet" "Start graphical UI" "Start a shell for gnunet commands" "Configure" "Help" "Download X11 from Apple" "Check for updates" "View gnunetd log file" --button1 "Run" --button2 "Quit"`
  button=`echo $sel | cut -d\  -f1`
  select=`echo $sel | cut -d\  -f2`

  if [ $button = 2 ]; then
    exit 0
  fi

  if [ $select = 0 ]; then
    cgnunetd
    if [ $pid = 0 ]; then
      $ROOTDIR/bin/gnunetd
    else
      kill $pid
    fi
    sleep 5
  fi

  if [ $select = 1 ]; then
    cgnunetd
    if [ $pid = 0 ]; then
      $ROOTDIR/bin/gnunetd
    fi
    if [ ! -d /usr/X11R6 ]; then
      $CD textbox --title "X11 not installed" --text-from-file $ROOTDIR/doc/gnunetmac-nox.txt --button1 Cancel
    else
      /usr/bin/open-x11 $ROOTDIR/bin/gnunet-gtk
    fi
  fi

  if [ $select = 2 ]; then
    rm -f /tmp/tmp.$$
    cat > /tmp/tmp.$$ <<EOF
#!/bin/bash
PATH=\$PATH:$ROOTDIR/bin; export PATH
exec \`nidump passwd . | egrep ^\$USER: | cut -d: -f10\`
EOF
    chmod +x /tmp/tmp.$$
    /usr/bin/open -a /Applications/Utilities/Terminal.app /tmp/tmp.$$
  fi

  if [ $select = 3 ]; then
    $ROOTDIR/GNUnet-config
  fi

  if [ $select = 4 ]; then
    $CD textbox --title "Welcome" --text-from-file $ROOTDIR/doc/gnunetmac.txt --button1 OK
  fi

  if [ $select = 5 ]; then
    open /Applications/Safari.app http://www.apple.com/macosx/features/x11/download/
  fi

  if [ $select = 6 ]; then
    open /Applications/Safari.app http://www.chem.jyu.fi/~eloranta/GNUnet.html
  fi

  if [ $select = 7 ]; then
    open -e ~/gnunet/logs
  fi
done

# not reached

