ipkg-build 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/bin/sh
  2. # ipkg-build -- construct a .ipk from a directory
  3. # Carl Worth <cworth@east.isi.edu>
  4. # based on a script by Steve Redler IV, steve@sr-tech.com 5-21-2001
  5. # 2003-04-25 rea@sr.unh.edu
  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="$(which find)"
  12. FIND="${FIND:-$(which gfind)}"
  13. TAR="${TAR:-$(which tar)}"
  14. GZIP="$(which gzip)"
  15. # try to use fixed source epoch
  16. if [ -n "$SOURCE_DATE_EPOCH" ]; then
  17. TIMESTAMP=$(date --date="@$SOURCE_DATE_EPOCH")
  18. # look up date of last commit
  19. elif [ -d "$TOPDIR/.git" ]; then
  20. GIT="$(which git)"
  21. TIMESTAMP=$(cd $TOPDIR; $GIT log -1 -s --format=%ci)
  22. elif [ -d "$TOPDIR/.svn" ]; then
  23. SVN="$(which svn)"
  24. TIMESTAMP=$($SVN info "$TOPDIR" | sed -n "s/^Last Changed Date: \(.*\)/\1/p")
  25. else
  26. TIMESTAMP=$(date)
  27. fi
  28. ipkg_extract_value() {
  29. sed -e "s/^[^:]*:[[:space:]]*//"
  30. }
  31. required_field() {
  32. field=$1
  33. grep "^$field:" < $CONTROL/control | ipkg_extract_value
  34. }
  35. pkg_appears_sane() {
  36. local pkg_dir=$1
  37. local owd=$PWD
  38. cd $pkg_dir
  39. PKG_ERROR=0
  40. pkg=`required_field Package`
  41. version=`required_field Version | sed 's/Version://; s/^.://g;'`
  42. arch=`required_field Architecture`
  43. if echo $pkg | grep '[^a-zA-Z0-9_.+-]'; then
  44. echo "*** Error: Package name $name contains illegal characters, (other than [a-z0-9.+-])" >&2
  45. PKG_ERROR=1;
  46. fi
  47. if [ -f $CONTROL/conffiles ]; then
  48. rm -f $CONTROL/conffiles.resolved
  49. for cf in `$FIND $(sed -e "s!^/!$pkg_dir/!" $CONTROL/conffiles) -type f`; do
  50. echo "${cf#$pkg_dir}" >> $CONTROL/conffiles.resolved
  51. done
  52. rm $CONTROL/conffiles
  53. if [ -f $CONTROL/conffiles.resolved ]; then
  54. mv $CONTROL/conffiles.resolved $CONTROL/conffiles
  55. chmod 0644 $CONTROL/conffiles
  56. fi
  57. fi
  58. cd $owd
  59. return $PKG_ERROR
  60. }
  61. ###
  62. # ipkg-build "main"
  63. ###
  64. ogargs=""
  65. noclean=0
  66. usage="Usage: $0 [-c] [-C] [-o owner] [-g group] <pkg_directory> [<destination_directory>]"
  67. while getopts "cg:ho:v" opt; do
  68. case $opt in
  69. o ) owner=$OPTARG
  70. ogargs="--owner=$owner"
  71. ;;
  72. g ) group=$OPTARG
  73. ogargs="$ogargs --group=$group"
  74. ;;
  75. c ) ;;
  76. C ) noclean=1;;
  77. v ) echo $version
  78. exit 0
  79. ;;
  80. h ) echo $usage >&2 ;;
  81. \? ) echo $usage >&2
  82. esac
  83. done
  84. shift $(($OPTIND - 1))
  85. # continue on to process additional arguments
  86. case $# in
  87. 1)
  88. dest_dir=$PWD
  89. ;;
  90. 2)
  91. dest_dir=$2
  92. if [ "$dest_dir" = "." -o "$dest_dir" = "./" ] ; then
  93. dest_dir=$PWD
  94. fi
  95. ;;
  96. *)
  97. echo $usage >&2
  98. exit 1
  99. ;;
  100. esac
  101. pkg_dir=$1
  102. if [ ! -d $pkg_dir ]; then
  103. echo "*** Error: Directory $pkg_dir does not exist" >&2
  104. exit 1
  105. fi
  106. # CONTROL is second so that it takes precedence
  107. CONTROL=
  108. [ -d $pkg_dir/CONTROL ] && CONTROL=CONTROL
  109. if [ -z "$CONTROL" ]; then
  110. echo "*** Error: Directory $pkg_dir has no CONTROL subdirectory." >&2
  111. exit 1
  112. fi
  113. if ! pkg_appears_sane $pkg_dir; then
  114. echo >&2
  115. echo "ipkg-build: Please fix the above errors and try again." >&2
  116. exit 1
  117. fi
  118. tmp_dir=$dest_dir/IPKG_BUILD.$$
  119. mkdir $tmp_dir
  120. echo $CONTROL > $tmp_dir/tarX
  121. # Preserve permissions (-p) when creating data.tar.gz as non-root user
  122. ( cd $pkg_dir && $TAR $ogargs -X $tmp_dir/tarX --format=gnu --sort=name -cpf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/data.tar.gz )
  123. installed_size=`stat -c "%s" $tmp_dir/data.tar.gz`
  124. sed -i -e "s/^Installed-Size: .*/Installed-Size: $installed_size/" \
  125. $pkg_dir/$CONTROL/control
  126. ( cd $pkg_dir/$CONTROL && $TAR $ogargs --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/control.tar.gz )
  127. rm $tmp_dir/tarX
  128. echo "2.0" > $tmp_dir/debian-binary
  129. pkg_file=$dest_dir/${pkg}_${version}_${arch}.ipk
  130. rm -f $pkg_file
  131. ( cd $tmp_dir && $TAR $ogargs --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" ./debian-binary ./data.tar.gz ./control.tar.gz | $GZIP -n - > $pkg_file )
  132. rm $tmp_dir/debian-binary $tmp_dir/data.tar.gz $tmp_dir/control.tar.gz
  133. rmdir $tmp_dir
  134. echo "Packaged contents of $pkg_dir into $pkg_file"