#!/bin/sh
# test we are root
if [ "$USER" != "root" ] ; then
   echo "Run as root please, exiting." 
   exit 1                                                      
fi

export CFLAGS="-mtune=generic -Os -pipe"
export CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe "

P=android-file-transfer
V=gitzip 2018/01/22
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc cmake readline-dev fuse-dev "
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://codeload.github.com/whoozle/$P-linux/zip/master -O master  " $USER 
unzip master
mkdir $P
cd $P-linux-master
mkdir build
cd build
cmake ..
make -j5
make install DESTDIR=/tmp/$P
cd /tmp
# install-strip not supported

# no dev use source
##### 

# doc
#####
mkdir -p $P-doc/usr/local/share/doc/$P
mv $P/usr/local/share/gtk-doc/ $P-doc/usr/local/share/
cp $P-linux-master/LICENSE $P-doc/usr/local/share/doc/$P
cp $P-linux-master/FAQ* $P-doc/usr/local/share/doc/$P
cp $P-linux-master/README* $P-doc/usr/local/share/doc/$P

# main
#####
mkdir -p $P/usr/local/share/doc/$P
echo 'see doc tcz for license' > $P/usr/local/share/doc/$P/COPYING
# strip
cd $P 
find . | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
cd /tmp

# TCZ them
#######
LIST="$P $P-doc "
for Z in $LIST
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find usr -not -type d > /tmp/$Z.tcz.list
        sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
	cd /tmp
done

echo 'fuse.tcz
usb-utils.tcz' > $P.tcz.dep


cat > $P.tcz.info << "EOF"
Title:          android-file-transfer.tcz  
Description:    Connect PC to Android for file tranfers
Version:        gitzip 2018/01/22
Author:         Vladimir Menshakov  
Original-site:  https://github.com/whoozle/android-file-transfer-linux
Copying-policy: GPL v3
Size:		
Extension_by:   aus9 at gmx dot com
Tags:           xfce android 
Comments:       One method of transferring files from/to Android/PC
                Does not use udev/rules
 
                If you're happy with gmtp/gvfs/mtpfs or any other 
                mtp software, you might not need this TCZ 
                
                See the website for known problems, eg samsung phones
                are read only. (untested as I have non-samsung)

                Android Instructions 
                - Enable USB debugging 
                -> System settings -> about phone
                -> TAP "build number" 7 times to enable developer mode
                -> Go into new dir and turn on usb debugging

                Data cable is connected PC to a live phone

                On phone enable file transfer (default is charging)
                ---on most androids, its a pull down notification.
                
                PC Instructions 
                ----vary depending on your home persistance,
                commands below are non-root commands
                -> Make a dir to access files EG $ mkdir ~/Android
                -> $ aft-mtp-mount ~/Android
                (you may see this type of error EG # device is already in use)
                -> ignore that error and use your fav File Manager to navigate to 
                that new dir. (EG Android)
                Thunar works as expected. Ditto spacefm

                My error has more info at the website but I can ignore it as
                Thunar or SpaceFM etc still mounted phone
                image of error....and no need to load mtp TCZs
                https://imgur.com/a/rG3D4
                proof Thunar sees Android OK (not a Samsung)
                https://imgur.com/a/tJzEq
 


                Compiled for 64 9.x
Change-log:     2018/01/07  First version 
Current:        2018/01/07
EOF

cat > $P-doc.tcz.info << "EOF"
Title:          android-file-transfer-doc.tcz
Description:    docs
Version:        gitzip 2018/01/22
Author:         Vladimir Menshakov 
Original-site:  https://github.com/whoozle/android-file-transfer-linux
Copying-policy: GPL v3
Size:		
Extension_by:   aus9 at gmx dot com
Tags:           xfce 
Comments:       some docs
                
                Compiled for 64 9.x
Change-log:     2018/01/07  First version 
Current:        2018/01/07 
EOF

submitqc --libs