#!/bin/sh

if test $# != 2 -o $0 != "./install_gnupoc_s60_31"; then
  echo "Usage: ./install_gnupoc_s60_31 <path-to-sdk-zip> <target-dir>"
  echo " e.g.: ./install_gnupoc_s60_31 S60_SDK_3.1_CPP_v1.0.1_en.zip ~/symbian-sdks/s60_31"
  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

if [ -f _e/S60_SDK_3.1_CPP_v1.0.1_en/data2.cab ]; then unshield -g SelfRegFiles2 x _e/S60_SDK_3.1_CPP_v1.0.1_en/data2.cab; mv SelfRegFiles2/* _e; rmdir SelfRegFiles2; fi

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

unshield -g icon x _e/data2.cab
mv icon/s60.ico $DEST
rmdir icon

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

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

unzip -qn _e/Examples.zip
mv Examples $DEST/examples
mv S60Ex $DEST/s60ex

### Fixing
./lowercase $DEST/epoc32/tools
./lowercase -map_paths headers $DEST/epoc32/include
./lowercase -symlink $DEST/epoc32/include
./lowercase -map_files libraries $DEST/epoc32/release
./lowercase -symlink $DEST/epoc32/release
./lowercase $DEST/epoc32/data
./fixinclude $DEST/epoc32/include
./fixinclude $DEST/s60ex
./fixexamples $DEST/s60ex
./lowercase -symlink $DEST/s60ex
./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_31-include-patch | (cd $DEST/epoc32/include; patch -p1)
cat gnupoc-s60_31-patch | (cd $DEST/epoc32/tools; patch -p1 )
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 elf2e32 mifconv makekeys signsis extmake rtf2ptml cjpeg def2dll elftran genstubs getexports tranasm

rm -rf _e

__END

exit 0
