Newsgroups: comp.os.linux.announce
From: mustafa@seas.smu.edu (Mustafa Kocaturk)
Subject: K-FTP, Menus to Manage Anonymous FTP Downloads
Message-ID: <1994Aug4.122457.24744@cs.cornell.edu>
Date: Thu, 4 Aug 1994 12:24:57 GMT
Approved: linux-announce@tc.cornell.edu (Lars Wirzenius)

Begin2
Title        = Simple Menu Interface to Manage Anonymous FTP Downloads
Version      = 1.1 (Initial version)
Desc1        = K-FTP is a shell-based tty GUI for ftp logins and maintaining
Desc2        = anonymous ftp logins (including the ~/.netrc file), downloads,
Desc3        = and local directories for different sites.  It includes a file
Desc4        = and directory browsing function, is easy-to-use and customize:
Desc5        = Just read the script and modify certain variable definitions.
Author       = Mustafa Kocaturk
AuthorEmail  = mustafa@seas.smu.edu
Maintainer   = Mustafa Kocaturk
MaintEmail   = mustafa@seas.smu.edu
Site1        = seas.smu.edu
Path1        = /users/ee/mustafa/tmp
File1        = kftp
FileSize1    = 6004 bytes
Required1    = `bash`, `dialog` that comes with Slackware root diskette
Required2    = `sed`, `sort`, `ftp` or `termftp`
CopyPolicy1  = public domain
Keywords     = ftp, bash, dialog, menu, GUI, download, termftp
Entered      = 31JUL94
EnteredBy    = Mustafa Kocaturk
CheckedEmail = mustafa@seas.smu.edu
End

# -------- cut here and save text below as "kftp", excluding this line -------
#!/bin/sh
# Customizable variables
FTPDIR=~ftp/pub/download
FTPCMD=termftp
TMPFILE=/tmp/dialog$$.stderr
SEDFILE=/tmp/kftp$$.sed
PASSWD="`whoami`@`hostname`.`domainname`"
NETRC=${HOME}/.netrc

# Define the directory browsing function -- may be used recursively
browse () {
 local ii d j
 bcnt=$[ $bcnt + 1 ]
 ii="";j=0;
 pushd $1 > /dev/null
 for d in `ls -aF` ; do
  j=$[ $j + 1 ]
  ii="$ii $d $j"
 done;
 if [ ! "$ii" = "" ]; then 
  while [ ! "$d" = "" ]; do
   dialog --title "K-FTP Browser Level $bcnt" --menu "Current Directory: \"`pwd`\", Select the entry to browse:" 23 65 12 $ii 2> $TMPFILE
   d=`cat $TMPFILE`
   rm -f $TMPFILE
#   echo $d; sleep 3;
   case $d in
   *.tgz | *.tar.z | *.tar.Z | *.tar.gz | *.taz)
    tar tzPvvf $d | less
    ;;
   *.gz | *.z | *.Z | *.zip )
    zless $d
    ;;
   *.zip | *.ZIP )
    unzip -v $d |less
    ;;
   */ )
    browse $d
    ;;
   *.arc | *.ARC )
    arc l $d |less
    ;;
   *.zoo | *.ZOO )
    zoo l $d |less
    ;;
   *.lha | *.LHA )
    lharc v $d |less
    ;;
   "")
    ;;
   *)
    less $d
    ;;
   esac
  done
 fi
 popd > /dev/null
 bcnt=$[ $bcnt - 1 ]
 }

# Wrapper for pbrowse, use non-recursively.
pbrowse () {
 if [ ! "$1" = "" ]; then
  if [ ! -d $1 ] ; then mkdir $1; fi
#  pushd $1;
  bcnt=0;
  browse $1;
#  popd
 fi
 }

# Main script
cd $FTPDIR
while true; do
i="";j=0;
h=`sed -n '/^[ 	]*machine[    ]*\<\([^        ]*\)\>.*/s//\1/p' $NETRC | sort`
# i=`sort $NETRC | awk '$1 ~ /^machine$/{i++; print $2,i}'`
for d in $h; do j=$[ $j + 1 ]; i="$i $d $j"; done;
# Reset virtual console
echo -e "\033c"
dialog --menu "K-FTP Menu" 13 40 6 \
"Connect" "to a host and quit" "Add" "a Host" "Remove" "selected hosts" \
"Browse" "Local Files" "Info" "about K-FTP" "Quit" "K-FTP" 2> $TMPFILE
r=`cat $TMPFILE`
rm -f $TMPFILE
case $r in
 Connect)
    dialog --title "K-FTP $r" --menu "$j FTP Hosts Defined:" 23 65 16 $i 2> $TMPFILE
    s=`cat $TMPFILE`
    rm -f $TMPFILE
    if [ ! "$s" = "" ]; then
     if [ ! -d $s ] ; then
      dialog --title "K-FTP $r" --infobox "Creating directory \"$s\"" 6 60
      mkdir $s
     else
      dialog --title "K-FTP $r" --yesno \
       "Would you like to browse local directory \"$s\" ?" 7 60 && pbrowse $s
     fi
     dialog --title "K-FTP $r" --infobox "Changing directory to \"$s\" and starting \"$FTPCMD $s\"" 6 60
     j="pushd $s ; exec $FTPCMD $s"
     eval $j
    fi
    ;;
 Add)
    dialog --title "K-FTP $r" --inputbox "Enter the name of the host to be added:" 8 60 2> $TMPFILE
    s=`cat $TMPFILE`
    rm -f $TMPFILE
    if [ ! "$s" = "" ]; then
     if [ -d $s ]; then
      echo machine $s already in autologin menu.
     else
      mkdir $s
      echo machine $s login anonymous password $PASSWD >> $NETRC
      dialog --title "K-FTP $r" --msgbox "Added \"$s\" to $NETRC for anonymous ftp." 7 60
     fi
     dialog --title "K-FTP $r" --yesno "Would you like to connect to \"$s\" ?" 7 60 && \
      j="pushd $s ; exec $FTPCMD $s"
    fi
    ;;
 Remove)
    i=""; j=0;
    for d in $h ; do
     j=$[ $j + 1 ]
     i="$i $d $j off"
    done;
    dialog --title "K-FTP $r" --checklist "Select the hosts to be removed:" 23 60 16 $i 2> $TMPFILE
    s=`cat $TMPFILE`
    rm -f $TMPFILE
    for d in $s; do
     rm -f $SEDFILE
     echo "" > $SEDFILE
     if [ -d $d ]; then
      rmdir $d
     fi
     echo '/^[ 	]*machine[ 	]+$d[ 	]+login[ 	]+anonymous/!p' >> $SEDFILE
    done
    if [ -f $SEDFILE ] ; then
     sed -n -f $SEDFILE < $NETRC > $TMPFILE && \
      mv -f $TMPFILE $NETRC && \
      dialog --title "K-FTP $r" --infobox "Hosts removed from $NETRC." 5 60 && \
      chmod 600 $NETRC
     rm -f $SEDFILE
    fi
    ;;
 Browse)
    dialog --title "K-FTP $r" --menu "Select the FTP host download directory to browse:" 23 65 16 $i 2> $TMPFILE
    s=`cat $TMPFILE`
    rm -f $TMPFILE
    pbrowse $s
    ;;
 Info)
    cat << EOF > $TMPFILE

    K-FTP is a shell-based GUI for ftp logins and maintaining the
.netrc file.  It is hereby placed in the public domain  with  the
hope that it will be useful, but with no guarantee as to its mer-
chantability or fitness  for  a  particular  purpose.   Read  the
script and customize it; otherwise, use it at your own risk.

    K-FTP  was  intended to provide a GUI to the ftp user for se-
lecting hosts in the .netrc file.  It uses a  separate  directory
for  each host being connected to, in order to keep the dowloaded
files in an orderly hierarchy.  It changes the current  directory
to  a subdirectory corresponding to the name of the selected host
(the subdirectory is created if it does not exist), and  connects
to that host.

    The menu commands are self-explanatory.  The names of the ftp
command, directories, and the two temporary files can  be  recon-
figured  by  changing the shell variables FTPCMD, FTPDIR, TMPFILE
and SEDFILE, respectively, defined in the script's  header.   The
``dialog'' command that was provided with Linux Slackware distri-
bution 1.0.2, ``sed'' and ``bash'' are required.   Archive  files
with  names  ending  in  *.tar.gz  and derivatives, *.zip, *.zoo,
*.arc and *.lha are recognized and their directories shown if the
corresponding unarchiving program has been installed.

    As  it  stands, the script has many shortcomings: it is slow;
problems exist related to the GUI interface  program  ``dialog'',
e.  g. you can quit this info box by pressing the ``Esc'' key in-
stead of the ``Enter'' key.

    For suggestions and questions, you can contact me via the  e-
mail  address given below.  You are free to modify the script and
redistribute it, but I will not be responsible for those modified
versions unless I am convinced that it is an improvement.  In any
case, I would appreciate a copy, if you think you improved it.

    Thanks to the  Linux  developers community for maintaining an
operating system  so  flexible,  sound, and  open  to  evolution.
Special  thanks  go  to the SMU SEAS administration for providing
me with Internet access, to my advisor for  supporting  my  Ph.D.
studies, and to my wife for funding our personal computer.

    Mustafa Kocaturk <mustafa@seas.smu.edu>

    EE Dept., SMU, Dallas TX 75275
EOF
    dialog --title "K-FTP $r" --textbox $TMPFILE 23 70
    rm -f $TMPFILE
    ;;
 Quit)
    rm -f $TMPFILE
    rm -f $SEDFILE
    exit
    ;;
 "" | * )
    exit
    ;;
esac
done
# -------- cut here and save text above as "kftp", then `chmod +x kftp`. -----
-- 
Mustafa Kocaturk   <mustafa@seas.smu.edu>

--
Send submissions for comp.os.linux.announce to: linux-announce@tc.cornell.edu
PLEASE remember Keywords: and a short description of the software.
