#! /bin/bash

# wr_uninstall:  script to uninstall XicTools-4.3 packages.
#
# Give command without arguments for usage info.
# Whiteley Research Inc., open source, Apache 2.0 license
# Stephen R. Whiteley 9/23/2017

# For Windows, you can run uninstall.bat instead of this script, the
# argument lists and effects are the same.  In fact, unless you are
# running Cygwin bash or equivalent, this script won't work in
# Windows, you should instead run the batch script from a CMD prompt.

command -v sudo 2>&1 > /dev/null
if [ $? != 0 ]; then
    echo
    echo \
"ERROR:
The sudo command is not found.  You can fake it by creating a file
named \"sudo\" somewhere in your search path, with content

#!/bin/bash
\$@

Make the file executable with "chmod 755 sudo".  This creates a
phony sudo command that simply executes the arguments.  You may need
to run the calling script as an administrator or root user."
    exit 1
fi

windows=no
if [ -n "$COMSPEC" ]; then
    if [ -n "$(expr match $COMSPEC '.*\(\.exe\)')" ]; then
        windows=yes
    fi
fi
if [ $windows = no ]; then
    if [ $(whoami) != root ]; then
        if [ x$1 != x -a x$1 != x-t ]; then
            echo "You're not root, validating using sudo."
            sudo -v
            if [ $? != 0 ]; then
                echo Validation failed.
                exit 1
            fi
        fi
    fi
fi

programs="adms fastcap fasthenry mozy mrouter vl wrspice xic xtlserv"

dryrun=no
if [ x"$1" = x-t ]; then
    dryrun=yes
    shift
fi

list=$*
if [ -z "$list" ]; then
    echo
    echo "Usage:  wr_uninstall [-t] arg1 [arg2 ...]"
    echo
    echo \
"The arguments are partial program names from among those listed
below."
    echo "  $programs"
    echo
    echo \
"Only the first few characters, to uniquely specify the name, are
required in each argument.  For each argument, if a corresponding
installed XicTools-4.3 package is found, it will be uninstalled.  The
argument can also be \"all\", in which case all installed XicTools-4.3
packages will be uninstalled.  If the first argument is \"-t\", the
removal commands are printed but not executed. 

It is likely that you will need administrator or super-user
permissions to successfully uninstall packages.  Except under Windows,
the sudo utility is used to grant root permission.  You must be listed
in /etc/sudoers, and give YOUR (not root's) password when asked."
    echo
    echo
    exit 0
fi

for a in $list; do
    case $a in
    al|all)
        list=$programs
        break
        ;;
    esac
done

names=""
for a in $list; do
    case $a in
    ad|adm|adms)
        names="$names adms"
        ;;
    fc|fastc|fastca|fastcap)
        names="$names fastcap"
        ;;
    fh|fasth|fasthe|fasthen|fasthenr|fasthenry)
        names="$names fasthenry"
        ;;
    mo|moz|mozy)
        names="$names mozy"
        ;;
    mr|mro|mrou|mrout|mroute|mrouter)
        names="$names mrouter"
        ;;
    v|vl)
        names="$names vl"
        ;;
    w|wr|wrs|wrsp|wrspi|wrspic|wrspice)
        names="$names wrspice"
        ;;
    xi|xic)
        names="$names xic"
        ;;
    xt|xtl|xtls|xtlse|xtlser|xtlserv)
        names="$names xtlserv"
        ;;
    esac
done

if [ -n "$names" ]; then

    if [ $windows = yes ]; then
        script_path=$(dirname $0)
        if [ $dryrun = no ]; then
            sudo $script_path/uninstall.bat $names
        else
            $script_path/uninstall.bat -t $names
        fi
    elif [ $(uname) = Darwin ]; then
        for a in $names; do
            case $a in
            adms)
                if [ $dryrun = yes ]; then
                    echo rm -rf /usr/local/xictools/adms
                    echo rm -f /usr/local/xictools/bin/admsXml

                    list=`pkgutil --pkgs | grep xictools_adms`
                    for a in $list; do
                        echo pkgutil --forget $a
                    done
                else
                    sudo rm -rf /usr/local/xictools/adms
                    sudo rm -f /usr/local/xictools/bin/admsXml

                    list=`pkgutil --pkgs | grep xictools_adms`
                    for a in $list; do
                        sudo pkgutil --forget $a
                    done
                fi
                ;;
            fastcap)
                if [ $dryrun = yes ]; then
                    echo rm -rf /usr/local/xictools/fastcap
                    echo rm -f /usr/local/xictools/bin/fcpp
                    echo rm -f /usr/local/xictools/bin/lstpack
                    echo rm -f /usr/local/xictools/bin/lstunpack
                    echo rm -f /usr/local/xictools/bin/fastcap
                    echo rm -f /usr/local/xictools/bin/cubegen
                    echo rm -f /usr/local/xictools/bin/capgen
                    echo rm -f /usr/local/xictools/bin/busgen
                    echo rm -f /usr/local/xictools/bin/pipedgen
                    echo rm -f /usr/local/xictools/bin/pyragen
              
                    list=`pkgutil --pkgs | grep xictools_fastcap`
                    for a in $list; do
                        echo pkgutil --forget $a
                    done
                else
                    sudo rm -rf /usr/local/xictools/fastcap
                    sudo rm -f /usr/local/xictools/bin/fcpp
                    sudo rm -f /usr/local/xictools/bin/lstpack
                    sudo rm -f /usr/local/xictools/bin/lstunpack
                    sudo rm -f /usr/local/xictools/bin/fastcap
                    sudo rm -f /usr/local/xictools/bin/cubegen
                    sudo rm -f /usr/local/xictools/bin/capgen
                    sudo rm -f /usr/local/xictools/bin/busgen
                    sudo rm -f /usr/local/xictools/bin/pipedgen
                    sudo rm -f /usr/local/xictools/bin/pyragen
              
                    list=`pkgutil --pkgs | grep xictools_fastcap`
                    for a in $list; do
                        sudo pkgutil --forget $a
                    done
                fi
                ;;
            fasthenry)
                if [ $dryrun = yes ]; then
                    echo rm -rf /usr/local/xictools/fasthenry
                    echo rm -f /usr/local/xictools/bin/fasthenry
                    echo rm -f /usr/local/xictools/bin/zbuf
              
                    list=`pkgutil --pkgs | grep xictools_fasthenry`
                    for a in $list; do
                        echo pkgutil --forget $a
                    done
                else
                    sudo rm -rf /usr/local/xictools/fasthenry
                    sudo rm -f /usr/local/xictools/bin/fasthenry
                    sudo rm -f /usr/local/xictools/bin/zbuf
              
                    list=`pkgutil --pkgs | grep xictools_fasthenry`
                    for a in $list; do
                        sudo pkgutil --forget $a
                    done
                fi
                ;;
            mozy)
                if [ $dryrun = yes ]; then
                    echo rm -rf /usr/local/xictools/mozy
                    echo rm -f /usr/local/xictools/bin/mozy
                    echo rm -f /usr/local/xictools/bin/xeditor
                    echo rm -f /usr/local/xictools/bin/httpget
                    echo rm -f /usr/local/xictools/bin/hlp2html
                    echo rm -f /usr/local/xictools/bin/hlpsrv
              
                    list=`pkgutil --pkgs | grep xictools_mozy`
                    for a in $list; do
                        echo pkgutil --forget $a
                    done
                else
                    sudo rm -rf /usr/local/xictools/mozy
                    sudo rm -f /usr/local/xictools/bin/mozy
                    sudo rm -f /usr/local/xictools/bin/xeditor
                    sudo rm -f /usr/local/xictools/bin/httpget
                    sudo rm -f /usr/local/xictools/bin/hlp2html
                    sudo rm -f /usr/local/xictools/bin/hlpsrv
              
                    list=`pkgutil --pkgs | grep xictools_mozy`
                    for a in $list; do
                        sudo pkgutil --forget $a
                    done
                fi
                ;;
            mrouter)
                if [ $dryrun = yes ]; then
                    echo rm -rf /usr/local/xictools/mrouter
                    echo rm -f /usr/local/xictools/bin/mrouter
              
                    list=`pkgutil --pkgs | grep xictools_mrouter`
                    for a in $list; do
                        echo pkgutil --forget $a
                    done
                else
                    sudo rm -rf /usr/local/xictools/mrouter
                    sudo rm -f /usr/local/xictools/bin/mrouter
              
                    list=`pkgutil --pkgs | grep xictools_mrouter`
                    for a in $list; do
                        sudo pkgutil --forget $a
                    done
                fi
                ;;
            vl)
                if [ $dryrun = yes ]; then
                    echo rm -rf /usr/local/xictools/vl
                    echo rm -f /usr/local/xictools/bin/vl
              
                    list=`pkgutil --pkgs | grep xictools_vl`
                    for a in $list; do
                        echo pkgutil --forget $a
                    done
                else
                    sudo rm -rf /usr/local/xictools/vl
                    sudo rm -f /usr/local/xictools/bin/vl
              
                    list=`pkgutil --pkgs | grep xictools_vl`
                    for a in $list; do
                        sudo pkgutil --forget $a
                    done
                fi
                ;;
            wrspice)
                if [ $dryrun = yes ]; then
                    echo rm -rf /usr/local/xictools/wrspice.current
                    echo rm -f /usr/local/xictools/wrspice
                    echo rm -f /usr/local/xictools/bin/multidec
                    echo rm -f /usr/local/xictools/bin/proc2mod
                    echo rm -f /usr/local/xictools/bin/printtoraw
                    echo rm -f /usr/local/xictools/bin/wrspice
                    echo rm -f /usr/local/xictools/bin/wrspiced
              
                    list=`pkgutil --pkgs | grep xictools_wrspice`
                    for a in $list; do
                        echo pkgutil --forget $a
                    done
                else
                    sudo rm -rf /usr/local/xictools/wrspice.current
                    sudo rm -f /usr/local/xictools/wrspice
                    sudo rm -f /usr/local/xictools/bin/multidec
                    sudo rm -f /usr/local/xictools/bin/proc2mod
                    sudo rm -f /usr/local/xictools/bin/printtoraw
                    sudo rm -f /usr/local/xictools/bin/wrspice
                    sudo rm -f /usr/local/xictools/bin/wrspiced
              
                    list=`pkgutil --pkgs | grep xictools_wrspice`
                    for a in $list; do
                        sudo pkgutil --forget $a
                    done
                fi
                ;;
            xic)
                if [ $dryrun = yes ]; then
                    echo rm -rf /usr/local/xictools/xic.current
                    echo rm -f /usr/local/xictools/xic
                    echo rm -f /usr/local/xictools/bin/wrdecode
                    echo rm -f /usr/local/xictools/bin/wrencode
                    echo rm -f /usr/local/xictools/bin/wrsetpass
                    echo rm -f /usr/local/xictools/bin/xic
              
                    list=`pkgutil --pkgs | grep xictools_xic`
                    for a in $list; do
                        echo pkgutil --forget $a
                    done
                else
                    sudo rm -rf /usr/local/xictools/xic.current
                    sudo rm -f /usr/local/xictools/xic
                    sudo rm -f /usr/local/xictools/bin/wrdecode
                    sudo rm -f /usr/local/xictools/bin/wrencode
                    sudo rm -f /usr/local/xictools/bin/wrsetpass
                    sudo rm -f /usr/local/xictools/bin/xic
              
                    list=`pkgutil --pkgs | grep xictools_xic`
                    for a in $list; do
                        sudo pkgutil --forget $a
                    done
                fi
                ;;
            xtlserv)
                if [ $dryrun = yes ]; then
                    echo rm -f /usr/local/xictools/license/README
                    echo rm -f /usr/local/xictools/bin/xtlserv
                    echo rm -f /usr/local/xictools/bin/xtjobs
              
                    list=`pkgutil --pkgs | grep xictools_xtlserv`
                    for a in $list; do
                        echo pkgutil --forget $a
                    done
                else
                    sudo rm -f /usr/local/xictools/license/README
                    sudo rm -f /usr/local/xictools/bin/xtlserv
                    sudo rm -f /usr/local/xictools/bin/xtjobs
              
                    list=`pkgutil --pkgs | grep xictools_xtlserv`
                    for a in $list; do
                        sudo pkgutil --forget $a
                    done
                fi
                ;;
            *)
                echo Unknown program $a
                ;;
            esac
        done
    else
        command -v rpm 2>&1 > /dev/null
        if [ $? = 0 ]; then
            list=$(rpm -qa | grep ^xictools_)
            if [ -n "$list" ]; then
                for a in $names; do
                    pname=xictools_$a
                    for b in $list; do
                        if [[ $b = $pname* ]]; then
                            echo rpm -e $b
                            if [ $dryrun = no ]; then
                                sudo rpm -e $b
                            fi
                        fi
                    done
                done
            fi
        fi

        command -v dpkg 2>&1 > /dev/null
        if [ $? = 0 ]; then
            list=$(dpkg-query --show | grep ^xictools-)
            if [ -n "$list" ]; then
                for a in $names; do
                    pname=xictools-$a
                    for b in $list; do
                        if [[ $b = $pname* ]]; then
                            echo dpkg -r $b
                            if [ $dryrun = no ]; then
                                sudo dpkg -r $b
                            fi
                        fi
                    done
                done
            fi
        fi
    fi
else
    echo "Nothing found to uninstall."
fi