#!/bin/bash

#########################################################################
# This program is free software: you can redistribute it and/or modify  #
# it under the terms of the version 3 of the GNU General Public License #
# as published by the Free Software Foundation.                         #
#                                                                       #
# This program 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. See the GNU      #
# General Public License for more details.                              #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with this program. If not, see <http://www.gnu.org/licenses/>.  #
#                                                                       #
# Written by and Copyright (C) Francois Fleuret                         #
# Contact <francois@fleuret.org> for comments & bug reports             #
#########################################################################

# A command-line tool to restart, shutdown or get the status of a
# D-Link DNS323.

set -e

function rm_temp () {
    if [[ ${TEMP} ]]; then
        rm ${TEMP}
        unset TEMP
    fi
}

function authentify_on_dns323 () {
    if [[ ! ${already_authentified} ]]; then
        echo "Authentifying on ${DNS323_HOSTNAME}."
        curl -s > /dev/null \
            -L http://${DNS323_HOSTNAME}/goform/formLogin \
            -d "f_LOGIN_NAME=admin&f_LOGIN_PASSWD=${DNS323_ADMIN_PASSWORD}&f_login_type=0" || (echo "Failed." >&2 && exit 1)
        already_authentified=1
    fi
}

function check_unmounted () {
    if [[ ! ${force_operation} ]]; then
        if [[ $(mount | grep ^//${DNS323_HOSTNAME}) ]]; then
            echo "//${DNS323_HOSTNAME} appears to be mounted." >&2
            exit 1
        fi
    fi
}

function show_help () {
    cat >&2 <<EOF
$(basename $0) [-f|--force] [-h|--help] <status|restart|shutdown> ...

  -h, --help     shows this help.
  -f, --force    forces shutdown or restart even if it looks like the samba
                 drive is mounted.
  status         gets temperature and RAID info.
  shutdown       shuts the unit down.
  restart        restarts the unit.

EOF
}

######################################################################

trap rm_temp SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM ERR

if [[ ${DNS323_HOSTNAME} ]] && \
    [[ ${DNS323_ADMIN_PASSWORD} ]]; then

    while [[ $1 ]]; do

        case $1 in

            ################################################################

            -f|--force)
                force_operation=1
                ;;

            ################################################################

            -h|--help)
                show_help
                ;;

            ################################################################

            status)

                authentify_on_dns323

                # If you think what follows is fugly, please have a
                # look at the DNS323's web app HTML

                TEMP=$(mktemp /tmp/status.XXXXXX)

                curl -s \
                    -L http://${DNS323_HOSTNAME}/goform/adv_status \
                    -d "" > ${TEMP}

                cat ${TEMP} | \
                    html2text | \
                    grep "\(Sync Time Remaining\|Volume Name\|Volume Type\|Total Hard Drive Capacity\|Used Space\|Unused Space\)": | \
                    sed -e "s/^[ \t]*//"

                grep "var temper" ${TEMP} | sed -e "s/^.*\"\([0-9]*\):\([0-9]*\)\".*$/Temperature:                \1F \/ \2C/"

                rm_temp

                ;;

            ################################################################

            shutdown)

                authentify_on_dns323

                check_unmounted

                curl > /dev/null \
                    -s \
                    -L http://${DNS323_HOSTNAME}/goform/sysShutDown \
                    -d ""

                echo "Shutdown initiated."

                ;;

            ################################################################

            restart)

                authentify_on_dns323

                check_unmounted

                curl -s > /dev/null \
                    -L http://${DNS323_HOSTNAME}/goform/System_restart \
                    -d ""

                echo "Restart initiated."

                ;;

            ################################################################

            *)
                echo "Unknown operation $1" >&2
                show_help
                exit 1
                ;;
        esac

        shift

    done

else

    echo "Please set \$DNS323_HOSTNAME and \$DNS323_ADMIN_PASSWORD."

fi
