ipkg-build 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/sh
  2. # ipkg-build -- construct a .ipk from a directory
  3. # Carl Worth <[email protected]>
  4. # based on a script by Steve Redler IV, [email protected] 5-21-2001
  5. # 2003-04-25 [email protected]
  6. # Updated to work on Familiar Pre0.7rc1, with busybox tar.
  7. # Note it Requires: binutils-ar (since the busybox ar can't create)
  8. # For UID debugging it needs a better "find".
  9. set -e
  10. version=1.0
  11. FIND="$(command -v find)"
  12. FIND="${FIND:-$(command -v gfind)}"
  13. TAR="${TAR:-$(command -v tar)}"
  14. GZIP="$(command -v gzip)"
  15. # try to use fixed source epoch
  16. if [ -n "$PKG_SOURCE_DATE_EPOCH" ]; then
  17. TIMESTAMP=$(date --date="@$PKG_SOURCE_DATE_EPOCH")
  18. elif [ -n "$SOURCE_DATE_EPOCH" ]; then
  19. TIMESTAMP=$(date --date="@$SOURCE_DATE_EPOCH")
  20. else
  21. TIMESTAMP=$(date)
  22. fi
  23. ipkg_extract_value() {
  24. sed -e "s/^[^:]*:[[:space:]]*//"
  25. }
  26. required_field() {
  27. field=$1
  28. grep "^$field:" < $CONTROL/control | ipkg_extract_value
  29. }
  30. pkg_appears_sane() {
  31. local pkg_dir=$1
  32. local owd=$PWD
  33. cd $pkg_dir
  34. PKG_ERROR=0
  35. pkg=`required_field Package`
  36. version=`required_field Version | sed 's/Version://; s/^.://g;'`
  37. arch=`required_field Architecture`
  38. if echo $pkg | grep '[^a-zA-Z0-9_.+-]'; then
  39. echo "*** Error: Package name $name contains illegal characters, (other than [a-z0-9.+-])" >&2
  40. PKG_ERROR=1;
  41. fi
  42. if [ -f $CONTROL/conffiles ]; then
  43. rm -f $CONTROL/conffiles.resolved
  44. for cf in `$FIND $(sed -e "s!^/!$pkg_dir/!" $CONTROL/conffiles) -type f`; do
  45. echo "${cf#$pkg_dir}" >> $CONTROL/conffiles.resolved
  46. done
  47. rm $CONTROL/conffiles
  48. if [ -f $CONTROL/conffiles.resolved ]; then
  49. mv $CONTROL/conffiles.resolved $CONTROL/conffiles
  50. chmod 0644 $CONTROL/conffiles
  51. fi
  52. fi
  53. cd $owd
  54. return $PKG_ERROR
  55. }
  56. ###
  57. # ipkg-build "main"
  58. ###
  59. file_modes=""
  60. usage="Usage: $0 [-v] [-h] [-m] <pkg_directory> [<destination_directory>]"
  61. while getopts "hvm:" opt; do
  62. case $opt in
  63. v ) echo $version
  64. exit 0
  65. ;;
  66. h ) echo $usage >&2 ;;
  67. m ) file_modes=$OPTARG ;;
  68. \? ) echo $usage >&2
  69. esac
  70. done
  71. shift $(($OPTIND - 1))
  72. # continue on to process additional arguments
  73. case $# in
  74. 1)
  75. dest_dir=$PWD
  76. ;;
  77. 2)
  78. dest_dir=$2
  79. if [ "$dest_dir" = "." -o "$dest_dir" = "./" ] ; then
  80. dest_dir=$PWD
  81. fi
  82. ;;
  83. *)
  84. echo $usage >&2
  85. exit 1
  86. ;;
  87. esac
  88. pkg_dir=$1
  89. if [ ! -d $pkg_dir ]; then
  90. echo "*** Error: Directory $pkg_dir does not exist" >&2
  91. exit 1
  92. fi
  93. # CONTROL is second so that it takes precedence
  94. CONTROL=
  95. [ -d $pkg_dir/CONTROL ] && CONTROL=CONTROL
  96. if [ -z "$CONTROL" ]; then
  97. echo "*** Error: Directory $pkg_dir has no CONTROL subdirectory." >&2
  98. exit 1
  99. fi
  100. if ! pkg_appears_sane $pkg_dir; then
  101. echo >&2
  102. echo "ipkg-build: Please fix the above errors and try again." >&2
  103. exit 1
  104. fi
  105. tmp_dir=$dest_dir/IPKG_BUILD.$$
  106. mkdir $tmp_dir
  107. echo $CONTROL > $tmp_dir/tarX
  108. cd $pkg_dir
  109. for file_mode in $file_modes; do
  110. case $file_mode in
  111. /*:*:*:*)
  112. ;;
  113. *)
  114. echo "ERROR: file modes must use absolute path and contain user:group:mode"
  115. echo "$file_mode"
  116. exit 1
  117. ;;
  118. esac
  119. path=$(echo "$file_mode" | cut -d ':' -f 1)
  120. user_group=$(echo "$file_mode" | cut -d ':' -f 2-3)
  121. mode=$(echo "$file_mode" | cut -d ':' -f 4)
  122. chown "$user_group" "$pkg_dir/$path"
  123. chmod "$mode" "$pkg_dir/$path"
  124. done
  125. $TAR -X $tmp_dir/tarX --format=gnu --sort=name -cpf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/data.tar.gz
  126. installed_size=`stat -c "%s" $tmp_dir/data.tar.gz`
  127. sed -i -e "s/^Installed-Size: .*/Installed-Size: $installed_size/" \
  128. $pkg_dir/$CONTROL/control
  129. ( cd $pkg_dir/$CONTROL && $TAR --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/control.tar.gz )
  130. rm $tmp_dir/tarX
  131. echo "2.0" > $tmp_dir/debian-binary
  132. pkg_file=$dest_dir/${pkg}_${version}_${arch}.ipk
  133. rm -f $pkg_file
  134. ( cd $tmp_dir && $TAR --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" ./debian-binary ./data.tar.gz ./control.tar.gz | $GZIP -n - > $pkg_file )
  135. rm $tmp_dir/debian-binary $tmp_dir/data.tar.gz $tmp_dir/control.tar.gz
  136. rmdir $tmp_dir
  137. echo "Packaged contents of $pkg_dir into $pkg_file"