#!/bin/sh

osarch=$1
usagemsg="Usage: $0 linux-x64|linux"
if [ -z "$osarch" ]; then
    echo $usagemsg
    exit 1;
fi
# https://stackoverflow.com/questions/3265803/bash-string-equality
if [ "$osarch" != "linux-x64" ] && [ "$osarch" != "linux" ]; then
    echo "Parameter is (slightly?) incorrect."
    echo $usagemsg    
    exit 1
fi

gsExt="tesseract"
echo "Creating $gsExt tarball and zip for $osarch"

if [ ! -d $gsExt ]; then
    mkdir $gsExt

    cp setup.ba* $gsExt/.

    if [ ! -d linux ]; then
	echo "No linux folder, can't proceed tarring and zipping up $gsExt."
	exit 1;
    else
	echo "Copying linux into $gsExt folder"
	#mv linux $gsExt/.
	cp -r linux $gsExt/.

	echo "Copying licence files"
	cp packages/*LICENSE.txt $gsExt/linux/.

	echo "Copying user info files"
	cp README.txt GETTING-OCR-SUPPORT-FOR-MORE-LANGS.txt $gsExt/.

        echo "Copying perllib"
        cp -r perllib $gsExt/.
        
	echo "Removing $gsExt/linux/man"
	rm -rf $gsExt/linux/man

	echo "Removing all but folder 'tessdata' from $gsExt/linux/share folder"
	#rm -rf $gsExt/linux/share/aclocal
	#rm -rf $gsExt/linux/share/doc
	#rm -rf $gsExt/linux/share/info/
	#rm -rf $gsExt/linux/share/libtool/
	#rm -rf $gsExt/linux/share/man
	mv $gsExt/linux/share $gsExt/linux/share.del
	mkdir $gsExt/linux/share
	mv $gsExt/linux/share.del/tessdata $gsExt/linux/share/.
	rm -rf $gsExt/linux/share.del
    fi
else
    echo "Folder $gsExt already exists. Using existing."
    echo "For creating a fresh tarball and zip, delete the existing $gsExt folder"
    echo "at this script level ($0) first."
fi

# just try to create it for the OS
outfile="$gsExt-$osarch"
if [ -f "$outfile.tar.gz" ]; then
    rm "$outfile.tar.gz"
fi
tar -cvzf "$outfile.tar.gz" $gsExt
    
if [ -f "$outfile.zip" ]; then
    rm "$outfile.zip"
fi
zip -r "$outfile.zip" $gsExt

#rm -rf $gsExt

echo "Done making $gsExt tarball/zip"
echo ""
