#!/bin/bash

vers=2026.02.02.A
# rebuild all index files found in specified folder

echo "Rebuild WWW Indexes version $vers"

# find files in CWD that are not world-readable, and make them world-readable:
#    find . \( -not -perm -444 -and -type f \) -exec chmod +r "{}" \;

switch_posthide=1  # hide us after successful run


switch_force_recurse=





################################################################################
###
###  TRIGGERS
###
################################################################################

# will not recurse into subfolders of trees containing ".do not recurse"
# presence of do_force_recurse_file overrides and propogates

# will ignore trees containing ".do not index"

# will obtain rsync target from ".server_target_path.txt"
#   will not sync if file is not present
#   format like: root@192.168.1.11:/Volumes/vFTP1/www/N0ZYC
#   requires file ".allow_source.txt" on root of taget, must contain name of source folder

# will not initiate rsync to server if script folder contains ".do not backup"

# will sync but not link to folders that contain the .hide marker





################################################################################
###
###  CONFIGURE
###
################################################################################

# file containing rsync target path
target_path_file=".server_target_path.txt"

# marker file to disable recursive processing (do_force_recurse_file overrides)
do_not_recurse_file=".do not recurse"

# marker to stop recursively reindexing (also halts do_ignore_DNR_file propogation)
do_not_index_file=".do not index"

# marker to dryrun the sync
do_not_sync_file=".do not sync"

# force deep recursion swtich
do_ignore_DNR_file=".do ignore do not recurse"





################################################################################
###
###  FUNCTIONS
###
################################################################################

########################################
##  ABORT
########################################

abort () {
line=$1
msg=$2
echo "ABORT on line $((line-1)): $msg"
exit 1
}



########################################
##  INDEX FOLDER
########################################

index_folder () {
local root f dni n dnr

root=$1

# index this folder
#echo "make_www_index.command \"$root\""
#read -p "press any key to continue: " -n1 x ; echo
#echo "CWD: \"$(pwd)\""
#echo "make_www_index.command \"$root\""

make_www_index.command "$root" ; rc=$?
if [ $rc != 0 ] ; then
  abort $LINENO "MAKE WWW INDEX ERROR - INDEX ABORTED"
fi

# recurse into subfolders
# loop through all objects in root folder

dnr="${root}/${do_not_recurse_file}"
#echo "switch_force_recurse=$switch_force_recurse"
if [ -e "$dnr" ] ; then
  test
#  echo "do_not_recurse_file found at $dnr"
else
  test
#  echo "do_not_recurse_file not found at $dnr"
fi

if [[ ("$switch_force_recurse") || (! -e "$dnr") ]] ; then
#  echo "recursing into $root"
  # either we're deep recursing or this folder doesn't contain a do_not_recurse_file
  for f in "$root"/* ; do
    # only consider subdirectories
    if [ -d "$f" ] ; then
      # do not recurse into image folders
      n=${f##*/}
      if [[ ("$n" == "full") || ("$n" == "max") || ("$n" == "thumb") || ("$n" == "athumb") || ("$n" == "videos") ]] ; then
        #ff="${f}/index.html"
        #if [ -e "$ff" ] ; then
        #  echo "REMOVING OOPS AT \"$ff\""
        #  rm "$ff"
        #fi
        continue
      fi
      # check subdirectory for presense of "do not index" marker file
      dni="${f}/${do_not_index_file}"
      if [ -e "$dni" ] ; then
        # do not index this subfolder
        echo "DO NOT INDEX file \"$dni\" found, will NOT index into"
      else
        # step up one level on the path back to the global athumbs and index this child folder
        global_athumb_url="../${global_athumb_url}"
        export global_athumb_url
        index_folder "$f"
        global_athumb_url=${global_athumb_url:3}
        export global_athumb_url
      fi
    fi
  done
else
  test
#  echo "did not recurse into $root"
fi

}





################################################################################
###
###  START
###
################################################################################


##  SELECT INDEXING ROOT

parent=$1
if [ -z "$parent" ] ; then
  abort $LINENO "parent path not specified"
elif [ ! -f "$parent" ] ; then
  abort $LINENO "parent file not found at \"$parent\""
fi

#echo "parent=\"$parent\""
parent=${parent%/*}
#echo "parent=\"$parent\""
if [ "$parent" == "." ] ; then
  # launched from terminal instead of Finder
  parent=$(pwd)
fi

echo "parent=\"$parent\""
cd "$parent" || abort $LINENO "failed to change directory to \"$parent\""



##  CHECK FOR FLAG FILES THAT MUST BE IN ROOT FOLDER

if [ "$switch_force_recurse" ] ; then
  echo "will force recursion"
elif [ -f "$do_ignore_DNR_file" ] ; then
  # will force reursion (overrides do_not_recurse_file, but still stops at do_not_index_file)
  switch_force_recurse=1
  echo "found force recurse file at \"$do_ignore_DNR_file\" file"
else
  test
  echo "did not find force recurse file at \"$do_ignore_DNR_file\" file"
fi



##  FIND WWW TARGET PATH

# find and read target path file.  if one is not found in this folder, search previous folders for one
pre=$(pwd)
if [ "$pre" == "~" ] ; then
  pre="$HOME"
fi

post=""
global_athumb_url="athumb"
while true ; do
  tpf="${pre}/$target_path_file"
  if [ -f "$tpf" ] ; then
    # found target path file
    tp=$(cat "$tpf" | head -n1)
    echo "target path file at \"$tpf\" specifies target path \"$tp\""
    break
  fi
  tpf=
  echo "pre=\"$pre\""
  if [ -z "$pre" ] ; then
    # we backed all the way up to / and didn't find a target path file, give up on sync
    break
  fi

  # substitute true path if PRE is a symbolic link
  if [ -L "$pre" ] ; then
    echo "\"$pre\" is a LINK"
    pre=$(readlink -f "$pre")
    echo "translated to \"$pre\""
  fi

  # prepend current folder name onto post, back up one folder, and check again
  post="/${pre##*/}${post}"
  pre=${pre%/*}
  global_athumb_url="../${global_athumb_url}"
done



##  ESTABLISH GLOBAL ATHUMB FOLDER

global_athumb="${pre}/athumb"
if [ -d "$global_athumb" ] ; then
  echo "global_athumb=\"$global_athumb\""  #  this local folder path doesn't change with depth
  echo "global_athumb_url=\"$global_athumb_url\""  #  this needs to increase with depth
  export global_athumb
  export global_athumb_url
else
  echo "global athumb not found at \"$global_athumb\""
  global_athumb=""
  global_athumb_url=
fi



##  BEGIN INDEXING

t1=$SECONDS
index_folder "$parent"



##  STOP (AFTER INDEX, BEFORE SYNC) IF WE HAVE NO TARGET PATH FILE

if [ -z "$tpf" ] ; then
  echo "target path file not found at or before \"$(pwd)/${target_path_file}\", will not sync to server"    
  exit 0
fi



##  WE HAVE A WWW TARGET PATH, SYNC TREE

# do a dry run sync if do_not_sync_file file is present.  otherwise do a live sync
echo "pre=\"$pre\""
echo "post=\"$post\""
echo "tpf=\"$tpf\""


#echo "DEBUG HALT"
#exit 0


source="${pre}${post}"
#target="${tp}${post}"
do_not_sync_file="${source}/${do_not_sync_file}"

echo "source=\"$source\""
echo "target=\"$target\""
echo "do_not_sync_file=\"$do_not_sync_file\""

if [ -e "$do_not_sync_file" ] ; then
  echo "we have a target path but do_not_sync_file is present, will dryrun the sync with server"
#  echo "CMD: sync_folder_to_server.command -n -dotfiles -s \"$source\" -t \"$target\""
#  sync_folder_to_server.command -n -dotfiles -s "$source" -t "$target"
#  echo "CMD: sync_folder_to_server.command -n -dotfiles -s \"$source\" -tf \"$tpf\""
#  sync_folder_to_server.command -n -dotfiles -s "$source" -tf "$tpf"
  echo "CMD: sync_folder_to_server.command -n -dotfiles -s \"$source\" -tf \"$tpf\" -tp \"$post\""
  sync_folder_to_server.command -n -dotfiles -s "$source" -tf "$tpf" -tp "$post"
else
  echo "syncing \"$0\" to server..."
#  echo "CMD: sync_folder_to_server.command -dotfiles -s \"$source\" -t \"$target\""
#  sync_folder_to_server.command -dotfiles -s "$source" -t "$target"
#  echo "CMD: sync_folder_to_server.command -dotfiles -s \"$source\" -tf \"$tpf\""
#  sync_folder_to_server.command -dotfiles -s "$source" -tf "$tpf"
  echo "CMD: sync_folder_to_server.command -dotfiles -s \"$source\" -tf \"$tpf\" -tp \"$post\""
  sync_folder_to_server.command -dotfiles -s "$source" -tf "$tpf" -tp "$post"
fi


##  DONE
t2=$SECONDS
((t3=t2-t1))
t4=$(date -j -u -f "%s" "$t3" "+%H:%M:%S")


echo
echo "rebuild complete in $t4"
echo

# hide us if switched
if [ $switch_posthide ] ; then
  osascript -e 'tell application "System Events" to set visible of application process "Terminal" to false'
fi



















