#!/bin/sh

# This script moves required files from ~/page-palette to ~/page.  It also
# creates the version file and updates the files WIN_INSTALL/page.iss
# and ms-install.iss which are used by the Inno Setup Program to
# create the exe file in a separate step. The latter file is used when
# running the Inno Setup Compiler under Windows 10 in a virtual
# machine. I am using Inno Setup Compiler version 5.4.3.

# It is necessary to set the bash variable V to the new version number.

# This collects files from the current directory and put them in the
# directory DEST.

# DEST=~/page
DEST=~/page-me/page
TARDIR=~/page-me
DEVDIR=page-me
# Version number of the release/
V=8.1

# Modify template.iss and rewrite page.iss
echo "set vTcl(version) $V" > version

#sed -e "s:NV:$V:g" < WIN_INSTALL/template.iss > WIN_INSTALL/page.iss
# For running installer under Windows 10.
sed -e "s:DEVDIR:$DEVDIR:g" < WIN_INSTALL/template > WIN_INSTALL/template-ms.iss 
sed -e "s:NV:$V:g" < WIN_INSTALL/template-ms.iss > WIN_INSTALL/page.iss 

# Remove all the files from within ~/page

if [ ! -e $DEST ];
then

	echo "mkdir $DEST"
	mkdir $DEST
fi

rm -rf $DEST/*

# Copy files to ~/page.

FILES=" \
		 page.py \
         page.tcl \
         version \
         release \
         LICENSE 
"
#         WRITE.BAT

DIRS=" \
		 color_chart \
         docs \
         examples \
         images  \
		 Img \
         lib \
		 themes \
         page-icons \
         WIN_INSTALL "

for file in $FILES
do
	if [ ! -e $DEST/$file ];
	then 
		echo "File: $file"
		cp $file $DEST
	fi	
done

for dir in $DIRS
do
	if [ ! -d $DEST/$dir ];
	then
		echo "Dir: $dir"
		cp -dpr $dir $DEST
    fi
done

# Remove files we do not wish to distribute.

echo "\n*.save"
find $DEST/ -name "*.save"

find $DEST/ -name "*.save" -print0 | xargs -0 rm

echo "\n*.save.*"
find $DEST/ -name "*.save.*"

find $DEST/ -name "*.save.*" -print0 | xargs -0 rm

echo "\nRCS"
find $DEST/ -name RCS

find $DEST/ -name RCS -print0 | xargs -0 rm -rf

echo "\n*.txt"
find $DEST/ -name "*.txt" -print0 

find $DEST/ -name "*.txt" -print0 | xargs -0 rm -rf

echo "\n#*#"
find $DEST/ -name "#*#" -print0 

find $DEST/ -name "#*#" -print0 | xargs -0 rm -rf

echo "\n*.orig"
find $DEST/ -name "*.orig" -print0 

find $DEST/ -name "*.orig" -print0 | xargs -0 rm -rf

echo "\n*.new"
find $DEST/ -name "*.new" -print0 

find $DEST/ -name "*.new" -print0 | xargs -0 rm -rf

echo "\n*.no"
find $DEST/ -name "*.no" -print0 

find $DEST/ -name "*.no" -print0 | xargs -0 rm -rf        

echo "\n*.full"
find $DEST/ -name "*.full" -print0 

find $DEST/ -name "*.full" -print0 | xargs -0 rm -rf

echo "\n*.partial"
find $DEST/ -name "*.partial" -print0 

find $DEST/ -name "*.partial" -print0 | xargs -0 rm -rf

echo "\n*.tmp"
find $DEST/ -name "*.tmp" -print0 

find $DEST/ -name "*.tmp" -print0 | xargs -0 rm -rf

echo "\n*.bak*"
find $DEST/ -name "*.bak*"
find $DEST/ -name "*.bak*" -print0 | xargs -0 rm -rf

echo "\noutput.*"
find $DEST/ -name "output.*" -print0 

find $DEST/ -name "output.*" -print0 | xargs -0 rm -rf

echo "*\nchanges"
find $DEST/ -name "*changes" -print0 

find $DEST/ -name "changes" -print0 | xargs -0 rm -rf

echo "\ntt"
find $DEST/ -name "tt" -print0 

find $DEST/ -name "tt" -print0 | xargs -0 rm -rf

echo "\nunknown*"
find $DEST/ -name "unknown*" -print0 

find $DEST/ -name "unknown*" -print0 | xargs -0 rm -rf

find $DEST/ -name "" -print0 | xargs -0 rm -rf

find $DEST/ -name "unknown*" -print0 | xargs -0 rm -rf

find $DEST/ -name "*~" -print0 | xargs -0 rm -rf

find $DEST/ -name "#*" -print0 | xargs -0 rm -rf

find $DEST/ -name "tmp*" -print0 

find $DEST/ -name "tmp*" -print0 | xargs -0 rm -rf





echo "\nrm"

rm $DEST/output.stuff

# Build the tgz file for the release.

echo "\ntar the files"
cd $TARDIR
echo "\nChanged to $PWD"

echo "tar zcvf $DEST-$V.tgz page"

tar zcvf $DEST-$V.tgz page 


