summarylogtreecommitdiffstats
path: root/common-functions.sh
blob: 81c05875e35551261df9323a9634e2e608306267 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash

# Some colored makepkg-like functions
msg() {
    printf "${green}==>${bold} $1${all_off}\n"
}

msg_yellow() {
    printf "${yellow}==>${bold} $1${all_off}\n"
}

msg2() {
    printf "${blue}  ->${bold} $1${all_off}\n"
}

msg2_yellow() {
    printf "${yellow}  ->${bold} $1${all_off}\n"
}

read_msg2() {
    read -p "${blue}  ->${bold} $1${all_off}"
}

msg3() {
    printf "${yellow}    ->${bold} $1${all_off}\n"
}

error() {
    printf "${red}==> error:${bold} $1${all_off}\n"
}

error2() {
    printf "${red}    *${bold} $1${all_off}\n"
}

# Colors
all_off="$(tput sgr0)"
bold="${all_off}$(tput bold)"
blue="${bold}$(tput setaf 4)"
green="${bold}$(tput setaf 2)"
red="${bold}$(tput setaf 1)"
yellow="${bold}$(tput setaf 3)"

# Version information
print_version() {
    echo "$(basename $0) $(pacman -Q vmware-patch | cut -d ' ' -f2)"
    echo "Copyright (c) 2013-2015  Nobody"
    echo
    echo "THIS SCRIPT IS PROVIDED AS-IS FOR ANY PURPOSE WHATSOEVER. YOU ARE FREE TO SHARE IT, MODIFY IT,"
    echo "TAKE CREDIT OF IT, AND SELL IT ON THE STREETS."
    echo
    echo "FIGHT THE POWER."
}

# Make sure we are root
root_check() {
    if (( $EUID != 0 )); then
        error "This script needs to be run as root."
        exit 1
    fi
}

# Is a VMware product installed?
vmware_check() {
    if [[ ! -f /usr/bin/vmware-installer ]]; then
        error "No VMware product found. Exiting.."
        exit 1
    fi
}

# Product name
set_product_name() {
    ver=$(vmware-installer -l |& grep -Po "(player|workstation) *\K(\d+\.){2}\d+")
    if vmware-installer -l |& grep -q "workstation"; then
        if [[ $ver = 12.* ]]; then
            name="VMware Workstation Pro"
        else
            name="VMware Workstation"
        fi
    else
        if [[ $ver = 12.* ]]; then
            name="VMware Workstation Player"
        elif [[ $ver = 7.* ]]; then
            name="VMware Player (Pro)"
        else
            name="VMware Player (Plus)"
        fi
    fi
}

# Select kernel from menu
menu() {
    # Don't show previous menu
    clear

    # Menu
    echo "Select kernels for which to build modules (leave empty for current kernel):"
    for i in ${!kernels_all[@]}; do 
        printf "%3d%s) %s\n" "$((i+1))" "${choices[i]:- }" "${kernels_all[i]}"
    done
}

# Wrapper function
list_kernels() {
    # Wait for Enter
    while menu && read -rp "Press Enter when done: " num && [[ $num ]]; do
        # Select correct item
        (( num -- ))

        # Set plus mark (+)
        if [[ ${choices[num]} ]]; then
            choices[num]=""
        else
            choices[num]="+"
        fi
    done

    # Chosen kernels
    for i in ${!kernels_all[@]}; do 
        if [[ ${choices[i]} ]]; then
            kernels+=("${kernels_all[i]}")
        fi
    done
}

# Compile leftover module locations
remove_leftover_module_dirs() {
    for i in /usr/lib/modules/*; do
        if [[ $(ls "$i") = misc ]]; then
            misc+=("$i")
        fi
    done

    # Remove them
    if [[ $misc ]]; then
        msg_yellow "Removing leftover module location.."
        for i in ${misc[@]}; do
            msg2 "$i/"
            rm -r "$i"
        done
    fi
}

# Remove old backups
# ls: supports multiple arguments as opppsed to [ -d ]
remove_old_backups() {
    if ls -d source-*.*/ 2>/dev/null | grep -qv "$ver/"; then
        msg_yellow "Cleaning up old backups.."
        # Print full paths
        for i in $(readlink -f source-*.*/ | grep -v "$ver"); do
            msg2 "$i/"
            rm -r "$i"
        done
    fi
}

# Patch function
patch_sources() {
    if [[ $verbose ]]; then
        msg3 "Patching.."
        patch -p0 -f -i "../$patch"
    else
        patch -p0 -s -f -i "../$patch"
    fi
}

# Print vmware-modconfig-*.logs and exit
print_logs() {
    if [[ $ver = 12.* ]]; then
        logs=(/tmp/vmware-root/vmware-[0-9]*.log)
    else
        logs=(/tmp/vmware-root/vmware-modconfig*.log)
    fi

    for log in ${logs[@]}; do
        error2 "$log"
    done
    exit 1
}