#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Description: This program is used to start boinc service in DRBL live environment.

#
. /opt/drbl/sbin/drbl-conf-functions

# config file
BOINC_CONF=/etc/drbl/boinc-url-authkey.conf

#
if [ ! -e "$BOINC_CONF" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "BOINC url and authrized key file $BOINC_CONF was not found!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "Program terminated!"
  exit 1
fi

#
ask_and_load_lang_set

# config network first:
config_drbl_live_network

#
echo "Now adding BOINC project(s)..."
while read url auth; do
  [ -n "$(echo $url | grep -E "^#")" ] && continue  # skip the comment lines
  if [ -n "$url" -a -n "$auth" ]; then
     echo "Attaching project $url..."
     boinc_cmd --project_attach $url $auth
  fi
done < $BOINC_CONF

# 
echo "Hostname for this machine ?"
hostname_def="DRBL$(LANG=C date +%m%d)"
echo -n "[$hostname_def] "
read HN

[ -z "$HN" ] && HN="$hostname_def"
hostname $HN
echo "$HN" > /etc/hostname

#
drbl-live.sh --skip-pause-in-the-end start

# Set all of clients as text mode
drbl-client-switch -nl remote-linux-txt

# Turn on boinc-client service
drbl-client-service boinc-client on

#
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_all_set_you_can_turn_on_clients"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n $msg_press_enter_to_continue
# This is a weird problem about tftpd-hpa when it is restared in 'drbl-all-service start' in the above. It's:
# -------
# /etc/init.d/tftpd-hpa restart
# echo -n "press key... "
# read
# -------
# and we always get such an error when running it:
# -------
# Restarting HPA's tftpd: in.tftpd.
# press key... ./td: line 4: read: read error: 0: Resource temporarily unavailable
# -------
# Therefore we use < /dev/stdin to avoid tihs.
read dummy_var < /dev/stdin
