#!/bin/sh
#------------------------------------------------------------------------------
# /var/install/bin/shfs-addhost - add host to shfs 
#
# Copyright (c) 2004 Jens Vehlhaber <jvehlhaber(at)buchenwald(dot)de>
#
# Creation:     23.10.2004 jv
# Last Update:  $Id$
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#------------------------------------------------------------------------------

# include eislib
# /var/install/include/eislib

p_scann=0
n_login=0
host_name=""
host_user="root"
host_port=22
host_dir=""    

### ---------------------------------------------------------------------------
### show header
### ---------------------------------------------------------------------------
show_header() 
{
    clrhome
    colecho "Append host to SSH filesystem" gn
    echo ""
    echo ""
}


### ---------------------------------------------------------------------------
### make a copy from config file
### ---------------------------------------------------------------------------
copy_config_file() 
{
    r_val1=$1
    r_val2=$2
    cat /etc/config.d/shfs | grep -v "$r_val1" | grep -v "$r_val2" > /tmp/shfs.tmp
}


### ---------------------------------------------------------------------------
### append entry to host list
### ---------------------------------------------------------------------------
append_to_hostlist()
{
    n_found=0
    idx=1 
    
    # reade config
    . /etc/config.d/shfs

    # get the idx number 
    if [ $SHFS_MNT_N -gt 0 ] 
    then 
        while [ $idx -le $SHFS_MNT_N ]
        do
            eval r_host='$SHFS_MNT_'$idx'_REMOTE_HOST'
            if [ "$r_host" = "$host_name"  ]
            then
                n_found=$idx
                break
            fi
            idx=`expr $idx + 1`
        done
    fi
    
    # copy config file 
    if [ $n_found -gt 0 ]
    then
        copy_config_file "^SHFS_MNT_${n_found}" "^#"
    else
        n_found=`expr $SHFS_MNT_N + 1`
        copy_config_file "^SHFS_MNT_N=" "^SHFS_MNT_${n_found}"
        echo "SHFS_MNT_N='$n_found'" >> /tmp/shfs.tmp
    fi
    
    # append host data
    (
        echo "SHFS_MNT_${n_found}_REMOTE_HOST='$host_name'"
        echo "SHFS_MNT_${n_found}_REMOTE_USER='$host_user'"
        if [ "$host_user" = "root" ]
        then
            echo "SHFS_MNT_${n_found}_REMOTE_DIR='/'"
        else
            echo "SHFS_MNT_${n_found}_REMOTE_DIR=''"
        fi
        echo "SHFS_MNT_${n_found}_REMOTE_PORT='$host_port'"
        echo "SHFS_MNT_${n_found}_LOCAL='/mnt/$host_name'"
        echo "SHFS_MNT_${n_found}_ON_BOOT='yes'"        
        echo "SHFS_MNT_${n_found}_OPTIONS='persistent'"
    ) >> /tmp/shfs.tmp
    
    # make new config file
    /var/install/config.d/shfs-update.sh /tmp/shfs.tmp
    
    # create mountpoint
    if [ ! -d "/mnt/$host_name" ]
    then
        mkdir /mnt/$host_name
    fi

    # make new fstab file
    /var/install/config.d/shfs.sh fstab    
}


### ---------------------------------------------------------------------------
### get hostname
### ---------------------------------------------------------------------------
get_hostname()
{
    show_header
    echo -n "Input Hostname or IP: "
    read host_name
    echo ""
    echo ""
}


### ---------------------------------------------------------------------------
### get username
### ---------------------------------------------------------------------------
get_username()
{
    if [ ! -z $1 ]
    then
        show_header
    fi
    echo -n "Input login Username: "
    read tmp_name
    echo ""
    echo ""    
    if [ ! -z $tmp_name ]
    then
        host_user=$tmp_name
    fi
    n_login=0       
}


### ---------------------------------------------------------------------------
### get ssh port
### ---------------------------------------------------------------------------
get_ssh_port()
{
    show_header
    echo -n "Input SSH Port (default=22): "
    read host_port
    echo ""
}


### ---------------------------------------------------------------------------
### check host 
### ---------------------------------------------------------------------------
check_host()
{
    /usr/local/shfs/pscan "$host_name" -b $host_port -e $host_port 1>/dev/null
    p_scann=$?
}

### ---------------------------------------------------------------------------
### main
### ---------------------------------------------------------------------------
if_mod=`lsmod | grep "shfs"`
if [ -z "$if_mod" ]
then 
    echo ""
    /usr/local/bin/colecho "Kernelmodul SHFS not loaded" br x br
    echo
    exit 1
fi

get_hostname
get_username
check_host

while [ 1 ] 
do
    check_host
    if [ $p_scann -eq 0 -o ! $n_login -eq 0 ]
    then
        show_header
        if [ $n_login -eq 0 ] 
        then
            colecho "             Host not found" br x br
        else
            colecho "             SSH key not transfered" br x br
        fi    
        echo
        echo
        echo
        echo
        echo
        echo "             1: Change Hostname"
        echo "             2: Change Username"
        echo "             3: Change host SSH port"
        echo
        echo
        echo
        echo
        echo -e "Command (1-3, ENTER=Return) \c"
        read input_select
        case $input_select
        in
            1) get_hostname ;;
            2) get_username new ;;
            3) get_ssh_port ;;
            *) break;;
        esac
    else
        echo "check SSH-key"
        ssh_key="$( grep -nE \* /root/.ssh/id_rsa.pub | sed 's/^[0-9]*://g' )"
        # ssh ${host_user}@${host_name} -i /root/.ssh/id_rsa -p $host_port -o StrictHostKeyChecking=no "mkdir -p -m 700 ~/.ssh | echo $ssh_key >> ~/.ssh/authorized_keys"
        # cat $SSHDIR/$SSHKEY.pub | ssh ${host_user}@${host_name} -i /root/.ssh/id_rsa -p $host_port -o StrictHostKeyChecking=no "bash -c 'mkdir -p -m 0755 ~/.ssh; cat >> ~/.ssh/authorized_keys; chmod 0600 ~/.ssh/authorized_keys'"
        ssh ${host_user}@${host_name} -i /root/.ssh/id_rsa -p $host_port -o StrictHostKeyChecking=no "bash -c 'mkdir -p -m 0700 ~/.ssh; echo $ssh_key >> ~/.ssh/authorized_keys'"
        n_login=$?
        if [ $n_login -eq 0 ]
        then
            append_to_hostlist
            echo
            echo
            if /var/install/bin/ask "Mount host as /mnt/${host_name} now?"
            then
                echo
                # mount:
                # mount /mnt/${host_name}
                # or shfmount:
                if [ "$host_user" = "root" ]
                then
                    host_dir=":/"
                fi
                shfsmount -P $host_port "${host_user}@${host_name}${host_dir}" "/mnt/${host_name}"  
            fi 
            break  
        fi
    fi
done

echo

/var/install/bin/anykey