#!/bin/sh
#
# Usage: mkres <root> [<template>]
#
# <template> defaults to $TEMPLATE or "template" and must be a subdirectory of root
#

ROOT="$1"
if [ -z "$1" ]; then
    echo "ERROR: root must be specified" >&2
    exit 1
fi

if [ -z "$2" -a -z "$TEMPLATE" ]; then
    TEMPLATE=template
fi

if [ -z "$VER" ]; then
    echo "ERROR: VER is not set" >&2
    exit 1
fi

if [ -z "$GUIVER" ]; then
    echo "ERROR: GUIVER is not set" >&2
    exit 1
fi

if [ ! -e "$ROOT/$TEMPLATE/dist.plist" ]; then
    echo "ERROR: invalid root or template, $ROOT/$TEMPLATE/dist.plist is missing" >&2
    exit 1
fi

for i in `find "$ROOT/$TEMPLATE" -name \*plist -or -name \*rtf`; do
    dst=`echo $i | sed s:/$TEMPLATE::`
    echo $i '->' $dst
    sed -e "s/%VER%/$VER/g" -e "s/%VERFULL%/$VERFULL/g" -e "s/%GUIVER%/$GUIVER/g" $i > $dst
done