#!/bin/sh

# This shell script unpacks Linksys' original source tarball and
# installs the modifications to run telnetd.

# This software is released under the GPL.

yesno()
{
  local yes

  while true
  do
    echo -n "$1 (Y/N) "
    read yes
    case $yes in
      y|Y)
        return 0
        ;;

      n|N)
        return 1
        ;;
    esac
  done
}

if [ ! -r NAS200_V34R62.tgz ]
then
  echo "Tarball for Linksys sources not found."
  if yesno "Want me to try download them with wget?"
  then
    wget ftp://ftp.linksys.com/opensourcecode/nas200/v34r62/NAS200_V34R62.tgz
  fi
fi

if [ -r NAS200_V34R62.tgz -a -r nas200_telnetd.tgz ]
then
  if [ -e NAS200_V34R62_GPL ]
  then
    echo "Sources will be unpacked to NAS200_V34R62_GPL directory"
    if yesno "Erase existing file/dir?"
    then
      echo "Erasing NAS200_V34R62_GPL"
      rm -rf NAS200_V34R62_GPL/
    fi
  fi

  if [ ! -e NAS200_V34R62_GPL ]
  then
    echo "Unpacking Linksys sources"
    if tar xfz NAS200_V34R62.tgz
    then
      echo "Unpacking target filesystem"
      if tar xfz NAS200_V34R62_GPL/source/mipsel/target-default.tgz -C NAS200_V34R62_GPL/source/mipsel
      then
        echo "Unpacking Telnetd modifications"
        if tar xfz nas200_telnetd.tgz
        then
          if yesno "Source unpacked. Build now?"
          then
            make -C NAS200_V34R62_GPL
          fi
        else
          echo "Aborting: error unpacking telnetd modifications"
        fi
      else
        echo "Aborting: error unpacking target file system"
      fi
    else
      echo "Aborting: error unpacking Linksys sources"
    fi
  else
    echo "Aborting: directory could not be erased"
  fi
else
  echo "Aborting: not all needed files were found"
fi

