Project

General

Profile

Paste
Download (2.67 KB) Statistics
| Branch: | Revision:

root / bin / scripts-utils.sh @ 1e4af651

1
#!/bin/sh
2

    
3
# This script contains useful functions for other scripts.
4

    
5
# Check if scripts-config.sh is imported.
6
if [ -z $scripts_config ] ; then
7
    echo "Import of scripts-config.sh required."
8
    . scripts-config.sh
9
fi
10

    
11
scripts_utils='imported'
12

    
13
ask_password() {
14
    # read -s doesn't work with sh.
15
    # usage: pass=$(ask_password "password please:")
16
    echo $1 >&2
17
    echo -n ">" >&2
18
    stty_avant=$(stty -g)
19
    stty -echo
20
    read password
21
    stty $stty_avant
22
    echo "$password"
23
    unset password
24
}
25

    
26
generate_password() {
27
    # ARGS: [password_length]
28
    # The password contains special characters. '/' must be excluded to avoid sed malfunction.
29

    
30
    local site_password='/'
31

    
32
    if [ -z $1 ] ; then
33
        local password_length=20
34
    else
35
        local password_length=$1
36
    fi
37

    
38
    while echo "$site_password" | grep -Fq '/' ; do
39
        site_password=$(dd if=/dev/random count=1 | uuencode -m - | head -n 2 | tail -n 1 | cut -c-$password_length)
40
    done
41

    
42
    echo $site_password
43
}
44

    
45
count_d7_sites() {
46
    find $d7_dir_sites -type d ! -name all -maxdepth 1 | wc -l
47
}
48

    
49
check_arguments() {
50
    # ARGS: number of arguments passed to script, number of arguments required, [help text]
51
    if [ $1 -lt $2  ] ; then
52
        echo "Number of arguments insuffisant."
53
	echo -e $3
54
        exit 1
55
    fi
56
}
57

    
58
generate_settings_local() {
59
    # ARGS: site_name, site_password, d7_settings_local_template, d7_site_settings_local
60
    sed "s/\%\%DBUSER\%\%/$1/ ; s/\%\%DBNAME\%\%/$1/ ; s/\%\%DBPASS\%\%/$2/ ; s/\%\%SITE_NAME\%\%/$1/" < $3 > $4
61
}
62

    
63
give_dir() {
64
    # ARG: file
65
    # Return the abosulte directory path of a file or a dir.
66
    settings_location=$(realpath $1)
67
    echo $(dirname $settings_location)
68
}
69

    
70
work_tree_clean() {
71
    git_status_output=$(git status --porcelain)
72
    if [ -z "$git_status_output" ] ; then
73
	return 0
74
    else
75
	return 1
76
    fi
77
}
78

    
79
mail_unclean_work_tree() {
80
    cd $dir_multi_assos
81
    git_status_output=$(git status)
82
    echo "$git_status_output" | mail -s "$1" $email_multi_assos
83
}
84

    
85
commit_if_unclean() {
86
    if ! work_tree_clean ; then
87
	commit_message="COMMIT OF UNCLEAN STUFF"
88
	commit -a -m "$commit_message"
89
	mail_unclean_work_tree "[git] $commit_message"
90
    fi
91
}
92

    
93
commit() {
94
    # ARG: commit message
95
    if [ -z "$1" ] ; then
96
	echo "Empty commit message. Nothing was commited."
97
	return 2
98
    fi
99
    cd $dir_multi_assos
100
    git commit -a -m "$1"
101
}
102

    
103
site_exists() {
104
    # Check if site database exists.
105
    if mysql --defaults-extra-file=$myassos_cnf -e "USE $1" 2>/dev/null ; then
106
	echo "Database $1 already exits."
107
	return 0
108
    fi
109

    
110
    # Check if site folder already exists.
111
    dir=$d7_dir_sites/$1
112
    if [ -d "$dir" ] ; then
113
	echo "Foder $dir already exists."
114
	return 0
115
    fi
116
    return 1
117
}