#! /bin/bash -e

#####################################################
#                                                   #
#   Encode an unitypackage archive                  #
#                                                   #
#   (c) Grame, 2017                                 #
#                                                   #
#####################################################


# Structure of a unitypackage archive
#----------------------------------------------------------------------------------
#   FaustPlugin_<dspname>.unitypackage/
#        <md5 key of file1>/
#           asset (file1 renamed)
#           pathname (file containing the address of file1 in the Unity hierarchy
#        <md5 key of file2>/
#           asset (file2 renamed)
#           pathname (file containing the address of file2 in the Unity hierarchy
#        <md5 key of file3>/
#           ...

# The Unity hierarchy will be the same as the original folder hierarchy.
# However, the first folder needs to be called "Assets", if not the script creates it before packing.
# When the package is imported, Unity retrieves the hierarchy from the pathname files

#-------------------------------------------------------------------
#Dispatch command arguments

for p in $@; do
    if [ $p = "-help" ] || [ $p = "-h" ]; then
        echo "encoderunitypackage <folder>"
        echo "Builds a unity package according to the specified sub folder hierarchy."
        echo "Handles if the folder isn't called Assets/ (adds a parent folder called Assets/)."
        echo "If the package is called FaustPlugin_<pluginname>, change <pluginname> to the correct name."
        echo "See architecture/unity/README.md for more info."
        exit
    fi

    if [ ${p:0:1} = "-" ]; then
	    OPTIONS="$OPTIONS $p"
    elif [[ -d "$p" ]]; then
	    FOLDER="$FOLDER $p"
	else
	    OPTIONS="$OPTIONS $p"        
	fi
done

for p in $FOLDER; do

    # If the parent folder is not called "Assets", create it (to respect the Unity hierarchy).
    if [ ! $p = "Assets" ]; then
        mkdir Assets
        mv $p Assets
        p=Assets
    fi

    # Retrieve the name of the dsp file stored in ".VAR"
    if [ -f .VAR ]; then
        FNAME=FaustPlugin_$(cat .VAR)
    else
        FNAME=FaustPlugin_\<pluginname\>
    fi

    rm -rf .VAR
    TMP=$(mktemp -d unity.XXXX)

    # Finds all files in $p to encode them
    FILES=$(find $p -type f)

    for f in $FILES; do
        FILE=$(basename $f)

        # Computes the md5 key of each file, for Darwin system the command is md5, if not the command is md5sum
        if [[ $(uname) == Darwin ]]; then
            DIR=$(md5 -q $f)
        else
            DIR=$(md5sum $f | cut -f1 -d" ")
        fi

        # Creates the folder named with the md5 key of the file
        if [ -d "$DIR" ]; then
            rm -rf "$DIR"
        fi
        mkdir -p "$DIR"
        mv $DIR $TMP/$DIR

        # Retrieves the file address and creates the pathname file
        echo "$f" >> $TMP/$DIR/pathname

        # Moves and renames the file in the $DIR (md5 key folder)
        mv "$f" "$TMP/$DIR/asset"

    done

    # Make sure all .* files have been deleted
    FILES=$(find $p -name "\.*")
    for f in $FILES; do
        rm -rf $f
    done

    # Compresses the folder and renames it FaustPlugin_<dspname>.unityackage
    # The archive needs to be a tar gz archive to be correctly read by Unity
    cd unity.*
    tar zcvf $FNAME.unitypackage * &> /dev/null
    mv "$FNAME.unitypackage" ../
    cd ..
    rm -rf unity.* $p

done


