#!/bin/bash -e

#---------------------------------------------------------------------

# Important:  Don't use an editor, or other program,  that trims white
# space on this text file.  It contains embedded patches  that include
# trailing white space.

#---------------------------------------------------------------------

# This  is a  Laclin-only script that  creates a source tarball for  8
# XUL-compatible Mozilla programs:

# Most useful:
#
# AlbusLuna     - Revived Classic Firefox
# Ambassador    - Revived Chatzilla
# Basilisk      - A newer-style UXP-based browser
# Epyrus        - A Classic Thunderbird fork (*)

# Less maintained, abandoned, or obsolete:
#
# Blue Gorilla  - Revived SeaMonkey (abandoned)
# Borealis      - A Netscape-like but improved browser
# Icedove       - Revived Icedove aka Classic Thunderbird
# Interlink     - Another Thunderbird fork

#---------------------------------------------------------------------

SPCDIR=`echo /src/0*/*_uxp-browsers/special`
SPCEXT=$SPCDIR/extensions/install

#---------------------------------------------------------------------

# Note:  Borealis and Interlink are  built in the same directory tree.
# So, no "SPC_BOREALIS" parameter is required.

SPC_ALBUSLUNA=$SPCDIR/spc-albusluna
SPC_AMBASSADOR=$SPCDIR/spc-ambassador
SPC_BASILISK=$SPCDIR/spc-basilisk
SPC_BLUEGORILLA=$SPCDIR/spc-bluegorilla
SPC_EPYRUS=$SPCDIR/spc-epyrus
SPC_ICEDOVE=$SPCDIR/spc-icedove
SPC_INTERLINK=$SPCDIR/spc-interlink
SPC_UXP=$SPCDIR/spc-uxp

#---------------------------------------------------------------------

echo Checking for some required files:
ls -l $SPCDIR/buildme.sh                            || exit 1
ls -l $SPCDIR/uxp-browsers.txt                      || exit 1

BASEDIR=uxp-browsers                                || exit 1
TARFILE=$BASEDIR.tar.bz2                            || exit 1

ABOVE=`pwd`                                         || exit 1
TOPDIR=$ABOVE/$BASEDIR                              || exit 1

rm -fr $BASEDIR $TARFILE $TOPDIR                    || exit 1
mkdir  $BASEDIR                                     || exit 1

#---------------------------------------------------------------------

cd $TOPDIR                                          || exit 1

#---------------------------------------------------------------------

cp -a $SPCEXT xpi                                   || exit 1

pushd xpi                                           || exit 1
for dir in `ff -had | sort`
do
    pushd $dir                                      || exit 1
    for x in `deext *.xpi`
    do
        UUID=`cat $x.uuid`                          || exit 1
        rm $x.uuid                                  || exit 1

# Note: The {} shouldn't be used for "normal" extension-file names.

        NN=`echo $UUID | grep -e '-.*-.*-.*-' | wc -l`
        if [ "@$NN" \!= "@0" ]; then
            mv $x.xpi "{$UUID}.xpi"                 || exit 1
        else
            mv $x.xpi "$UUID.xpi"                   || exit 1
        fi
    done
    popd                                            || exit 1
done
rm -fr *.notused                                    || exit 1
popd                                                || exit 1

#---------------------------------------------------------------------

cd $TOPDIR                                          || exit 1
rm -fr palemoon-source albusluna                    || exit 1

tar zxf $SPC_ALBUSLUNA/upstream-albusluna.tar.gz    || exit 1
tar zxf $SPC_EPYRUS/upstream-epyrus.tar.gz          || exit 1

mv     palemoon-source albusluna                    || exit 1
cd                     albusluna                    || exit 1

sed -e 's@^Vendor=.*@Vendor=laclin@' \
    -e 's@^Name=.*@Name=albusluna@' \
    -e 's@^RemotingName=.*@RemotingName=albusluna@' \
    -i palemoon/app/application.ini                 || exit 1

# In the past:
# cp -p   $SPC_ALBUSLUNA/albusluna-appname.bmp \
#       palemoon/branding/official/appname.bmp      || exit 1

pushd   palemoon/branding/                          || exit 1
tar zxvf  $SPC_ALBUSLUNA/albusluna-more-brand*.tgz  || exit 1
tar zxvf  $SPC_ALBUSLUNA/albusluna-wordmark.tgz     || exit 1

IMG=$SPC_ALBUSLUNA/albusluna-bgabout.jpg
if [ -f $IMG ]; then
    for x in `find . -type f -name about-background.jpg`
    do
        cp -p $IMG $x                               || exit 1
    done
fi

pwd
ls -l official/
popd                                                || exit 1

cp -p $SPC_ALBUSLUNA/albusluna-aboutDialog.xul \
         palemoon/base/content/aboutDialog.xul      || exit 1

cp -p $SPC_ALBUSLUNA/albusluna-baseMenuOverlay.xul \
         palemoon/base/content/baseMenuOverlay.xul  || exit 1

cp -p         $SPC_ALBUSLUNA/albusluna-bookmarks.html \
      palemoon/locales/generic/profile/bookmarks.html.in \
                                                    || exit 1
mkdir -p   ../rest/mozilla                          || exit 1

#ifdef NOTDEF
# rm -fr                   platform                 || exit 1
# ln -s    ../rest/mozilla platform                 || exit 1
#endif

#---------------------------------------------------------------------

cd $TOPDIR                                          || exit 1
rm -fr ambassador                                   || exit 1

tar zxf $SPC_AMBASSADOR/upstream-ambassador.tar.gz  || exit 1
cd ambassador                                       || exit 1

sed -e 's@^Vendor=.*@Vendor=laclin@' \
    -e 's@^Name=.*@Name=ambassador@' \
    -e 's@^RemotingName=.*@RemotingName=ambassador@' \
    -i ambassador/app/application.ini               || exit 1

rm -fr                 platform                     || exit 1
ln -s  ../rest/mozilla platform                     || exit 1

#---------------------------------------------------------------------

cd $TOPDIR                                          || exit 1
rm -fr basilisk                                     || exit 1

tar zxf $SPC_BASILISK/upstream-basilisk.tar.gz      || exit 1
cd basilisk                                         || exit 1
rm -fr platform                                     || exit 1

sed -e 's@^Vendor=.*@Vendor=laclin@' \
    -e 's@^Name=.*@Name=basilisk@' \
    -e 's@^RemotingName=.*@RemotingName=basilisk@' \
    -i basilisk/app/application.ini                 || exit 1

#---------------------------------------------------------------------

cd $TOPDIR                                          || exit 1
rm -fr bluegorilla                                  || exit 1

tar zxf \
    $SPC_BLUEGORILLA/upstream-bluegorilla.tar.gz    || exit 1
cd bluegorilla                                      || exit 1

patch -p0 << END                                    || exit 1
--- suite/installer/package-manifest.in
+++ suite/installer/package-manifest.in
@@ -831,9 +831,6 @@
 @BINPATH@/mozMapi32.dll
 #endif

-#ifdef MOZ_MOVEMAIL
-@RESPATH@/isp/movemail.rdf
-#endif
 @RESPATH@/isp/rss.rdf
 @RESPATH@/isp/Bogofilter.sfd
 @RESPATH@/isp/DSPAM.sfd
END

sed -e 's@^Vendor=.*@Vendor=laclin@' \
    -e 's@^Name=.*@Name=bluegorilla@' \
    -e 's@^RemotingName=.*@RemotingName=bluegorilla@' \
    -i suite/app/application.ini                    || exit 1

# "mozilla"  needs to  be moved  into place  at build time.  A symlink
# won't work in this case.

rm -fr mozilla                                      || exit 1

#---------------------------------------------------------------------

cd $TOPDIR                                          || exit 1
rm -fr rest icedove*                                || exit 1

tar zxf $SPC_ICEDOVE/upstream-icedove.tar.gz        || exit 1
mv icedove rest                                     || exit 1

cd rest                                             || exit 1

sed -e 's@^Vendor=.*@Vendor=laclin@' \
    -e 's@^Name=.*@Name=icedove@' \
    -e 's@^RemotingName=.*@RemotingName=icedove@' \
    -i mail/app/application.ini                     || exit 1

rm -fr mozilla suite uxp UXP                        || exit 1
ln -nsf ../albusluna/platform mozilla               || exit 1

#---------------------------------------------------------------------

cd $TOPDIR/                                         || exit 1
chmod 755 `find . -type f -name mach`

treetextsub --keeptimes \
    --pattern=binaryoutcast.com \
    --newtext=binarymoocast.cow  || true

treetextsub --keeptimes \
    --pattern=binaryoutcast.org \
    --newtext=binarycowcast.moo  || true

treetextsub --keeptimes \
    --pattern=palemoon.org \
    --newtext=alespoon.xyz       || true

treetextsub \
    --pattern=http://start.alespoon.xyz/ \
    --newtext=chrome://albushome/content/index.html \
    --keeptimes                  || true

treetextsub \
    --pattern=https://www.alespoon.xyz/firstrun.shtml \
    --newtext=chrome://albushome/content/firstrun.html \
    --keeptimes                  || true

treetextsub \
    --pattern=http://www.alespoon.xyz/releasenotes.shtml \
    --newtext=chrome://albushome/content/release.html \
    --keeptimes                  || true

treetextsub \
    --pattern=https://www.alespoon.xyz/releasenotes.shtml \
    --newtext=chrome://albushome/content/release.html \
    --keeptimes                  || true

#---------------------------------------------------------------------

cd $TOPDIR/albusluna/platform/                      || exit 1
echo apply platform patches to AlbusLuna UXP
patch -p0 < $SPCDIR/misc/platform01.patch           || exit 1

#---------------------------------------------------------------------

echo interlink
cd $TOPDIR                                          || exit 1
rm -fr interlink                                    || exit 1
tar zxf $SPC_INTERLINK/upstream-interlink.tgz       || exit 1
cd     interlink                                    || exit 1

rm -fr mozilla platform                             || exit 1
ln -s  ../rest/mozilla .                            || exit 1
ln -s  mozilla platform                             || exit 1

chmod 755 `find . -type d`                          || exit 1

treetextsub --keeptimes \
    --pattern=binaryoutcast.com --newtext=binarymoocast.xyz  || true
treetextsub --keeptimes \
    --pattern=palemoon.org      --newtext=alespoon.xyz       || true

#---------------------------------------------------------------------

echo misc.

cd $TOPDIR                                          || exit 1
cp -p $SPCDIR/buildme.sh       .                    || exit 1
cp -p $SPCDIR/uxp-browsers.txt .                    || exit 1
cp -p $SPCDIR/CHANGES          .                    || exit 1
chmod 755 buildme.sh                                || exit 1

cat > 00README << END                               || exit 1
See "uxp-browsers.txt".
END
cp -p 00README readme.txt                           || exit 1

#---------------------------------------------------------------------

cd $TOPDIR/albusluna                                || exit 1

cp -p $SPC_ALBUSLUNA/albusluna-icon.png \
      albusluna.png                                 || exit 1
perl  $SPC_ALBUSLUNA/setup-branding-albusluna.pl    || exit 1

#---------------------------------------------------------------------

cd $TOPDIR                                          || exit 1
for x in \
    albusluna/albusluna/app/profile/albusluna.js \
    basilisk/basilisk/app/profile/basilisk.js \
    bluegorilla/suite/browser/browser-prefs.js \
    interlink/navigator/app/defaults/pref/application.js
do
    ls $x                                           || exit 1
    echo >> $x                                      || exit 1
    cat $SPCDIR/misc/user-add.js >> $x              || exit 1
done

#---------------------------------------------------------------------
# Fix "Borealis" bookmarks.

patch -p1 << END
--- uxp-browsers.old/interlink/navigator/app/defaults/profile/bookmarks.html
+++ uxp-browsers/interlink/navigator/app/defaults/profile/bookmarks.html
@@ -13,10 +13,7 @@
 <DL><p>
     <DT><H3 PERSONAL_TOOLBAR_FOLDER="true" ID="NC:PersonalToolbarFolder">Personal Toolbar Folder</H3>
     <DL><p>
-        <DT><A HREF="https://repo.alespoon.xyz/binaryoutcast/binoc-central">Unified BinOC Repository</a>
-        <DT><A HREF="http://xref.binarymoocast.xyz/binoc-central/">BinOC Cross Reference</a>
-        <DT><A HREF="https://repo.alespoon.xyz/MoonchildProductions/UXP">UXP Repository</a>
-        <DT><A HREF="http://xref.alespoon.xyz/uxp-trunk/">UXP Cross Reference</a>
+        <DT><A HREF="https://google.com/">Google</a>
     </DL><p>
 </DL><p>
END

#---------------------------------------------------------------------
# Set up "RemotingName" for browsers.

# This code  allows UXP browsers to run concurrently.  Note: This code
# may not be needed as these cases should have been taken care of fur-
# ther up.

sed -e 's/^RemotingName=.*/RemotingName=albusluna/' \
    -i albusluna/albusluna/app/application.ini

sed -e 's/^RemotingName=.*/RemotingName=ambassador/' \
    -i ambassador/ambassador/app/application.ini

sed -e 's/^RemotingName=.*/RemotingName=bluegorilla/' \
    -i bluegorilla/suite/app/application.ini

sed -e 's/^RemotingName=.*/RemotingName=borealis/' \
    -i interlink/navigator/app/application.ini

#---------------------------------------------------------------------
# Set up "RemotingName" for email programs.

# This  code  prevents  UXP email programs from  running concurrently.
# Note:  This code may not be needed  as these cases  should have been
# taken care of further up.

sed -e 's/^RemotingName=.*/RemotingName=thundergroup/' \
    -i bluegorilla/mail/app/application.ini

sed -e 's/^RemotingName=.*/RemotingName=thundergroup/' \
    -i epyrus/mail/app/application.ini

sed -e 's/^RemotingName=.*/RemotingName=thundergroup/' \
    -i interlink/mail/app/application.ini

sed -e 's/^RemotingName=.*/RemotingName=thundergroup/' \
    -i rest/mail/app/application.ini

#---------------------------------------------------------------------
# Alternate "RemotingName" mode.

# If you'd  like to  allow Thunderbird UXP forks  to run concurrently,
# enable  this block.  Warning: If you set up  different  Thunderbirds
# to share email profiles, this may lead to data corruption.

if [ 0 == 1 ]; then
    NN=0
    for x in `find . -type f -name application.ini | grep /mail/app/`
    do
        NN=`expr $NN + 1`
        sed -e "s/^RemotingName=.*/RemotingName=laclin$NN/" \
            -i $x                                   || exit 1
    done
fi

#---------------------------------------------------------------------
# Additional fixes and/or patches.

cd $TOPDIR                                          || exit 1

chmod 755 \
    `find . -type f -name config.guess` \
    `find . -type f -name mach`                     || exit 1

treetextsub \
    --pattern='pref("extensions.autoDisableScopes", 15);' \
    --newtext='pref("extensions.autoDisableScopes", 0);' \
    --keeptimes

sed -e 's/^Vendor=.*/Vendor=laclin/' \
    -i `find . -type f -name application.ini`       || exit 1

sed -e 's/"Binary Outcast"/"Laclin"/' \
    -i `find . -type f -name brand.dtd`             || exit 1

FN=RemoteSpellCheckEngineChild.h
sed -e "s@illa/$FN@illa/mozSpellChecker.h@" \
    -i `find . -type f -name ContentChild.cpp`      || exit 1

sed -e '/#include."mozilla\/Telemetry.h"/d' \
    -e '/XRE_TelemetryAccumulate/d'   \
    -e '/XRE_StartupTimelineRecord/d' \
    -i `find . -type f -name nsMailApp.cpp` \
       `find . -type f -name nsSuiteApp.cpp`        || exit 1

sed -e '/chrome\/recording/d' \
    -e '/components\/telemetry\.xpt/d' \
    -e '/nssdbm3/d' \
    -i `find . -type f -name package-manifest.in`   || exit 1

sed -e 's/^MOZ_APP_VERSION=.*/MOZ_APP_VERSION=52.9/' \
    -i bluegorilla/suite/confvars.sh                || exit 1

FN=package-manifest.in
sed -e '/mozProtocolHandler/d' \
    -e '/recording-cmdline/d' \
    -i `find . -type f -name $FN | \
grep -P '(bluegorilla)'`                            || exit 1

FN=application-branding.js
for x in `find interlink/ -type f -name $FN | grep -v /unofficial`
do
    cp -p interlink/mail/branding/unofficial/$FN $x || exit 1
done

#---------------------------------------------------------------------
# Wrap it up.

cd $TOPDIR                                          || exit 1

rm -fr */.git*                                      || exit 1
settreedate .                                       || exit 1
cd ..                                               || exit 1

tar jcf      $TARFILE $BASEDIR                      || exit 1
tardate      $TARFILE                               || exit 1
rm -fr                $BASEDIR                      || exit 1
ls -l $ABOVE/$TARFILE                               || exit 1

#---------------------------------------------------------------------
# End of file.
