#!/bin/sh
#
# Remote Drive Share
# Copyright (c) 2010, Intel Corporation.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope 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, write to the Free Software Foundation, Inc., 
# 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
#

IFS=$'\n'
ttyport=""

for match in `dmesg | grep "ttyS[0-3]"`; do
      port0=`echo $match | grep -o "ttyS[0-3]"`
      if [ `echo $match | grep -i "I/O"` ]; then
        if [ `echo $match | grep -i "0x3F8"` ] || [ `echo $match | grep -i "0x2F8"` ] || [ `echo $match | grep -i "0x3E8"` ] || [ `echo $match | grep -i "0x2E8"` ]; then
          :
        else
          ttyport="/dev/"$port0
        fi
      fi
done

getkey()
{
  local __error1=1
  local __error2=1
  local __returnkey=""
  local __keyvar1=""
  local __keyvar2=""
  while [ $__error1 -ne 0 -a $__error2 -ne 0 ]; do
    read -n 1 -t 1 __keyvar1
    __error1=$?
    read -n 1 -t 1 __keyvar2 <$ttyport
    __error2=$?
  done
  if [ ! -z $__keyvar1 ]; then
    __returnkey=$__keyvar1
  elif [ ! -z $__keyvar2 ]; then
    __returnkey=$__keyvar2
  fi
  echo $__returnkey
}

puttext()
{
  local __textvar1=$1
  if [ "$__textvar1" == "clear" ]; then
    clear
    clear >$ttyport
  else
    echo $__textvar1
    echo $__textvar1 >$ttyport
  fi
}

if [ "$ttyport" = "" ]; then
  clear
  echo "  ****************************************************************************"
  echo "  **   Remote Drive Share for PCs featuring Intel(r) vPro(tm) technology    **"
  echo "  **                                                                        **"
  echo "  **      A remote serial connection was not found on this system.          **"
  echo "  **    Remote Drive Share requires this connection and will now halt.      **"
  echo "  ****************************************************************************"
  while true; do
    :
  done
else
  # Let the console know that the iso is booted and running on an AMT machine
  echo "AWK" >$ttyport
fi

ifconfig eth0
if [ $? -ne 0 ]; then
  puttext "clear"
  puttext "  ****************************************************************************"
  puttext "  **   Remote Drive Share for PCs featuring Intel(r) vPro(tm) technology    **"
  puttext "  **                                                                        **"
  puttext "  **   An appropriate Intel network adapter was not found on this system.   **"
  puttext "  **      Remote Drive Share requires this adapter and will now halt.       **"
  puttext "  ****************************************************************************"
  while true; do
    :
  done
fi

if ! [ `fdisk -l | grep "/dev/sda1"` ]; then
  puttext "clear"
  puttext "  ****************************************************************************"
  puttext "  **   Remote Drive Share for PCs featuring Intel(r) vPro(tm) technology    **"
  puttext "  **                                                                        **"
  puttext "  **       No available Device/Partitions were found on this system.        **"
  puttext "  **   Remote Drive Share requires an available device and will now halt.   **"
  puttext "  ****************************************************************************"
  while true; do
    :
  done
fi

if ! [ `ifconfig eth0 | grep "inet addr"` ]; then
  puttext "clear"
  puttext "  ****************************************************************************"
  puttext "  **   Remote Drive Share for PCs featuring Intel(r) vPro(tm) technology    **"
  puttext "  **                                                                        **"
  puttext "  **         A working DHCP server was not found on this network.           **"
  puttext "  **    Remote Drive Share requires a valid IP address and will now halt.   **"
  puttext "  ****************************************************************************"
  while true; do
    :
  done
fi

drivesharernd=`md5sum /proc/meminfo | awk '{ print \$1 }' | sed -e "s/^.\{29\}//g"`

echo "# Global Parameters" >/usr/local/samba/lib/smb.conf
echo "[global]" >>/usr/local/samba/lib/smb.conf
echo "netbios name = driveshare"$drivesharernd >>/usr/local/samba/lib/smb.conf
echo "server name = Samba Server %v" >>/usr/local/samba/lib/smb.conf
echo "workgroup = DriveShare" >>/usr/local/samba/lib/smb.conf
echo "security = share" >>/usr/local/samba/lib/smb.conf
echo "log file = /var/log/samba/log.%m" >>/usr/local/samba/lib/smb.conf
echo "client lanman auth = yes" >>/usr/local/samba/lib/smb.conf
echo "lanman auth = yes" >>/usr/local/samba/lib/smb.conf
echo "smb passwd file = /etc/smbpasswd" >>/usr/local/samba/lib/smb.conf

echo "[drive]" >>/usr/local/samba/lib/smb.conf
echo "path = /mnt" >>/usr/local/samba/lib/smb.conf
echo "read only = no" >>/usr/local/samba/lib/smb.conf
echo "user = root" >>/usr/local/samba/lib/smb.conf

error=99
mounted=0
for match in `fdisk -l | grep "sd[a-d][1-4]"`; do
  mount0=`echo $match | grep -o "sd[a-d][1-4]"`
  mkdir /mnt/$mount0
  if [ `echo $match | grep -i "NTFS"` ]; then
    ntfs-3g /dev/$mount0 /mnt/$mount0 2>/tmp/errors.log
    error=$?
  else
    mount /dev/$mount0 /mnt/$mount0 2>/tmp/errors.log
    error=$?
  fi

  if [ $error -ne 0 ]; then
    umount /mnt/$mount0 2>/tmp/errors.log
    rmdir /mnt/$mount0 2>/tmp/errors.log
  else
    mounted=$((mounted + 1))
  fi
done

if [ $mounted -ne 0 ]; then
  nmbd -D  2>/tmp/errors.log
  error=$?
  if [ $error -eq 0 ]; then
    smbd -D  2>/tmp/errors.log
    error=$?
    if [ $error -ne 0 ]; then
      puttext "clear"
      puttext "  ****************************************************************************"
      puttext "  **   Remote Drive Share for PCs featuring Intel(r) vPro(tm) technology    **"
      puttext "  **                                                                        **"
      puttext "  **                 Could not start Samba SMBD server                      **"
      puttext "  **   Remote Drive Share requires a Samba SMBD server and will now halt.   **"
      puttext "  ****************************************************************************"
    fi
  else
    puttext "clear"
    puttext "  ****************************************************************************"
    puttext "  **   Remote Drive Share for PCs featuring Intel(r) vPro(tm) technology    **"
    puttext "  **                                                                        **"
    puttext "  **                 Could not start Samba NMBD server                      **"
    puttext "  **   Remote Drive Share requires a Samba NMBD server and will now halt.   **"
    puttext "  ****************************************************************************"
  fi

  if [ $error -eq 0 ]; then
    puttext "clear"
    puttext "  ****************************************************************************"
    puttext "  **   Remote Drive Share for PCs featuring Intel(r) vPro(tm) technology    **"
    puttext "  **                                                                        **"
    puttext "  **             Device/Partition(s) currently being shared                 **"
    puttext "  ****************************************************************************"
    puttext ""
    for match in `fdisk -l | grep "Device Boot"`; do
      puttext "     "$match
      break
    done

    var0=0 
    for match in `fdisk -l | grep "/dev/sd[a-d][1-4]"`; do
      mount0=`echo $match | grep -o "sd[a-d][1-4]"`
      var0=$((var0 + 1))
      if [ -d /mnt/$mount0 ]; then
        puttext "  "$var0") "$match
      else
        puttext "  "$var0") /dev/"$mount0"   Error creating a share for this Device" 
      fi
    done

    ipaddr=`ifconfig eth0 | grep inet | awk -F: '{ print $2 }' | awk '{ print $1 }'`

    password=`md5sum /proc/meminfo | awk '{ print \$1 }' | sed -e "s/^.\{24\}//g"`
    (echo $password; echo $password) | pdbedit -a -u root -t >/dev/null

    puttext ""
    puttext "  Use the following path, user, and password to map a drive in Windows"
    puttext "         Path/Share Name:  \\\\"$ipaddr"\\drive"
    puttext "         User Name:        root          Password:      "$password
    puttext "         Workgroup Name:   DriveShare    System Name:   driveshare"$drivesharernd
    puttext ""
    puttext "  Or use the following net share command to map a drive in Windows"
    puttext "  Note: replace 'x:' with an unused share letter for your system"
    puttext "         net use x: \\\\"$ipaddr"\\drive /user:root "$password
    puttext ""
    puttext "  IMPORTANT: At the end of your sharing session, type 'x' to"
    puttext "             save any changes you made to the shared Devices..."

    share=""
    while [ -z $share ]; do
      share=$(getkey)
      if [ "$share" != "x" ]; then
        puttext " You must type 'x' to save changes to the shared drives..."
        share=""
      fi
    done

    puttext "clear"
    puttext "  ****************************************************************************"
    puttext "  **   Remote Drive Share for PCs featuring Intel(r) vPro(tm) technology    **"
    puttext "  **                                                                        **"
    puttext "  **      Share session has finished and Device/Partition(s) unmounted      **"
    puttext "  ****************************************************************************"

    for match in `fdisk -l | grep "sd[a-d][1-4]"`; do
      mount0=`echo $match | grep -o "sd[a-d][1-4]"`
      if [ -d /mnt/$mount0 ]; then
        umount /mnt/$mount0 2>/tmp/errors.log
      fi
    done
  fi
else
  puttext "clear"
  puttext "  ****************************************************************************"
  puttext "  **   Remote Drive Share for PCs featuring Intel(r) vPro(tm) technology    **"
  puttext "  **                                                                        **"
  puttext "  **        Error mounting Devices - No Devices are being shared            **"
  puttext "  ****************************************************************************"
fi

while true; do
  :
done


