#!/bin/sh

#Note regarding adaptation for DebianDog distros:.
# - Edit /etc/frsibee/frisbee.conf: Change 'editor=' to specify correct editor
# - Move /usr/local/frisbee/func to /usr/lib/frisbee/.
# - Remove /usr/lib/dhcpcd-hooks/99-frisbee_tray and its link in
#   /usr/libexec/dhcpcd-hooks, from DebianDogs.

if test $(pwd) = "/"{

 export TEXTDOMAIN=frisbee
 export OUTPUT_CHARSET=UTF-8

 #Clear all flags...
 rm -f etc/frisbee/.* 2>/dev/null #residue

 #Remove residue from prior frisbee versions...
 rm -f usr/local/frisbee/hook_notify
 rm -rf etc/frisbee/iface 2>/dev/null
 rm -f root/Startup/network_tray_modeset
 rm -f etc/dhcpcd_dropwait_secs
 rm -f etc/frisbee/interfaces

 #Remove residue from prior frisbee versions...
 if test -d lib/dhcpcd/dhcpcd-hooks{
  rm -f lib/dhcpcd/dhcpcd-hooks/99-frisbee
  rm -f lib/dhcpcd/dhcpcd-hooks/99-down
  rm -f lib/dhcpcd/dhcpcd-hooks/99-ifup
  rm -f lib/dhcpcd/dhcpcd-hooks/99-release
  rm -f lib/dhcpcd/dhcpcd-hooks/99-timeout
  rm -f lib/dhcpcd/dhcpcd-hooks/99-up
 }
 if test ! -h usr/libexec/dhcpcd-hooks -a -d usr/libexec/dhcpcd-hooks{
  rm -f usr/libexec/dhcpcd-hooks/99-frisbee
  rm -f usr/libexec/dhcpcd-hooks/99-down
  rm -f usr/libexec/dhcpcd-hooks/99-ifup
  rm -f usr/libexec/dhcpcd-hooks/99-release
  rm -f usr/libexec/dhcpcd-hooks/99-timeout
  rm -f usr/libexec/dhcpcd-hooks/99-up
 }
 if test -d usr/lib/dhcpcd/dhcpcd-hooks{
  rm -f usr/lib/dhcpcd/dhcpcd-hooks/99-frisbee
  rm -f usr/lib/dhcpcd/dhcpcd-hooks/99-down
  rm -f usr/lib/dhcpcd/dhcpcd-hooks/99-ifup
  rm -f usr/lib/dhcpcd/dhcpcd-hooks/99-release
  rm -f usr/lib/dhcpcd/dhcpcd-hooks/99-timeout
  rm -f usr/lib/dhcpcd/dhcpcd-hooks/99-up
 }

 #140526 Remove files and menu entries from Frisbee betas.
 rm -f usr/sbin/install-frisbee
 rm -f usr/share/applications/install-frisbee.desktop
 rm -fr usr/share/frisbee #150604

 #140526 Remove files and placeholders for renamed and moved internal scripts.
 rm -f usr/local/bin/fgprs_*connect

 #150301 Remove possible residue from dhcpcd.conf...
 if grep -sq 'nohook 10-wpa_supplicant' etc/frisbee/dhcpcd.conf{
  sed -i '/nohook 10-wpa_supplicant/d' etc/frisbee/dhcpcd.conf
 }

 #140818 150228 Disable old frisbee initialization script file but add to files list, or remove it.
 if test -f etc/init.d/frisbee{
  chmod a-x etc/init.d/frisbee #150301
  shell {
   sleep 3
   test $(ls root/.packages/frisbee-1.*.files 2>/dev/null) \
    && sed -i 's%/etc/init.d/frisbee.sh%/etc/init.d/frisbee\n&%' root/.packages/frisbee-1.*.files
  } &
 }

 ###End of residue removal section.


 #160610 Add frisbee menu item if connectwizard not present (e.g., *dogs).
 if test ! -x usr/sbin/connectwizard{
  sed -i '/^NoDisplay=/ s/=.*/=false/' \
   usr/share/applications/frisbee.desktop
 }

 # Allow installer to make frisbee the default network manager.
 if Xdialog  --title "Frisbee" --default-no --timeout 60 --ok-label "Yes, set as default" --cancel-label "No" --left --yesno "$(gettext "Frisbee is installed as one of the Connect Wizard network manager options.")\n\n$(gettext "Do you want frisbee to be the default network manager at the next boot-up or\nat the initial boot of a distro package?")" 0 0{
  echo -e '#!/bin/sh\nexec frisbee' > usr/local/bin/defaultconnect
  sed -i -e '/^frisbee_mode=/ s/=.*/=1/' \
  -e '/^wireless_enabled=/ s/=.*/=1/' \
  -e '/^wireless_autostart=/ s/=.*/=1/' \
  -e '/^announce_state_changes=/ s/=.*/=1/' \
  etc/frisbee/frisbee.conf
  test -f etc/dhcpcd_state_notify || touch etc/dhcpcd_state_notify
 } else {
  rm -f etc/dhcpcd_state_notify
 }

 #160213 remove files from old locations - for builds, assume updated connectwizard_2nd is present....
 rm -f usr/local/bin/frisbee_mode_disable
 rm -f usr/local/frisbee/connect
 rm -f usr/local/frisbee/disconnect
 rm -f usr/local/bin/frisbee_cli
 if ! grep -qE 'frisbee --|frisbee_cli --' usr/local/apps/Connect/AppRun{
  ln -snf frisbee usr/local/bin/frisbee_mode_disable
  ln -snf ../bin/frisbee usr/local/frisbee/connect
  ln -snf ../bin/frisbee usr/local/frisbee/disconnect
  test $(ls root/.packages/frisbee-1.*.files 2>/dev/null) \
   && sed -i -e 's%/usr/local/bin/frisbee$%&\n/usr/local/bin/frisbee_mode_disable%' \
   -e 's%/usr/local/frisbee/frisbee-gprs-connect$%/usr/local/frisbee/connect\n/usr/local/frisbee/disconnect\n&%' \
   root/.packages/frisbee-1.*.files
 } elif grep -q 'frisbee_cli --' usr/local/apps/Connect/AppRun{
  ln -snf frisbee usr/local/bin/frisbee_cli
  test $(ls root/.packages/frisbee-1.*.files 2>/dev/null) \
   && sed -i -e 's%/usr/local/bin/frisbee$%&\n/usr/local/bin/frisbee_cli%' \
   root/.packages/frisbee-1.*.files
 }

 #Remove old gprs.conf, to generate new one.
 rm -f etc/gprs.conf
 rm -f root/.config/gprs.conf

 #Remove replaced options file, if not used by pgprs.
 test -f usr/sbin/pgprs && rm -f etc/ppp/options.gprs
}