#!/bin/bash

# get the environment variables from the directory of this script
homedir=`dirname $0`
. $homedir/env.sh
scriptname=xpisetup
. $homedir/xpienv.sh

# this sets up the directory structures and imports dtds

echo Creating directories...

cd $basedir
mkdir -p $xpidir
cd $xpidir
mkdir -p $chromedir
cd $chromedir

parts="$country unix win mac"
# build up the list of directories out of which to make jar files
for part in $parts
 do
  jardir=$language-$part
  mkdir -p $jardir/locale/$language-$country
 done

echo Copying files...

# first do general stuff (install.js)
echo making $xpidir/install.js
$pythonbin $scriptsdir/replace.py TEMPLATELANGFULL "$languagepretty" TEMPLATELANGCODE $language TEMPLATEREGIONCODE $country <$basedir/install.js >$xpidir/install.js

# now do each directory
for part in $parts
 do
  cd $chromedir
  partdestdir=$language-$part
  if [[ $part == $country ]]
   then
    # we always have this...
    partsrcdir=$mozversion/$language/common
   else
    partsrcdir=$mozversion/$language/$part
   fi
  if [[ -d $srcdir/$partsrcdir ]]
   then
    echo found source for $partsrcdir, copying...
    cp -pR $srcdir/$partsrcdir/* $partdestdir/locale/$language-$country/
   else
    echo didn"'"t find source for $partsrcdir, copying and changing template...
    partsrcdir=$templatelanguage-$part-$mozversion
    cp -pR $srcdir/$partsrcdir/* $partdestdir/locale/$language-$country/
    # find regular files and de-templatise them
    for f in `find $partdestdir/locale/$language-$country -type f`
     do
      replace TEMPLATELANGFULL $languageprety TEMPLATELANGCODE $language TEMPLATEREGIONCODE $country $language-$country <$f >$f.new
      if [[ -s $f.new ]] 
       then
        rm -f $f
        mv $f.new $f
       else
        rm -f $f.new
      fi
     done
  fi

  echo converting rdf templates...
  # find rdf files and de-templatise them
  if [[ $part == $country ]]
   then
    rdfsrcdir=template-country-$mozversion
   else
    rdfsrcdir=template-$part-$mozversion
  fi
  cd $rdfdir/$rdfsrcdir/
  for f in `find . -type f | grep \.rdf$`
   do
    outf=$chromedir/$partdestdir/locale/$language-$country/$f
    # just in case not there
    mkdir -p `dirname $outf`
    $pythonbin $scriptsdir/replace.py TEMPLATELANGFULL "$languagepretty" TEMPLATELANGCODE $language TEMPLATEREGIONCODE $country <$f >$outf
   done
 done

echo Done. use xpibuild to make the xpi 

