summaryrefslogtreecommitdiff
path: root/src/install.sh
blob: 5d3c2e739bcabcafcf03ed345b31b69ee78e3fc8 (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
#!/bin/sh

extract () {
    # keep old files here, so we dont overwrite any configs
    tar -h --keep-old-files -p -vvJxf $1 -C ${SYSROOT} 2>${LOG_FILE} | grep -v ^d | tr -s " " | cut -d" " -f6 | cut -c2- 
}

install_package () {
    local pkg_file="$1"
    local name="$2"
    local info_file="$pkg_file.info"

    local installed_dir="${SYSROOT}${INSTALLED_DIR}/$name"
    local info="$installed_dir/info"
    local files="$installed_dir/files"
    local checksum="$installed_dir/checksum"

    set -- $(sha512sum $pkg_file)
    local package_checksum=$1
    if [ ! -f $checksum ] || [ "$(cat $checksum)" != "$package_checksum" ]; then

        [ ! -d $installed_dir ] && mkdir -p "$installed_dir"

        [ -f "$files" ] && {
            for read -r file; do
                [ -z "${file%%/etc*}" ] || 
                    rm -f ${SYSROOT}$file
            done < $files
            rm $files
        }

        ${VERBOSE} && printf "${BLACK}Extracting $name...\n"

        # dont overwrite anything in /etc; this is where configs are saved
        # TODO define which files should be preserved
        extract $pkg_file > $files

        [ -f $info_file ] && cp $info_file $info
        echo $package_checksum > $checksum
        return 0
    fi
    ${VERBOSE} && printf "${BLACK}Skipping $name; already installed...\n"
    return 1
}

get_package_filecount() {
    set -- $(get_package_download_info $1)
    echo $4
}

total_filecount() {
    local packages=$@
    local count=0
    for package in $packages; do
        c=$(tar -tvvf $package | grep -v ^d | wc -l)
        count=$((count+c))
    done
    echo $count
}

run_postinstall () {
    postinstall="${SYSROOT}/var/lib/xipkg/postinstall"
    [ -d $postinstall ] &&
        for f in $(ls $postinstall); do
            file=$postinstall/$f

            chmod 755 $file

            [ "${SYSROOT}" = "/" ] && {
                sh "/var/lib/xipkg/postinstall/$f" 2> ${LOG_FILE} > ${LOG_FILE}

            } || {
                xichroot ${SYSROOT} "/var/lib/xipkg/postinstall/$f" 2> ${LOG_FILE} > ${LOG_FILE}

            } 
        
            [ "$?" = "0" ] && {
                rm $file &&
                printf "${GREEN}${CHECKMARK} postinstall $f!\n"
            } || {
                ${QUIET} || printf "${RED}${CROSSMARK} failed postinstall $f!\n"
            }

        done 
}


install () {
    local packages=$@
    ${VERBOSE} && printf "${BLACK}Requested to install: $@\n${RESET}"

    if [ "$#" = "0" ]; then
        packages=$(ls ${SYSROOT}${INSTALLED_DIR})
    fi

    local missing=""
    for package in $packages; do
        [ ! -f $package ] && missing="$missing $(basename $package)"
    done

    if [ "${#missing}" != "0" ]; then
        ${VERBOSE} && printf "${BLACK}couldnt find: $missing\n${RESET}"
    else
        
        local total=$(total_filecount $packages 2>/dev/null || echo 1)
        local files_files=""
        for package in $packages; do
            local name=$(basename $package .xipkg | cut -d. -f2)
            ${VERBOSE} && printf "${BLACK}installing $name from $package \n${RESET}"
            install_package $package $name &

            mkdir -p "${SYSROOT}${INSTALLED_DIR}/$name/"
            filelist="${SYSROOT}${INSTALLED_DIR}/$name/files"
            touch $filelist
            files_files="$files_files $filelist"
        done
        wait_for_extract $total ${files_files}
        run_postinstall
    fi
}

reinstall () {  
    local packages=$@
    remove $@
    install $@
}

do_install () {
    [ "$#" = "0" ] && set -- $(installed)

    toinstall=${CACHE_DIR}/toinstall

    echo "" > $toinstall
    tofetch=""
    for f in $@; do
        [ -f "$f" ] && echo $f >> $toinstall || tofetch="$tofetch$f "
    done

    get $tofetch
    install $(cat $toinstall)
}