Advertisement
Guest User

fvwm-menu-udisks

a guest
Jul 18th, 2011
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 21.40 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #=============================================================================#
  4. #          FILE: fvwm-menu-udisks (obdevicemenu ported to FVWM, attempt 2)    #
  5. #       VERSION: 1.4.0                                                        #
  6. #                                                                             #
  7. #   DESCRIPTION: an Openbox pipe menu for the management of removable media   #
  8. #                with Udisks ported to FVWM                                   #
  9. #       LICENSE: GPL2                                                         #
  10. #        AUTHOR: Jamie Nguyen (ported by greatant)                            #
  11. #=============================================================================#
  12.  
  13. # Copyright (C) 2011 Jamie Nguyen
  14. #
  15. # This program is free software; you can redistribute it and/or modify it
  16. # under the terms of the GNU General Public License v2 as published by the
  17. # Free Software Foundation.
  18. #
  19. # This program is distributed in the hope that it will be useful, but WITHOUT
  20. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  21. # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  22. # more details.
  23. #
  24. # You should have received a copy of the GNU General Public License along with
  25. # this program; if not, write to the Free Software Foundation, Inc.,
  26. # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  27.  
  28. #-------------------------------------#
  29. #           CONFIGURATION             #
  30. #-------------------------------------#
  31. # {{{
  32.  
  33. declare udisks="/usr/bin/udisks"
  34. declare filemanager="/usr/bin/Thunar"
  35. declare notify="/usr/bin/notify-send"
  36. declare notify_options="--expire-time=3000"
  37. declare optical_devices="-E ^/dev/sr[0-9]+"
  38. declare removable_devices="-E ^/dev/sd[b-z][0-9]*|^/dev/mmcblk[0-9]+p*[0-9]*"
  39. declare mount_options="--mount-options nosuid,noexec,noatime"
  40.  
  41. declare -i show_internal=0
  42. declare -i show_notifications=1
  43. declare -i show_optical_device_filename=1
  44. declare -i show_removable_device_filename=1
  45. declare -i fancy_sort=0
  46.  
  47. declare separator_removable_media="Removable media"
  48. declare separator_optical_media="Optical media"
  49. declare separator_device_info="Device information"
  50. declare info_udisks_failed="Udisks failed."
  51. declare info_no_devices="None"
  52. declare info_mounted="(mounted)"
  53. declare info_no_label="No label"
  54. declare msg_unmount_all_removable="Unmount all removable devices?"
  55. declare msg_unmount_all_optical="Unmount all optical devices?"
  56. declare msg_mounting_device="Mounting"
  57. declare msg_unmounting_device="Unmounting"
  58. declare msg_unmounted="unmounted successfully."
  59. declare msg_ejecting_device="Ejecting"
  60. declare item_eject="Eject"
  61. declare item_info="Info"
  62. declare item_mount="Mount"
  63. declare item_open="Open"
  64. declare item_unmount="Unmount"
  65. declare item_unmount_all="Unmount all"
  66. declare item_device_filename="device:"
  67. declare item_label="label:"
  68. declare item_vendor="vendor:"
  69. declare item_model="model:"
  70. declare item_type="type:"
  71. declare item_media="media:"
  72. declare item_device_size="size (device):"
  73. declare item_partition_size="size (partition):"
  74.  
  75. declare CONFIGFILE
  76.  
  77. if [[ -n "${XDG_CONFIG_HOME}" ]]; then
  78.     CONFIGFILE="${XDG_CONFIG_HOME}/obdevicemenu/config"
  79. else
  80.     CONFIGFILE="${HOME}/.config/obdevicemenu/config"
  81. fi
  82.  
  83. if [[ ! -f "${CONFIGFILE}" ]]; then
  84.     CONFIGFILE="/etc/obdevicemenu.conf"
  85. fi
  86.  
  87. if [[ -f "${CONFIGFILE}" ]]; then
  88.     . "${CONFIGFILE}"
  89.     if [[ $? -ne 0 ]]; then
  90.         printf '%s\n' "DestroyMenu recreate FvwmMenuUdisks"
  91.         printf '%s\n' "AddToMenu FvwmMenuUdisks fvwm-menu-udisks Title"
  92.         printf '%s\n' "+ \"Failed to source configuration file\""
  93.         exit 1
  94.     fi
  95. fi
  96.  
  97. if [[ ! -f "${udisks}" ]]; then
  98.         printf '%s\n' "DestroyMenu recreate FvwmMenuUdisks"
  99.         printf '%s\n' "AddToMenu FvwmMenuUdisks fvwm-menu-udisks Title"
  100.         printf '%s\n' "+ \"Udisks not installed\""
  101.     exit 1
  102. fi
  103. # }}}
  104.  
  105. #-------------------------------------#
  106. #            INTERNAL API             #
  107. #-------------------------------------#
  108. # {{{
  109.  
  110. notify() {
  111.     if [[ "${show_notifications}" = "1" ]] && [[ -f "${notify}" ]]; then
  112.         if [ -z "${notify_options}" ]; then
  113.             $notify "$*"
  114.         else
  115.             $notify ${notify_options} "$*"
  116.         fi
  117.     else
  118.         $*
  119.     fi
  120. }
  121.  
  122. # functions to retrieve information
  123. info_blank() {
  124.     $udisks --show-info ${1} \
  125.         | grep -m 1 -w "^[[:space:]]*blank:" \
  126.         | awk '{print $2}'
  127. }
  128. info_ejectable() {
  129.     $udisks --show-info ${1} \
  130.         | grep -m 1 -w "^[[:space:]]*ejectable:" \
  131.         | awk '{print $2}'
  132. }
  133. info_has_media() {
  134.     $udisks --show-info ${1} \
  135.         | grep -m 1 -w "^[[:space:]]*has media:" \
  136.         | awk '{print $3}'
  137. }
  138. info_internal() {
  139.     $udisks --show-info ${1} \
  140.         | grep -m 1 -w "^[[:space:]]*system internal:" \
  141.         | awk '{print $3}'
  142. }
  143. info_label() {
  144.     local info_label=
  145.     info_label="$($udisks --show-info ${1} \
  146.         | grep -m 1 -w "^  label:" \
  147.         | awk '{$1="";gsub(/_/,"");print substr($0, index($0,$2))}')"
  148.     if [[ -n "${info_label}" ]]; then
  149.         printf '%s\n' "${info_label}"; return
  150.     fi
  151. }
  152. info_media() {
  153.     $udisks --show-info ${1} \
  154.     | grep -m 1 -w "^[[:space:]]*media:" \
  155.     | awk '{gsub(/_/,"");print $2}'
  156. }
  157. info_model() {
  158.     $udisks --show-info ${1} \
  159.         | grep -m 1 -w "^[[:space:]]*model:" \
  160.         | awk '{$1="";gsub(/_/,"");print substr($0, index($0,$2))}'
  161. }
  162. info_mounted() {
  163.     $udisks --show-info ${1} \
  164.         | grep -m 1 -w "^[[:space:]]*is mounted:" \
  165.         | awk '{print $3}'
  166. }
  167. info_mountpath() {
  168.     $udisks --show-info ${1} \
  169.         | grep -m 1 -w "^[[:space:]]*mount paths:" \
  170.         | awk '{$1="";$2="";print substr($0, index($0,$3))}'
  171. }
  172. info_type() {
  173.     $udisks --show-info ${1} \
  174.         | grep -m 1 -w "^[[:space:]]*type:" \
  175.         | awk '{gsub(/_/,"");print $2}'
  176. }
  177. info_vendor() {
  178.     $udisks --show-info ${1} \
  179.         | grep -m 1 -w "^[[:space:]]*vendor:" \
  180.         | awk '{$1="";gsub(/_/,"");print substr($0, index($0,$2))}'
  181. }
  182. convert_size() {
  183.     local -i old_size="${1}"
  184.     local new_size=
  185.  
  186.     printf '%s\n' "${old_size}" | grep -ow -E ^[1-9]{1}[0-9]*$ >/dev/null 2>&1
  187.     [[ $? -ne 0 ]] && return 1
  188.  
  189.     if [[ "${old_size}" -gt "21474836480" ]]; then
  190.         new_size="$((${old_size}/1073741824)) GB"
  191.     elif [[ "${old_size}" -gt "10485760" ]]; then
  192.         new_size="$((${old_size}/1048576)) MB"
  193.     elif [[ "${old_size}" -gt "10240" ]]; then
  194.         new_size="$((${old_size}/1024)) kB"
  195.     else
  196.         new_size="${old_size} bytes"
  197.     fi
  198.  
  199.     printf '%s\n' "${new_size}"
  200. }
  201. info_device_size() {
  202.     local info_device_size=
  203.     info_device_size="$($udisks --show-info ${1} \
  204.         | grep -m 1 -w "^[[:space:]]*size:" \
  205.         | awk '{print $2}')"
  206.     if [[ -n "${info_device_size}" ]]; then
  207.         info_device_size="$(convert_size "${info_device_size}")"
  208.         printf '%s\n' "${info_device_size}"
  209.     fi
  210. }
  211. info_partition_size() {
  212.     local info_partition_size=
  213.     info_partition_size="$($udisks --show-info ${1} \
  214.         | grep -m 1 -w "  size:" \
  215.         | awk '{print $2}')"
  216.     if [[ -n "${info_partition_size}" ]]; then
  217.         info_partition_size="$(convert_size "${info_partition_size}")"
  218.         printf '%s\n' "${info_partition_size}"
  219.     fi
  220. }
  221.  
  222. # functions to perform udisks commands
  223. action_mount() {
  224.     local devname="${1}"
  225.     notify "$(printf '%s\n' "${msg_mounting_device} ${devname} ...")"
  226.     notify "$($udisks --mount ${devname} ${mount_options})"
  227. }
  228. action_unmount() {
  229.     local devname="${1}"
  230.     notify "$(printf '%s\n' "${msg_unmounting_device} ${devname} ...")"
  231.     notify "$($udisks --unmount ${devname})"
  232.     # if udisks unmounts successfully, there is no output from udisks
  233.     # so notify-send fails and will return 1
  234.     if [[ $? = '1' ]]; then
  235.         notify "$(printf '%s\n' "${devname} ${msg_unmounted}")"
  236.     fi
  237. }
  238. action_unmount_all() {
  239.     for devname in $*; do
  240.         notify "$(printf '%s\n' "${msg_unmounting_device} ${devname} ...")"
  241.         notify "$($udisks --unmount ${devname})"
  242.         if [[ $? = '1' ]]; then
  243.             notify "$(printf '%s\n' "${devname} ${msg_unmounted}")"
  244.         fi
  245.     done
  246. }
  247. action_eject() {
  248.     local devname="${1}"
  249.     local -i mounted=
  250.     mounted="$(info_mounted "${devname}")"
  251.     if [[ "${mounted}" -eq 1 ]]; then
  252.         action_unmount "${devname}"
  253.     fi
  254.     notify "$(printf '%s\n' "${msg_ejecting_device} ${devname} ...")"
  255.     notify "$($udisks --eject ${devname})"
  256. }
  257. action_open() {
  258.     local devname="${1}"
  259.     local info_mountpath="$(info_mountpath "${devname}")"
  260.     if [[ -n "${info_mountpath}" ]]; then
  261.         $filemanager "${info_mountpath}"
  262.     else
  263.         $filemanager
  264.     fi
  265. }
  266. action_info() {
  267.     local devname="${1}"
  268.     local devmajor="${devname%%[0-9]*}"
  269.  
  270.     $udisks --show-info ${devname} >/dev/null 2>&1
  271.     if [[ $? -ne 0 ]]; then
  272.         printf '%s\n' "DestroyMenu FvwmMenuUdisksInfo"
  273.         printf '%s\n' "AddToMenu FvwmMenuUdisksInfo fvwm-menu-udisks Title"
  274.         printf '%s\n' "+ \"${info_udisks_failed}\""
  275.         exit 0
  276.     fi
  277.  
  278.     local info_label="$(info_label "${devname}")"
  279.     local info_vendor="$(info_vendor "${devmajor}")"
  280.     local info_model="$(info_model "${devmajor}")"
  281.     local info_device_size="$(info_device_size "${devmajor}")"
  282.     local info_type="$(info_type "${devname}")"
  283.     local info_media="$(info_media ${devname})"
  284.  
  285.     if [[ "${devname}" != "${devmajor}" ]]; then
  286.         local info_partition_size="$(info_partition_size "${devname}")"
  287.     fi
  288.  
  289.     printf '%s\n' "DestroyMenu FvwmMenuUdisksInfo"
  290.     printf '%s\n' "AddToMenu FvwmMenuUdisksInfo \"${separator_device_info}\" Title"
  291.  
  292.     [[ -n "${info_device_filename}" ]] && \
  293.         printf '%s\n' "+ \"${item_device_filename} ${devname}\""
  294.     [[ -n "${info_label}" ]] && \
  295.         printf '%s\n' "+ \"${item_label} ${info_label}\""
  296.     [[ -n "${info_vendor}" ]] && \
  297.         printf '%s\n' "+ \"${item_vendor} ${info_vendor}\""
  298.     [[ -n "${info_model}" ]] && \
  299.         printf '%s\n' "+ \"${item_model} ${info_model}\""
  300.     [[ -n "${info_revision}" ]] && \
  301.         printf '%s\n' "+ \"${item_revision} ${info_revision}\""
  302.     [[ -n "${info_type}" ]] && \
  303.         printf '%s\n' "+ \"${item_type} ${info_type}\""
  304.     [[ -n "${info_version}" ]] && \
  305.         printf '%s\n' "+ \"${item_version} ${info_version}\""
  306.     [[ -n "${info_media}" ]] && \
  307.         printf '%s\n' "+ \"${item_media} ${info_media}\""
  308.     [[ -n "${info_partition_size}" ]] && \
  309.         printf '%s\n' "+ \"${item_partition_size} ${info_partition_size}\""
  310.     [[ -n "${info_device_size}" ]] && \
  311.         printf '%s\n' "+ \"${item_device_size} ${info_device_size}\""
  312. }
  313. fancy_sort() {
  314.     # this is a very hacky way to sort devices so that /dev/sdc11 wont come
  315.     # before /dev/sdc2, which happens due to a shortcoming of the sort command
  316.  
  317.     # we wont tell bash that partition_number is a number, otherwise it
  318.     # breaks when leading zeros are added
  319.     local devname= devmajor= partition_number=
  320.     local -i j=
  321.  
  322.     # first lets put a leading zero in front of single digits (sdc1 -> sdc01).
  323.     # we are going to ignore /dev/mmcblk* devices... too complicated to sort.
  324.     j=0
  325.     for devname in ${removable[@]}; do
  326.         if [[ "${devname}" =~ ^/dev/dm-[0-9]+ ]]; then
  327.             devmajor="${devname%%[0-9]*}"
  328.         elif [[ "${devname}" =~ ^/dev/fd[0-9]+ ]]; then
  329.             devmajor="${devname%%[0-9]*}"
  330.         elif [[ "${devname}" =~ ^/dev/sd[a-z][0-9]+ ]]; then
  331.             devmajor="${devname%%[0-9]*}"
  332.         else
  333.             j=$((++j)); continue
  334.         fi
  335.  
  336.         if [[ "${devname}" = "${devmajor}" ]]; then
  337.             j=$((++j)); continue
  338.         fi
  339.  
  340.         partition_number="${devname#${devmajor}}"
  341.         removable[${j}]=${devmajor}$(printf '%02d' "${partition_number}")
  342.         j=$((++j))
  343.     done
  344.  
  345.     # now the device array can be sorted properly
  346.     removable=( $(printf '%s\n' "${removable[@]}" | sort) )
  347.  
  348.     # now lets remove those leading zeros that we added
  349.     j=0
  350.     for devname in ${removable[@]}; do
  351.         if [[ "${devname}" =~ ^/dev/dm-[0-9]+ ]]; then
  352.             devmajor="${devname%%[0-9]*}"
  353.         elif [[ "${devname}" =~ ^/dev/fd[0-9]+ ]]; then
  354.             devmajor="${devname%%[0-9]*}"
  355.         elif [[ "${devname}" =~ ^/dev/sd[a-z][0-9]+ ]]; then
  356.             devmajor="${devname%%[0-9]*}"
  357.         else
  358.             j=$((++j)); continue
  359.         fi
  360.  
  361.         if [[ "${devname}" = "${devmajor}" ]]; then
  362.             j=$((++j)); continue
  363.         fi
  364.  
  365.         partition_number="${devname#${devmajor}}"
  366.         removable[${j}]=${devmajor}${partition_number#0}
  367.         j=$((++j))
  368.     done
  369. }
  370.  
  371. # main functions
  372. device_menu() {
  373.     local -a removable=( )
  374.     local -a optical=( )
  375.     local -a mounted_removable=( )
  376.     local -a mounted_optical=( )
  377.     local -i j=0
  378.  
  379.     printf '%s\n' "DestroyMenu recreate FvwmMenuUdisks"
  380.     printf '%s\n' "AddToMenu FvwmMenuUdisks MissingSubmenuFunction FuncFvwmMenuUdisks"
  381.  
  382.     $udisks --enumerate-device-files >/dev/null 2>&1
  383.     if [[ $? -ne 0 ]]; then
  384.         printf '%s\n' "+ fvwm-menu-udisks Title"
  385.         printf '%s\n' "+ \"${info_udisks_failed}\""
  386.         exit 0
  387.     fi
  388.  
  389.     # removable media
  390.     printf '%s\n' "+ \"${separator_removable_media}\" Title"
  391.  
  392.     removable=( $($udisks --enumerate-device-files \
  393.         | grep -ow ${removable_devices} | sort) )
  394.  
  395.     if [[ ${fancy_sort} -eq 1 ]]; then
  396.         fancy_sort
  397.     fi
  398.  
  399.     for devname in ${removable[@]}; do
  400.         local devmajor=
  401.         local info_label=
  402.         local -i info_internal=
  403.         local -i mounted=
  404.         local -i partitions=
  405.  
  406.         # check here to see if a device such as /dev/sdb has partitions. if there
  407.         # are partitions, such as /dev/sdb1, then hide /dev/sdb from being shown.
  408.         if [[ "${devname}" =~ ^/dev/mmcblk[0-9]+p*[0-9]* ]]; then
  409.             devmajor="${devname%%p[0-9]*}"
  410.             if [[ "${devname}" = "${devmajor}" ]]; then
  411.                 partitions="$($udisks --enumerate-device-files \
  412.                     | grep -ow -E ^${devname}p[0-9]+ -c)"
  413.                 if [[ "${partitions}" -gt 0 ]]; then
  414.                     continue
  415.                 fi
  416.             fi
  417.         else
  418.             devmajor="${devname%%[0-9]*}"
  419.             if [[ "${devname}" = "${devmajor}" ]]; then
  420.                 partitions="$($udisks --enumerate-device-files \
  421.                     | grep -ow -E ^${devname}[0-9]+ -c)"
  422.                 if [[ "${partitions}" -gt 0 ]]; then
  423.                     continue
  424.                 fi
  425.             fi
  426.         fi
  427.  
  428.         info_internal="$(info_internal "${devmajor}")"
  429.  
  430.         if [[ "${info_internal}" -eq 1 ]] && [[ "${show_internal}" -eq 0 ]]; then
  431.             continue
  432.         fi
  433.  
  434.         if [[ "${info_internal}" -eq 0 ]] || [[ "${show_internal}" -eq 1 ]]; then
  435.        
  436.             j=$((++j))
  437.            
  438.             info_label="$(info_label "${devname}")"
  439.             # if no label is present, then use information about the device instead
  440.             if [[ -z "${info_label}" ]]; then
  441.                 info_label="$(info_label "${devmajor}")"
  442.                 [[ -z "${info_label}" ]] && info_label="$(info_model "${devmajor}")"
  443.                 [[ -z "${info_label}" ]] && info_label="$(info_vendor "${devmajor}")"
  444.                 if [[ -z "${info_label}" ]]; then
  445.                     info_label="No label"
  446.                 else
  447.                     info_label="No label (${info_label})"
  448.                 fi
  449.             fi
  450.  
  451.             mounted="$(info_mounted "${devname}")"
  452.             if [[ "${mounted}" -eq 0 ]]; then
  453.                 if [[ "${show_removable_device_filename}" -eq 1 ]]; then
  454.                     printf '%s\n' "+ \"${devname#/dev/}: ${info_label}\" Popup ${devname}"
  455. #                   printf '%s' "<menu id=\"mount${devname}\" "
  456. #                   printf '%s' "label=\"${devname#/dev/}: ${info_label}\" "
  457. #                   printf '%s' "execute=\"$0 --mount-menu removable "
  458. #                   printf '%s' "${devname} ${info_label}\" />"
  459. #                   printf '\n'
  460.                 else
  461.                     printf '%s\n' "+ \"${info_label}\" Popup ${devname}"
  462. #                   printf '%s' "<menu id=\"mount${devname}\" "
  463. #                   printf '%s' "label=\"${info_label}\" "
  464. #                   printf '%s' "execute=\"$0 --mount-menu removable "
  465. #                   printf '%s' "${devname} ${info_label}\" />"
  466. #                   printf '\n'
  467.                 fi
  468.             else
  469.                 if [[ "${show_removable_device_filename}" -eq 1 ]]; then
  470.                     printf '%s\n' "+ \"${devname#/dev/}: ${info_label} ${info_mounted}\" Popup ${devname}"
  471. #                   printf '%s' "<menu id=\"mount${devname}\" "
  472. #                   printf '%s' "label=\"${devname#/dev/}: "
  473. #                   printf '%s' "${info_label} ${info_mounted}\" "
  474. #                   printf '%s' "execute=\"$0 --mount-menu removable "
  475. #                   printf '%s' "${devname} ${info_label}\" />"
  476. #                   printf '\n'
  477.                 else
  478.                     printf '%s\n' "+ \"${info_label} ${info_mounted}\" Popup ${devname}"
  479. #                   printf '%s' "<menu id=\"mount${devname}\" "
  480. #                   printf '%s' "label=\"${info_label} ${info_mounted}\" "
  481. #                   printf '%s' "execute=\"$0 --mount-menu removable "
  482. #                   printf '%s' "${devname} ${info_label}\" />"
  483. #                   printf '\n'
  484.                 fi
  485.                 mounted_removable[${#mounted_removable[*]}]=${devname}
  486.             fi
  487.         fi
  488.     done
  489.  
  490.     if [[ $j -eq 0 ]]; then
  491.         printf '%s\n' "+ \"${info_no_devices}\""
  492.     fi
  493.  
  494.     if [[ "${#mounted_removable[*]}" -gt 0 ]]; then
  495.         printf '%s\n' "+ \"${item_unmount_all}\" Exec $0 --unmount-all-removable ${mounted_removable[*]}"
  496.     fi
  497.  
  498.     # optical media
  499.     printf '%s\n' "+ \"${separator_optical_media}\" Title"
  500.  
  501.     optical=( $($udisks --enumerate-device-files \
  502.         | grep -ow ${optical_devices} | sort) )
  503.  
  504.     if [[ "${#optical[*]}" -eq 0 ]]; then
  505.         printf '%s\n' "+ \"${info_no_devices}\""
  506.         return 0
  507.     fi
  508.  
  509.     for devname in ${optical[@]}; do
  510.         local info_label=
  511.         local -i info_blank=
  512.         local -i info_has_media=
  513.         local -i mounted=
  514.  
  515.         info_has_media="$(info_has_media "${devname}")"
  516.         if [[ "${info_has_media}" -eq 0 ]]; then
  517.             printf '%s\n' "+ \"${devname#/dev/}: ${info_no_devices}\""
  518.         else
  519.             info_blank="$(info_blank "${devname}")"
  520.             if [[ "${info_blank}" -eq 1 ]]; then
  521.                 info_label="Blank media"
  522.             else
  523.                 info_label="$(info_label ${devname})"
  524.                 [[ -z "${info_label}" ]] && info_label="$(info_model "${devname}")"
  525.                 [[ -z "${info_label}" ]] && info_label="No label"
  526.             fi
  527.  
  528.             mounted="$(info_mounted "${devname}")"
  529.             if [[ "${mounted}" -eq 0 ]]; then
  530.                 if [ "${show_optical_device_filename}" -eq 1 ]; then
  531.                     printf '%s\n' "+ \"${devname#/dev/}: ${info_label}\" Popup ${devname}"
  532. #                   printf '%s' "<menu id=\"mount${devname}\" "
  533. #                   printf '%s' "label=\"${devname#/dev/}: ${info_label}\" "
  534. #                   printf '%s' "execute=\"$0 --mount-menu optical "
  535. #                   printf '%s' "${devname} ${info_label}\" />"
  536. #                   printf '\n'
  537.                 else
  538.                     printf '%s\n' "+ \"${info_label}\" Popup ${devname}"
  539. #                   printf '%s' "<menu id=\"mount${devname}\" "
  540. #                   printf '%s' "label=\"${info_label}\" "
  541. #                   printf '%s' "execute=\"$0 --mount-menu optical "
  542. #                   printf '%s' "${devname} ${info_label}\" />"
  543. #                   printf '\n'
  544.                 fi
  545.             else
  546.                 if [[ "${show_optical_device_filename}" -eq 1 ]]; then
  547.                     printf '%s\n' "+ \"${devname#/dev/}: ${info_label} ${info_mounted}\" Popup ${devname}"
  548. #                   printf '%s' "<menu id=\"mount${devname}\" "
  549. #                   printf '%s' "label=\"${devname#/dev/}: "
  550. #                   printf '%s' "${info_label} ${info_mounted}\" "
  551. #                   printf '%s' "execute=\"$0 --mount-menu optical "
  552. #                   printf '%s' "${devname} ${info_label}\" />"
  553. #                   printf '\n'
  554.                 else
  555.                     printf '%s\n' "+ \"${info_label} ${info_mounted}\" Popup ${devname}"
  556. #                   printf '%s' "<menu id=\"mount${devname}\" "
  557. #                   printf '%s' "label=\"${info_label} ${info_mounted}\" "
  558. #                   printf '%s' "execute=\"$0 --mount-menu optical "
  559. #                   printf '%s' "${devname} ${info_label}\" />"
  560. #                   printf '\n'
  561.                 fi
  562.                 mounted_optical[${#mounted_optical[*]}]=${devname}
  563.             fi
  564.         fi
  565.     done
  566.    
  567.     if [[ "${#mounted_optical[*]}" -gt 0 ]]; then
  568.         printf '%s\n' "+ \"${item_unmount_all}\" Exec $0 --unmount-all-optical ${mounted_optical[*]}"
  569.     fi
  570. }
  571.  
  572. mount_menu() {
  573. #   local media_type="${1}"
  574.     local devname="${1}"
  575. #   local info_label="${3}"
  576.     local devname_short="${devname##*/}"
  577.     local -i info_ejectable=
  578.     local -i mounted=
  579.  
  580.     printf '%s\n' "DestroyMenu recreate ${devname}"
  581.     printf '%s\n' "AddToMenu ${devname} MissingSubmenuFunction FuncFvwmMenuUdisksInfo"
  582.     printf '%s\n' "+ DynamicPopdownAction DestroyMenu ${devname}"
  583.  
  584.     printf '%s\n' "+ \"${devname}\" Title"
  585.  
  586.  
  587.     mounted="$(info_mounted "${devname}")"
  588.  
  589.     if [[ "${mounted}" -eq 0 ]]; then
  590.         printf '%s\n' "+ \"${item_open}\" Exec $0 --mount-and-open ${devname}"
  591. #       printf '%s\n' "<item label=\"${item_open}\">"
  592. #       printf '%s\n' "<action name=\"Execute\">"
  593. #           printf '%s\n' "<command>$0 --mount-and-open ${devname}</command>"
  594. #       printf '%s\n' "</action>"
  595. #       printf '%s\n' "</item>"
  596.  
  597.         printf '%s\n' "+ \"${item_mount}\" Exec $0 --mount-device ${devname}"
  598. #       printf '%s\n' "<item label=\"${item_mount}\">"
  599. #       printf '%s\n' "<action name=\"Execute\">"
  600. #       printf '%s\n' "<command>$0 --mount-device ${devname}</command>"
  601. #       printf '%s\n' "</action>"
  602. #       printf '%s\n' "</item>"
  603.     else
  604.         printf '%s\n' "+ \"${item_open}\" Exec $0 --open-directory ${devname}"
  605. #       printf '%s\n' "<item label=\"${item_open}\">"
  606. #       printf '%s\n' "<action name=\"Execute\">"
  607. #       printf '%s\n' "<command>$0 --open-directory ${devname}</command>"
  608. #       printf '%s\n' "</action>"
  609. #       printf '%s\n' "</item>"
  610.  
  611.         printf '%s\n' "+ \"${item_unmount}\" Exec $0 --unmount-device ${devname}"
  612. #       printf '%s\n' "<item label=\"${item_unmount}\">"
  613. #       printf '%s\n' "<action name=\"Execute\">"
  614. #       printf '%s\n' "<command>$0 --unmount-device ${devname}</command>"
  615. #       printf '%s\n' "</action>"
  616. #       printf '%s\n' "</item>"
  617.     fi
  618.    
  619.     info_ejectable="$(info_ejectable "${devname}")"
  620.    
  621.     if [[ "${info_ejectable}" -eq 1 ]]; then
  622.         printf '%s\n' "+ \"${item_eject}\" Exec $0 --eject-device ${devname}"
  623. #       printf '%s\n' "<item label=\"${item_eject}\">"
  624. #       printf '%s\n' "<action name=\"Execute\">"
  625. #       printf '%s\n' "<command>$0 --eject-device ${devname}</command>"
  626. #       printf '%s\n' "</action>"
  627. #       printf '%s\n' "</item>"
  628.     fi
  629.  
  630.     printf '%s\n' "+ \"${item_info}\" Popup FvwmMenuUdisksInfo"
  631. #   printf '%s' "<menu id=\"showinfo${devname##*/}\" "
  632. #   printf '%s' "label=\"${item_info}\" "
  633. #   printf '%s' "execute=\"$0 --show-info ${devname}\" />"
  634. #   printf '\n'
  635.  
  636. action_info ${devname}
  637.  
  638.  
  639. }
  640. # }}}
  641.  
  642. if [[ $# == 0 ]]; then
  643.     device_menu
  644.     exit 0
  645. fi
  646.  
  647. case "${1}" in
  648.     "--mount-menu")
  649.         declare media_type="${2}"
  650.         declare devname="${3}"
  651.         shift 3
  652.         mount_menu ${media_type} ${devname} "${*}"
  653.         ;;
  654.     "--mount-device")
  655.         action_mount "${2}"
  656.         ;;
  657.     "--unmount-device")
  658.         action_unmount "${2}"
  659.         ;;
  660.     "--eject-device")
  661.         action_eject "${2}"
  662.         ;;
  663.     "--open-directory")
  664.         action_open "${2}"
  665.         ;;
  666.     "--mount-and-open")
  667.         action_mount "${2}" && action_open "${2}"
  668.         ;;
  669.     "--show-info")
  670.         action_info "${2}"
  671.         ;;
  672.     "--unmount-all-removable")
  673.         shift 1 && action_unmount_all $@
  674.         ;;
  675.     "--unmount-all-optical")
  676.         shift 1 && action_unmount_all $@
  677.         ;;
  678. esac
  679.  
  680. # vim: set ts=4 sw=4 noet foldmethod=marker :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement