#! /bin/bash # # author sudodus alias nio-wiklund at launchpad # # date editor comment # 2013-08-14 sudodus created selector # 2013-08-14 sudodus changed name to select-tarball # 2013-09-01 sudodus converted for dialog # 2013-11-08 sudodus more clear what is the default # 2013-11-08 sudodus own directory for tarballs (plus symlink) # 2015-04-13 sudodus fix for live session & persistence: tarstr=... #----------------------------------------------------------------------------- function special-tarball-select { # check for tarballs if [ -d "/tarballs" ] then tarpath="/tarballs" elif [ -d "${HOME}/tarballs" ] then tarpath="${HOME}/tarballs" elif [ -d "${PWD}/tarballs" ] then tarpath="${PWD}/tarballs" else echo "Error finding the tarball path" tarpath="${HOME}" fi echo hello > /tarballs/hello-xyz.txt 2> /dev/null if [ $? -eq 0 ] then rm /tarballs/hello-xyz.txt tarstr="${tarpath}"/*.tar.[gx]z else tarstr=$(find / -name "*.tar.[gx]z") fi for i in $tarstr do item=$(($item + 1)) seltxt="$seltxt$item \"$i\" " done # select tarball, go or quit ans=$(dialog --backtitle 'Select tarball - avoid spaces in tarball names' \ --title "Please select a tarball with the arrow keys, scroll if necessary" \ --default-item $choice --colors --menu "Available tarballs" 17 76 $(($item + 1)) \ $seltxt q "Quit, no tarball selected" 3>&1 1>&2 2>&3 3>&- ) choice="$ans" item=0 for i in $tarstr do item=$(($item + 1)) targ0="$i" if [ "$item" == "$choice" ] then target=$targ0 fi done if [ "$ans" == "q" ] || [ "$ans" == "" ] then > "$selected" exit fi # write selection # echo "choice=$choice" # echo "$target" echo "$target" > "$selected" } function general-file-select { #echo $tarball if test -f "$tarball" then choice="$tarball" else choice="$PWD/" fi ans=$(dialog --backtitle \ 'space-bar: copy selection to text-entry window, slash: show directory content' --no-shadow \ --fselect "$choice" 8 78 3>&1 1>&2 2>&3 3>&- ) target="$ans" # write selection #echo "$target" echo "$target" > "$selected" } ## main program ############################################## # variables choice=1 cnt= target= tarball= LC_ALL=C LANG=C # usage if [ "$(whoami)" != "root" ] then echo "Usage:" echo "Select tarball to use with the One Button Installer" echo "sudo $0" exit fi # check for previous selection if [ -f /usr/share/OBI/selected ] then selected="/usr/share/OBI/selected" elif [ -f "${HOME}"/selected ] then selected="${HOME}/selected" elif [ -d "${PWD}/selected" ] then selected="${PWD}/selected" else selected="selected" echo "Error finding the OBI selected path" fi if test -s "$selected" then read tarball < "$selected" fi # check for tarballs if [ -d "/tarballs" ] then tarpath="/tarballs" elif [ -d "${HOME}/tarballs" ] then tarpath="${HOME}/tarballs" elif [ -d "${PWD}/tarballs" ] then tarpath="${PWD}/tarballs" else echo "Error finding the tarball path" fi item=0 for source in "${tarpath}"/*.tar.[gx]z do item=$(($item + 1)) if ! test -f "$source" then dialog --backtitle "One Button Installer - Select tarball" --msgbox \ "No tarball found (tarballs/filename.tar.gz)" 0 0 tarball= general-file-select exit elif ! test -s "$source" then ans=$(dialog --backtitle "One Button Installer - Select tarball" \ --yesno "Zero sized tarball found ($source)\n Remove it? (y/n)" 0 0 \ 3>&1 1>&2 2>&3 3>&- ) if [ "$?" == "0" ] then sudo rm "$source" else exit fi elif [ "$tarball" == "$source" ] then choice=$item fi done # list for selection ans= item=0 cmdsav= seltxt= # select function ans=$(dialog --backtitle "One Button Installer - Select tarball" \ --yes-label "Tarball selector (easy)" --no-label "General File selector (difficult)" \ --yesno "Select tarballs in standard directory (easy), or\n anywhere with the general file selector (difficult)?" 0 0 \ 3>&1 1>&2 2>&3 3>&- ) if [ "$?" == "0" ] then special-tarball-select else general-file-select fi