Advertisement
Thaodan

Untitled

May 21st, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.68 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # wine prefix chooser and manager
  4. #
  5. # Copyright (C) 2012  Björn Bidar
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. # variables
  22. #  version vars:
  23. ############################################
  24. USE_VER=3.2
  25. USE_REV=dd5afba0b5
  26.  
  27. . ${LIBSH:-/usr/lib/libsh}
  28.  
  29.  
  30. # settings
  31. ############################################
  32.  
  33. DMSG_ICON=wine  # icon for gui d_msg output
  34. DMSG_DIALOG_DISABLED=true # we dont need dialog for cli so disable it
  35. WDEBUG=fixme-all  # define wich wine debug out will shown in if $WINEDEBUG is not set
  36. default_win_arch=win32 # define the default wine architecture
  37. err_input_messages="no options given run $appname -h for help, or -H for long help:wrong options or only prefix given run $appname -h for help, or -H for long help" # err messages for test_input
  38. export WINE_PREFIXES="$WINEPREFIX_PATH" # for winetricks
  39.  
  40.  
  41. #############################################
  42.  
  43.  
  44.  
  45. u_help () { # display short help
  46. d_msg 'help' "`cat <<_HELP
  47. Usage: $appname wineprefix command/file options
  48. supported files: *.exe;*.bat;*.cmd;*.reg;*.dll
  49.  
  50.  syntax: $appname [prefix] [command/file] [options]
  51.  
  52. type $appname -H for long-help
  53. _HELP`"
  54. }
  55.  
  56. u_long_help () { # display long help
  57. d_msg 'help' "\
  58. Usage: $appname wineprefix command/file options
  59. supported files: *.exe;*.bat;*.cmd;*.reg;*.dll
  60.  
  61.  syntax: $appname [options] [prefix] [command/file] [start taget options]
  62.  
  63.  
  64. general options:
  65.   -g    --gui           enable gui output
  66.   -h    --help          show help
  67.   -H    --long-help     show this text, long help
  68.   -V    --version       show version
  69.     --revision      show revision
  70.   -v    --verbose       be more verbose
  71.     --debug         help us in debuging
  72.  
  73. options:
  74.   -b    --binpath <binpath> define in wich path $appname will search for wine and CO
  75.   -r    -run-debug      don't set WINEDEBUG to ${default_w_db:='default $WINEDBUG not set'}  (\$default_w_db)
  76.   -d    --desktop           start file/command in virtual desktop
  77.   -D    --directory         tell $appname to change directory to started file
  78.   -p    --prefix            replacement for prefix, $appname will ask for prefix is this option is given ( usefull to start Windows programms out of file manager)
  79.  commands:
  80.    winefile open winefilebrowser
  81.    winecfg  open winecontrol
  82.    appconf  control the software of the chosen prefix
  83.    control  open control
  84.    cmd      open winecommandline
  85.    open     open prefix directory with xdg-open
  86.  
  87.  file specific  options:
  88.  
  89.    *.bat/*.cmd file:
  90.      -w, --window open file in new window
  91.  
  92.    *.reg file:
  93.      -i import regefile
  94.      -e export the given part of registry to file
  95.      syntax for export to regfile :
  96.      $appname [prefix] [regfilename.reg] -e [regpath]
  97.  
  98. ENVIRONMENT
  99.    
  100.     WINEPATH       
  101.      set path where $appname searchs for wine
  102.    WINEPREFIX_PATH
  103.      path where $appname searchs for wineprefixs, default is \$HOME
  104. "
  105. }
  106.  
  107.  
  108. # tools
  109.  
  110. u_create_prefix () {
  111.  
  112.     if d_msg f prefix "prefix $1 don't exist, create it?" ; then  # if prefix doesn't exist create one yes or no?
  113.       if [ `uname -m` = x86_64 ]; then
  114.      buttons='win32:1,win64:0'
  115.      case $DMSG_GUI in
  116.         1|true) input=`d_msg i 'prefix select' "Which Windows architecture the prefix should support please enter win64(64bits) or win32(32bits), default $default_win_arch"`;;
  117.         *)
  118.           echo "Which Windows architecture the prefix should support please enter win64(64bits) or win32(32bits), default $default_win_arch"
  119.           read input
  120.           ;;
  121.      esac
  122.      case  "$input" in
  123.        win32) export WINEARCH=win32 ;;
  124.        win64) export WINEARCH=win64 ;;
  125.        *) d_msg 'prefix arch' "$default_win_arch used" ; export WINEARCH=$default_win_arch ;;
  126.      esac
  127.       fi
  128.     else
  129.       return 1
  130.     fi
  131.  
  132. }
  133.  
  134. skip_run() {
  135.   while [ ! $# -eq 0 ] ; do
  136.     run_int=`echo $run_int | sed "s/$1//"`
  137.     shift
  138.   done
  139. }
  140.  
  141. add_run() {
  142.   while [ $# -gt 0 ] ; do
  143.     run_int=$run_int:$1
  144.     shift
  145.   done
  146. }
  147.  
  148.  
  149. # main function
  150.  
  151. main () {
  152.     shload libuse/base
  153.     old_ifs=$IFS
  154.     IFS=:
  155.     for run_i in $run_int ; do
  156.     IFS=$old_ifs
  157.     $run_i
  158.     IFS=:
  159.     done
  160.     IFS=$old_ifs
  161.     unset IFS
  162.     if check_prefix "$1" || u_create_prefix "$1"   ; then
  163.     prefix ${WINEPREFIX_PATH:-$HOME/.}/"$1"
  164.     else
  165.     return 1
  166.     fi
  167.  
  168.     runed_exe="$2"
  169.     shift 2 # shift $+1 to remove $1 from $@ that arguments go collected to started program # maybe replace file type detection by extension with detection through mime-type
  170.     case "$runed_exe" in # detect wich file or options given
  171.     *.EXE|*.exe|*.bin)  "${WINE:-wine}"$cmd $wine_args "$runed_exe" $@ ;; # exec executable file
  172.     *.dll|*.ax) "$BINPATH"regsvr32 $@ ;;
  173.     *.bat|*.BAT]|*.cmd|*.CMD) # exec bat/cmd file
  174.            case $runed_exe in
  175.            -w|--window)  "$BINPATH"wineconsole --backend=user cmd.exe $1 $2  $3 ;; # if option -w (--window) start file in new window
  176.            *)  "${WINE:-wine}" cmd.exe /c "$runed_exe $@" ;;
  177.        esac
  178.        ;;
  179.        *.reg) # import regfile into prefix
  180.        case $1 in
  181.            -e)  "$BINPATH"regedit /e "$runed_exe" $2 ;;
  182.            -i)  "$BINPATH"regedit "$runed_exe"    ;;
  183.            *)  d_msg ! faile 'no option for import(-i) or export (-e) given' ;;
  184.        esac
  185.        ;;
  186.        *.msi) wine "$BINPATH"msiexec.exe /i "$runed_exe" "$@" ;;
  187. # built in commands
  188.   ##############
  189.        appconf|uninstaller)  "$BINPATH"wine $wine_args uninstaller.exe $@ ;;
  190.        cmd)      "${WINE:-wine}" $wine_args cmd.exe "$@" ;; #console --backend=curses
  191.        control)  "${WINE:-wine}" $wine_args control.exe $@ ;;
  192.        open)  
  193.        if echo  "$1" | grep -q '[Aa-Zz]:' ; then
  194.        xdg-open "$( winepath -u "$1" )"
  195.        else
  196.        xdg-open "$WINEPREFIX"/"$1"  
  197.        fi
  198.        ;;
  199.        *) sh -c  "exec "$runed_exe" $@"  ;; #we use exec cause its safer cause "$runed_exe" cant be a internal function
  200.   esac
  201. }
  202.  
  203. if [ ! -t 1 ] ; then # test if is usenew runned outside from terminal and use DMSG_GUI=true to get gui output from d_msg if outsite of terminal
  204.   DMSG_GUI=1
  205. fi
  206.  
  207. if [ ! $# = 0  ] ; then
  208.   ext=1
  209.   while test "$ext" != "" # main loop
  210.     do 
  211.     posixly=$POSIXLY_CORRECT
  212.     export POSIXLY_CORRECT=1 # set POSIXLY_CORRECT to true cause we want that getopt stops after first non option  input to prevent that it parses the arguments of $runed_exe
  213.     run_int=set_wine_db
  214.     u_optspec=rb:dphHvVg #-: # short options
  215.     u_optspec_long=run-debug,binpath:,desktop,prefix,help,long-help,verbose,debug,revision,gui,version # long options
  216.     PROCESSED_OPTSPEC=$( getopt -o $u_optspec --long $u_optspec_long \
  217.     -n $appname  -- "$@")  || d_msg ! error  "$( read_farray "$err_input_messages" 2 )" || exit 1 # parsed optspec
  218.     eval set -- "$PROCESSED_OPTSPEC"
  219.     export POSIXLY_CORRECT=$posixly
  220.     unset posixly
  221.     while [ ! $# = 0 ] ; do # TODO: improve this with getopt
  222.       case "$1" in
  223.         -h|--help) u_help ; shift ;;
  224.         --long-help|-H|-HL) u_long_help ; shift ;;
  225.         --debug)
  226.           set -o verbose
  227.           set -o xtrace
  228.           shift
  229.           continue
  230.           ;;
  231.         -v|--verbose) set -o verbose  ; shift ; continue ;;
  232.         --revision) d_msg revision "$USE_REV" ; shift ;;
  233.         -V|--version)   d_msg version "$USE_VER" ; shift ;;
  234.         -g|--gui)   DMSG_GUI=1 ; shift ; continue ;; # display msg in gui      
  235.         -r|--run-debug) skip_run set_wine_db ; shift ;;
  236.         -b|--binpath)   import libuse.wine_misc ; set_wine_ver "$2" ; shift 2 ;;  # set which wine version usenew should use
  237.         -d|--desktop)   argument_d=$(echo eval echo \$$#);  wine_args="explorer /desktop=$( basename $( $argument_d ) | sed 's/.exe//g'),800x600" ; unset argument_d ;shift ;;
  238.         -p|--prefix)
  239.           argument_p=$( echo eval echo \$$# )
  240.           argument_p=$( $argument_p )
  241.           file=`basename $argument_p`
  242.           case $DMSG_GUI in
  243.             1|true) prefix="`d_msg i 'enter prefix' "Please enter prefix to start $file"`";;
  244.             *)
  245.               echo "Please enter prefix to start $file"
  246.               read prefix
  247.               test ! -z $prefix
  248.               ;;
  249.           esac  || exit 1
  250.           unset argument_p file
  251.           shift
  252.         ;;
  253.         --) shift; break ;; # if [ $1 =  -- ] ; then no more options in input  
  254.       esac
  255.     done
  256.     if [ $# = 0 ] ; then
  257.       true
  258.     elif test_input $@ $prefix  ; then
  259.       IFS=$old_ifs
  260.       main $prefix $@
  261.       #         old_ifs=$IFS
  262.       #         IFS=:
  263.       #         for run_i in $run_int_p ; do
  264.       #           IFS=$old_ifs
  265.       #           $run_i
  266.       #           IFS=:
  267.       #     done
  268.     else
  269.       false
  270.     fi
  271.     error_status=$?
  272.     ext=
  273.   done
  274. else
  275.   echo $(read_farray "$err_input_messages" 1)
  276.   false
  277. fi
  278. exit $error_status
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement