OSDN Git Service

[update] : Added --depend
[alterlinux/alterlinux.git] / build.sh
index 925c72b..07f42e1 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -30,11 +30,13 @@ DEFAULT_ARGUMENT=""
 ARGUMENT=("${@}")
 alteriso_version="3.1"
 norepopkg=()
+legacy_mode=false
+rerun=false
 
 # Load config file
 [[ ! -f "${defaultconfig}" ]] && "${tools_dir}/msg.sh" -a 'build.sh' error "${defaultconfig} was not found." && exit 1
 for config in "${defaultconfig}" "${script_path}/custom.conf"; do
-    [[ -f "${config}" ]] && source "${config}"
+    [[ -f "${config}" ]] && source "${config}" && loaded_files+=("${config}")
 done
 
 umask 0022
@@ -42,8 +44,7 @@ umask 0022
 # Message common function
 # msg_common [type] [-n] [string]
 msg_common(){
-    local _msg_opts=("-a" "build.sh") _type="${1}"
-    shift 1
+    local _msg_opts=("-a" "build.sh") _type="${1}" && shift 1
     [[ "${1}" = "-n" ]] && _msg_opts+=("-o" "-n") && shift 1
     [[ "${msgdebug}" = true ]] && _msg_opts+=("-x")
     [[ "${nocolor}"  = true ]] && _msg_opts+=("-n")
@@ -131,7 +132,7 @@ _usage () {
     echo "    -x | --bash-debug            Enable bash debug mode(set -xv)"
     echo "         --channellist           Output the channel list and exit"
     echo "         --config                Load additional config file"
-    echo "         --gitversion            Add Git commit hash to image file version"
+    echo "         --[no]gitversion        Add Git commit hash to image file version"
     echo "         --logpath <file>        Set log file path (use with --log)"
     echo "         --[no]log               (No) log ;re-run script with tee"
     echo "         --msgdebug              Enables output debugging"
@@ -142,7 +143,7 @@ _usage () {
     echo "         --nodebug               Disable all debug messages"
     echo "         --noefi                 No efi boot (Use only for debugging)"
     echo "         --noloopmod             No check and load kernel module automatically"
-    echo "         --nodepend              No check package dependencies before building"
+    echo "         --[no]depend            (No) check package dependencies before building"
     echo "         --noiso                 No build iso image (Use with --tarball)"
     echo "         --nosigcheck            No pacman signature check"
     echo "         --pacman-debug          Enable pacman debug mode"
@@ -181,11 +182,12 @@ mount_airootfs () {
 # Helper function to run make_*() only one time.
 run_once() {
     if [[ ! -e "${lockfile_dir}/build.${1}" ]]; then
+        umount_work
         msg_debug "Running ${1} ..."
         mount_airootfs
         eval "${@}"
         mkdir -p "${lockfile_dir}"; touch "${lockfile_dir}/build.${1}"
-        umount_work
+        
     else
         msg_debug "Skipped because ${1} has already been executed."
     fi
@@ -245,7 +247,7 @@ _run_with_pacmanconf(){
 # コマンドをchrootで実行する
 _chroot_run() {
     msg_debug "Run command in chroot\nCommand: ${*}"
-    eval -- arch-chroot "${airootfs_dir}" "${@}"
+    arch-chroot "${airootfs_dir}" "${@}" || return "${?}"
 }
 
 _cleanup_common () {
@@ -413,6 +415,7 @@ prepare_build() {
     if [[ "$(bash "${tools_dir}/channel.sh" --version "${alteriso_version}" ver "${channel_name}")" = "3.0" ]]; then
         msg_warn "The module cannot be used because it works with Alter ISO3.0 compatibility."
         modules=("legacy")
+        legacy_mode=true
         [[ "${include_extra-"unset"}" = true ]] && modules=("legacy-extra")
     fi
 
@@ -447,6 +450,7 @@ prepare_build() {
     [[ "${customized_password}" = false ]] && password="${defaultpassword}"
 
     # gitversion
+    [[ ! -d "${script_path}/.git" ]] && [[ "${gitversion}" = true ]] && msg_error "There is no git directory. You need to use git clone to use this feature." "1"
     [[ "${gitversion}" = true ]] && iso_version="${iso_version}-${gitrev}"
 
     # Generate tar file name
@@ -476,8 +480,8 @@ prepare_build() {
     if [[ ! "${logging}" = false ]]; then
         [[ "${customized_logpath}" = false ]] && logging="${out_dir}/${iso_filename%.iso}.log"
         mkdir -p "$(dirname "${logging}")" && touch "${logging}"
-        msg_warn "Re-run sudo ${0} ${ARGUMENT[*]} --nodepend --nolog --nocolor 2>&1 | tee ${logging}"
-        sudo "${0}" "${ARGUMENT[@]}" --nolog --nocolor --nodepend 2>&1 | tee "${logging}"
+        msg_warn "Re-run sudo ${0} ${ARGUMENT[*]} --nodepend --nolog --nocolor --rerun 2>&1 | tee ${logging}"
+        sudo "${0}" "${ARGUMENT[@]}" --nolog --nocolor --nodepend --rerun 2>&1 | tee "${logging}"
         exit "${?}"
     fi
 
@@ -547,18 +551,20 @@ make_packages_repo() {
     readarray -t _pkglist_install < <("${tools_dir}/pkglist.sh" "${pkglist_args[@]}")
 
     # Package check
-    #readarray -t _pkglist < <("${tools_dir}/pkglist.sh" "${pkglist_args[@]}")
-    #readarray -t repopkgs < <(pacman-conf -c "${build_pacman_conf}" -l | xargs -I{} pacman -Sql --config "${build_pacman_conf}" --color=never {} && pacman -Sg)
-    #local _pkg
-    #for _pkg in "${_pkglist[@]}"; do
-    #    msg_info "Checking ${_pkg}..."
-    #    if printf "%s\n" "${repopkgs[@]}" | grep -qx "${_pkg}"; then
-    #        _pkglist_install+=("${_pkg}")
-    #    else
-    #        msg_info "${_pkg} was not found. Install it with yay from AUR"
-    #        norepopkg+=("${_pkg}")
-    #    fi
-    #done
+    if [[ "${legacy_mode}" = true ]]; then
+        readarray -t _pkglist < <("${tools_dir}/pkglist.sh" "${pkglist_args[@]}")
+        readarray -t repopkgs < <(pacman-conf -c "${build_pacman_conf}" -l | xargs -I{} pacman -Sql --config "${build_pacman_conf}" --color=never {} && pacman -Sg)
+        local _pkg
+        for _pkg in "${_pkglist[@]}"; do
+            msg_info "Checking ${_pkg}..."
+            if printf "%s\n" "${repopkgs[@]}" | grep -qx "${_pkg}"; then
+                _pkglist_install+=("${_pkg}")
+            else
+                msg_info "${_pkg} was not found. Install it with yay from AUR"
+                norepopkg+=("${_pkg}")
+            fi
+        done
+    fi
 
     # Create a list of packages to be finally installed as packages.list directly under the working directory.
     echo -e "# The list of packages that is installed in live cd.\n#\n" > "${build_dir}/packages.list"
@@ -657,7 +663,7 @@ make_customize_airootfs() {
     # -k changed in AlterISO3 from passing kernel name to passing kernel configuration.
 
     # Generate options of customize_airootfs.sh.
-    _airootfs_script_options=(-p "${password}" -k "\"${kernel} ${kernel_filename} ${kernel_mkinitcpio_profile}\"" -u "${username}" -o "${os_name}" -i "${install_dir}" -s "${usershell}" -a "${arch}" -g "${locale_gen_name}" -l "${locale_name}" -z "${locale_time}" -t "${theme_name}")
+    _airootfs_script_options=(-p "${password}" -k "${kernel} ${kernel_filename} ${kernel_mkinitcpio_profile}" -u "${username}" -o "${os_name}" -i "${install_dir}" -s "${usershell}" -a "${arch}" -g "${locale_gen_name}" -l "${locale_name}" -z "${locale_time}" -t "${theme_name}")
     [[ "${boot_splash}" = true ]] && _airootfs_script_options+=("-b")
     [[ "${debug}" = true       ]] && _airootfs_script_options+=("-d")
     [[ "${bash_debug}" = true  ]] && _airootfs_script_options+=("-x")
@@ -674,7 +680,7 @@ make_customize_airootfs() {
     # Create script
     for _script in "${_script_list[@]}"; do
         if [[ -f "${_script}" ]]; then
-            (echo && cat "${_script}")  >> "${airootfs_dir}/${_main_script}"
+            (echo -e "\n#--$(basename "${_script}")--#\n" && cat "${_script}")  >> "${airootfs_dir}/${_main_script}"
             remove "${_script}"
         else
             msg_debug "${_script} was not found."
@@ -702,9 +708,10 @@ make_setup_mkinitcpio() {
         cp "${script_path}/system/initcpio/install/${_hook}" "${airootfs_dir}/etc/initcpio/install"
     done
 
+    sed -i "s|%COWSPACE%|${cowspace}|g" "${airootfs_dir}/etc/initcpio/hooks/archiso"
     sed -i "s|/usr/lib/initcpio/|/etc/initcpio/|g" "${airootfs_dir}/etc/initcpio/install/archiso_shutdown"
     cp "${script_path}/system/initcpio/install/archiso_kms" "${airootfs_dir}/etc/initcpio/install"
-    cp "${script_path}/system/initcpio/archiso_shutdown" "${airootfs_dir}/etc/initcpio"
+    cp "${script_path}/system/initcpio/script/archiso_shutdown" "${airootfs_dir}/etc/initcpio"
     cp "${script_path}/mkinitcpio/mkinitcpio-archiso.conf" "${airootfs_dir}/etc/mkinitcpio-archiso.conf"
     [[ "${boot_splash}" = true ]] && cp "${script_path}/mkinitcpio/mkinitcpio-archiso-plymouth.conf" "${airootfs_dir}/etc/mkinitcpio-archiso.conf"
 
@@ -713,7 +720,7 @@ make_setup_mkinitcpio() {
       exec 17<>"${build_dir}/gpgkey"
     fi
 
-    _chroot_run "mkinitcpio -c /etc/mkinitcpio-archiso.conf -k /boot/${kernel_filename} -g /boot/archiso.img"
+    _chroot_run mkinitcpio -c "/etc/mkinitcpio-archiso.conf" -k "/boot/${kernel_filename}" -g "/boot/archiso.img"
 
     [[ "${gpg_key}" ]] && exec 17<&-
     
@@ -991,9 +998,9 @@ make_alteriso_info(){
     # iso version info
     if [[ "${include_info}" = true ]]; then
         local _info_file="${isofs_dir}/alteriso-info" _version="${iso_version}"
-        #remove "${_info_file}"; touch "${_info_file}"
+        remove "${_info_file}"; touch "${_info_file}"
         [[ -d "${script_path}/.git" ]] && [[ "${gitversion}" = false ]] && _version="${iso_version}-${gitrev}"
-        "${tools_dir}/alteriso-info.sh" -a "${arch}" -b "${boot_splash}" -c "${channel_name%.add}" -d "${iso_publisher}" -k "${kernel}" -o "${os_name}" -p "${password}" -u "${username}" -v "${_version}" -m "$(printf "%s," "${modules[@]}")"
+        "${tools_dir}/alteriso-info.sh" -a "${arch}" -b "${boot_splash}" -c "${channel_name%.add}" -d "${iso_publisher}" -k "${kernel}" -o "${os_name}" -p "${password}" -u "${username}" -v "${_version}" -m "$(printf "%s," "${modules[@]}")" > "${_info_file}"
     fi
 
     return 0
@@ -1003,9 +1010,9 @@ make_alteriso_info(){
 make_overisofs() {
     local _over_isofs_list _isofs
     _over_isofs_list=("${channel_dir}/over_isofs.any""${channel_dir}/over_isofs.${arch}")
-    for_module '_over_isofs_list+=("${module_dir}/{}/over_isofs.any""${module_dir}/{}/over_isofs.${arch}")'
+    for_module '_over_isofs_list+=("${module_dir}/{}/over_isofs.any" "${module_dir}/{}/over_isofs.${arch}")'
     for _isofs in "${_over_isofs_list[@]}"; do
-        [[ -d "${_isofs}" ]] && cp -af "${_isofs}"/* "${isofs_dir}"
+        [[ -d "${_isofs}" ]] && [[ -n "$(find "${_isofs}" -mindepth 1 -maxdepth 2)" ]] &&  cp -af "${_isofs}"/* "${isofs_dir}"
     done
 
     return 0
@@ -1046,25 +1053,17 @@ make_iso() {
 
 
 # Parse options
-ARGUMENT=("${DEFAULT_ARGUMENT[@]}" "${@}")
-OPTS=("a:" "b" "c:" "d" "e" "g:" "h" "j" "k:" "l:" "o:" "p:" "r" "t:" "u:" "w:" "x")
-OPTL=("arch:" "boot-splash" "comp-type:" "debug" "cleaning" "cleanup" "gpgkey:" "help" "lang:" "japanese" "kernel:" "out:" "password:" "comp-opts:" "user:" "work:" "bash-debug" "nocolor" "noconfirm" "nodepend" "gitversion" "msgdebug" "noloopmod" "tarball" "noiso" "noaur" "nochkver" "channellist" "config:" "noefi" "nodebug" "nosigcheck" "normwork" "log" "logpath:" "nolog" "nopkgbuild" "pacman-debug" "confirm" "tar-type:" "tar-opts:" "add-module:")
-OPT="$(getopt -o "$(printf "%s," "${OPTS[@]}")" -l "$(printf "%s," "${OPTL[@]}")" --  "${ARGUMENT[@]}")" || exit 1
+ARGUMENT=("${DEFAULT_ARGUMENT[@]}" "${@}") OPTS=("a:" "b" "c:" "d" "e" "g:" "h" "j" "k:" "l:" "o:" "p:" "r" "t:" "u:" "w:" "x") OPTL=("arch:" "boot-splash" "comp-type:" "debug" "cleaning" "cleanup" "gpgkey:" "help" "lang:" "japanese" "kernel:" "out:" "password:" "comp-opts:" "user:" "work:" "bash-debug" "nocolor" "noconfirm" "nodepend" "gitversion" "msgdebug" "noloopmod" "tarball" "noiso" "noaur" "nochkver" "channellist" "config:" "noefi" "nodebug" "nosigcheck" "normwork" "log" "logpath:" "nolog" "nopkgbuild" "pacman-debug" "confirm" "tar-type:" "tar-opts:" "add-module:" "nogitversion" "cowspace:" "rerun" "depend")
+GETOPT=(-o "$(printf "%s," "${OPTS[@]}")" -l "$(printf "%s," "${OPTL[@]}")" -- "${ARGUMENT[@]}")
+getopt -Q "${GETOPT[@]}" || exit 1 # 引数エラー判定
+readarray -t OPT < <(getopt "${GETOPT[@]}") # 配列に代入
 
-eval set -- "${OPT}"
-msg_debug "Argument: ${OPT}"
-unset OPT OPTS OPTL DEFAULT_ARGUMENT
+eval set -- "${OPT[@]}"
+msg_debug "Argument: ${OPT[*]}"
+unset OPT OPTS OPTL DEFAULT_ARGUMENT GETOPT
 
 while true; do
     case "${1}" in
-        -a | --arch)
-            arch="${2}"
-            shift 2
-            ;;
-        -b | --boot-splash)
-            boot_splash=true
-            shift 1
-            ;;
         -c | --comp-type)
             case "${2}" in
                 "gzip" | "lzma" | "lzo" | "lz4" | "xz" | "zstd") sfs_comp="${2}" ;;
@@ -1072,21 +1071,6 @@ while true; do
             esac
             shift 2
             ;;
-        -d | --debug)
-            debug=true
-            shift 1
-            ;;
-        -e | --cleaning | --cleanup)
-            cleaning=true
-            shift 1
-            ;;
-        -g | --gpgkey)
-            gpg_key="${2}"
-            shift 2
-            ;;
-        -h | --help)
-            _usage 0
-            ;;
         -j | --japanese)
             msg_error "This option is obsolete in AlterISO 3. To use Japanese, use \"-l ja\"." "1"
             ;;
@@ -1095,23 +1079,11 @@ while true; do
             kernel="${2}"
             shift 2
             ;;
-        -l | --lang)
-            locale_name="${2}"
-            shift 2
-            ;;
-        -o | --out)
-            out_dir="${2}"
-            shift 2
-            ;;
         -p | --password)
             customized_password=true
             password="${2}"
             shift 2
             ;;
-        -r | --tarball)
-            tarball=true
-            shift 1
-            ;;
         -t | --comp-opts)
             if [[ "${2}" = "reset" ]]; then
                 sfs_comp_opt=()
@@ -1125,106 +1097,17 @@ while true; do
             username="$(echo -n "${2}" | sed 's/ //g' | tr '[:upper:]' '[:lower:]')"
             shift 2
             ;;
-        -w | --work)
-            work_dir="${2}"
-            shift 2
-            ;;
-        -x | --bash-debug)
-            debug=true
-            bash_debug=true
-            shift 1
-            ;;
-        --noconfirm)
-            noconfirm=true
-            shift 1
-            ;;
-        --confirm)
-            noconfirm=false
-            shift 1
-            ;;
-        --nodepend)
-            nodepend=true
-            shift 1
-            ;;
-        --nocolor)
-            nocolor=true
-            shift 1
-            ;;
-        --gitversion)
-            if [[ -d "${script_path}/.git" ]]; then
-                gitversion=true
-            else
-                msg_error "There is no git directory. You need to use git clone to use this feature." "1"
-            fi
-            shift 1
-            ;;
-        --msgdebug)
-            msgdebug=true;
-            shift 1
-            ;;
-        --noloopmod)
-            noloopmod=true
-            shift 1
-            ;;
-        --noiso)
-            noiso=true
-            shift 1
-            ;;
-        --noaur)
-            noaur=true
-            shift 1
-            ;;
-        --nochkver)
-            nochkver=true
-            shift 1
-            ;;
         --nodebug)
             debug=false
             msgdebug=false
             bash_debug=false
             shift 1
             ;;
-        --noefi)
-            noefi=true
-            shift 1
-            ;;
-        --channellist)
-            show_channel_list
-            exit 0
-            ;;
-        --config)
-            source "${2}"
-            shift 2
-            ;;
-        --pacman-debug)
-            pacman_debug=true
-            shift 1
-            ;;
-        --nosigcheck)
-            nosigcheck=true
-            shift 1
-            ;;
-        --normwork)
-            normwork=true
-            shift 1
-            ;;
-        --log)
-            logging=true
-            shift 1
-            ;;
         --logpath)
             logging="${2}"
             customized_logpath=true
             shift 2
             ;;
-        --nolog)
-            logging=false
-            shift 1
-            ;;
-        --nopkgbuild)
-            nopkgbuild=true
-            shift 1
-            ;;
         --tar-type)
             case "${2}" in
                 "gzip" | "lzma" | "lzo" | "lz4" | "xz" | "zstd") tar_comp="${2}" ;;
@@ -1241,10 +1124,41 @@ while true; do
             msg_debug "Added modules: ${additional_modules[*]}"
             shift 2
             ;;
-        --)
-            shift
-            break
-            ;;
+        -g | --gpgkey               ) gpg_key="${2}"     && shift 2 ;;
+        -h | --help                 ) _usage 0                      ;;
+        -a | --arch                 ) arch="${2}"        && shift 2 ;;
+        -d | --debug                ) debug=true         && shift 1 ;;
+        -e | --cleaning | --cleanup ) cleaning=true      && shift 1 ;;
+        -b | --boot-splash          ) boot_splash=true   && shift 1 ;;
+        -l | --lang                 ) locale_name="${2}" && shift 2 ;;
+        -o | --out                  ) out_dir="${2}"     && shift 2 ;;
+        -r | --tarball              ) tarball=true       && shift 1 ;;
+        -w | --work                 ) work_dir="${2}"    && shift 2 ;;
+        -x | --bash-debug           ) bash_debug=true    && shift 1 ;;
+        --gitversion                ) gitversion=true    && shift 1 ;;
+        --noconfirm                 ) noconfirm=true     && shift 1 ;;
+        --confirm                   ) noconfirm=false    && shift 1 ;;
+        --nodepend                  ) nodepend=true      && shift 1 ;;
+        --nocolor                   ) nocolor=true       && shift 1 ;;
+        --msgdebug                  ) msgdebug=true      && shift 1 ;;
+        --noloopmod                 ) noloopmod=true     && shift 1 ;;
+        --noiso                     ) noiso=true         && shift 1 ;;
+        --noaur                     ) noaur=true         && shift 1 ;;
+        --nochkver                  ) nochkver=true      && shift 1 ;;
+        --noefi                     ) noefi=true         && shift 1 ;;
+        --channellist               ) show_channel_list  && exit 0  ;;
+        --config                    ) source "${2}"      ;  shift 2 ;;
+        --pacman-debug              ) pacman_debug=true  && shift 1 ;;
+        --nosigcheck                ) nosigcheck=true    && shift 1 ;;
+        --normwork                  ) normwork=true      && shift 1 ;;
+        --log                       ) logging=true       && shift 1 ;;
+        --nolog                     ) logging=false      && shift 1 ;;
+        --nopkgbuild                ) nopkgbuild=true    && shift 1 ;;
+        --nogitversion              ) gitversion=false   && shift 1 ;;
+        --cowspace                  ) cowspace="${2}"    && shift 2 ;;
+        --rerun                     ) rerun=true         && shift 1 ;;
+        --depend                    ) nodepend=false      && shift 1 ;;
+        --                          ) shift 1            && break   ;;
         *)
             msg_error "Argument exception error '${1}'"
             msg_error "Please report this error to the developer." 1
@@ -1256,7 +1170,7 @@ done
 if (( ! "${EUID}" == 0 )); then
     msg_warn "This script must be run as root." >&2
     msg_warn "Re-run 'sudo ${0} ${ARGUMENT[*]}'"
-    sudo "${0}" "${ARGUMENT[@]}"
+    sudo "${0}" "${ARGUMENT[@]}" --rerun
     exit "${?}"
 fi
 
@@ -1296,7 +1210,6 @@ for _dir in build_dir cache_dir airootfs_dir isofs_dir lockfile_dir out_dir; do
     eval "${_dir}=\"$(realpath "$(eval "echo \$${_dir}")")\""
 done
 
-
 # Set for special channels
 if [[ -d "${channel_dir}.add" ]]; then
     channel_name="${1}"
@@ -1317,8 +1230,6 @@ if [[ ! "$(bash "${tools_dir}/channel.sh" --version "${alteriso_version}" ver "$
     fi
 fi
 
-set -eu
-
 prepare_env
 prepare_build
 show_settings