#!/bin/sh -e
#
#   /etc/init.d/zxdsl-update: compiles and installs driver for unicorn_usb_eth

. /lib/lsb/init-functions
options_file=/etc/modprobe.d/options

clean_options_file()
{
    temp_options=`mktemp`
    if [ $? -ne 0 ]; then
        log_failure_msg "Failed to create temporary file" 1>&2
        return 1
    fi
    if [ -r "$options_file" ]; then
        cat "$options_file" | egrep -v "^[[:space:]]*options[[:space:]]+unicorn_usb_eth">"$temp_options"
        if [ $? -eq 2 ]; then
            log_failure_msg "Failed to copy options file" 1>&2
            return 1
        fi
    else
        echo -n >"$temp_options"
        if [ $? -ne 0 ]; then
            log_failure_msg "Failed to copy options file" 1>&2
            return 1
        fi
    fi
    cat "$temp_options"
    if [ $? -ne 0 ]; then
        log_failure_msg "Failed to read options file" 1>&2
        return 1
    fi
    rm -f "$temp_options" >/dev/null
}

install_driver()
{
    modules_dir=/lib/modules/`uname -r`
    if [ -f $modules_dir/extra/unicorn_usb_eth.ko ]; then
        log_success_msg "Driver unicorn_usb_eth already installed"
        return 0
    fi
    compile_dir=`mktemp -d`
    if [ $? -ne 0 ]; then
        log_failure_msg "Failed to create temporary directory"
        return 1
    fi
    PREVIOUS_DIR=`pwd`
    cd "$compile_dir"
    tar zxf /root/drivers/unicorn-neo.tar.gz
    if [ $? -ne 0 ]; then
        log_failure_msg "Failed to unpack drivers package"
        return 1
    fi
    make 
    if [ $? -ne 0 ]; then
        log_failure_msg "Failed to compile driver"
        return 1
    fi
    make install
    if [ $? -ne 0 ]; then
        log_failure_msg "Failed to install driver"
        return 1
    fi
    #remove any previous options
    new_contents=`clean_options_file`
    if [ $? -ne 0 ]; then
        log_failure_msg "Failed to modify options file"
        return 1
    else
        echo "$new_contents">"$options_file"
    fi
    echo "options unicorn_usb_eth ActivationMode=3 VPI=0 VCI=35 PROTOCOL=pppoatm ENCAPS=vc-encaps"  >>"$options_file"
    if [ $? -ne 0 ]; then
        log_failure_msg "Failed to append options to options file"
        return 1
    fi
    
    cd "$PREVIOUS_DIR"
    rm -rf "$compile_dir"
    log_success_msg "Driver unicorn_usb_eth installed"
}


remove_driver()
{
    modules_dir=/lib/modules/`uname -r`
    if [ -f "$modules_dir/extra/unicorn_usb_eth.ko" ]; then
        if rm -f "$modules_dir/extra/unicorn_usb_eth.ko"; then
            log_progress_msg "Driver unicorn_usb_eth removed"
        else
            log_failure_msg "Driver unicorn_usb_eth not removed"
        fi
    else
        log_progress_msg "Driver unicorn_usb_eth not present, so not removed"
    fi
    new_contents=`clean_options_file`
    if [ $? -ne 0 ]; then
        log_failure_msg "Failed to modify options file"
        return 1
    else
        echo "$new_contents">"$options_file"
    fi
    
    log_success_msg "Driver unicorn_usb_eth removed"
}

case "$1" in
start)
    log_begin_msg "Installing unicorn_usb_eth driver..."
    install_driver
    log_end_msg $?
    ;;
stop)
    ;;
restart|force-reload)
    ;;
remove)
    remove_driver
    ;;
reinstall)
    remove_driver
    install_driver
    ;;
*)
    log_success_msg "Usage: $0 {start|stop|restart|force-reload|remove}"
    exit 1
    ;;
esac

exit 0
