#!/bin/bash
#createQtResource.sh <folder> <out.qrc> [<file_prefix>]

if [[ -z "$1" || -z "$2" ]]; then
  echo "To few arguments: $0 <folder> <out.qrc> [<file_prefix>]"
  exit 1
fi

QML_FOLDER=$1
QRC_FILE=$2
TMP_FILE=$QRC_FILE.$$.tmp
#echo "QML_FOLDER=$QML_FOLDER" 1>&2
#echo "QRC_FILE=$QRC_FILE" 1>&2

printf "<!DOCTYPE RCC><RCC version=\"1.0\"><qresource>\n" >$TMP_FILE
find $QML_FOLDER -type f -printf "    <file alias=\"%P\">$3%P</file>\n" >>$TMP_FILE
printf "</qresource></RCC>" >>$TMP_FILE
if ! diff "$QRC_FILE" "$TMP_FILE" &>/dev/null
then
    cp $TMP_FILE $QRC_FILE
    echo "$QRC_FILE was updated" 1>&2
else
    echo "$QRC_FILE was not updated" 1>&2
fi
rm $TMP_FILE 
