#!/bin/sh

if test $# != 2 -o $0 != "./install_gnupoc_s60_26_cw"; then
  echo "Usage: ./install_gnupoc_s60_26_cw <path-to-sdk-zip> <target-dir>"
  echo " e.g.: ./install_gnupoc_s60_26_cw s60_2nd_fp2_sdk.zip ~/symbian-sdks/s60_26_cw"
  exit 1
fi

SRC=$1
DEST=`echo $2 | sed s:/\$::`

mkdir -p $DEST

export PATH=unshield:$PATH

while read a
do
  echo "$a"
  sh -c "$a"
  if test $? != 0; then
    echo "Unexpected error: aborting."
    exit 1
  fi
done << __END

### Unzip & Copy selected files
mkdir -p _e
unzip -qn $SRC -d _e

unshield -g Epoc32 x _e/data2.cab
mv Epoc32 $DEST/epoc32

unshield -g Xtras_winscw x _e/data2.cab
mv Xtras_winscw/epoc32/release/winscw $DEST/epoc32/release
mv Xtras_winscw/epoc32/stationery $DEST/epoc32
mv Xtras_winscw/epoc32/winscw $DEST/epoc32
rm -rf Xtras_winscw

unshield -g Examples x _e/data2.cab
mv Examples $DEST/examples

unshield -g Series60Ex x _e/data2.cab
mv Series60Ex $DEST/series60ex

unshield -g Series60Doc x _e/data2.cab
mv Series60Doc $DEST/series60doc

unshield -g Series60Tools x _e/data2.cab
mv Series60Tools $DEST/series60tools

### Fixing
./lowercase $DEST/epoc32/tools
./lowercase $DEST/epoc32/include
./lowercase $DEST/epoc32/release
./lowercase $DEST/epoc32/data
./fixinclude $DEST/epoc32/include
./lowercase $DEST/series60ex
./fixinclude $DEST/series60ex
./fixexamples $DEST/series60ex
./lowercase $DEST/examples
./fixinclude $DEST/examples
./fixexamples $DEST/examples
for i in \`find $DEST/epoc32/tools -name \\\*.pl -or -name \\\*.pm\`; do ./dos2unix \$i; done
cp -pR $DEST/epoc32/tools $DEST/epoc32/tools_orig
cat gnupoc-s60_26-patch | (cd $DEST/epoc32; patch -p0 )
find $DEST/epoc32/tools -name \\\*.orig -exec rm {} \\\\;
cd $DEST/epoc32/tools; chmod a+x \*.pl bldmake abld makmake bmconv rcomp makesis epoc eshell petran pfsdump setupcomms makekeys extmake

rm -rf _e

__END

exit 0
