#!/bin/bash

dirs=`find -maxdepth 1 -name '*' -type d`
if [ "$dirs" != "" ]; then
	chmod -f 711 $dirs
fi

if [ -f NO_INDEX.xx ]; then 
	exit
fi

# create link in current directory
nazwa=`basename $0`
if ! [ -f "$nazwa" ]; then 
	rm -f "$nazwa"
	echo "Creating link in current directory (./$nazwa)" 
	ln -s $0
fi

rm -f *.xxc *~

echo Files:
(find -maxdepth 1 -type f; find -maxdepth 1 -type l) | \
grep -vx "\.\|\./.*\.xx\|\./.*\.xxc\|\./.*\.licz\|\./.*\.jpeg\|\./.*\.jpeg.m\
\|\./main.*\.html\|\./.*\.php\
\|\./create_index.*\.sh\|\./.*\.desc\|\./.*~"\
| sort | (
  while read name; do
	name=`basename "$name"`
	name0=`readlink -f $name`
	size=`stat -c "%s" "$name0"`
        date=`stat -c "%y" "$name0"|sed 's/\(....\)-\(..\)-\(..\) \(..:..\).*/\3-\2-\1 \4/'`
	chmod 644 "$name0"
	if [ -f "$name.desc" ]; then chmod 644 "$name.desc"; fi
	touch "$name.licz"
	echo $name $size $date -
	echo $name $size $date - >&2
  done
) > `if [ ! -f MANUAL.xx ]; then echo FILES.xxc; else echo /dev/null; echo "  (Pomijam)" >&2; fi`

echo Pictures:
(find -maxdepth 1 -name '*.jpeg' -type f; find -maxdepth 1 -name '*.jpeg' -type l) | sort | (
  while read name; do
	name=`basename "$name"`
	dim=`identify "$name" | cut -f3 -d' '`
	xs=`echo $dim | cut -f1 -dx`
	ys=`echo $dim | cut -f2 -dx`
	name0=`readlink -f "$name"`
	size=`stat -c "%s" "$name0"`
	if [ -f "WITH_DATE.xx" ]; then
          date=`stat -c "%y" "$name0"|sed 's/\(....\)-\(..\)-\(..\) \(..:..\).*/\3-\2-\1 \4/'`
	else
	  date="- -"
	fi
	namem=`readlink "$name.m"`
	if [ "$namem" == "" ]; then namem="$name.m"; fi
	if [ -f "$name.desc" ]; then chmod 644 "$name.desc"; fi
	if [ -f "$namem" ]; then chmod 644 "$namem"; fi
	chmod 644 "$name0"
	touch "$name.licz"
	echo "$name $size $date - $xs $ys -"
	echo "$name $size $date - $xs $ys -" >&2
  done
) > `if [ ! -f MANUAL.xx ]; then echo PICTURES.xxc; else echo /dev/null; echo "  (Pomijam)" >&2; fi`

if [ -f LOCATION.xx ]; then
  cat LOCATION.xx | (
    read remote remotem
    if [ "$remotem" == "" ]; then remotem="-"; fi
    while read file size date tim xs ys; do
    	if [ "$date" == "" ]; then date="-"; fi
    	if [ "$size" == "" ]; then size="-"; fi
    	if [ "$tim" == "" ]; then tim="-"; fi
    	if [ "$xs" == "" ]; then xs="-"; fi
    	if [ "$ys" == "" ]; then ys="-"; fi
    	echo $file $size $date $tim $remote $xs $ys $remotem
    	echo $file $size $date $tim $remote $xs $ys $remotem >&2
	touch "$file.licz"
    done
  ) >> PICTURES.xxc
fi

here=`pwd|sed "s/.*public_html//"|sed "s/^\///"`
where=`echo $here | sed "s/[^/]//g;s/\//..\//g"`".."
if [ "$here" == "" ]; then here="."; where="."; fi
echo "Ja jestem relatywnie od glownego katalogu tu: $here"
echo "Glowny katalog jest relatywnie ode mnie tak: $where"
if [ "$where" != "." ]; then
	rm -f index.php
	(#  echo "#!/usr/local/bin/php"
	   echo "<?php"
	   echo "header( \"Location: $where/?dir=$here\" );"
	   echo "?>"
	)>index.php
fi

czy=`grep -x "$here" $where/allowed_dirs.xxc`
if [ "$czy" == "" ]; then 
	echo $here>>$where/allowed_dirs.xxc; 
	echo "Dopisuje sie do allowed_dirs.xxc"
fi

touch index.licz
chmod -f 644 main*.html;
chmod -f 600 *.xx; 
chmod -f 644 TITLE*.xx README*.xx allowed_dirs.xxc 
chmod -f 666 *.licz
chmod -f 644 *.xxc
chmod -f 644 *.php
chmod -f 755 create_index*.sh