# Pulling Texts From Freewrite Alpha // 24-8-10

As described in the previous post, the Freewrite Alpha permits
offline saving of drafts through the USB port, which allows its
memory to be mounted as a virtual drive. One can then copy the
drafts directly onto a connected computer.
Unfortunately, the file names for the resulting text files are
generated according to very simple rules: The first 31 characters
of the draft with an appended `.txt` become the file name, i.e
the name cannot easily be kept stable, which is especially
annoying if one wants to put the drafts into a version control
system (git in my case).

To simplify this problem, I've written a short shell script that
is explained directly in the following source code.

---

#!/bin/sh
info=v.04
# display help, if FWAMOUNT is unset or '-h' argument given:
if test "$FWAMOUNT" = "" -o "$1" = "-h"
then cat <<EOH
$0 ($info) : copy Freewrite Alpha texts into local directory,
which should be a git working dir and will be status-checked.
Env.var FWAMOUNT must contain the name of the directory, where
the Alpha's memory is mounted.
Text names will be used up to the first space, e.g
"foobar This is a..." will be converted to "foobar.txt"
and all name characters other than 0-9A-Za-z- will be replaced by _
before copying into the current directory.
EOH
exit 1
fi
# check whether FWAMOUNT points to a readable directory:
if test -d "$FWAMOUNT" -a -r "$FWAMOUNT"
then
# run `git status` only on checked-in files for the current directory:
if git status -uno
then
# .. and if that doesn't throw an error, ask the user for feedback
# (we don't want to burden the script with too much logics for
# covering all imaginable situations with modified or deleted drafts,
# changes in file naming, pending merges etc; the user should know):
 echo : please check status of this working dir:
 echo : if all fine then enter Y or
 echo : else interrupt with CTRL-C or any other input
# .. meaning "if you don't feel fine with that git status, please
# fix your working directory or repo first and then come back again!"
 read inpt
# only continue, if the user is willing to get the current dir clobbered:
 if test "$inpt" = "Y"
 then
# save the current (git working) directory:
  gdir="`pwd -P`"
# cd to the mount point of the Alpha's memory:
  cd "$FWAMOUNT"
# process all files in the Alpha's memory:
  for ff in *
  do
# now we try to canonicalize the file names! first by
# removing the `.txt` suffix (it complicates things, and we will
# simply re-add it at the end):
   rn="${ff%.txt}"
# then by removing everything from the end up to the first SPC
# character (i.e only keep the left part of the name up to the
# first SPC char), and replace all characters *except* 0-9A-Za-z
# or minus/hyphen by underscores:
   rn=`echo "${rn%% *}" | tr -c '0-9A-Za-z-' _`
# now re-add the suffix, after removing a possible final underscore
# which sometimes gets added, depending on linebreaks in the draft:
   rn=${rn%_}.txt
# now we have a name consisting only of numbers, letters or minus
# signs, followed by the `.txt` suffix; report it:
   echo "copying '$ff' into $rn"
# use cat instead of copy to make sure the permissions of a
# possibly already existing previous version in the working dir
# remain unchanged:
   cat "$ff" > $gdir/$rn
  done
# cd back to the working directory (actually not needed right
# now, as there is nothing further happening in the script, but
# good practice anyway):
  cd $OLDPWD
  echo all done.
 else
# if the user did not enter Y after the git status display:
  echo 'aborted!'
  exit
 fi
else
# if git returned a non-zero status:
 echo "error: 'git pull' failed -- is the current dir a git working dir?"
 echo '  [get help by launching this script with argument -h]'
 exit 3
fi
else
# if FWAMOUNT doesn't seem to point to what we expect:
echo "error: cannot read directory FWAMOUNT='$FWAMOUNT'"
echo '  [get help by launching this script with argument -h]'
exit 2
fi