1#!/usr/bin/env bash
2
3PERL_SCRIPT="claws-mail-kdeservicemenu.pl"
4DESKTOP="claws-mail-attach-files.desktop"
5
6function check_environ {
7echo "Checking for kde4-config..."
8if [ ! -z "$(type 'kde4-config' 2> /dev/null)" ]; then
9  echo "Found kde4-config..."
10  SERVICEMENU_DIR="share/kde4/services/ServiceMenus"
11  DESKTOP_TEMPLATE="claws-mail-attach-files.desktop.kde4template"
12  KDECONFIG="kde4-config"
13else
14  echo "kde4-config not found..."
15  echo "Checking for kde-config..."
16  if [ ! -z "$(type 'kde-config' 2> /dev/null)" ]; then
17      echo "Found kde-config..."
18      SERVICEMENU_DIR="share/apps/konqueror/servicemenus"
19      DESKTOP_TEMPLATE="claws-mail-attach-files.desktop.template"
20      KDECONFIG="kde-config"
21  else
22    echo "kde-config not found..."
23    echo "asking user to find kde4-config or kde-config..."
24    KDECONFIG=$(kdialog --title "Locate kde-config or kde4-config" --getopenfilename / )
25    test -z $KDECONFIG && exit 1
26    if [[ $KDECONFIG == *4-config ]]; then
27      SERVICEMENU_DIR="share/kde4/services/ServiceMenus"
28      DESKTOP_TEMPLATE="claws-mail-attach-files.desktop.kde4template"
29    else
30      SERVICEMENU_DIR="share/apps/konqueror/servicemenus"
31      DESKTOP_TEMPLATE="claws-mail-attach-files.desktop.template"
32    fi
33  fi
34fi
35}
36
37function install_all {
38echo "Generating $DESKTOP ..."
39SED_PREFIX=${PREFIX//\//\\\/}
40sed "s/SCRIPT_PATH/$SED_PREFIX\\/bin\\/$PERL_SCRIPT/" $DESKTOP_TEMPLATE > $DESKTOP
41echo "Installing $PREFIX/$SERVICEMENU_DIR/$DESKTOP"
42mv -f $DESKTOP $PREFIX/$SERVICEMENU_DIR/$DESKTOP
43if [[ $? -ne 0 ]]
44then
45  kdialog --error "Could not complete installation."
46  exit
47fi
48echo "Installing $PREFIX/bin/$PERL_SCRIPT"
49cp -f $PERL_SCRIPT $PREFIX/bin/
50echo "Setting permissions ..."
51chmod 0644 $PREFIX/$SERVICEMENU_DIR/$DESKTOP
52chmod 0755 $PREFIX/bin/$PERL_SCRIPT
53echo "Finished installation."
54kdialog --msgbox "Finished installation."
55}
56
57function uninstall_all {
58echo "Removing $PREFIX/$SERVICEMENU_DIR/$DESKTOP"
59rm $PREFIX/$SERVICEMENU_DIR/$DESKTOP
60if [[ $? -ne 0 ]]
61then
62  kdialog --error "Could not complete uninstall."
63  exit
64fi
65echo "Removing $PREFIX/bin/$PERL_SCRIPT"
66rm $PREFIX/bin/$PERL_SCRIPT
67echo "Finished uninstall."
68kdialog --msgbox "Finished uninstall."
69}
70
71function show_help {
72    echo "Usage: $0 [--global|--local|--uninstall-global|--uninstall-local]"
73    echo
74    echo "    --global            attempts a system-wide installation."
75    echo "    --local             attempts to install in your home directory."
76    echo "    --uninstall-global  attempts a system-wide uninstallation."
77    echo "    --uninstall-local   attempts to uninstall in your home directory."
78    echo
79    exit 0
80}
81
82if [ -z $1 ]
83    then option="--$(kdialog --menu "Please select installation type" \
84				local "install for you only" \
85				global "install for all users" \
86				uninstall-local "uninstall for you only" \
87				uninstall-global "uninstall for all users"  2> /dev/null)"
88    else option=$1
89fi
90
91case $option in
92  "--global" )
93    check_environ
94    PREFIX=$($KDECONFIG --prefix)
95    echo "Installing in $PREFIX/$SERVICEMENU_DIR ..."
96    if [ "$(id -u)" != "0" ]; then
97	exec kdesu "$0 --global"
98    fi
99    install_all
100    ;;
101  "--local" )
102    check_environ
103    PREFIX=$($KDECONFIG --localprefix)
104    echo "Installing in $PREFIX$SERVICEMENU_DIR ..."
105    if [ ! -d $PREFIX/bin ]; then
106      mkdir $PREFIX/bin
107    fi
108    if [ ! -d $PREFIX/$SERVICEMENU_DIR ]; then
109      mkdir $PREFIX/$SERVICEMENU_DIR
110    fi
111    install_all
112    ;;
113  "--uninstall-global" )
114    check_environ
115    PREFIX=$($KDECONFIG --prefix)
116    echo "Uninstalling from $PREFIX/$SERVICEMENU_DIR ..."
117    if [ "$(id -u)" != "0" ]; then
118	exec kdesu "$0 --uninstall-global"
119    fi
120    uninstall_all
121    ;;
122  "--uninstall-local" )
123    check_environ
124    PREFIX=$($KDECONFIG --localprefix)
125    echo "Uninstalling from $PREFIX$SERVICEMENU_DIR ..."
126    uninstall_all
127    ;;
128  "-h" )
129    show_help
130    ;;
131  "--help" )
132    show_help
133    ;;
134  * )
135    show_help
136esac
137
138echo "Done."
139