#!/bin/sh
# Quick and dirty script to download and install various
# foo2XXX printer firmware and ICM files
#
# Current maintainers: Ljubomir Ljubojevic

# Based on excelent winetricks script with:
# Current maintainers: Austin English, Dan Kegel
# Copyright 2007, 2008, 2009 Google (Dan Kegel, dank@kegel.com)
# Copyright 2008, 2009 Austin English (austinenglish@gmail.com)
# Thanks to Detlef Riekenberg for lots of updates
# Thanks to Saulius Krasuckas for corrections and suggestions
# Thanks to Erik Inge Bolsø for several patches
# Thanks to Hugh Perkins for the directplay patch
# See also http://wiki.winehq.org/winetricks
#
# Note to contributors: please avoid gnu extensions in this shell script,
# as it has to run on MacOSX and Solaris, too.  A good book on the topic is
# "Portable Shell Programming" by Bruce Blinn

#---- Constants -------------------------------------------------

# Name of this version of winetricks (YYYYMMDD)
VERSION=20091101

# Internal variables; these locations are not too important
FIRMWARE_ICM_CACHE=$HOME/firmware_icm_cache
# Default to hiding the directory, by popular demand
test -d "$FIRMWARE_ICM_CACHE" || FIRMWARE_ICM_CACHE=$HOME/.firmware_icm_cache
FIRMWARE_ICM_TMP=$HOME/firmware_icm_cache
#FIRMWARE_ICM_TMP_WIN='c:\winetrickstmp'

# WINDIR="$WINEPREFIX/drive_c/windows"
# 
# # Which sourceforge mirror to use.  Rotate based on time, since
# # their mirror picker sometimes persistantly sends you to a broken
# # mirror.
# case `date +%S` in
# *[01]) SOURCEFORGE=http://internap.dl.sourceforge.net/sourceforge ;;
# *[23]) SOURCEFORGE=http://easynews.dl.sourceforge.net/sourceforge ;;
# *)     SOURCEFORGE=http://downloads.sourceforge.net;;
# esac

#firmware_icm_dir=$(dirname $0)
#source $firmware_icm_dir/getweb

#---- Functions -------------------------------------------------

# Check for known desktop environments
# Set variable DE to the desktop environment's name, lowercase
detectDE() {
    if [ x"$KDE_FULL_SESSION" = x"true" ]
    then
        DE=kde
    elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]
    then
        DE=gnome
    elif [ x"$DISPLAY" != x"" ]
    then
        DE=x
    else
        DE=none
    fi
}

warn() {
    echo "$@"

    test "$GUI" = 1 || return

    case $DE in
    gnome) zenity --error --title=Firmware_ICM --text="$@" --no-wrap ;;
    kde) kdialog --title Firmware_ICM --error "$@" ;;
    x) xmessage -title Firmware_ICM -center "  Error: $@  " ;;
    esac
}

die() {
    warn "$@"

    exit 1
}

# # Abort if user doesn't own the given directory (or its parent, if it doesn't exist yet)
# die_if_user_not_dirowner() {
#     if test -d "$1"
#     then
#         checkdir="$1"
#     else
#         # fixme: quoting problem?
#         checkdir=`dirname "$1"`
#     fi
#     nuser=`id -u`
#     nowner=`ls -l -n -d "$checkdir" | awk '{print $3}'`
#     if test x$nuser != x$nowner
#     then
#         die "You (`id -un`) don't own $checkdir. Don't run firmware_icm as another user!"
#     fi
# }

#----------------------------------------------------------------

usage() {
    set +x
    echo "Usage: $0 [options] package [package] ..."
    echo "This script can help you download firmware and ICM files for your printers"
    echo ""
    echo "Options:"
    echo " -q         quiet.  You must have already agreed to the EULAs."
    echo " -v         verbose"
    echo " -V         display Version"
    echo "Packages:"
    echo " foo2zjs      Get Minolta 2200 DL, 2300 DL, 2430 DL and Color PageWorks/Pro L"
    echo " foo2hp       Get HP LaserJet CP1215, 1600, 2600n .ICM files"
    echo " foo2xqx      Get HP LaserJet P1005, P1006, P1007, P1008, P1505 firmware files"
    echo " foo2lava     Get Konica Minolta magicolor:1600W,1680MF,1690MF,2480 MF,2490 MF,2530 DL,4690MF,Xerox Phaser 6115MFP .ICM files"
    echo " foo2qpdl     Get Samsung: CLP-300,CLP-310,CLP-315,CLX-2160,CLX-3160,CLX-3175,CLP-600,CLP-610,Xerox Phaser 6110(MFP) .ICM files"
    echo " foo2slx      Get Lexmark C500n .ICM files"
    echo " foo2hiperc   Get Oki C3100n,C3200n,C3300n,C3400n,C3530n MFP,C5100n/C5150n,C5200n,C5500n,C5600n,C5800n .ICM files"
    echo " foo2oak      Get HP Color LaserJet 1500 .ICM files"
#     echo " hp_1215      Get HP Color LaserJet CP1215 .ICM files"
#     echo " hp_1500      Get HP Color LaserJet 1500 .ICM files"
#     echo " hp_1600      Get HP Color LaserJet 1600 .ICM files"
#     echo " hp_2600n     Get HP Color LaserJet 2600n .ICM files"
#     echo " km_1600w     Get Konica Minolta 1600W .ICM files"
#     echo " km_1680      Get Konica Minolta 1680MF .ICM files"
#     echo " km_1690      Get Konica Minolta 1690MF .ICM files"
#     echo " km_2480      Get Konica Minolta 2480 MF .ICM files"
#     echo " km_2490      Get Konica Minolta 2490 MF .ICM files"
#     echo " km_2530      Get Konica Minolta 2530 DL .ICM files"
#     echo " km_4690      Get Konica Minolta 4690MF .ICM files"
#     echo " x_6115       Get Xerox Phaser 6115MFP .ICM files"
#     echo " m_2200       Get Minolta 2200 DL .ICM files"
#     echo " m-2300       Get Minolta 2300 DL .ICM files"
#     echo " km_2430      Get Konica Minolta 2430 DL .ICM files"
#     echo " cpwl         Get Minolta Color PageWorks/Pro L .ICM files"
#     echo " s_300        Get Samsung CLP-300 .ICM files"
#     echo " s_310        Get Samsung CLP-310 .ICM files"
#     echo " s_315        Get Samsung CLP-315 .ICM files"
#     echo " s_600        Get Samsung CLP-600 .ICM files"
#     echo " s_610        Get Samsung CLP-610 .ICM files"
#     echo " s_2160       Get Samsung CLX-2160 .ICM files"
#     echo " s_3160       Get Samsung CLX-3160 .ICM files"
#     echo " s_3175       Get Samsung CLX-3175 .ICM files"
#     echo " x_6110       Get Xerox Phaser 6110 and 6110MFP .ICM files"
#     echo " l_500         Get Lexmark C500 .ICM files"
#     echo " o_3100       Get Oki C3100 .ICM files"
#     echo " o_3200       Get Oki C3200 .ICM files"
#     echo " o_3300       Get Oki C3300 .ICM files"
#     echo " o_3400       Get Oki C3400 .ICM files"
#     echo " o_3530       Get Oki C3530 MFP .ICM files"
#     echo " o_5100       Get Oki C5100 / C5150n .ICM files"
#     echo " o_5200       Get Oki C5200 .ICM files"
#     echo " o_5500       Get Oki C5500 .ICM files"
#     echo " o_5600       Get Oki C5600 .ICM files"
#     echo " o_5800       Get Oki C5800 .ICM files"
#     echo " hp_1000      Get HP LJ 1000 firmware file"
#     echo " hp_1005      Get HP LJ 1005 firmware file"
#     echo " hp_1018      Get HP LJ 1005 firmware file"
#     echo " hp_1020      Get HP LJ 1020 firmware file"
#     echo " hp_p1005     Get HP LJ P1005 firmware file"
#     echo " hp_p1006     Get HP LJ P1006 firmware file"
#     echo " hp_p1007     Get HP LJ P1007 firmware file"
#     echo " hp_p1008     Get HP LJ P1008 firmware file"
#     echo " hp_p1505     Get HP LJ P1505 firmware file"
#     echo " m_2300dl_fw   Get Minolta 2300DL v2.55 firmware (experts only)"
     echo " all          Get everything above"
}

#----------------------------------------------------------------
# Trivial GUI just to handle case where user tries running without commandline

kde_showmenu() {
    title="$1"
    shift
    text="$1"
    shift
    col1name="$1"
    shift
    col2name="$1"
    shift
    while test $# -gt 0
    do
        args="$args $1 $1 off"
        shift
    done
    kdialog --title "$title" --separate-output --checklist "$text" $args
}

x_showmenu() {
    title="$1"
    shift
    text="$1"
    shift
    col1name="$1"
    shift
    col2name="$1"
    shift
    if test $# -gt 0
    then
        args="$1"
        shift
    fi
    while test $# -gt 0
    do
        args="$args,$1"
        shift
    done
    (echo "$title"; echo ""; echo "$text") | \
    xmessage -print -file - -buttons "Cancel,$args" | sed 's/Cancel//'
}

showmenu()
{
    case $DE in
    kde) kde_showmenu "$@" ;;
    gnome|x) x_showmenu "$@" ;;
    none) usage 1>&2; exit 1;;
    esac
}

dogui()
{
    if [ $DE = gnome ]
    then
        echo "zenity --title 'Select a package to install' --text 'Install?' --list --checklist --column '' --column Package --column Description --height 440 --width 600 \\" > "$FIRMWARE_ICM_TMP"/zenity.sh
        usage | grep '^ [a-z]' | sed 's/^ \([^ ]*\) *\(.*\)/FALSE "\1" '"'\2'/" | sed 's/$/ \\/' >> $FIRMWARE_ICM_TMP/zenity.sh
        todo="`sh "$FIRMWARE_ICM_TMP"/zenity.sh | tr '|' ' '`"
    else
        packages=`usage | awk '/^ [a-z]/ {print $1}'`
        todo="`showmenu "firmware_icm" "Select a package to install" "Install?" "Package" $packages`"
    fi

    if test "$todo"x = x
    then
       exit 0
    fi
}

#-----  Helpers  ------------------------------------------------

# Execute with error checking
try() {
    # "VAR=foo try cmd" fails to put VAR in the environment
    # with some versions of bash if try is a shell function?!
    # Adding this explicit export works around it.
#    export WINEDLLOVERRIDES
    echo Executing "$@"
    "$@"
    status=$?
    if test $status -ne 0
    then
        die "Note: command '$@' returned status $status.  Aborting."
    fi
}

# verify an sha1sum
verify_sha1sum() {
    wantsum=$1
    file=$2

    gotsum=`$SHA1SUM < $file | sed 's/ .*//'`
    if [ "$gotsum"x != "$wantsum"x ]
    then
       die "sha1sum mismatch!  Rename $file and try again."
    fi
}

# Download a file
# Usage: package url [sha1sum [filename]]
# Caches downloads in firmware_icm_cache/$package
download() {
    if [ "$4"x != ""x ]
    then
        file="$4"
    else
        file=`basename "$2"`
    fi
    cache="$FIRMWARE_ICM_CACHE/$1"
    mkdir -p "$cache"
    if test ! -f "$cache/$file"
    then
        cd "$cache"
        # Mac folks tend to have curl rather than wget
        # On Mac, 'which' doesn't return good exit status
        # Need to jam in --header "Accept-Encoding: gzip,deflate" else
        # redhat.com decompresses liberation-fonts.tar.gz!
        if [ -x "`which wget`" ]
        then
           # Use -nd to insulate ourselves from people who set -x in WGETRC
           # [*] --retry-connrefused works around the broken sf.net mirroring
           # system when downloading corefonts
           # [*] --read-timeout is useful on the adobe server that doesn't
           # close the connection unless you tell it to (control-C or closing
           # the socket)
           try wget -O "$file" -nd -c --read-timeout=300 --retry-connrefused --header "Accept-Encoding: gzip,deflate" "$2"
        else
           # curl doesn't get filename from the location given by the server!
           # fortunately, we know it
           try curl -L -o "$file" -C - --header "Accept-Encoding: gzip,deflate" "$2"
        fi
        cd "$olddir"
    fi
    if [ "$3"x != ""x ]
    then
        verify_sha1sum $3  "$cache/$file"
    fi
}

# gettgz() {
# # from getweb script from http://foo2zjs.rkkda.com
#     url="$1"
#     file="$2"
#     what="$3"
#     
#     wget $WGETOPTS -O $file "$url/$file" ||
# 	error "Couldn't download $url/$file"
#     gunzip <$file | tar xvf - $what
#     rm $file
# }

# Taken from user: http://www.macosxhints.com/users.php?mode=profile&uid=1062630
# function printtimestamp()
# {
#   stat --printf="%y" "$1"
# }

#----- One function per package, in alphabetical order ----------


load_foo2zjs() {
   ./getweb cpwl	# Get Minolta Color PageWorks/Pro L .ICM files
   ./getweb 2200	# Get Minolta 2200 DL .ICM files
   ./getweb 2300	# Get Minolta 2300 DL .ICM files
   ./getweb 2430	# Get Minolta 2430 DL .ICM files
   ./getweb 1000	# Get HP LaserJet 1000 firmware file
   ./getweb 1005	# Get HP LaserJet 1005 firmware file
   ./getweb 1018	# Get HP LaserJet 1018 firmware file
   ./getweb 1020	# Get HP LaserJet 1020 firmware file
   mkdir /usr/share/foo2zjs/icm
   cp -p km2430_0.icm /usr/share/foo2zjs/icm/km2430_0.icm
   cp -p km2430_1.icm /usr/share/foo2zjs/icm/km2430_1.icm
   cp -p km2430_2.icm /usr/share/foo2zjs/icm/km2430_2.icm
   cp -p CPWL12W.icm /usr/share/foo2zjs/icm/CPWL12W.icm
   cp -p CPWL24W.icm /usr/share/foo2zjs/icm/CPWL24W.icm
   cp -p CPWL6W.icm /usr/share/foo2zjs/icm/CPWL6W.icm
   cp -p DL2200RGB.icm /usr/share/foo2zjs/icm/DL2200RGB.icm
   cp -p DL2312.icm /usr/share/foo2zjs/icm/DL2312.icm
   cp -p DL2324.icm /usr/share/foo2zjs/icm/DL2324.icm

   mkdir /usr/share/foo2zjs/firmware
#   cp  /usr/share/foo2zjs/firmware/sihp1000.dl
   ./arm2hpdl sihp1000.img > sihp1000.dl
   touch -r sihp1000.img sihp1000.dl
   install -p -m 644 sihp1000.dl /usr/share/foo2zjs/firmware/

#   cp  /usr/share/foo2zjs/firmware/sihp1005.dl
   ./arm2hpdl sihp1005.img > sihp1005.dl
   touch -r sihp1005.img sihp1005.dl
   install -p -m 644 sihp1005.dl /usr/share/foo2zjs/firmware/

#   cp  /usr/share/foo2zjs/firmware/sihp1018.dl
   ./arm2hpdl sihp1018.img > sihp1018.dl
   touch -r sihp1018.img sihp1018.dl
   install -p -m 644 sihp1018.dl /usr/share/foo2zjs/firmware/

#   cp  /usr/share/foo2zjs/firmware/sihp1020.dl
   ./arm2hpdl sihp1020.img > sihp1020.dl
   touch -r sihp1020.img sihp1020.dl
   install -p -m 644 sihp1020.dl /usr/share/foo2zjs/firmware/
}

load_foo2hp() {
    ./getweb 1215      # Get HP LaserJet CP1215 .ICM files
    ./getweb 1600      # Get HP LaserJet 1600 .ICM files
    ./getweb 2600n     # Get HP LaserJet 2600n .ICM files
   mkdir /usr/share/foo2hp/icm
   cp -p hp1215-argyll-0.icm /usr/share/foo2hp/icm/hp1215-argyll-0.icm
   cp -p hpclj2600n-0.icm /usr/share/foo2hp/icm/hpclj2600n-0.icm
   cp -p hpclj2600n-1.icm /usr/share/foo2hp/icm/hpclj2600n-1.icm
   cp -p km2430_0.icm /usr/share/foo2hp/icm/km2430_0.icm
   cp -p km2430_1.icm /usr/share/foo2hp/icm/km2430_1.icm
   cp -p km2430_2.icm /usr/share/foo2hp/icm/km2430_2.icm
}

load_foo2xqx() {
    ./getweb P1005     # Get HP LaserJet P1005 firmware file
    ./getweb P1006     # Get HP LaserJet P1006 firmware file
#    ./getweb P1007     # Get HP LaserJet P1007 firmware file
#    ./getweb P1008     # Get HP LaserJet P1008 firmware file
    ./getweb P1505     # Get HP LaserJet P1505 firmware file
   mkdir /usr/share/foo2xqx/firmware
#   cp -p   /usr/share/foo2xqx/firmware/sihpP1005.dl
   ./arm2hpdl sihpP1005.img > sihpP1005.dl
   touch -r sihpP1005.img sihpP1005.dl
   install -p -m 644 sihpP1005.dl /usr/share/foo2xqx/firmware/

#   cp -p  /usr/share/foo2xqx/firmware/sihpP1006.dl
   ./arm2hpdl sihpP1006.img > sihpP1006.dl
   touch -r sihpP1006.img sihpP1006.dl
   install -p -m 644 sihpP1006.dl /usr/share/foo2xqx/firmware/

#   cp -p  /usr/share/foo2xqx/firmware/sihpP1505.dl
   ./arm2hpdl sihpP1505.img > sihpP1505.dl
   touch -r sihpP1505.img sihpP1505.dl
   install -p -m 644 sihpP1006.dl /usr/share/foo2xqx/firmware/

}

load_foo2lava() {
   ./getweb 1600w	# Get Konica Minolta magicolor 1600W .ICM files
   ./getweb 1680	# Get Konica Minolta magicolor 1680MF .ICM files
   ./getweb 1690	# Get Konica Minolta magicolor 1690MF .ICM files
   ./getweb 2480	# Get Konica Minolta magicolor 2480 MF .ICM files
   ./getweb 2490	# Get Konica Minolta magicolor 2490 MF .ICM files
   ./getweb 2530	# Get Konica Minolta magicolor 2530 DL .ICM files
   ./getweb 4690	# Get Konica Minolta magicolor 4690MF .ICM files
   ./getweb 6115	# Get Xerox Phaser 6115MFP .ICM files
   mkdir /usr/share/foo2lava/icm
   cp -p km-1600-rgb-392-bpp1.icm /usr/share/foo2lava/icm/km-1600-rgb-392-bpp1.icm
   cp -p km2530-jconner-d50.icm /usr/share/foo2lava/icm/km2530-jconner-d50.icm
   cp -p km2530_0.icm /usr/share/foo2lava/icm/km2530_0.icm
   cp -p km2530_1.icm /usr/share/foo2lava/icm/km2530_1.icm
   cp -p km2530_2.icm /usr/share/foo2lava/icm/km2530_2.icm
}

load_foo2qpdl() {
   ./getweb 300      # Get Samsung CLP-300 .ICM files
   ./getweb 310      # Get Samsung CLP-310 .ICM files
   ./getweb 315      # Get Samsung CLP-315 .ICM files
   ./getweb 2160     # Get Samsung CLX-2160 .ICM files
   ./getweb 3160     # Get Samsung CLX-3160 .ICM files
   ./getweb 3175     # Get Samsung CLX-3175 .ICM files
   ./getweb 6110     # Get Xerox Phaser 6110 or 6110MFP .ICM files
   ./getweb 600      # Get Samsung CLP-600 .ICM files
   ./getweb 610      # Get Samsung CLP-610 .ICM files
   mkdir /usr/share/foo2qpdl/icm
   cp -p samclp300-0.icm /usr/share/foo2qpdl/icm/samclp300-0.icm
   cp -p samclp315-argyll-0.icm /usr/share/foo2qpdl/icm/samclp315-argyll-0.icm
}

load_foo2slx() {
    ./getweb 500	# Get Lexmark C500n .ICM files
   mkdir /usr/share/foo2slx/icm
   cp -p lexRPCA2000.icm  /usr/share/foo2slx/icm/lexRPCA2000.icm
}

load_foo2hiperc() {
   ./getweb 3100	# Get Oki C3100n .ICM files
   ./getweb 3200	# Get Oki C3200n .ICM files
   ./getweb 3300	# Get Oki C3300n .ICM files
   ./getweb 3400	# Get Oki C3400n .ICM files
   ./getweb 3530	# Get Oki C3530n MFP .ICM files
   ./getweb 5100	# Get Oki C5100n / C5150n .ICM files
   ./getweb 5200	# Get Oki C5200n .ICM files
   ./getweb 5500	# Get Oki C5500n .ICM files
   ./getweb 5600	# Get Oki C5600n .ICM files
   ./getweb 5800	# Get Oki C5800n .ICM files
   mkdir /usr/share/foo2hiperc/icm
   cp -p C3400-L1-BIN-ICC_0603150.icm /usr/share/foo2hiperc/icm/C3400-L1-BIN-ICC_0603150.icm
   cp -p C3400-L2-BIN-ICC_0604110.icm /usr/share/foo2hiperc/icm/C3400-L2-BIN-ICC_0604110.icm
   cp -p OK3200L1.icm /usr/share/foo2hiperc/icm/OK3200L1.icm
   cp -p OK3200L2.icm /usr/share/foo2hiperc/icm/OK3200L2.icm
   cp -p OK5600L1.icm /usr/share/foo2hiperc/icm/OK5600L1.icm
   cp -p OK5600L2.icm /usr/share/foo2hiperc/icm/OK5600L2.icm
   cp -p OK5600L3.icm /usr/share/foo2hiperc/icm/OK5600L3.icm
}

load_foo2oak() {
   ./getweb 1500	# Get HP Color LaserJet 1500 .ICM files
   mkdir /usr/share/foo2oak/icm
   cp -p hpclj2600n-0.icm /usr/share/foo2oak/icm/hpclj2600n-0.icm
   cp -p hpclj2600n-1.icm /usr/share/foo2oak/icm/hpclj2600n-1.icm
}

load_all() {
load_foo2zjs
load_foo2hp
load_foo2xqx
load_foo2lava
load_foo2qpdl
load_foo2slx
load_foo2hiperc
load_foo2oak
}


# load_hp_1215() {
#    ./getweb 1215
#    cp hp1215-argyll-0.icm ../../
#    cp hpclj2600n-0.icm
#    cp hpclj2600n-1.icm
#    cp km2430_2.icm
# #     download . http://download.microsoft.com/download/
# # 
# #     try cabextract -q --directory="$FIRMWARE_ICM_TMP" "FIRMWARE_ICM_CACHE"/VisualBasic6-KB896559-v1-ENU.exe
# #     try cp "$FIRMWARE_ICM_TMP"/mscomctl.ocx "$WINDIR"/system32/mscomctl.ocx
# #     try cp "$FIRMWARE_ICM_TMP"/comctl32.ocx "$WINDIR"/system32/comctl32.ocx
# #     try $WINE regsvr32 comctl32.ocx
# #     try $WINE regsvr32 mscomctl.ocx
# }

#----------------------------------------------------------------


#----------------------------------------------------------------

print_version() {
    echo "$VERSION"
}

#--------- Main program -----------------------------------------

# On Solaris, choose more modern commands (needed for id -u).
case `uname -s` in
SunOS) PATH="/usr/xpg6/bin:/usr/xpg4/bin:$PATH"
      ;;
esac

case "$1" in
-V|--version)
    echo "Firmware_icm version $VERSION.  (C) 2009 Ljubomir Ljubojevic.  LGPL."
    exit 0
    ;;
esac

detectDE

GUI=0
case x"$1" in
x) GUI=1 ;;
x-h|x--help|xhelp) usage ; exit 1 ;;
esac

# # Prevent running with wrong user id.
# # It's bad to create files as the wrong user!
# die_if_user_not_dirowner "$WINEPREFIX"
# die_if_user_not_dirowner "$FIRMWARE_ICM_CACHE"

mkdir -p "$FIRMWARE_ICM_TMP"

case $GUI in
1) dogui ; set $todo ;;
esac

#if [ ! -x "`which "$WINE"`" ]
#then
#    die "Cannot find wine ($WINE)"
#fi

#test -d "$WINEPREFIX" || $WINE cmd /c echo yes > /dev/null 2>&1
mkdir -p "$FIRMWARE_ICM_CACHE"
olddir=`pwd`
# Clean up after failed runs, if needed
rm -rf "$FIRMWARE_ICM_TMP"/*

# The folder-name is localized!
#programfilesdir_win="`unset WINEDEBUG; $WINE cmd.exe /c echo "%ProgramFiles%"`"
#test x"$programfilesdir_win" != x || die "$WINE cmd.exe /c echo '%ProgramFiles%' returned empty string"
#programfilesdir_unix="`unset WINEDEBUG; $WINE winepath -u "$programfilesdir_win"`"
#test x"$programfilesdir_unix" != x || die "winepath -u $programfilesdir_win returned empty string"

# (Fixme: get fonts path from SHGetFolderPath
# See also http://blogs.msdn.com/oldnewthing/archive/2003/11/03/55532.aspx)
#
## Did the user rename Fonts to fonts?
#if test ! -d "$WINDIR"/Fonts && test -d "$WINDIR"/fonts
#then
#    winefontsdir="$WINDIR"/fonts
#else
#    winefontsdir="$WINDIR"/Fonts
#fi

# Mac folks tend to not have sha1sum, but we can make do with openssl
if [ -x "`which sha1sum`" ]
then
   SHA1SUM="sha1sum"
elif [ -x "`which openssl`" ]
then
   SHA1SUM="openssl dgst -sha1"
else
   die "No sha1sum utility available."
fi

if [ ! -x "`which cabextract`" ]
then
    die "Cannot find cabextract.  Please install it (e.g. 'sudo apt-get install cabextract' or 'sudo yum install cabextract')."
fi

if [ ! -x "`which unzip`" ]
then
    die "Cannot find unzip.  Please install it (e.g. 'sudo apt-get install unzip' or 'sudo yum install unzip')."
fi

while test "$1" != ""
do
    PACKAGE=$1
    case $1 in
    -q) FIRMWARE_ICM_QUIET="/q"
        FIRMWARE_ICM_QUIET_T="/qt" # Microsoft Control Pad
        FIRMWARE_ICM_UNIXQUIET="-q"
        FIRMWARE_ICM_SILENT="/silent"
        FIRMWARE_ICM_S="/S"                 # for NSIS installers
        WINEDEBUG=${WINEDEBUG:-"fixme-all"}
        export WINEDEBUG
        ;;
    -v) set -x;;
    foo2zjs) load_foo2zjs;;
    foo2hp) load_foo2hp ;;
    foo2xqx) load_foo2xqx ;;
    foo2lava) load_foo2lava ;;
    foo2qpdl) load_foo2qpdl ;;
    foo2slx) load_foo2slx ;;
    foo2hiperc) load_foo2hiperc ;;
    foo2oak) load_foo2oak ;;
    hp_1215) load_hp_1215 ;;
    hp_1500) load_hp_1500 ;;
    hp_1600) load_hp_1600 ;;
    hp_2600n) load_hp_2600n ;;
    km_1600w) load_km_1600w ;;
    km_1680) load_km_1680 ;;
    km_1690) load_km_1690 ;;
    km_2480) load_km_2480 ;;
    km_2490) load_km_2490 ;;
    km_2530) load_km_2530 ;;
    km_4690) load_km_4690 ;;
    x_6115) load_x_6115 ;;
    m_2200) load_m_2200 ;;
    m_2300) load_m_2300 ;;
    km_2430) load_km_2430 ;;
    cpwl) load_cpwl ;;
    s_300) load_s_300 ;;
    s_310) load_s_310 ;;
    s_315) load_s_315 ;;
    s_600) load_s_600 ;;
    s_610) load_s_610 ;;
    s_2160) load_s_2160 ;;
    s_3160) load_s_3160 ;;
    s_3175) load_s_3175 ;;
    x_6110) load_x_6110 ;;
    l_500) load_l_500 ;;
    o_3100) load_o_3100 ;;
    o_3200) load_o_3200 ;;
    o_3300) load_o_3300 ;;
    o_3400) load_o_3400 ;;
    o_3530) load_o_3530 ;;
    o_5100) load_o_5100 ;;
    o_5200) load_o_5200 ;;
    o_5500) load_o_5500 ;;
    o_5600) load_o_5600 ;;
    o_5800) load_o_5800 ;;
    hp_1000) load_hp_1000 ;;
    hp_1005) load_hp_1005 ;;
    hp_1018) load_hp_1018 ;;
    hp_1020) load_hp_1020 ;;
    hp_p1005) load_hp_p1005 ;;
    hp_p1006) load_hp_p1006 ;;
    hp_p1007) load_hp_p1007 ;;
    hp_p1008) load_hp_p1008 ;;
    hp_p1505) load_hp_p1505 ;;
    m_2300dl_fw) load_m_2300dl_fw ;;
    all) load_all ;;
    version) print_version;;
    volnum) volnum;;
    *) echo Unknown arg $1; usage ; exit 1;;
    esac
    # Provide a bit of feedback
    test "$FIRMWARE_ICM_QUIET" = "" && case $1 in
    -q) echo Setting quiet mode;;
    -v) echo Setting verbose mode;;
    *) echo "Install of $1 done" ;;
    esac
    shift
    # cleanup
    rm -rf "$FIRMWARE_ICM_TMP"/*
done

test "$FIRMWARE_ICM_QUIET" = "" && echo firmware_icm done. || true