#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   exit 1
fi

# python no flags...Juanito removes po files as python2 setup.py compile_translations fails 
# setup.py mentions pybabel and we have only python3.9-babel + xgettext but we do have gettext -> check later on python3.9 attempt

P=wicd
V=1.7.4
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="submitqc dbus-python-dev wireless_tools net-tools pygtk-dev dhcpcd wpa_supplicant-dbus  "
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 \
http://tinycorelinux.net/7.x/x86_64/tcz/src/wicd/$SRC.tar.gz \
http://tinycorelinux.net/7.x/x86_64/tcz/src/wicd/wicd_netentry.patch \
http://tinycorelinux.net/7.x/x86_64/tcz/src/wicd/wicd_urwid3.patch " $USER

# convert Juanito's live patches into source patches
sed 's|usr/local/share/wicd/||' -i *.patch
 
tar xvf $SRC*gz
cd wicd-1.7.4
rm -f po/*.po  
patch -Np1 curses/netentry_curses.py	< ../wicd_netentry.patch
patch -Np1 curses/wicd-curses.py < ../wicd_urwid3.patch
sed -e "/wpath.logrotate\|wpath.systemd/d" -e "/detection failed/ a\                self.init=\'init\/default\/wicd\'" -i.orig setup.py
# as we are not installing some things I remove some lines from configure
# setup.py default is ('logrotate=', None,.....and no-install-logrotate fails but removed config line too

python2 setup.py configure \
--no-install-acpi --no-install-init --no-install-kde --no-install-pmutils --no-install-gnome-shell-extensions \
--autostart=/usr/local/etc/xdg/autostart --backends=/usr/local/share/wicd/backends --bin=/usr/local/bin --cli=/usr/local/share/wicd/cli \
--curses=/usr/local/share/wicd/curses --daemon=/usr/local/share/wicd/daemon --desktop=/usr/local/share/applications \
--dbus=/usr/local/etc/dbus-1/system.d --dbus-service=/usr/local/share/dbus-1/system-services  \
--docdir=/usr/local/share/doc/wicd --encryption=/usr/local/etc/wicd/encryption/templates  --etc=/usr/local/etc/wicd --gtk=/usr/local/share/wicd/gtk \
--icons=/usr/local/share/icons/hicolor --images=/usr/local/share/wicd/icons --lib=/usr/local/lib/wicd \
--mandir=/usr/local/share/man --pixmaps=/usr/local/share/pixmaps --python=/usr/local/bin/python2 --scripts=/usr/local/etc/wicd/scripts \
--sbin=/usr/local/sbin --share=/usr/local/share/wicd --wicdgroup=staff

python2 setup.py install --prefix=/usr/local --root=/tmp/$P
cd /tmp

# doc
##### heaps of file perms to fix
mkdir -p $P-doc/usr/local/share/doc/
mv $P/usr/local/share/man $P-doc/usr/local/share
mv $P/usr/local/share/doc/wicd $P-doc/usr/local/share/doc/
find $P-doc -type f | xargs chmod 644
# delete unloved files
cd $P-doc/usr/local/share/doc/$P
rm -rf CHANGES INSTALL LICENSE 
cd /tmp


# main
#####
mkdir -p $P/usr/local/etc/init.d/
cat > $P/usr/local/etc/init.d/wicd << "EOF"
#!/bin/sh
# original by  Sercan Arslan <arslanserc@gmail.com> AKA Arslan S. # note the dot
# changed to dbus style aus9 
start() {
     if [ ! -e /var/run/wicd/wicd.pid ]; then
        echo "starting wicd daemon..."
        /usr/local/sbin/wicd 2>/dev/null
        [ -d /var/run/wicd ] || mkdir -p /var/run/wicd
        [ -e  /var/run/wicd/wicd.pid ] || touch /var/run/wicd/wicd.pid
     fi
}

stop() {
    echo "stopping wicd daemon..."
    pkill -f wicd-daemon.py 2>/dev/null
    rm -rf /var/run/wicd/wicd.pid
}
  
status() {
      if [ -e /var/run/wicd/wicd.pid ]; then
      echo -e "\nwicd is running.\n"
      exit 0
         else
         echo -e "\nwicd is not running.\n"
         exit 1
      fi
}

case $1 in
     start) start
     ;;
     stop) stop
     ;;
     status) status
     ;;
     restart) stop; start
     ;;
     *) echo -e "\n$0 [start|stop|restart|status]\n"
     ;;
esac

EOF
chmod 755 $P/usr/local/etc/init.d/wicd 

# submitqc finds wrong perms many fixes below
chmod 755 $P/usr/local/bin/*
chmod 644 $P/usr/local/etc/dbus-1/system.d/wicd.conf
chmod 644 $P/usr/local/etc/wicd/encryption/templates/*
chmod 644 $P/usr/local/etc/wicd/dhclient.conf.template.default
chmod 755 $P/usr/local/lib/python2.7/site-packages/wicd/*.py
chmod 755 $P/usr/local/sbin/*
chmod 644 $P/usr/local/share/dbus-1/system-services/org.wicd.daemon.service
chmod 644 $P/var/lib/wicd/WHEREAREMYFILES
chmod 644 $P/usr/local/share/wicd/gtk/wicd.ui
find $P -name .empty_on_purpose  | xargs chmod -v 644
find $P -name *.png   | xargs chmod -v 644
find $P -name *.py   | xargs chmod -v 755
echo 'X-FullPathIcon=/usr/local/share/pixmaps/wicd-gtk.png ' >> $P/usr/local/share/applications/wicd.desktop
find $P -name *.desktop  | xargs chmod -v 644
sed 's|--no-tray|--tray|' -i $P/usr/local/share/applications/wicd.desktop
sed 's|Application|System|' -i $P/usr/local/share/applications/wicd.desktop
sed 's|Wicd Network Manager|Wicd with tray support|' -i $P/usr/local/share/applications/wicd.desktop
sed 's|without|with|' -i $P/usr/local/share/applications/wicd.desktop
# sed was supposed to substitute the first Application but has done a g on me so new fix
sed 's|Type=System|Type=Application|' -i $P/usr/local/share/applications/wicd.desktop

# doc TCE got all dox so start again 
mkdir -p $P/usr/local/share/doc/$P/
echo 'GPL v2' > $P/usr/local/share/doc/$P/LICENSE
sed 's|bash|sh|' -i $P/usr/local/bin/wicd-cli
# we don't want librsvg as a dep 
rm -rf $P/usr/local/share/icons/hicolor/scalable
rm -rf $P/usr/local/share/wicd/icons/hicolor/scalable
# change xpm to png pixmap
rm -rf $P/usr/local/share/pixmaps/wicd-gtk.xpm
cp $P/usr/local/share/icons/hicolor/48x48/apps/wicd-gtk.png $P/usr/local/share/pixmaps

# env value python on 15x needs python2 
sed 's|python|python2|' -i $P/usr/local/lib/python2.7/site-packages/wicd/*.py
sed 's|python|python2|' -i $P/usr/local/share/wicd/backends/*.py
sed 's|python|python2|' -i $P/usr/local/share/wicd/cli/*.py
sed 's|python|python2|' -i $P/usr/local/share/wicd/curses/*.py
sed 's|python|python2|' -i $P/usr/local/share/wicd/daemon/*.py
sed 's|python|python2|' -i $P/usr/local/share/wicd/gtk/*.py

# test of wicd-curses fails due to undefined symbol: acs_map which apparently is ncursew rebuild issue
# therefore I have removed wicd-curses.....updating for current ncursesw against python-urwid will result in python3 series
rm -rf $P/usr/local/bin/wicd-curses
rm -rf $P/usr/local/share/wicd/curses
# Juanito has no pyc files
rm -rf $P/usr/local/lib/python2.7/site-packages/wicd/*.pyc
# attempting to reduce size by del translations.py* results in error
# ImportError: No module named translations

# TCE 
####
LIST2="$P $P-doc  "
for Z in $LIST2
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find . -not -type d | cut -c 2- | sort > /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

cat > $P-doc.tcz.info << "EOF"
Title:          wicd-doc.tcz
Description:    man pages and docs
Version:        1.7.4
Author:         Tom Van Braeckel, Adam Blackburn, Dan O'Reilly, Andrew Psaltis & David Paleino 
Original-site:  http://tinycorelinux.net/7.x/x86_64/tcz/src/wicd
Copying-policy: GPL v2
Size:           24K
Extension_by:   aus9 @linuxquestions.org
Tags:           network manager python python2
Comments:       Also contains Netherlands man pages
                
Change-log:     2024/10/23 v 1.7.4
Current:        2024/10/23 
EOF
echo 'man-db.tcz ' > $P-doc.tcz.dep

cat > $P.tcz.info << "EOF"
Title:          wicd.tcz
Description:    gtk python2 wifi manager
Version:        1.7.4
Author:         Tom Van Braeckel, Adam Blackburn, Dan O'Reilly, Andrew Psaltis & David Paleino 
Original-site:  http://tinycorelinux.net/7.x/x86_64/tcz/src/wicd
Copying-policy: GPL v2
Size:           248K
Extension_by:   aus9 @linuxquestions.org
Tags:           network manager python python2
Comments:       Load any required wifi device firmware, usb modules (if usb)
                before wicd. Recommend you load wireless-regdb too + see its info.
                GUI fails to disconnect/connect ethernet instead
                $ sudo ifconfig eth0 down 
                $ sudo ifconfig eth0 up 
                $ sudo udhcpc -i eth0
              
                Please restart dbus irrespective of current state. (Unless automating 
                below). If not, expect dbus connection errors.  Software that depends 
                on dbus eg large web browser may also need restart (not reboot). YMMV

                $ sudo /usr/local/etc/init.d/dbus restart
                $ sudo /usr/local/etc/init.d/wicd start

                Ways to open GUI:
                -- menu -> Network (or System)  -> Wicd 
                -- click icon in file manager for /usr/share/applications/Wicd*
                -- $ wicd-gtk --tray (or --no-tray)
                -- Assign keybindings if your desktop supports them.
                Note no harm in using --tray on desktops that have no panel support.
                eg hackedbox fvwm etc

                These methods attempt to put a tray into your panel, if you are
                running a DE/WM that supports gtk2 applets eg icewm
                I assume right handed mouse user: (reverse for lefties please)
                -- left click opens GUI
                -- mouse hover -> should show the connection and LAN address
                -- right click -> connection info -> shows speed and data 
                -- right click -> connect... may attempt to autoconnect to your
                   neighbour if you are not paying attention.

                For menu or left click applet -> gtk2 GUI should open.
                Against router name, click on properties -> pulldown encryption method 
                and choose method, likely WPA 1/2 passphrase
                Input passsphrase in box below
                Click on connect & wait -> must say "connected" at bottom of GUI
                
                If you wish to backup over reboot 
                add these to /opt/.filetool.lst
                usr/local/etc/wicd
                var/lib/wicd/configurations
               
                If you wish to automate over reboot add these to /opt/bootlocal.sh (tested)
                /usr/local/etc/init.d/dbus start
                /usr/local/etc/init.d/wicd start
                Note here dbus does not need a restart.
                
                TCE=python-urwid is broken due to a ncursesw issue
                so wicd-curses has been removed from this TCE
                TCE for wicd-doc offers things like 
                $ man wicd-wireless-settings.conf
                
Change-log:     2016/03/13 v 1.7.4 (Juanito)
                2017/03/01 patched for wicd-curses errors
Current:        2024/10/23 rebuilt on 15x - changed initid script - no install script (aus9) 
EOF

# the gtk python expects gtk2 > 2.16 ...yep....gtk2 is a dep of pygtk
# ethtool needed to disable/enable ethernet

echo 'dbus-python.tcz
dhcpcd.tcz
net-tools.tcz
pygtk.tcz
wpa_supplicant-dbus.tcz   ' > $P.tcz.dep

submitqc --libs 
rm -rf *.zsync

