#!/bin/bash
#EMACS_MODES:fill=0

if helptest $* 2> /dev/null || test "$1" = "-help" -o "$1" = "/help"
then
  echo
  echo "MOVE-PUBLIC-PICS instructions and help"
  echo ""
  echo ""
  echo
  return 1 2> /dev/null ; exit 1
fi

export S=/var/www/html/public
export T=$PWD

#		You can specify a target directory, not a subdirectory.
if test -d "$1" ; then
  cd $1/..
  if test "$" != "$PWD" ; then
    export T=$1
    shift
  fi
fi
echo "Target directory is $T."

#		You need a command-line argument.
if test "$1" == "" ; then
  echo "You need a command line argument."
  move-public-pics /help
  exit $?
fi

#		You need to change to target directory.
cd $T
if test "$T" != "$PWD" ; then
  echo "Unable to change directory to $D."
  move-public-pics /help
  exit $?
fi

#		Find directory $1 or VIEW-$1.
for D in $1 view-$1 ; do
  if test -d $D -a -f $D.htm ; then
    if "$D" = "$1" ; then
      export DD=${D/view-/}
    else
      export DD=$D
    fi
    break
  fi
done

#		Directory and HTM file both have to exist.
if test -d $D -a -f $D.htm ; then
else
  echo "Unable to find directory $D and file $D.htm."
  move-public-pics /help
  exit $?
fi

#		The hard part - do the actual move.
cd $S
#		Keep safety copies in ./OLD/ directory.
mkdir -p ./old/
cp -inrpv $D/ $D.htm ./old/
#		Move the files from Source to Target.
mv -ivn $D/ $D.htm $T/
#		Edit files for local access.
cd $T
for i in $( find . -name "*.htm" ) ; do
  cp -inpv $i $i.bak
  sed "s?/public/$DD/?./?" $i.bak > $i
done

if false ; then
  mkdir -p       ./bak/
  cp -inpv *.htm ./bak/
  for i in *.htm ; do
    echo $i
    sed "s?/public/view-mike-bird-1/?./?" ./bak/$i > ./$i
  done
fi
